权限管理,pymysql模块
權(quán)限管理
權(quán)限管理重點
MySQL 默認(rèn)有個root用戶,但是這個用戶權(quán)限太大,一般只在管理數(shù)據(jù)庫時候才用。如果在項目中要連接 MySQL 數(shù)據(jù)庫,則建議新建一個權(quán)限較小的用戶來連接。
在 MySQL 命令行模式下輸入如下命令可以為 MySQL 創(chuàng)建一個新用戶:
create user "賬戶名"@"主機(jī)名" identified by 密碼 create user "tom"@"localhost" identified by "123";新用戶創(chuàng)建完成,但是此刻如果以此用戶登陸的話,會報錯,因為我們還沒有為這個用戶分配相應(yīng)權(quán)限,分配權(quán)限的命令如下:
授予所有數(shù)據(jù)庫所有表的所有權(quán)限給jerry這個用戶 并允許jerry在任意一臺電腦登錄 如果用戶不存在會自動創(chuàng)建 grant all on *.* to "jerry"@"%" identified by "123" with grant option;with grant option這個用戶可以將擁有的權(quán)限授予別人授予username用戶在所有數(shù)據(jù)庫上的所有權(quán)限。
如果此時發(fā)現(xiàn)剛剛給的權(quán)限太大了,如果我們只是想授予它在某個數(shù)據(jù)庫上的權(quán)限,那么需要切換到root 用戶撤銷剛才的權(quán)限,重新授權(quán):
授予day45數(shù)據(jù)庫所有表的所有權(quán)限給jack這個用戶 并允許jerry在任意一臺電腦登錄
授予day45數(shù)據(jù)庫的emp表的所有權(quán)限給rose這個用戶 并允許jerry在任意一臺電腦登錄
授予day45數(shù)據(jù)庫的emp表的name字段的查詢權(quán)限給maria這個用戶 并允許jerry在任意一臺電腦登錄
另外每當(dāng)調(diào)整權(quán)限后,通常需要執(zhí)行以下語句刷新權(quán)限:
flush privileges;收回權(quán)限
REVOKE all privileges [column] on db.table from user@"host";如何授權(quán)就如何收回 因為不同權(quán)限信息存到不同的表中
REVOKE all privileges on day45.emp from maria@"%";當(dāng)你在云服務(wù)器部署了 mysql環(huán)境時 你的程序無法直接連接到服務(wù)器 需要授予在任意一臺電腦登錄的權(quán)限
grant all on *.* to "jerry"@"%" identified by "123" with grant option;刪除剛才創(chuàng)建的用戶:
DROP USER 用戶名@localhost;仔細(xì)上面幾個命令,可以發(fā)現(xiàn)不管是授權(quán),還是撤銷授權(quán),都要指定響應(yīng)的host(即 @ 符號后面的內(nèi)容),因為以上及格命令實際上都是在操作mysql 數(shù)據(jù)庫中的user表,可以用如下命令查看相應(yīng)用戶及對應(yīng)的host:
SELECT User, Host FROM user;權(quán)限表
MySQL服務(wù)器通過MySQL權(quán)限表來控制用戶對數(shù)據(jù)庫的訪問,MySQL權(quán)限表存放在mysql數(shù)據(jù)庫里,由mysql_install_db腳本初始化。這些MySQL權(quán)限表分別user,db,table_priv,columns_priv和host。下面分別介紹一下這些表的結(jié)構(gòu)和內(nèi)容:
user權(quán)限表:記錄允許連接到服務(wù)器的用戶帳號信息,里面的權(quán)限是全局級的。
db權(quán)限表:記錄各個帳號在各個數(shù)據(jù)庫上的操作權(quán)限。
table_priv權(quán)限表:記錄數(shù)據(jù)表級的操作權(quán)限。
columns_priv權(quán)限表:記錄數(shù)據(jù)列級的操作權(quán)限。
host權(quán)限表:配合db權(quán)限表對給定主機(jī)上數(shù)據(jù)庫級操作權(quán)限作更細(xì)致的控制。這個權(quán)限表不受GRANT和REVOKE語句的影響。
權(quán)限列表
ALTER: 修改表和索引。
CREATE: 創(chuàng)建數(shù)據(jù)庫和表。
DELETE: 刪除表中已有的記錄。
DROP: 拋棄(刪除)數(shù)據(jù)庫和表。
INDEX: 創(chuàng)建或拋棄索引。
INSERT: 向表中插入新行。
REFERENCE: 未用。
SELECT: 檢索表中的記錄。
UPDATE: 修改現(xiàn)存表記錄。
FILE: 讀或?qū)懛?wù)器上的文件。
PROCESS: 查看服務(wù)器中執(zhí)行的線程信息或殺死線程。
RELOAD: 重載授權(quán)表或清空日志、主機(jī)緩存或表緩存。
SHUTDOWN: 關(guān)閉服務(wù)器。
ALL: 所有權(quán)限,ALL PRIVILEGES同義詞。
USAGE: 特殊的 "無權(quán)限" 權(quán)限。
用 戶賬戶包括 "username" 和 "host" 兩部分,后者表示該用戶被允許從何地接入。tom@'%' 表示任何地址,默認(rèn)可以省略。還可以是 "tom@192.168.1.%"、"tom@%.abc.com" 等。數(shù)據(jù)庫格式為 db@table,可以是 "test.*" 或 "*.*",前者表示 test 數(shù)據(jù)庫的所有表,后者表示所有數(shù)據(jù)庫的所有表。
子句 "WITH GRANT OPTION" 表示該用戶可以為其他用戶分配權(quán)限。?
補(bǔ)充知識
grant和revoke可以在幾個層次上控制訪問權(quán)限
1,整個服務(wù)器,使用 grant ALL 和revoke ALL
2,整個數(shù)據(jù)庫,使用on database.*
3,特點表,使用on database.table
4,特定的列
5,特定的存儲過程
user表中host列的值的意義
% 匹配所有主機(jī)
localhost localhost不會被解析成IP地址,直接通過UNIXsocket連接
127.0.0.1 會通過TCP/IP協(xié)議連接,并且只能在本機(jī)訪問;
::1 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1
grant 普通數(shù)據(jù)用戶,查詢、插入、更新、刪除 數(shù)據(jù)庫中所有表數(shù)據(jù)的權(quán)利。
grant select on testdb.* to common_user@’%’
grant insert on testdb.* to common_user@’%’
grant update on testdb.* to common_user@’%’
grant delete on testdb.* to common_user@’%’
或者,用一條 MySQL 命令來替代:
grant select, insert, update, delete on testdb.* to common_user@’%’
grant 數(shù)據(jù)庫開發(fā)人員,創(chuàng)建表、索引、視圖、存儲過程、函數(shù)。。。等權(quán)限。
grant 創(chuàng)建、修改、刪除 MySQL 數(shù)據(jù)表結(jié)構(gòu)權(quán)限。
grant create on testdb.* to developer@’192.168.0.%’;
grant alter on testdb.* to developer@’192.168.0.%’;
grant drop on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 外鍵權(quán)限。
grant references on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 臨時表權(quán)限。
grant create temporary tables on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 索引權(quán)限。
grant index on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 視圖、查看視圖源代碼 權(quán)限。
grant create view on testdb.* to developer@’192.168.0.%’;
grant show view on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 存儲過程、函數(shù) 權(quán)限。
grant create routine on testdb.* to developer@’192.168.0.%’; -- now, can show procedure status
grant alter routine on testdb.* to developer@’192.168.0.%’; -- now, you can drop a procedure
grant execute on testdb.* to developer@’192.168.0.%’;
grant 普通 DBA 管理某個 MySQL 數(shù)據(jù)庫的權(quán)限。
grant all privileges on testdb to dba@’localhost’
其中,關(guān)鍵字 “privileges” 可以省略。
grant 高級 DBA 管理 MySQL 中所有數(shù)據(jù)庫的權(quán)限。
grant all on *.* to dba@’localhost’
MySQL grant 權(quán)限,分別可以作用在多個層次上。
1. grant 作用在整個 MySQL 服務(wù)器上:
grant select on *.* to dba@localhost; -- dba 可以查詢 MySQL 中所有數(shù)據(jù)庫中的表。
grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有數(shù)據(jù)庫
2. grant 作用在單個數(shù)據(jù)庫上:
grant select on testdb.* to dba@localhost; -- dba 可以查詢 testdb 中的表。
3. grant 作用在單個數(shù)據(jù)表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;
4. grant 作用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to dba@localhost;
5. grant 作用在存儲過程、函數(shù)上:
grant execute on procedure testdb.pr_add to ’dba’@’localhost’
grant execute on function testdb.fn_add to ’dba’@’localhost’
注意:修改完權(quán)限以后 一定要刷新服務(wù),或者重啟服務(wù),刷新服務(wù)用:FLUSH PRIVILEGES。
IDE工具介紹
生產(chǎn)環(huán)境還是推薦使用mysql命令行,但為了方便我們測試,可以使用IDE工具,不能依賴這種ide
掌握: 1. 測試+鏈接數(shù)據(jù)庫 2. 新建庫 3. 新建表,新增字段+類型+約束 4. 設(shè)計表:外鍵 5. 新建查詢 6. 備份庫/表注意: 批量加注釋:ctrl+?鍵 批量去注釋:ctrl+shift+?鍵pymysql模塊
dos命令安裝 pip3 install pymysql準(zhǔn)備工作
create database userinfo; use userinfo; create table regis(name char(10),password int(10)); insert into regis values('xuxu',123456);建立鏈接、執(zhí)行sql、關(guān)閉(游標(biāo))
import pymysql user=input('用戶名: ').strip() pwd=input('密碼: ').strip()#鏈接 conn=pymysql.connect(host='localhost',user='root',password='1234',database='userinfo',charset='utf8') #游標(biāo) cursor=conn.cursor() #執(zhí)行完畢返回的結(jié)果集默認(rèn)以元組顯示 #cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #這種以字典的形式輸出的可以很直觀的看到字段和記錄#執(zhí)行sql語句 sql='select * from regis where name="%s" and password="%s"' %(user,pwd) #注意%s需要加引號 print(sql) res=cursor.execute(sql) #執(zhí)行sql語句,返回sql查詢成功的記錄數(shù)目 print(res)cursor.close() conn.close()if res:print('登錄成功') else:print('登錄失敗')執(zhí)行結(jié)果: 用戶名: xuxu 密碼: 123456 select * from regis where name="xuxu" and password="123456" 1 登錄成功execute()之sql注入
注意:符號--會注釋掉它之后的sql,正確的語法:--后至少有一個任意字符
根本原理:就根據(jù)程序的字符串拼接name='%s',我們輸入一個xxx' -- haha,用我們輸入的xxx加'在程序中拼接成一個判斷條件name='xxx' -- haha'
最后那一個空格,在一條sql語句中如果遇到select * from t1 where id > 3 -- and name='xuxu';則--之后的條件被注釋掉了1、sql注入之:用戶存在,繞過密碼 xuxu' -- 任意字符2、sql注入之:用戶不存在,繞過用戶與密碼 xxx' or 1=1 -- 任意字符出現(xiàn)過的bug
xxxx后面所跟的單引號或雙引號要看你自己之前定義格式時所用的單引號或雙引號--的兩邊都要有空格才行解決辦法
原來是我們對sql進(jìn)行字符串拼接sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)print(sql)res=cursor.execute(sql)改寫為(execute幫我們做字符串拼接,我們無需且一定不能再為%s加引號了) sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引號,因為pymysql會自動為我們加上 res=cursor.execute(sql,[user,pwd]) #pymysql模塊自動幫我們解決sql注入的問題,只要我們按照pymysql的規(guī)矩來。增、刪、改:conn.commit()
import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標(biāo) cursor=conn.cursor()#執(zhí)行sql語句 #part1 # sql='insert into userinfo(name,password) values("root","123456");' # res=cursor.execute(sql) #執(zhí)行sql語句,返回sql影響成功的行數(shù) # print(res)#part2 # sql='insert into userinfo(name,password) values(%s,%s);' # res=cursor.execute(sql,("root","123456")) #執(zhí)行sql語句,返回sql影響成功的行數(shù) # print(res)#part3 sql='insert into userinfo(name,password) values(%s,%s);' res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) #執(zhí)行sql語句,返回sql影響成功的行數(shù) print(res)conn.commit() #提交后才發(fā)現(xiàn)表中插入記錄成功 cursor.close() conn.close()查:fetchone,fetchmany,fetchall
import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標(biāo) cursor=conn.cursor()#執(zhí)行sql語句 sql='select * from userinfo;' rows=cursor.execute(sql) #執(zhí)行sql語句,返回sql影響成功的行數(shù)rows,將結(jié)果放入一個集合,等待被查詢# cursor.scroll(3,mode='absolute') # 相對絕對位置移動 # cursor.scroll(3,mode='relative') # 相對當(dāng)前位置移動 res1=cursor.fetchone() res2=cursor.fetchone() res3=cursor.fetchone() res4=cursor.fetchmany(2) res5=cursor.fetchall() print(res1) print(res2) print(res3) print(res4) print(res5) print('%s rows in set (0.00 sec)' %rows)conn.commit() #提交后才發(fā)現(xiàn)表中插入記錄成功 cursor.close() conn.close()''' (1, 'root', '123456') (2, 'root', '123456') (3, 'root', '123456') ((4, 'root', '123456'), (5, 'root', '123456')) ((6, 'root', '123456'), (7, 'lhf', '12356'), (8, 'eee', '156')) rows in set (0.00 sec) '''獲取插入的最后一條數(shù)據(jù)的自增ID
import pymysql conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') cursor=conn.cursor()sql='insert into userinfo(name,password) values("xxx","123");' rows=cursor.execute(sql) print(cursor.lastrowid) #在插入語句后查看conn.commit()cursor.close() conn.close()
?
轉(zhuǎn)載于:https://www.cnblogs.com/596014054-yangdongsheng/p/9993725.html
總結(jié)
以上是生活随笔為你收集整理的权限管理,pymysql模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: #pragma comment(link
- 下一篇: umask 和 新建文件、目录的默认权限