运行phpunit时建立数据库连接时出错

时间:2020-04-24 作者:Haibin Liu

我在跟踪https://make.wordpress.org/cli/handbook/plugin-unit-tests/ 为插件运行单元测试。

我创建了一个新插件。

$ wp scaffold plugin hello-plugin-1
Success: Created plugin files.
Success: Created test files.
运行install-wp-tests.sh.

$ cd wp-content/plugins/hello-plugin-1
$ ./bin/install-wp-tests.sh wordpress_test_1 root root localhost latest
+ install_wp
+ \'[\' -d /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/ \']\'
+ return
+ install_test_suite
++ uname -s
+ [[ Darwin == \\D\\a\\r\\w\\i\\n ]]
+ local ioption=-i.bak
+ \'[\' \'!\' -d /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib \']\'
+ \'[\' \'!\' -f wp-tests-config.php \']\'
+ download https://develop.svn.wordpress.org/tags/5.4/wp-tests-config-sample.php /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
++ which curl
+ \'[\' /usr/bin/curl \']\'
+ curl -s https://develop.svn.wordpress.org/tags/5.4/wp-tests-config-sample.php
++ echo /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/
++ sed \'s:/\\+$::\'
+ WP_CORE_DIR=/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/
+ sed -i.bak \'s:dirname( __FILE__ ) . \'\\\'\'/src/\'\\\'\':\'\\\'\'/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress//\'\\\'\':\' /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak s/youremptytestdbnamehere/wordpress_test_1/ /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak s/yourusernamehere/root/ /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak s/yourpasswordhere/root/ /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ sed -i.bak \'s|localhost|localhost|\' /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php
+ install_db
+ \'[\' false = true \']\'
+ PARTS=(${DB_HOST//\\:/ })
+ local PARTS
+ local DB_HOSTNAME=localhost
+ local DB_SOCK_OR_PORT=
+ local EXTRA=
+ \'[\' -z localhost \']\'
++ echo
++ grep -e \'^[0-9]\\{1,\\}$\'
+ \'[\' \']\'
+ \'[\' -z \']\'
+ \'[\' -z localhost \']\'
+ EXTRA=\' --host=localhost --protocol=tcp\'
+ mysqladmin create wordpress_test_1 --user=root --password=root --host=localhost --protocol=tcp
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
运行phpunit 并出现此DB连接错误。

$ phpunit
PHP Warning:  mysqli_real_connect(): (HY000/2002): No such file or directory in /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/wp-db.php on line 1626
PHP Stack trace:
PHP   1. {main}() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/includes/install.php:0
PHP   2. require_once() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/includes/install.php:29
PHP   3. require_wp_db() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-settings.php:126
PHP   4. wpdb->__construct() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/load.php:426
PHP   5. wpdb->db_connect() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/wp-db.php:631
PHP   6. mysqli_real_connect() /private/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress/wp-includes/wp-db.php:1626

wp_die called
Message : <p><code>No such file or directory</code></p>
<h1>Error establishing a database connection</h1>
<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can&#8217;t contact the database server at <code>localhost</code>. This could mean your host&#8217;s database server is down.</p>
<ul>
<li>Are you sure you have the correct username and password?</li>
<li>Are you sure you have typed the correct hostname?</li>
<li>Are you sure the database server is running?</li>
</ul>
<p>If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="https://wordpress.org/support/forums/">WordPress Support Forums</a>.</p>

Title : 
我们需要在中设置DB吗wp-config.php 在里面/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress 在我运行phpunit之前?我试过了,但没有成功。知道哪里出了问题吗?

我在跑步WP-CLI 2.4.0PHPUnit 7.5.9.

2 个回复
SO网友:Haibin Liu

我通过更改localhost127.0.0.1 在里面/var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php.

SO网友:Scruffy Paws

我遇到了同样的问题。为了在运行MAMP Pro的MacOS本地主机上实现这一点,我将数据库主机从localhost127.0.0.1 在两个地方。。。

我的wp配置。php文件

我是在跑步时遇到同样的错误后才这样做的install-wp-tests.shinstall-wp-tests.sh 命令

这解决了您在运行后提到的错误phpunit./bin/install-wp-tests.sh wordpress_test_1 root root 127.0.0.1 latest