Mysql 开启远程访问 给指定数据库创建用户并授权

Mysql 8.0

#创建用户 db_user 换成root表示root用户
MySQL [(none)]> create user db_user@'%' identified by 'db_pass';

#授权  db_name 用*替代表示访问所有数据库
MySQL [(none)]> grant all privileges on db_name.* to db_user@'%' with grant option; 

#退出数据库控制台,特别注意有分号
MySQL [(none)]> exit;

Mysql 8.0以下

MySQL [(none)]> grant all privileges on db_name.* to db_user@'%' identified by 'db_pass';
 
#授权语句,特别注意有分号
MySQL [(none)]> flush privileges;

#退出数据库控制台,特别注意有分号
MySQL [(none)]> exit;

创建用户

create user 'username'@'%' identified by 'password';
username 用户名 %所有ip password 密码

授权

grant all privileges on dbname.* to 'username'@'%';

flush privileges;