wp-cli: unable to run phpunit on MAMP
Asked Answered
G

3

18

I'm following the plugin test setup/install instructions. I got wp scaffold plugin-tests my-plugin to run. But then at the next step when I try to run bash bin/install-wp-tests.sh wordpress_test root '' localhost latest I get the following error:

mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (61)'
Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'

My local Wordpress site is running with MAMP (which is working). I'm not sure if that's relevant for the install script since I think it's creating a temporary DB to run the tests... Does it matter if it uses the built-in OSX mysql or MAMP's MySQL?

Here's the output from wp --info

$ ./vendor/wp-cli/wp-cli/bin/wp --info
PHP binary: /Applications/MAMP/bin/php/php5.6.10/bin/php
PHP version:    5.6.10
php.ini used:   /Applications/MAMP/bin/php/php5.6.10/conf/php.ini
WP-CLI root dir:    /Applications/MAMP/htdocs/pipeline/wp-content/plugins/wp-github-pipeline/vendor/wp-cli/wp-cli
WP-CLI global config:   
WP-CLI project config:  
WP-CLI version: 0.19.2

Update 2 I figured out that originally MySQL wasn't installed... that's why I couldn't connect! But now it is. I ran the install script, and this works...

$ ./vendor/wp-cli/wp-cli/bin/wp db tables
wp_users
wp_usermeta
wp_posts
wp_comments
wp_links
wp_options
wp_postmeta
wp_terms
wp_term_taxonomy
wp_term_relationships
wp_commentmeta

But when I run phpunit I get this:

$ phpunit
PHP Warning:  mysqli_real_connect(): (HY000/2002): No such file or directory in /private/tmp/wordpress/wp-includes/wp-db.php on line 1452
PHP Stack trace:
PHP   1. {main}() /private/tmp/wordpress-tests-lib/includes/install.php:0
PHP   2. require_once() /private/tmp/wordpress-tests-lib/includes/install.php:21
PHP   3. require_wp_db() /private/tmp/wordpress/wp-settings.php:79
PHP   4. wpdb->__construct() /private/tmp/wordpress/wp-includes/load.php:350
PHP   5. wpdb->db_connect() /private/tmp/wordpress/wp-includes/wp-db.php:649
PHP   6. mysqli_real_connect() /private/tmp/wordpress/wp-includes/wp-db.php:1452

Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /private/tmp/wordpress/wp-includes/wp-db.php on line 1452

Part of my problem is that I'm not clear on whether wp-cli should be running entirely on native (cli) PHP/Mysql, or MAMP's PHP/Mysql, or some combination of both.

Update 4 I'm pretty sure the final problem is that phpunit needs to be installed in MAMP, but I'm running it from OSX...

$which phpunit
/usr/bin/phpunit

Mentioned in this gist.

Update 6

It turns out you can no longer install phpunit using pear. So I added it as a composer dependency under require-dev, but when I run that version I get the same error!

$ ./vendor/phpunit/phpunit/phpunit
PHP Warning:  mysqli_real_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /private/tmp/wordpress/wp-includes/wp-db.php on line 1452
PHP Stack trace:
PHP   1. {main}() /private/tmp/wordpress-tests-lib/includes/install.php:0
PHP   2. require_once() /private/tmp/wordpress-tests-lib/includes/install.php:21
PHP   3. require_wp_db() /private/tmp/wordpress/wp-settings.php:79
PHP   4. wpdb->__construct() /private/tmp/wordpress/wp-includes/load.php:350
PHP   5. wpdb->db_connect() /private/tmp/wordpress/wp-includes/wp-db.php:649
PHP   6. mysqli_real_connect() /private/tmp/wordpress/wp-includes/wp-db.php:1452

Warning: mysqli_real_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /private/tmp/wordpress/wp-includes/wp-db.php on line 1452

I even added it to my path to be sure...

$ which phpunit
/Applications/MAMP/htdocs/pipeline/wp-content/plugins/wp-github-pipeline/vendor/phpunit/phpunit/phpunit

Update 7

