mysql5.7 新建远程用户,Centos7中 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。

案例:

GRANT ALL privileges ON test.* TO usr@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; #设置密码时要三种字符并存

Query OK, 0 rows affected, 1 warning (0.01 sec)

flush privileges; #刷新表权限

Query OK, 0 rows affected (0.01 sec)

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)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值