mysql5.7 用户 创建 、授权、远程登录

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)

退出,重新用新创建的用户登录就成功了

猜你喜欢

转载自blog.csdn.net/weixin_41808843/article/details/88979786