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