mysql8+有新的安全要求,不能像之前的版本那樣一次性創建用戶並授權需要先創建用戶,再進行授權操作
創建用戶:
use mysql; //mysql用戶表在 mysql.user表中。
create user 'username'@'localhost' identified by 'pass123'; //創建用戶命令 create user 'username'@'%' identified by 'pass123';
username:自己創建的用戶名;
'%' :哪台主機上可以登錄mysql,%是通配符指的是任意IP,也可以指定具體的IP,或者localhost代表本機才可以登錄。
查看新創建用戶username的權限
select * from user where user = 'username'\G; 可以查看新創建的用戶;
show grants for username@localhost; //show grants for 'username'@'localhost';
mysql> show grants for 'username'@'localhost';
+----------------------------------------------+
| Grants for username@localhost |
+----------------------------------------------+
| GRANT USAGE ON *.* TO `username`@`localhost` |
+----------------------------------------------+
為username@localhost用戶賦予超級用戶權限:
grant all privileges on *.* to 'username'@'localhost' with grant option;
grant:授權
all privileges:所有的權限
on *.*:在哪個數據庫的那個表
to username@localhost:對哪個用戶的哪個主機
with grant option: 是不是 將username用戶自己本身的權限賦給其他賬戶
用 grant給一些用戶添加權限:
普通用戶權限添加如下:
grant usage,select,insert,update,delete,create temporary tables,execute on jikedb.* to username@localhost; //此時沒有with grant option 表示不給其他用戶賦權限
flush privileges;
usage:無權限,當你想創建一個沒有權限的用戶時候,指定usage
show:的權限
view:視圖的權限(mysql8.0+賦權限出錯)ERROR 3619 (HY000): Illegal privilege level specified for VIEW
create temporary tables:創建臨時表的權限
excute:執行的權限
收回權限的命令:
revoke delete on jikedb.* from username@localhost; //意思是收回username@localhost下jikedb庫所有的表的刪除操作
新創建的用戶username@localhost 要想使用,登錄后需要修改密碼如下:
刪除用戶:
drop user username@localhost; //username,localhost加不加引號都可以