一、添加用戶
跟以往版本不一樣,MySQL5.7 mysql.user表沒有password字段,這個字段改爲了 authentication_string;
這裏咱們使用命令進行建立用戶:mysql
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
如建立一個test用戶,密碼爲test123,能夠進行遠程登陸:sql
create user 'test'@'%' identified by 'test123'
username - 你將建立的用戶名,
host - 指定該用戶在哪一個主機上能夠登錄,此處的"localhost",是指該用戶只能在本地登陸,不能在另一臺機器上遠程登陸,若是想遠程登陸的話,將"localhost"改成"%",表示在任何一臺電腦上均可以登陸;也能夠指定某臺機器能夠遠程登陸;
password - 該用戶的登錄密碼,密碼能夠爲空,若是爲空則該用戶能夠不須要密碼登錄服務器。數據庫
二、刪除用戶
若是用戶建立錯了,確定要支持刪除操做,使用命令:服務器
DROP USER 'username'@'host';
三、受權
受權test用戶有testDB數據庫的某一部分權限:ide
grant select,update on testDB.* to test@'%' identified by 'test123';
受權test用戶有testDB數據庫的全部操做權限:spa
grant all privileges on testDB.* to 'test'@'%' identified by 'test123';
受權test用戶擁有全部數據庫的某些權限:input
grant select,delete,update,create,drop on *.* to 'test'@'%' identified by 'test123';
privileges - 用戶的操做權限,如select,delete,update,create,drop等(詳細列表可自行百度),若是要授予全部的權限可以使用all(參考第二種受權方式);% 表示對全部非本地主機受權,不包括localhost。
string
案例:
it
mysql> GRANT ALL privileges ON test.* TO usr@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; #設置密碼時要三種字符並存
io
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)
退出,從新用新建立的用戶登陸就成功了