mysql 5.7.21 新建用户、授权 [注意区分反引号和单引号]

一、新建用户

  1. 只允许本地访问 localhost, 127.0.0.1
create user 'cat'@'localhost' identified by '123456';  
  1. 允许外网 IP 访问
create user 'cat'@'%' identified by '123456';

二、创建数据库

create database cat_db DEFAULT CHARSET utf8 COLLATE utf8_general_ci; 

三、给新用户分配数据库权限

  1. 授予外网IP对数据库的所有权限 注意: 看清单引号还是反引号
-- 如下两种写法在mysql 5.7.21 下都是可以的
grant all privileges on cat_db.* to 'cat'@'%' ; 
grant all privileges on cat_db.* to cat@'%' ; 
-- 下面 `cat_db` 用的是反引号而不是单引号
grant all privileges on `cat_db`.* to 'cat'@'%' ; 
  1. 注意 以下是错误写法
-- 注意:以下的写法(全部单引号)全部不行  不行  不行
grant all privileges on 'cat_db'.* to 'cat'@'%' ; 
grant  privileges on 'cat_db'.* to 'cat'@'%' ; 
grant  privileges on cat_db.* to 'cat'@'%' ; 
grant  privileges on cat_db.* to cat@'%' ; 

四、刷新权限

flush privileges; 

猜你喜欢

转载自blog.csdn.net/qq_32138419/article/details/89362837