为什么即使我指定了主机和端口,MySQL 仍通过套接字连接?

qff*_*qff 6 mysql

当尝试连接到本地主机上端口 3306 上运行的 MySQL 数据库时(使用命令行工具mysql),我收到错误:

错误 2002 (HY000): 无法通过套接字 '/tmp/mysql.sock' 连接到本地 MySQL 服务器 (2)

这是我正在使用的命令:

> mysql -h localhost -P 3306
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Run Code Online (Sandbox Code Playgroud)

指定用户和密码也不起作用:

> mysql -h localhost -P 3306 -u dbuser -p
Password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Run Code Online (Sandbox Code Playgroud)

当我指定主机和端口时,为什么它尝试使用 UNIX 套接字文件(就像它在这里所做的那样)?

qff*_*qff 6

您需要使用127.0.0.1而不是localhost

> mysql -h 127.0.0.1 -P 3306
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8347701
Server version: 5.6.35-80.0-log Percona Server (GPL), Release 80.0, 
Revision f113994f31
...
Run Code Online (Sandbox Code Playgroud)

你在!