解决连接不到服务器MySQL问题
1. 查看use表
mysql -u root -p
mysql> use mysql;
mysql> select host,user,password from user;
+--------------+------+-------------------------------------------+
| host | user | password |
+--------------+------+-------------------------------------------+
| localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
+--------------+------+-------------------------------------------+
host列控制可访问IP
2. 开启远程连接
将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。
mysql> use mysql;
mysql> grant all privileges on *.* to root@'%' identified by "password";
mysql> flush privileges;
mysql> select host,user,password from user;
+--------------+------+-------------------------------------------+
| host | user | password |
+--------------+------+-------------------------------------------+
| localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| % | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
+--------------+------+-------------------------------------------+
这样机器就可以以 用户名:root 密码:password 远程访问该机器上的MySql.
3. 远程访问方式
(1)命令行
mysql -h ip -P port -u 用户名 -p
Enter password:密码
(2) Navicat Premium 可视化数据库连接工具
4. 修改后依旧无法访问
检查防火墙是否开启,禁止访问.
/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT #开启3306端口