Solution to remote connection Mysql failure

Today, I spent a whole night playing with mysql on the virtual machine Ubuntu, and then tried to connect using java. It took a long time without success, but the connection was successful on the Debian configured by my classmate, which means there is something wrong with my configuration.

After struggling for a long time, I finally made a rough guess by understanding the abnormal information.

For remote connection, enter the IP and port of the host where mysql is located to determine the logical address of the host, and then determine which user to log in through the user and password. for example:

String url = "jdbc:mysql://192.168.183.134:3306/mysql";

That is to first connect to the host with IP 192.168.183.134 at the network layer, and then connect to the specific port 3306 (transport layer) of this host. Then after the connection is established at the transport layer, log in with the account and password at the application layer to access the mysql database:

Connection conn = DriverManager.getConnection(url, user, password);

At first, I suspected that there was a problem with the port settings, and then I added port=3306 to /etc/mysql/my.cnf. However, the connection still failed and the port access was denied.

Then use the command$netstat -apn to find a piece of information about port 3306:

tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN

A closer look shows that the IP provided here is 127.0.0.1. Based on socket programming experience, generally when receiving messages from any host, the IP will be set to 0.0.0.0, and does 127.0.0.1 (loopback address) mean that it can only Local access? Then when I opened my classmate's Debian to check the ports occupied by his mysql, it was exactly what I thought:

tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN

After some searching, I concluded that mysql only provides local access by default, and additional settings are required to enable remote access. This also makes sense from a security perspective.

The setting method is still to modify the configuration file /etc/mysql/my.cnf

[client]
default-character-set=utf8
[mysqld]
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
port=3306
bind-address=0.0.0.0

What is related to utf8 is that when entering Chinese character data locally, it was found that the insertion failed. Hanzi and VARCHAR are not compatible. When entering STATUS in mysql mode to check the status:

Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8

After this modification, the first two character sets have all become utf8, supporting Chinese. The other modifications are as I said, bind the IP to 0.0.0.0, that is, accept connections from hosts with any address, and bind the port to 3306.

But it still cannot be accessed, but the error message has changed. This time it is clear that it shows that the user does not have permission to connect, which means that the user can only connect locally. After searching for information, the solution is as follows:

1. Create a new user to remotely connect to the mysql database

mysql> grant all on *.* to team@'%' identified by 'java123' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

2. Support root user to allow remote connection to mysql database

mysql> grant all on *.* to 'root'@'%' identified by 'cplusplus' with grant option;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

Reference: How to configure mysql to allow remote connections

The solution to the failure of remote connection to Mysql. The solution to the failure of remote connection to Mysql.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/134326862