Common reasons why MySQL cannot be connected remotely

foreword

MySQL is one of the most popular databases at present, and it is also the database of choice for persistent storage of small and medium-sized enterprises.

Different from our daily study, in practical application, the MySQL service will be mounted on a certain server. If MySQL is deployed on a certain cloud server, in this way, it is not very convenient to connect to the server every time to operate the database, and then enter the database operation.

Therefore, learning how to connect to MySQL remotely is a compulsory course when the database is on the server. Below I will describe three common reasons for remote connection errors.


Reason 1:

The remote access permission of MySQL is not open

If the most common problem of being unable to connect remotely is because the remote access permission is not enabled.

solution:

Enter MySQL, use the following command to view the remote access authority, etc., just create the user’s remote access authority and refresh the command. Don’t forget to refresh the command every time you modify the command.

Common commands are as follows:

# 查看当前远程连接的权限
select User,authentication_string,Host from user;
# 创建用户远程访问权限
create user root@'%' identified by '123456';
# 允许指定主机(IP地址)访问权限
grant all privileges on test.* to '用户名'@'ip地址' identified by '密码';
# 无法创建请删除重新试一遍
drop user root@'ip地址';
# 赋予用户操作的全部权限
grant all privileges on *.* to root@'%';
# 刷新指令
flush privileges;

Reason 2:

The firewall is enabled on the server, making the MySQL access port unusable

If the firewall is enabled on the server and the MySQL port opening policy is not set, then you will not be able to remotely access MySQL through the corresponding port.

solution:

Close the firewall, or open the corresponding MySQL port, for example, the default MySQL port is 3306, just open it.

Common commands are as follows:

# 查看防火墙状态
firewall-cmd --state
# 启动防火墙
systemctl start firewalld.service
# 关闭防火墙
systemctl stop firewalld.service
# 设置开机自启动
systemctl enable firewalld.service
# 开放防火墙端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重新加载配置
firewall-cmd --reload
# 查看开放的防火墙端口
firewall-cmd --zone=public --list-ports

Reason 3:

The docker chain is cleared, making MySQL inaccessible

If your MySQL is deployed in a docker container, and the firewall is enabled after the deployment is complete. At this time, the docker chain may be cleared. Even if you enable remote access, you can only access MySQL by entering the container, and you cannot access it remotely.

solution:

Restart docker, you can restore the docker chain.

# 重启docker
systemctl restart docker

Guess you like

Origin blog.csdn.net/weixin_50762970/article/details/126281684