[Resolved] 2002-can't connect to server on 192.168.xx.xx (10061) MySQL database cannot be connected remotely

The MySQL database cannot be connected to the MySQL server in the Linux system. Let me summarize the pitfalls I have encountered. I hope partners can pay attention to it.

The virtual machines and services I use, the database client link tool

VM

docker 

MySQL57

navicat

What are the following common reasons for the above problems?

1. The firewall in Linux is not turned off

Turn off firewall command

systemctl stop firewall #Temporarily close the firewall 
systemctl disable firewall #Permanently close the firewall

2. The port number in the remote MySQL is inconsistent with the port number on navicat

Log in to MySQL using the following command

docker exec -it mysql /bin/bash enters the container

Login to MySQL

mysql -u username -p password -P port number -h (ip address)

After logging in to the database, view the port number of the current database

show global variables like 'port';

 

If the port number is incorrect, change the port number in navicat.

 

3. It may be that the firewall in the server is not open, causing the connection to fail.

Submit a rule to allow port 3306

iptables -I INPUT -p top --aport 3306 -j ACCEPT

4. The port is not monitored

View network port information in the virtual machine:

# netstat -ntpl 

After checking the firewall status, it was found that the data packets on port 3306 were all lost.

# iptables -vnl

Clear firewall rules

# iptables -F

5. The root permissions in the MySQL database are insufficient. It may be that the localhost remote service cannot recognize it.

Method 1: Direct authorization (recommended)

Log in to mysql

Execute the following command

Set permission command

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
从任何主机上使用root用户,密码:你的password(你的root密码)连接到mysql服务器:
  # mysql -u root -proot
  mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OPTION

Method 2: Log in to mysql locally, change the "host" item in the "user" table in the "mysql" database, and change "localhost" to "%"

#mysql -u root -proot
  mysql>use mysql;
  mysql>update user set host = '%' where user = 'root';
  mysql>select host, user from user;

6. Use docker ps -a to check whether the process in MySQL is running normally.

 I have restarted here. If you have not restarted, enter the following command...

docker start mysql或ID号  #开启mysql服务

docker start mysql或ID号  #关闭mysql服务

docker restart mysql或ID号  #重启mysql服务

Use docker ps -a again to check whether mysql is started

7. Check whether the virtual machine is connected to the Internet normally in the Network Sharing Center in Windows. If it is not connected to the network, please start the network.

1) Windows 11 turns on the virtual machine network adapter

 

 

 Enable the above virtual machine network

2) Also check the network status in the virtual machine

So far

The database mysql can be connected successfully. The above is personally verified. I hope it can bring inspiration to everyone. Even if the relevant problems are not solved, it will also provide inspiration to everyone so that you will not get lost. 

Guess you like

Origin blog.csdn.net/weixin_43608968/article/details/129341893