After reading the comments at the bottom of this blog post, I see that the install script was referencing the OSX version of mysqladmin. I'm not sure if this matters, but I prepended MAMPs version in the path, and re-ran the install script. It seems to install the Wordpress files in /tmp/ anyway. Same error when I run phpunit

Grodno answered 11/8, 2015 at 13:7 Comment(13)
By default, mamp runs mysql on port 8889. Have you changed it ? What is the output of telnet localhost 3306 ?Mildred
@Ploutox I did change it so MAMP runs mysql on the standard port. I added the output of telnet to the question above.Grodno
Yes it matters which mysql is being used. Did you try exporting MAMP's mysql and mysqladmin as described here? https://mcmap.net/q/122627/-how-to-override-the-path-of-php-to-use-the-mamp-pathCarny
pls check if mysql runs via Terminal: ps aux | grep sql. Furthermore with this command you can check what runs on which port: lsof -i -P | grep -i "listen". If mysql is running on the correct port, do you have perhaps a firewall which gets into your way? Ohter option, can you connect to mysql if you run the command mysql -uroot -p in the terminal?Virtually
Could you go to localhost:3306 and see if a file named 'download' is downloaded? Otherwise your mysql is not running on that port(or not running at all).Moisesmoishe
@Virtually I posted the transcripts of all three of your commands above. It looks like the first two are good, but the last one fails. Is this my problem?Grodno
@Moisesmoishe When I visit localhost:3306 in the browser I get "Page not found: connection refused"Grodno
ps commands states that mysql is running, but at the lsof command no port 3306 is available, which is strange. The latter can explain the mysql command error. It seems that mysql is not listening on port 3306, so your connection tries on port 3306 does not work. What does mysqladmin -u root -p status give you back. Have a look at /Applications/MAMP/logs/mysql_error_log.err and /Applications/MAMP/tmp/mysql/my.cnf perhaps you find there something strange...Virtually
regarding the ps command you have 2 times mysql running: /Applications/MAMP/Library/bin/mysqld and /Applications/MAMP/Library/bin/mysqld_safe. I am not sure if this is correct. In my opinion it should only be oneVirtually
Regarding to this both should be running: serverfault.com/a/70623Procarp
If your user set up to run from localhost or an IP address?Clementina
@SamSwift MySQL user? LocalhostGrodno
@SDP, yes, that's what I meant, sorry I wasn't very clear on that.Clementina
G
1

I finally got phpunit to run!!

I couldn't find this documented anywhere...

At some point during the installation process, Wordpress core files are installed in /tmp/wordpress/. That Wordpress installation has it's own wp-config.php file which had incorrect values. When I corrected those values to match the wp-config.php of my site, phpunit worked without problems!

I'm not sure how this happened, but my theory is that the first time I ran the install script with the wrong credentials. But later I corrected them (I re-ran the install script several times). But I think the install script didn't overwrite the original files.

+400 to @Nikita Zernov for so much help!

Grodno answered 21/8, 2015 at 18:59 Comment(0)
P
4

If you are using MAMP, issue may be relevant to your MySQL Server settings. Make sure to check Allow network access option in MAMP settings:

enter image description here

Update 1

Create phpinfo.php file in your root directory (usually /Applications/MAMP/htdocs for MAMP). Paste the following content:

<?php phpinfo() ?>

Then check the Loaded Configuration File property. Open it using nano or other text editor in terminal. Then find and change this 3 propertiespdo_mysql.default_socket, mysql.default_socket, mysqli.default_socket to your socket file.

Referenced from http://maccrazy.com/lion-upgrade-killed-my-php-site-and-how-i-fixed-it

