MySQL: connecting to different ports on localhost

From lxadm | Linux administration tips, tutorials, HOWTOs and articles
Jump to: navigation, search

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:

# mysql -s -h localhost --port=3332
MariaDB [(none)]>

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:

# mysql -s -h 127.0.0.1 --port=3332
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")

# mysql -s -h 127.0.0.1 --port=3333
MariaDB [(none)]>