mysqli :: mysqli():(HY000/2002):无法通过套接字'MySQL'连接到本地MySQL服务器(2) [英] mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)

查看:59
本文介绍了mysqli :: mysqli():(HY000/2002):无法通过套接字'MySQL'连接到本地MySQL服务器(2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用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.

我的问题是为什么它只允许我使用本地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?


hosts文件

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. 与您期望的方式相比其他方式可能有所不同 基于网络的程序.对于与本地主机的连接,MySQL程序 尝试使用Unix套接字文件连接到本地服务器. 即使给--port或-P选项指定了端口,也会发生这种情况 数字.为确保客户端建立与 本地服务器上,使用--host或-h指定主机名值 127.0.0.1,或本地服务器的IP地址或名称.您还可以通过以下方式显式指定连接协议,即使是本地主机也可以: 使用--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 中更改套接字:打开MySQL配置文件my.cnf以找到MySQL在何处创建套接字,并将PHP的mysqli.default_socket设置为该路径.在我的系统上是/var/run/mysqld/mysqld.sock.
  3. 在打开连接时直接在PHP脚本中配置套接字.例如:

  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')

这篇关于mysqli :: mysqli():(HY000/2002):无法通过套接字'MySQL'连接到本地MySQL服务器(2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