设置MySQL允许外网访问

1.修改配置文件
#5.7
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

#5.6及之前的版本
sudo vim /etc/mysql/my.cnf

#把bind-address参数的值改成你的内/外网IP或0.0.0.0,或者直接注释掉这行。

#8.0在Windows下

进入C:\ProgramData\MySQL\MySQL Server 8.0,打开my.cnf,如果没有bind-address,直接新增一行(bind-address=0.0.0.0)。

 

2.登录数据库
mysql -u root -p
#输入密码


3.转到系统数据库
mysql> use mysql;


4.查询host
mysql> select user,host from user;


5.创建host
#如果没有"%"这个host值,就执行下面这两句:
mysql> update user set host='%' where user='root';
mysql> flush privileges;


6.授权用户
#任意主机以用户root和密码123456连接到mysql服务器
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;

#IP为192.168.1.100的主机以用户myuser和密码123456连接到mysql服务器
mysql> grant all privileges on *.* to 'myuser'@'192.168.1.100' identified by '123456' with grant option;

 

* 在8.0以上的版本中,要去掉“identified by ‘密码’”

mysql> grant all privileges on *.* to 'root'@'%' with grant option;


7.刷新权限
mysql> flush privileges;

 

 

补充说明

问题
在MySQL8.0以上的版本中,当使用grant权限列表on数据库to '用户名'@'访问主机' identified by '密码'时会出现以下错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'ident
ified by '密码' with grant option' at line 1

 

原因

8.0以上版本已经将创建账户和赋予权限的方式分开了

 

解决办法

创建账户:create user '用户名'@'访问主机' identified by '密码';
赋予权限:grant 权限列表 on 数据库 to '用户名'@'访问主机';(修改权限时在后面加with grant option)

 

posted @ 2020-10-18 01:47  Clotho_Lee  阅读(577)  评论(0编辑  收藏  举报