In the MySQL multi-instance environment, the problem of connecting to the specified instance locally on the server side (sock connection)

 

A problem related to sock connection.

 

In order to test some features of MySQL, multiple MySQL instances are installed on one machine. As shown in the screenshot below, there are two instances, one port is 8000 and the other port is 8001.
When using mysql -uroot -p -P8001 to connect to the MySQL instance with port number 8001, show variables like '%port%' shows that the connection is to the instance 8000, which is inexplicable?
The author was also taken aback by this problem at first, and then I figured out that it was the problem of TCP connection and sock connection.

 

 

This involves a sock connection problem. When connecting to a MySQL instance locally (that is, the server side), the default is to use sock to connect, and the default sock file is found from the /tmp/mysql.sock path.

That is to say, when connecting to the MySQL instance on the server side,
1, the default is to connect to the MySQL instance
2 in the sock mode. If the host address is not specified, that is, the -h parameter, it is a parameter that ignores the port number. value, which is the value of -P.
For example, using the above mysql -uroot -p -P8001 method to connect to the database instance, the default is to use the sock connection (the sock parameter -S is not specified, the default /tmp/mysql.sock), and the port number parameter is ignored, also That is, -P8001
, such as the following screenshot, -P specifies a port number that does not exist at all, and still connects to an instance normally, which instance depends on which instance the default /tmp/mysql.sock points to (in the case of multiple instances )

The principle of sock is not difficult to understand: the sock file itself records the process id of a MySQL instance, and the mysql command directly connects to the instance locally through the process id in the sock, because the port number can be ignored.

 

How do I connect to a specific instance locally on the server?

1. Clearly specify the path of the sock file in the connection name, no need to specify the port number (mysql -uroot -p -S/***/mysql.sock)

 

 

 2. Connect by specifying port number + host address (specifying TCP): mysql -uroot -p -P*** -h127.0.0.1

 

Write it down, there are more than N instances installed on one machine, and they accidentally fall into the pit again...

 

When connecting to the MySQL instance locally, that is, the server side, if the host address is not specified, the connection is made by sock by default, and when sock is not specified, the corresponding sock file is found according to the default path
. The port number parameter in the connection command will be ignored, which may cause some confusion if you are not careful.
A similar situation occurs only when connecting from the server side. If it is connected from the client side (non-server side), the IP will definitely be specified. Once the IP is specified, the corresponding port number will take effect, so the client will not happen. Similar situation.

I have always been afraid of some default configurations, which actually cover up a lot of problems.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324884203&siteId=291194637