mysqli::mysqli(): (HY000/2002): 无法通过套接字 'MySQL' 连接到本地 MySQL 服务器 (2)

2022-01-30 00:00:00 php mysql mysqli

当我尝试使用 php mysqli 类连接到 mysql 数据库时出现此错误.使用以下代码:

I get this error when I try to connect to the mysql database using php mysqli class. Using following code:

$db = new MySQLi("localhost","kamil","*****");
if (mysqli_connect_errno())
{
    echo "An error occured. Please try again later.";
    exit();
}

密码是 * 以确保安全.

password is * for security.

我创建了用户 kamil 对外部 IP 地址和本地主机具有所有权限.当我运行时: select user,host from mysql.user 它会正确显示这两个用户.

I have created user kamil with all privileges on external ip address and localhost. When I run: select user,host from mysql.user it properly displays those two users.

我做了一些研究并使用了这个基准:https://stackoverflow.com/a/2183134/1839439看看它连接到什么.事实证明,它只能连接到 127.0.0.1127.0.0.1:3306 这是本地主机,但是当我提供 localhost它抛出了这个错误.

I did some research and used this benchmark: https://stackoverflow.com/a/2183134/1839439 to see what it connects to. As it turns out it is only able to connect to 127.0.0.1 and 127.0.0.1:3306 which is localhost, however when I supply localhost it throws out this error.

我的问题是为什么它只允许我使用 localhost ip 地址而不是名称或外部 ip 连接到数据库.如果我想在网站上使用 mysql 或者如果我可以使用 127.0.0.1,是否需要不同的主机?

My question is why does it only allow me to connect to DB using localhost ip address and not the name or external ip. Do I need a different host if I want to be able to use mysql on website or if I can use 127.0.0.1?


主机文件

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1       raspberrypi

<小时>该用户的Mysql用户表结果:


Mysql user table results for this user:

| kamil            | 109.255.177.28 |
| kamil            | localhost      |

推荐答案

当您只使用localhost"时,MySQL 客户端库会尝试使用 Unix 域套接字而不是 TCP/IP 连接进行连接.错误告诉您,名为 MySQL 的套接字不能用于建立连接,可能是因为它不存在(错误号 2).

When you use just "localhost" the MySQL client library tries to use a Unix domain socket for the connection instead of a TCP/IP connection. The error is telling you that the socket, called MySQL, cannot be used to make the connection, probably because it does not exist (error number 2).

来自 MySQL 文档:

在 Unix 上,MySQL 程序特别对待主机名 localhost,在与其他方式相比,这可能与您期望的方式不同基于网络的程序.对于到 localhost、MySQL 程序的连接尝试使用 Unix 套接字文件连接到本地服务器.即使给出 --port 或 -P 选项来指定端口,也会发生这种情况数字.确保客户端与客户端建立 TCP/IP 连接本地服务器,使用 --host 或 -h 指定主机名值127.0.0.1,或本地服务器的 IP 地址或名称.您还可以通过以下方式显式指定连接协议,即使对于 localhost 也是如此使用 --protocol=TCP 选项.

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.

有几种方法可以解决这个问题.

There are a few ways to solve this problem.

  1. 您可以只使用 TCP/IP 而不是 Unix 套接字.您可以在连接时使用 127.0.0.1 而不是 localhost 来做到这一点.不过,Unix 套接字使用起来可能更快、更安全.
  2. 您可以在php.ini<中更改套接字/code>:打开 MySQL 配置文件 my.cnf 以查找 MySQL 创建套接字的位置,并将 PHP 的 mysqli.default_socket 设置为该路径.在我的系统上是 /var/run/mysqld/mysqld.sock.
  3. 打开连接时直接在PHP脚本中配置socket.例如:

  1. You can just use TCP/IP instead of the Unix socket. You would do this by using 127.0.0.1 instead of localhost when you connect. The Unix socket might by faster and safer to use, though.
  2. You can change the socket in php.ini: open the MySQL configuration file my.cnf to find where MySQL creates the socket, and set PHP's mysqli.default_socket to that path. On my system it's /var/run/mysqld/mysqld.sock.
  3. Configure the socket directly in the PHP script when opening the connection. For example:

$db = new MySQLi('localhost', 'kamil', '***', '', 0, 
                              '/var/run/mysqld/mysqld.sock')

相关文章