非常奇怪的问题。
在我的Mac上,我已经安装了apache、mysql等,以便在本地进行一些wordpress / PHP开发。
mysql安装正常,这是下面的版本
mysql Ver 8.0.22 for osx10.16 on x86_64 (Homebrew)我可以通过应用程序sequel pro连接到它,创建/管理数据库。

但是,当我设置wordpress网站或通过PHP测试连接时。我无论如何也不能和它联系在一起。
这是我的wordpress config.php文件的db部分:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );
/** MySQL database username */
define( 'DB_USER', 'root' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );这是我在尝试安装wordpress时得到的页面:

我还尝试了通过一个简单的PHP脚本,如下所示:
<?php
$c = new mysqli("localhost", "root", "password", "wordpress") or die('Could not connect the database : Username or password incorrect');
echo 'Database Connected successfully';
?>我得到了这个错误:
Warning: mysqli::__construct(): (HY000/2002): No such file or directory如果我将localhost更改为127.0.0.1,我会得到这个错误:
Warning: Packets out of order. Expected 0 received 1. Packet size=68 in /users/asdasda/test.php on line 3
Warning: mysqli::__construct(): MySQL server has gone away in /users/asdasda/test.php on line 3
Warning: mysqli::__construct(): Error while reading greeting packet. PID=36547 in /users/asdasda/test.php on line 3发布于 2020-12-01 08:48:14
好了,我想通了。
我必须用以下几行代码在/etc/中创建一个php.ini文件:
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
pdo_mysql.default_socket=/tmp/mysql.sock发布于 2020-12-01 07:57:46
您应该避免使用localhost,而应使用IP。localhost使用IP地址127.0.0.1
https://stackoverflow.com/questions/65082363
复制相似问题