踩坑!Navicat连接云服务器数据库
前言:
使用Navicat连接远程服务器经常遇到各种问题,在这里记录一下。
云服务器:CentOS 7.9
Mysql:8.0.24

步骤:
云服务器官网配置防火墙
以腾讯云为例,在防火墙配置中,确认开启了3306 Mysql服务端口

2. 进入云服务环境,配置Mysql授权
2.1:默认情况下,mysql用户不允许从远程登录,只能在localhost登录。需要我们进入mysql进行授权。
登录mysql:mysql - u root -p
切换mysql数据库:use mysql;
查询user表用户信息:select user,host from user;

默认情况下我们的root用户的host是localhost,为了可以远程连接mysql,需要将host设置为%。(%表示任何ip都可以连接到mysql)
修改root用户的host:
update user set host='%' where user='root' and host='localhost';
赋予任何主机上以root身份访问数据的权限:
grant all privileges on *.* to root@'%' with grant option;
FLUSH PRIVILEGES;
然后退出mysql环境,在本地打开Navicat,输入云服务的Mysql用户名和密码即可。

3. 理论上按照上述步骤就可以连接成功了,但是如果仍然无法连接成功,请继续往后看看楼主踩过的坑。。。
常见问题:
Can't connect to MySQL server on 'xx.xx.xx.xx' (10060 "Unknown error")
Host 'xx.xx.xx.xx' is not allowed to connect to this Mysql server
Host'::1' is not allowed to connect to this MySQL server


首先要确保上述云服务器防火墙,mysql配置已经正确配置。
然后进入到云服务器环境中,找到自己的mysql配置文件,一般是在etc目录下的my.cnf。
在[mysqld]下新增 bind-address=0.0.0.0
代表允许所有IP访问Mysql。然后重启Mysql服务: systemctl restart mysql

如何Navicat仍然无法连接Mysql,请使用万能大法,重启云服务器,重启Navicat,卸载重装Mysql。
