Problem Scenario
Using Docker locally to launch a MySQL container and map the local 33067
port to the container MySQL 3306
, we assume that the IP address of the container is to 172.17.0.2
use the local MySQL client to try to connect the MySQL service inside the container.
Method 1
Because it is a local 33067 port, specify the port number when you connect.
At this point Mysql-client actually enters the local MySQL service and uses status
commands to view
12345678910111213141516 |
Connection id:49current database:current User: [email protected]ssl:not in use current pager:stdoutusing Outfil E: 'Using delimiter:; Server version:5.5.52-0ubuntu0.14.04.1 (Ubuntu) Protocol version:10connection:localhost via UNIX socketserver Characterset:latin1db characterset:latin1client Characterset:utf8conn. Characterset:utf8unix socket:/var/run/mysqld/mysqld.sockuptime:1 Hour min. sec |
The connecttion information is a local UNIX socket, so the connection is not connected to MySQL within the container.
Method 2
Attempt to specify the local port corresponding to the host and map, because the local port is mapped, host is localhost
1 |
mysql-uroot-hlocalhost-p33067 |
The local MySQL service is still connected.
Method 3
Use the IP address of the container directly, the default port is 3306
1 |
mysql-uroot-h172.17.0.2 |
The connection succeeds and enters the MySQL service inside the container.
Method 4
Use the 127.0.0.1 address and specify the locally mapped port 33067
1 |
mysql-uroot-h127.0.0.1-p33067 |
The connection succeeds and enters the MySQL service inside the container.
Analysis
Method 1 and Method 2 connect the host is actually localhost (do not specify host, default is localhost), at this time regardless of what the port specifies, mysql-client try to use the UNIX socket communication method, that is, the local socket , so if you want to connect the MySQL service for the specified IP, you must specify the IP, even if the IP map is localhost.
If the scenario above is mapped to a specific port on-premises due to the MySQL service port in the container, even if this is the case, the connection still needs to specify the local IP 127.0.0.1 instead of the local hostname localhost, because once localhost is used, Mysql-client tries to connect to the local MySQL service using a local UNIX socket.
As a result, MySQL does not specify the IP connection by default is localhost, trying to connect to the local MySQL service, regardless of the port is.
Reference: http://sanyuesha.com/2016/10/14/mysql-connect-localhost-and-ip-difference/#more
MySQL does not specify the IP connection by default is localhost