今天遇到一个问题就是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
比如:
[[email protected] 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 connectionid is 8Server version:5.7.29MySQL Community Server (GPL)
Copyright (c)2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clearthe current input statement.
MySQL [(none)]>status--------------mysql Ver15.1 Distrib 10.2.31-MariaDB, for Linux (x86_64) using readline 5.1Connectionid: 8Current database:
Current user: root@172.18.0.1SSL: Notinuse
Current pager: stdout
Using outfile:‘‘Using delimiter: ;
Server: MySQL
Server version:5.7.29MySQL Community Server (GPL)
Protocol version:10Connection:127.0.0.1 via TCP/IP
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
TCP port:3305Uptime:18 min 44sec
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的错误,也可以用上面的思路来解决一下。
原文:https://www.cnblogs.com/FengGeBlog/p/12761749.html