如何通过管道将 PHP 应用程序连接到 MySQL?

How to connect a PHP app to MySQL via pipes?

我发现可以通过 their documentation of MySQLi 中的管道将 PHP 连接到 MySQL 这一事实,但我终究找不到任何人解释需要什么。

host参数声明:

When possible, pipes will be used instead of the TCP/IP protocol.

但是什么时候"possible"?我有自己的机器,我绝对有必要的权限来实现这一点,我只是不知道如何。检查 host_info 时连接到主机 localhost 报告 "Localhost via UNIX socket"。

尝试关注该页面的一条(被否决的)评论,并连接到主机 .socket 参数设置为 mysql,导致 2002 连接错误。

我如何告诉它(总是)通过管道连接?

命名管道仅在 Windows 下有效。

还有

Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.

实际PHP文档中没有提到,但应该仍然有效。

今天我遇到了同样的问题,在 Windows 上解决这个问题需要很多时间。 我可以使用以下代码建立命名管道连接:

$connection = new mysqli('.', 'user', 'pass', 'database', null, '\\.\pipe\name_of_pipe');

我要连接的服务器具有以下配置:

[mysqld]
skip-networking
enable-named-pipe
socket=name_of_pipe

使用 '127.0.0.1''localhost'NULL 作为主机名无效。您还必须指定命名管道的路径,而不仅仅是管道的名称。 不幸的是,PHP 文档在这里有点薄弱...