mysql 5.7 学习
Posted 牵牛花
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 5.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的权限,只能连库,啥也不能干
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)修改简单密码:
- mysql> SET PASSWORD FOR ‘root‘@‘localhost‘ = PASSWORD(‘123‘);
- Query OK, 0 rows affected, 1 warning (0.00 sec)
以上是关于mysql 5.7 学习的主要内容,如果未能解决你的问题,请参考以下文章