If you want to use mysql command to connect to MySQL server running on localhost, but on nonstandard port (not 3306), you may notice that whatever port you specify, it will connect you to the default MySQL server running on port 3306:
This is because if you specify "-h localhost" - it will connect you to MySQL socket, and --port will be ignored (no TCP/IP will be used, and hence the --port option has no meaning).
To connect to a different port, simply use an address which is not "localhost", but 127.0.0.1:
To connect to different MySQL servers on different ports on localhost, you can specify the port number in the hostname when connecting to the MySQL server. For example:
Copy codemysql -h localhost -P <port> -u <username> -p
Replace <port>
with the port number of the MySQL server you want to connect to, and <username>
with your MySQL username. You will be prompted to enter your password.
If you are using a MySQL client such as MySQL Workbench, you can specify the port number in the "Connection Method" field under "Connection Properties".
Alternatively, you can specify the port number in the my.cnf
configuration file, which is located in the /etc/mysql
directory on most systems. Add the following lines to the file, replacing <port>
with the desired port number:
Copy code[client] port = <port>
After making these changes, you should be able to connect to different MySQL servers on different ports on localhost.