Procarp answered 20/8, 2015 at 19:17 Comment(25)
Can the radio be set to "Only from this Mac"?Grodno
If you need to be able to connect to MySQL locally choose only from this Mac, otherwise from other computersProcarp
So I can run wp db tables and it works. But when I run phpunit it returns the "Unable to connect to database" screen...Grodno
Does Wordpress work? Does it connect to MySQL database?Procarp
I think so. Just added Update 2 above, which elaboratesGrodno
What do you see on line 1452 in /private/tmp/wordpress/wp-includes/wp-db.php? You can do that in OS X terminal by the following command: nano +1452 /private/tmp/wordpress/wp-includes/wp-db.phpProcarp
1452: mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); Grodno
Maybe this can help: https://mcmap.net/q/116055/-warning-mysql_connect-2002-no-such-file-or-directory-trying-to-connect-via-unix-tmp-mysql-sock-in, so you will need to change $socket variable.Procarp
Should wp-cli be connecting to MAMPs mysql? Or OSXs? Or both?Grodno
It should connect to one where wordpress database is located, i think in your case it is MAMP's MySQL as you mentioned in your answer of this question.Procarp
Ok. I'm pretty sure that mean's MAMP. The reason it's confusing is that the install scripts seems to create a testing database also... which I'm pretty sure at the moment is in OSXs MySQL. Maybe that's my problem? Should I have run the test install script with the MAMP configurations instead?Grodno
It's also worth noting that I don't have msyql.sock in either of the locations mentioned in that other SO questionGrodno
I am sorry about that. As your MySQL is running under MAMP, socket is located is here: /Applications/MAMP/tmp/mysql/mysql.sockProcarp
It's weird. I don't have mysql.sock in that location either. I just upgraded to MAMP Pro 3.4Grodno
The webstart window says that's where it should be, but it's not. Very strange. Mysql on MAMP appears to be working fine in the mean time.Grodno
Sorry I was unclear. Wordpress sites are connection to MySQL without problem. But still not the wp-cli unit tests. phpunitGrodno
Can you update your question with your php unit test file?Procarp
Done. Update 3 aboveGrodno
I just discovered that I DO have /Applications/MAMP/tmp/mysql/mysql.sock! It was hidden though. I'm not sure whyGrodno
I added a symlink to the mysql.sock file. wp ... commands work and when I use mysql from the command line see MAMP databases etc. The only problem left is that phpunit seems to be looking in the wrong place still. I'm not sure, but the error above makes me think maybe I need to ALSO install phpunit locally, inside MAMP? ugh!Grodno
I checked and those settings were already properly set in the php.ini file.Grodno
I think we almost have this figured out... see Update 4 aboveGrodno
Sure here's what I found: dolinaj.net/software-installation/mac/…Procarp
I saw that too, but when I try to follow those instruction, pear installation fails for phpunit. I copied the output in Update 5 aboveGrodno
I'm pretty sure I'm running my local MAMP version of phpunit now, but it's doing the same thing. (Update 6) Am I missing something here?Grodno
G
1

I finally got phpunit to run!!

I couldn't find this documented anywhere...

At some point during the installation process, Wordpress core files are installed in /tmp/wordpress/. That Wordpress installation has it's own wp-config.php file which had incorrect values. When I corrected those values to match the wp-config.php of my site, phpunit worked without problems!

I'm not sure how this happened, but my theory is that the first time I ran the install script with the wrong credentials. But later I corrected them (I re-ran the install script several times). But I think the install script didn't overwrite the original files.

+400 to @Nikita Zernov for so much help!

Grodno answered 21/8, 2015 at 18:59 Comment(0)
C
0

Normally if you ftp or telnet to localhost you'll get conection refused, if you want to fix the error change your httpd.conf to ServerName localhost

Also if you want to do remote login try enable remote login by going to System Preferences -> Sharing -> Remote Login box (check it).

For MySQL Problem

Make sure ‘skip-networking’ is commented out in configuration file(in this case /opt/local/etc/mysql55/my.conf) or when starting mysql server, it does’t start with ‘–skip-networking’

In skip-networking mode, the mysql instance doesn’t “listen for TCP/IP connections at all. All interaction with mysqld must be made using named pipes or shared memory (on Windows) or Unix socket files (on Unix)” from MySql Docs

Claire answered 17/8, 2015 at 11:39 Comment(1)
MAMP appears to have it's own magical version of that property. In my.cnf I see MAMP_skip-networking_MAMP. Do you think I should comment this out? What is your theory?Grodno

© 2022 - 2024 — McMap. All rights reserved.