警告:mysqli_real_connect():(HY000/2002):第 1452 行的 /private/tmp/wordpress/wp-includes/wp-db.php 中没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32809896/
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
Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /private/tmp/wordpress/wp-includes/wp-db.php on line 1452
提问by SomeNorwegianGuy
I'm trying to run PHPUnit to unittest a WordPress plugin, but the error in the title keeps showing up.
我正在尝试运行 PHPUnit 来对 WordPress 插件进行单元测试,但标题中的错误不断出现。
I used WP-CLI to setup the unittests, but also WP-CLI throws a similar error when I try to run it.
我使用 WP-CLI 来设置单元测试,但是当我尝试运行它时,WP-CLI 也会抛出类似的错误。
I use MAMP to run the database.
我使用 MAMP 来运行数据库。
I have setup WP-CLI and PHPUnit as phars, that are aliased in ~/.bash-profile, and ran with the default "php" supplied by OS X. Changing this, and running WP-CLI and PHPUnit with the latest PHP version supplied by MAMP fixed WP-CLI(It was running and connecting to the database just fine) but PHPUnit was still throwing the same error.
我已将 WP-CLI 和 PHPUnit 设置为 phars,它们在 ~/.bash-profile 中具有别名,并使用 OS X 提供的默认“php”运行。更改此设置,并使用最新的 PHP 版本运行 WP-CLI 和 PHPUnit由 MAMP 提供固定 WP-CLI(它正在运行并连接到数据库就好了)但 PHPUnit 仍然抛出相同的错误。
I have tried editing the wp-config.php file, and setting the host to ":/path/to/mamp/mysql.socket", "localhost:/path/to/mamp/mysql.socket" and "127.0.0.1", none of which helped.
我尝试编辑 wp-config.php 文件,并将主机设置为“:/path/to/mamp/mysql.socket”、“localhost:/path/to/mamp/mysql.socket”和“127.0.0.1” ”,这些都没有帮助。
I'm totally stuck, and don't know what to try next.
我完全被困住了,不知道下一步该尝试什么。
回答by Doug Thompson
I just ran into this error - have you checked the schema exists that your wp-config.php
is specifying?
我刚刚遇到了这个错误 - 您是否检查过您wp-config.php
指定的模式是否存在?
In my case I'd outright forgotten to create it, so the fix was as simple as a CREATE DATABASE wordpress
.
在我的情况下,我完全忘记了创建它,所以修复就像一个CREATE DATABASE wordpress
.
I've also run into this error when the wp-config.php
database host is wrong (try swapping localhost
and 127.0.0.1
).
当wp-config.php
数据库主机错误(尝试交换localhost
和127.0.0.1
)时,我也遇到了这个错误。
回答by Nilpo
First, be sure that MySql is actually running. It won't create a socket file if the process hasn't started.
首先,确保 MySql 确实在运行。如果进程尚未启动,则不会创建套接字文件。
netstat -tulpn | grep mysql
or
或者
ps -e | grep mysql
If MySql is running, changing your database host from localhost
to 127.0.0.1
in wp-config.php works, but it's only a workaround.
如果 MySql 正在运行,在 wp-config.php 中将数据库主机从 更改localhost
为127.0.0.1
有效,但这只是一种解决方法。
When you specify localhost
, the mysqli_real_connect()
function tries to connect to your database through a Unix socket that it cannot find (hence the "no such file" error.) When you specify 127.0.0.1
, it attempts to connect to your database using the default TCP port (typically 3306) which works.
当您指定 时localhost
,该mysqli_real_connect()
函数会尝试通过它无法找到的 Unix 套接字连接到您的数据库(因此会出现“没有此类文件”错误)。当您指定 时127.0.0.1
,它会尝试使用默认 TCP 端口(通常为 3306 )哪个有效。
That does not fix the problem that PHP does not know where to find your MySql socket. You need to configure the following options in your php.ini
. The location to the socket will vary depending on your OS, but you can typically find it by running locate mysql.sock
. Here's the settings that work for me on CentOS 6.8.
这并不能解决 PHP 不知道在哪里可以找到 MySql 套接字的问题。您需要在您的php.ini
. 套接字的位置因您的操作系统而异,但您通常可以通过运行locate mysql.sock
. 这是在 CentOS 6.8 上对我有用的设置。
php.ini
配置文件
pdo_mysql.default_socket = /var/lib/mysql/mysql.sock
mysqli.default_socket = /var/lib/mysql/mysql.sock
Other systems commonly use /tmp/mysqld.sock
. You can verify the configured location by checking the MySql configuration file my.cfg
.
其他系统通常使用/tmp/mysqld.sock
. 您可以通过检查 MySql 配置文件来验证配置的位置my.cfg
。
Once you've configured PHP to point to the correct socket location, restart Apache/nginx so that it picks up the new settings.
一旦您将 PHP 配置为指向正确的套接字位置,请重新启动 Apache/nginx 以便它选择新设置。
Now you can use localhost
as intended.
现在您可以localhost
按预期使用了。
回答by Gilson Jelembi
Just go to your your phpMyAdmin, click on your database and copy the ip where is running the service and replace on your wp-config.php file:
只需转到您的 phpMyAdmin,单击您的数据库并复制运行服务的 ip 并替换您的 wp-config.php 文件:
/** MySQL hostname */ define('DB_HOST', 'localhost');
/** MySQL 主机名 */define('DB_HOST', 'localhost');
/** MySQL hostname */ define('DB_HOST', 'ip_where_is_running_mysqlserver');
/** MySQL 主机名 */define('DB_HOST', 'ip_where_is_running_mysqlserver');
回答by Vicar
It made me confused but I solved eventually.
这让我很困惑,但我最终解决了。
My MySQL port is 3308, so I change "127.0.0.1" to "localhost:3308" in
我的 MySQL 端口是 3308,所以我将“127.0.0.1”更改为“localhost:3308”
$cfg['Servers'][$i]['host'].
It works!
有用!
In addition, I set
另外,我设置
$cfg['Servers'][$i]['controluser'] = '';
$cfg['Servers'][$i]['controlpass'] = '';
And...
和...
$cfg['Servers'][$i]['auth_type'] = 'cookie';
But I think the most key is port changed.
但我认为最关键的是端口改变。
回答by MissionNext
On the reported line in /wp-includes/wp-db.php (Line 1489 in WordPress v 4.5) the code reads:
在 /wp-includes/wp-db.php 中的报告行(WordPress v 4.5 中的第 1489 行),代码如下:
mysqli_real_connect( $this->duh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
The issue goes away by adding a @
in front. So the code should read:
通过@
在前面添加一个,问题就消失了。所以代码应该是:
@mysqli_real_connect( $this->duh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
Add the missing @
, save and upload the modified file to get the warning to disappear.
添加丢失的@
,保存并上传修改后的文件以使警告消失。