mysql 5.7 學習
MySQL5.7 添加用戶、刪除用戶與授權
mysql -uroot -proot
MySQL5.7 mysql.user表沒有password字段改 authentication_string;
一. 創建用戶:
命令:CREATE USER ‘username‘@‘host‘ IDENTIFIED BY ‘password‘;
例子: CREATE USER ‘dog‘@‘localhost‘ IDENTIFIED BY ‘123456‘;
CREATE USER ‘dog2‘@‘localhost‘ IDENTIFIED BY ‘‘;
PS:username - 你將創建的用戶名,
host - 指定該用戶在哪個主機上可以登陸,此處的"localhost",是指該用戶只能在本地登錄,不能在另外一臺機器上遠程登錄,如果想遠程登錄的話,將"localhost"改為"%",表示在任何一臺電腦上都可以登錄;也可以指定某臺機器可以遠程登錄;
password - 該用戶的登陸密碼,密碼可以為空,如果為空則該用戶可以不需要密碼登陸服務器。
二.授權:
命令:GRANT privileges ON databasename.tablename TO ‘username‘@‘host‘
PS: privileges - 用戶的操作權限,如SELECT , INSERT , UPDATE 等(詳細列表見該文最後面).如果要授予所的權限則使用ALL.;databasename - 數據庫名,tablename-表名,如果要授予該用戶對所有數據庫和表的相應操作權限則可用*表示, 如*.*.
例子: GRANT SELECT, INSERT ON mq.* TO ‘dog‘@‘localhost‘;
三.創建用戶同時授權
mysql> grant all privileges on mq.* to [email protected] identified by ‘1234‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
PS:必須執行flush privileges;
否則登錄時提示:ERROR 1045 (28000): Access denied for user ‘user‘@‘localhost‘ (using password: YES )
四.設置與更改用戶密碼
命令:SET PASSWORD FOR ‘username‘@‘host‘ = PASSWORD(‘newpassword‘);
例子: SET PASSWORD FOR ‘dog2‘@‘localhost‘ = PASSWORD("dog");
五.撤銷用戶權限
命令: REVOKE privilege ON databasename.tablename FROM ‘username‘@‘host‘;
說明: privilege, databasename, tablename - 同授權部分.
例子: REVOKE SELECT ON mq.* FROM ‘dog2‘@‘localhost‘;
PS: 假如你在給用戶‘dog‘@‘localhost‘‘授權的時候是這樣的(或類似的):GRANT SELECT ON test.user TO ‘dog‘@‘localhost‘, 則在使用REVOKE SELECT ON *.* FROM ‘dog‘@‘localhost‘;命令並不能撤銷該用戶對test數據庫中user表的SELECT 操作.相反,如果授權使用的是GRANT SELECT ON *.* TO ‘dog‘@‘localhost‘;則REVOKE SELECT ON test.user FROM ‘dog‘@‘localhost‘;命令也不能撤銷該用戶對test數據庫中user表的Select 權限.
具體信息可以用命令SHOW GRANTS FOR ‘dog‘@‘localhost‘; 查看.
六.刪除用戶
命令: DROP USER ‘username‘@‘host‘;
七.查看用戶的授權
mysql> show grants for [email protected];
+---------------------------------------------+
| Grants for [email protected] |
+---------------------------------------------+
| GRANT USAGE ON *.* TO ‘dog‘@‘localhost‘ |
| GRANT INSERT ON `mq`.* TO ‘dog‘@‘localhost‘ |
+---------------------------------------------+
2 rows in set (0.00 sec)
PS:GRANT USAGE:mysql usage權限就是空權限,默認create user的權限,只能連庫,啥也不能幹
ps 之前收集了很多mysql 的命令,結果今天在mysql 5.7中失效了,因此在整理一下mysql> CREATE USER ‘root‘@‘192.168.1.17‘ IDENTIFIED BY ‘root‘;
Query OK, 0 rows affected (0.00 sec)
創建用戶
mysql> CREATE USER ‘root‘@‘%‘ IDENTIFIED BY ‘root‘;
Query OK, 0 rows affected (0.00 sec)
root 用戶可以在任何機器上登錄,且擁有任何表的任何權限
mysql> grant all privileges on *.* to [email protected]‘%‘ identified by ‘root‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)
- mysql> set global validate_password_policy=0;
- Query OK, 0 rows affected (0.05 sec)
- mysql>
- mysql>
- mysql> set global validate_password_mixed_case_count=0;
- Query OK, 0 rows affected (0.00 sec)
- mysql> set global validate_password_number_count=3;
- Query OK, 0 rows affected (0.00 sec)
- mysql> set global validate_password_special_char_count=0;
- Query OK, 0 rows affected (0.00 sec)
- mysql> set global validate_password_length=3;
- Query OK, 0 rows affected (0.00 sec)
- mysql> SHOW VARIABLES LIKE ‘validate_password%‘;
- +--------------------------------------+-------+
- | Variable_name | Value |
- +--------------------------------------+-------+
- | validate_password_dictionary_file | |
- | validate_password_length | 3 |
- | validate_password_mixed_case_count | 0 |
- | validate_password_number_count | 3 |
- | validate_password_policy | LOW |
- | validate_password_special_char_count | 0 |
- +--------------------------------------+-------+
- 6 rows in set (0.00 sec)
FLUSH PRIVILEGES;
4)修改簡單密碼:
[sql] view plain copy
- mysql> SET PASSWORD FOR ‘root‘@‘localhost‘ = PASSWORD(‘123‘);
- Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql 5.7 學習