我 运行 mysqld on Windows 10,如何从 WSL 连接到它的端口

I run mysqld on Windows 10, how do I connect to its port from WSL

mysql(出于充分的理由)在 XAMPP mysql 服务器位于 'localhost' 时非常努力地连接到套接字文件。但是由于 Win64 mysqld 当然不会在 Linux 文件系统的 Windows 子系统中放置套接字文件,因此它无法通过套接字文件进行连接。我如何强制它使用该端口?

$ mysql --port=3306 -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directo
ry")

Nmap 显示端口 3306 可以访问。

$ nmap localhost -p 3306

Starting Nmap 7.60 ( https://nmap.org ) at 2019-04-25 09:58 DST
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0019s latency).

PORT     STATE SERVICE
3306/tcp open  mysql

Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds

如果不需要,我宁愿不将 mysqld 绑定到我的外部网络。

如何告诉 mysql 软件,"Look I know what you think, but please use port 3306, and not the socket"?最好我也想知道如何告诉 PHP 相同的内容。

已解决,需要添加--protocol=TCP。示例:

$ mysql --protocol=TCP -p
Enter password:
ERROR 1045 (28000): Access denied for user 'henk'@'localhost' (using password: YES)

现在这只是我这边的一个 user/password 问题。

我会在 /etc/mysql/mariadb.conf.d/50-client.cnf 的设置中进行编辑,/etc/php/7.2/cli/php.ini 一旦我弄清楚需要的设置是什么。

编辑:

sudo vim /etc/mysql/mariadb.conf.d/50-client.cnf,在 [client] 下用 # 注释掉套接字行,添加一行 protocol = TCP。此文件的路径可能与实际 Oracle MySQL 安装不同,而不是 MariaDB。

[mysql]
# socket = 3306
protocol = TCP
如果您连接到 127.0.0.1 而不是本地主机,

PHP 的 pdo_mysql/mysqli/mysqlnd 将使用 TCP 协议,这围绕本地主机套接字文件/命名管道试探法起作用。