1、添加用戶
跟以往版本不同,MySQL5.7 mysql.user表沒有password字段,這個字段改成了 authentication_string;
這里我們使用命令進行創建用戶:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
如創建一個test用戶,密碼為test123,可以進行遠程登錄:
create user 'test'@'%' identified by 'test123'
username - 你將創建的用戶名,
host - 指定該用戶在哪個主機上可以登陸,此處的"localhost",是指該用戶只能在本地登錄,不能在另外一台機器上遠程登錄,如果想遠程登錄的話,將"localhost"改為"%",表示在任何一台電腦上都可以登錄;也可以指定某台機器可以遠程登錄;
password - 該用戶的登陸密碼,密碼可以為空,如果為空則該用戶可以不需要密碼登陸服務器。
2、刪除用戶
如果用戶創建錯了,肯定要支持刪除操作,使用命令:
DROP USER 'username'@'host';
3、授權
授權test用戶有testDB數據庫的某一部分權限:
grant select,update on testDB.* to test@'%' identified by 'test123';
授權test用戶有testDB數據庫的所有操作權限:
grant all privileges on testDB.* to 'test'@'%' identified by 'test123';
授權test用戶擁有所有數據庫的某些權限:
grant select,delete,update,create,drop on *.* to 'test'@'%' identified by 'test123';
privileges - 用戶的操作權限,如select,delete,update,create,drop等(詳細列表可自行百度),如果要授予所有的權限可使用all(參考第二種授權方式);% 表示對所有非本地主機授權,不包括localhost。
案例:
mysql> GRANT ALL privileges ON test.* TO usr@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; #設置密碼時要三種字符並存
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges; #刷新表權限
Query OK, 0 rows affected (0.01 sec)
[root@instance-ozyu8y37 ~]# mysql -ugeek -p
Enter password:
ERROR 1045 (28000): Access denied for user 'geek'@'localhost' (using password: YES) #報錯是說在geek用戶在本地登錄需要密碼,而我剛剛沒設置本地登錄,所以進入root設置
[root@instance-ozyu8y37 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1261
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT ALL privileges ON test.* TO usr@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
退出,重新用新創建的用戶登錄就成功了