Error establishing a database connection when running phpunit

I'm following https://make.wordpress.org/cli/handbook/plugin-unit-tests/ to run unit tests for plugins.

I create a new plugin.

$ wp scaffold plugin hello-plugin-1
Success: Created plugin files.
Success: Created test files.

Run 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.

Run phpunit and got this DB connection error.

$ 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 : pcodeNo such file or directory/code/p
h1Error establishing a database connection/h1
pThis either means that the username and password information in your codewp-config.php/code file is incorrect or we can#8217;t contact the database server at codelocalhost/code. This could mean your host#8217;s database server is down./p
ul
liAre you sure you have the correct username and password?/li
liAre you sure you have typed the correct hostname?/li
liAre you sure the database server is running?/li
/ul
pIf 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 : 

Do we need to set up the DB in wp-config.php in /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress before I run phpunit? I tried it and it did not work. Any idea what went wrong?

I'm running WP-CLI 2.4.0 and PHPUnit 7.5.9.

Topic wp-cli Wordpress

Category Web


I ran into the same issue. To get this working on my localhost which is MacOS running MAMP Pro, I had change my database host from localhost to 127.0.0.1 in two places...

  1. My wp-config.php file

    • I did this after I got this same error when I ran install-wp-tests.sh
  2. In the install-wp-tests.sh command

    • This solved the error that I got that you mentioned after running phpunit
    • Ex: ./bin/install-wp-tests.sh wordpress_test_1 root root 127.0.0.1 latest

I fixed the issue by changing localhost to 127.0.0.1 in /var/folders/qd/y3hp2mb90gx36d5swtxcndhh0000gn/T/wordpress-tests-lib/wp-tests-config.php.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.