mysql -h localhost和mysql -h 127.0.0.1的區別


今天遇到一個問題就是mysql -h localhost登陸不了容器內的MySQL服務,感覺很是奇怪,。因為3306端口是沒有問題的,所以就研究了一下,發現mysql -h 127.0.0.1和mysql -h IP兩種都是可以登陸的,只有寫為localhost時就是登陸不進去,后來研究發現:

mysql -h localhost和mysql -h 127.0.0.1的區別,通過localhost連接到mysql是使用UNIX socket,而通過127.0.0.1連接到mysql是使用TCP/IP

比如:

[root@chaofeng test]# mysql -P 3305 -h 127.0.0.1 -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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 10.2.31-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:        8
Current database:    
Current user:        root@172.18.0.1
SSL:            Not in use
Current pager:        stdout
Using outfile:        ''
Using delimiter:    ;
Server:            MySQL
Server version:        5.7.29 MySQL Community Server (GPL)
Protocol version:    10
Connection:        127.0.0.1 via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:        3305
Uptime:            18 min 44 sec

Threads: 1  Questions: 12  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.010

使用mysql -h 127.0.0.1和mysql -h IP連入的話,那么cennection哪里顯示的都是via TCP/IP協議,但是使用mysql -h localhost連接的話則是via UNIX socket,這就是兩個的區別。

當然我們也可以解決,就是在my.cnf文件里面的[mysql]區段里添加

protocol=tcp即可,。

當然有時候我們使用正確的密碼登陸了MySQL,還是會報access denied的錯誤,也可以用上面的思路來解決一下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM