Connecting to a TCP port using mysql

Simple Answer

Use -h127.0.0.1 instead of -hlocalhost when invoking mysql.

Details

When typing mysql on the command-line, by default it connects to a socket. This also happens when typing mysql -hlocalhost. It even happens when specifying a TCP port number mysql -hlocalhost -P3306. Simply type \s to see what server mysql is connected to. Here's an example:

$ mysql -hlocalhost -P3306
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> \s
--------------
mysql  Ver 14.12 Distrib 5.0.77, for redhat-linux-gnu (x86_64) using readline 5.1

Connection id:          1
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.0.77 Source distribution
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 55 days 2 hours 20 min 48 sec

Threads: 2  Questions: 188848126  Slow queries: 1  Opens: 247  Flush tables: 1  Open tables: 64  Queries per second avg: 39.670

The Connection field shows that we are indeed connected to a UNIX socket. However, if we type the dotted-decimal, it is connected to a TCP port:

$ mysql -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> \s
--------------
mysql  Ver 14.12 Distrib 5.0.77, for redhat-linux-gnu (x86_64) using readline 5.1

Connection id:          12
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.0.77 Source distribution
Protocol version:       10
Connection:             127.0.0.1 via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
TCP port:               3306
Uptime:                 55 days 2 hours 24 min 45 sec

Threads: 2  Questions: 188848139  Slow queries: 1  Opens: 247  Flush tables: 1  Open tables: 64  Queries per second avg: 39.668
--------------

Notice that the Connection now says 127.0.0.1 via TCP/IP.

No votes yet