1.登录到mysql: mysql -uroot -ppwd
2.查看user表:
mysql> use mysql
Database changed
mysql> select Host,User from user;
+------+------+-------------------------------------------+
| host | user |
+------+------+-------------------------------------------+
| localhost | root |
+------+------+-------------------------------------------+
1 row in set (0.00 sec)
第一种(改表法):
修改host字段的值,将localhost修改成需要远程连接数据库的ip地址。或者直接修改成%。修改成%表示,所有主机都可以通过root用户访问数据库。为了方便,我直接修改成%。命令:mysql> update user set host = '%' where user = 'root';
再次查看user表
+------+------+-------------------------------------------+
| host | user |
+------+------+-------------------------------------------+
| % | root |
+------+------+-------------------------------------------+
1 row in set (0.00 sec)
修改成功,输入命令mysql> FLUSH PRIVILEGES; 回车使刚才的修改生效,再次远程连接数据库成功。