php 警告: mysqli_connect(): (HY000/2002): 没有那个文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20073168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:51:20  来源:igfitidea点击:

Warning: mysqli_connect(): (HY000/2002): No such file or directory

phpmysqlmysqli

提问by kramer65

I'm trying to install vanilla forums on my Mac, and for this I just created a database and a user from the MySQL command line:

我正在尝试在我的 Mac 上安装 vanilla 论坛,为此我刚刚从 MySQL 命令行创建了一个数据库和一个用户:

mysql> CREATE DATABASE vanilla;
Query OK, 1 row affected (0.00 sec)

mysql> create user 'vanilla_user3'@'localhost' IDENTIFIED BY 'vanilla_password';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON * . * TO 'vanilla_user3'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

So I try to connect using the following code:

所以我尝试使用以下代码进行连接:

$con=mysqli_connect("localhost","vanilla_user3","vanilla_password","vanilla");
if (mysqli_connect_errno($con)) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

but unfortunately, I get an error saying

但不幸的是,我收到一条错误消息

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Users/kramer65/Sites/vanilla/info.php on line 3 Failed to connect to MySQL: No such file or directory

警告:mysqli_connect(): (HY000/2002): No such file or directory in /Users/kramer65/Sites/vanilla/info.php on line 3 Failed to connect to MySQL: No such file or directory

Any idea where I'm going wrong?

知道我哪里出错了吗?

回答by kramer65

Alright, I just found the solution. The problem turned out to be that the host shouldn't have been localhost, but 127.0.0.1. I always thought localhostand 127.0.0.1was the same, but it turned out to be different.

好的,我刚刚找到了解决方案。问题原来是主机不应该是localhost,但是127.0.0.1。我一直认为localhost127.0.0.1是一样的,但结果却是不同的。

So maybe as a tip for future users, always use the ip when in doubt.

因此,也许作为对未来用户的提示,如有疑问,请始终使用该 IP。

回答by mohitmayank

I had the same problem but the issue was something related to php.inifile.

我有同样的问题,但问题与php.ini文件有关。

I had to edit these two lines in /etc/php.ini(or wherever your php.iniis located):

我必须在/etc/php.ini(或您php.ini所在的任何地方)编辑这两行:

mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

Restart apache server to make sure the changes are reflected.

重新启动 apache 服务器以确保更改得到反映。

sudo apachectl restart

回答by Adam

Let's say your MAMP MySQL Port is set to 8889 like it is by default. Sometimes localhostalone is not enough in which case you have to put the MySQL port in there too so you would do localhost:8889or localhost:{whatever your MySQL port number is}. I'm still new to MySQL so I don't know the reason why, but to anyone out there who recently got the message: Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in ...adding the MySQL port number onto localhost was the fix for me.

假设您的 MAMP MySQL 端口设置为 8889,就像默认情况下一样。有时localhost单独是不够的,在这种情况下,您也必须将 MySQL 端口放在那里,这样您就可以执行localhost:8889localhost:{whatever your MySQL port number is}。我还是 MySQL 的新手,所以我不知道原因,但对于最近收到消息的任何人来说:Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in ...将 MySQL 端口号添加到 localhost 对我来说是解决方案。

回答by hpaknia

If using docker use the mysql container name as host name of mysql. For example if this is the docker compose file:

如果使用 docker,请使用 mysql 容器名称作为 mysql 的主机名。例如,如果这是 docker compose 文件:

mysqldb:
    image: mysql:5.7.22
    container_name: mysqlHost
    ports:
        - "33099:3306"

Host name will be mysqlHostand port will be 3306and not 33099!

主机名将是mysqlHost,端口将是3306而不是33099

If you are not using docker compose, docker psreveals the running container names.

如果您没有使用 docker compose,则docker ps显示正在运行的容器名称。

回答by Andrew Njuki

find php.inifile in /opt/lampp/etc/then edit:

找到php.ini文件/opt/lampp/etc/然后编辑:

mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

after this in your DB connect file change from localhost to 127.0.0.1. everything will be ok. however, if you attempted the config.inc.phpfile, make sure its all set to 127.0.0.1, not localhost.

在此之后,在您的数据库连接文件中将 localhost 更改为127.0.0.1. 一切都会好起来的。但是,如果您尝试使用该config.inc.php文件,请确保将其全部设置为127.0.0.1,而不是 localhost。

回答by sergvb

set port in host:

在主机中设置端口:

$con = mysqli_connect("localhost:**3306**","vanilla_user3","vanilla_password","vanilla");