1.修改配置文件
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
把bind-address参数的值改成你的内/外网IP或0.0.0.0,或者直接注释掉这行.
2.登录数据库
mysql -u root -p XXXXX
3.查询host
use mysql;
select user,host from user;
4.创建host
如果没有”%”这个host值,就执行下面这两句(或者创建各一个新的用户):
mysql> update user set host='%' where user='root';
mysql> flush privileges;
mysql> CREATE USER 'test'@'%' IDENTIFIED BY 'password';
mysql> flush privileges;
5.授权用户
任意主机以用户root和密码mypwd连接到mysql服务器
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;
mysql> flush privileges;
IP为192.168.1.29 的主机以用户test和密码XXXX连接到mysql服务器,并且只能操作test数据库
mysql> GRANT ALL PRIVILEGES ON test.* TO 'test'@'192.168.1.29' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;
mysql> flush privileges;
PS:如果还不行,检查一下防火墙是否开启 3306端口