一、訪問 mysql 出錯
通過迴環訪問出錯,以爲服務掛了:
:~> mysql -h 127.0.0.1
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
再通過 localhost 訪問,發現實際服務 OK:
:~> mysql -h localhost
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 7514386
Server version: 5.6.24-72.2-log Source distribution
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
那麼,
mysql -h 127.0.0.1 和 mysql -h localhost 有啥區別呢?
二、mysql -h 127.0.0.1
通過 mysql -h 127.0.0.1
訪問的時候,使用TCP/IP
連接 3306 端口;
127.0.0.1 是通過網卡傳輸的,依賴網卡,並受到防火牆和網卡相關限制。
:~> mysql -h 127.0.0.1
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 7514214
Server version: 5.6.24-72.2-log Source distribution
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> status
--------------
mysql Ver 15.1 Distrib 5.5.41-MariaDB, for Linux (x86_64) using readline 5.1
Connection id: 7514213
Current database:
Currentuser: xxx@localhost
SSL: Notin use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.1.33-log Source distribution
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
Server characterset: utf8
Db characterset: utf8
Client characterset: latin1
Conn. characterset: latin1
TCP port: 3306
Uptime: 522 days 4 hours 46 min 25 sec
三、mysql -h localhost
通過 mysql -h localhost
訪問的時候,通過 UNIX socket
本地連接 ;
localhost 不經過網卡傳輸,所以不佔用網卡資源,也不受防火牆和網卡相關限制。
:~> mysql -h localhost
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 7514214
Server version: 5.6.24-72.2-log Source distribution
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> status
--------------
mysql Ver 15.1 Distrib 5.5.41-MariaDB, for Linux (x86_64) using readline 5.1
Connection id: 7514214
Current database:
Current user: xxxx@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MySQL
Server version: 5.6.24-72.2-log Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /tmp/mysql.sock
Uptime: 522 days 4 hours 46 min 25 sec