MySQL-数据库创建用户并授权远程访问

2,391 阅读1分钟
  • 首先先登录root用户
>>> mysql -uroot -p
  • 然后新建一个用户
>>> create user 'username'@'localhost' identified by 'password';
  • 授权用户远程访问,根据需求为此用户设定权限
# 1) 授权用户访问所有数据库
>>> grant all privileges on  *.* to 'username'@'%' identified by 'TEST123db!';

# 2) 授权用户访问只可访问特定的数据库中的所有表
>>> grant all privileges on  DatabaseName.* to 'username'@'%' identified by 'password';

# 3) 用户只有对特定数据库具有select 、insert 权限
>>> grant select,insert on  DatabaseName.* to 'username'@'%' identified by 'password';

# 4) 用户只有对特定数据库中的特定表具有select 、insert 权限
>>> grant select,insert on  DatabaseName.tablename to 'username'@'%' identified by 'password';
  • 最后,让修改生效,完成
>>> flush privileges;