標籤:並且 管理員 使用 參數說明 upd 5.7 直接 class auth
前言
From :78214336
mysql5.7版本中使用者管理與以前版本略有不同,在此記錄,以備忘
登陸
[[email protected] ~]# mysql -h 127.0.0.1 -P 3316 -u root -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 9Server version: 5.7.17 Source distributionCopyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql>
參數說明:
-h: 指定資料庫IP地址;
-P: 指定連接埠,預設的3306時,可以忽略;
-u: 指定登陸使用者名稱;
-p: 指定登陸密碼(小寫,注意與指定連接埠的大寫P區分);
指定操作資料庫
mysql> show databases; # 查看所有資料庫+--------------------+| Database |+--------------------+| information_schema || fhgk || mysql || performance_schema || sys |+--------------------+5 rows in set (0.01 sec)mysql> use mysql # 指定當前操作的資料庫Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql>
建立使用者
# 建立使用者mysql> CREATE USER ‘username‘@‘host‘ IDENTIFIED BY ‘password‘;# 刪除使用者mysql> DROP USER ‘username‘@‘host‘;
host參數說明:
% 匹配所有主機
localhost localhost不會被解析成IP地址,直接通過UNIXsocket串連
127.0.0.1 會通過TCP/IP協議串連,並且只能在本機訪問;
::1 ::1就是相容支援ipv6的,表示同ipv4的127.0.0.1
此時還沒有授權,只能登陸,無法做其餘操作
使用者授權
# 使用者授權mysql> grant privileges ON databasename.* TO ‘username‘@‘host‘;# 建立使用者的同時授權mysql> grant all privileges on databasename.* to ‘username‘@‘host‘ identified by ‘1234‘;# 授權重新整理mysql> flush privileges;# 查看使用者擁有許可權mysql> show grants for [email protected]‘%‘;+----------------------------------------------------------------------+| Grants for [email protected]% |+----------------------------------------------------------------------+| GRANT USAGE ON *.* TO ‘dev‘@‘%‘ || GRANT SELECT, INSERT, UPDATE, DELETE, ALTER ON `fhgk`.* TO ‘dev‘@‘%‘ |+----------------------------------------------------------------------+2 rows in set (0.00 sec)# 撤消使用者授權,撤消要求各參數與授權時使用的一致,可以相查看授權再撤消mysql> revoke privileges ON databasename.* FROM ‘username‘@‘host‘;
privileges參數說明: all privileges: 所有許可權; select: 查詢; insert: 新增記錄; update: 更新記錄; delete: 刪除記錄; create: 建立表; drop: 刪除表; alter: 修改表結構; index: 索引相關許可權; execute: 執行預存程序與call函數 references: 外鍵相關; create temporary tables:建立暫存資料表; lock tables 鎖表; create view 建立視圖; show view 查看視圖結構; create routine alter routine: event: trigger: 觸發器相關;
databasename.*參數說明:
此處可以針對具體的某個庫,如:【zjims.*】;
也可以針對具體庫中的某個對象,如:【zjims.t_user】;
還可以針對所有資料庫,如:【.】;
修改密碼
# 修改自己的密碼mysql> set password=password(‘newpassword‘);# 修改別人密碼——方法1mysql> set password for ‘username‘@‘host‘ = password(‘newpassword‘);# 修改別人密碼——方法2: 適用mysql5.7以前的版本,5.7以後的版本中mysql.user表沒有了password欄位mysql> update mysq.user set password=password(‘newpassword‘) where user=‘user‘ and host=‘host‘;# 修改別人密碼——方法3:適用mysql5.7mysql> update mysql.user set authentication_string=password(‘newpassword‘) where user=‘root‘;# 修改別人密碼——方法4mysql> alter user ‘test‘@‘%‘ identified by ‘newpassword‘;
重設管理員密碼
- 停止mysql服務:service mysqld stop 或 ./mysql.server stop;
- 以不檢查許可權方式啟動mysql:./mysqld –skip-grant-tables –user=mysql &;
- 以空密碼方式登陸:mysql -h 127.0.0.1 -P 3306 -u root;
- mysql5.7以前版本——修改root密碼:update mysq.user set password=password(‘newpassword’) where user=’root’;
- mysql5.7以後版本——修改root密碼:update mysql.user set authentication_string=password(‘newpassword’) where user=’root’;(只能用此種update方法修改)
- 重新整理許可權:flush privileges;
- 關閉mysql:shutdown;
- 以正常方式啟動mysql: service mysqld start 或 ./mysql.server start;
參考資料
- http://www.cnblogs.com/fslnet/p/3143344.html
- http://www.cnblogs.com/xujishou/p/6306765.html
- http://www.cnblogs.com/4php/p/4113593.html
[轉帖] mysql 使用者 許可權 密碼等操作記錄