mysqli :: mysqli():(HY000/2002):无法通过套接字'MySQL'连接到本地MySQL服务器(2) [英] mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket '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.1
和127.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.
- 您可以只使用TCP/IP而不是Unix套接字.连接时,可以使用
127.0.0.1
而不是localhost
来实现.不过,使用Unix套接字可能会更快,更安全. - 您可以在
php.ini
中更改套接字:打开MySQL配置文件my.cnf
以找到MySQL在何处创建套接字,并将PHP的mysqli.default_socket
设置为该路径.在我的系统上是/var/run/mysqld/mysqld.sock
. -
在打开连接时直接在PHP脚本中配置套接字.例如:
- You can just use TCP/IP instead of the Unix socket. You would do this by using
127.0.0.1
instead oflocalhost
when you connect. The Unix socket might by faster and safer to use, though. - You can change the socket in
php.ini
: open the MySQL configuration filemy.cnf
to find where MySQL creates the socket, and set PHP'smysqli.default_socket
to that path. On my system it's/var/run/mysqld/mysqld.sock
. 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屋!