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

emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • By default, [mamp runs mysql on port 8889](https://www.mamp.info/en/documentation/). Have you changed it ? What is the output of `telnet localhost 3306` ? – Aserre Aug 11 '15 at 13:34
  • @Ploutox I did change it so MAMP runs mysql on the standard port. I added the output of `telnet` to the question above. – emersonthis Aug 11 '15 at 14:25
  • Yes it matters which `mysql` is being used. Did you try exporting MAMP's `mysql` and `mysqladmin` as described here? http://stackoverflow.com/a/29990624/2646526 – heenenee Aug 16 '15 at 15:29
  • 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? – jerik Aug 16 '15 at 20:03
  • Could you go to http://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). – GuyT Aug 18 '15 at 11:15
  • @jerik 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? – emersonthis Aug 18 '15 at 17:03
  • @GuyT When I visit `localhost:3306` in the browser I get "Page not found: connection refused" – emersonthis Aug 18 '15 at 17:04
  • `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... – jerik Aug 18 '15 at 20:18
  • 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 one – jerik Aug 18 '15 at 20:25
  • Regarding to this both should be running: http://serverfault.com/a/70623 – Nikita Zernov Aug 21 '15 at 06:48
  • If your user set up to run from `localhost` or an IP address? – Can O' Spam Aug 21 '15 at 09:20
  • @SamSwift MySQL user? Localhost – emersonthis Aug 21 '15 at 09:36
  • @SDP, yes, that's what I meant, sorry I wasn't very clear on that. – Can O' Spam Aug 21 '15 at 09:37

3 Answers3

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

Nikita Zernov
  • 5,465
  • 6
  • 39
  • 70
  • Can the radio be set to "Only from this Mac"? – emersonthis Aug 20 '15 at 19:19
  • If you need to be able to connect to MySQL locally choose `only from this Mac`, otherwise `from other computers` – Nikita Zernov Aug 20 '15 at 19:20
  • So I can run `wp db tables` and it works. But when I run `phpunit` it returns the "Unable to connect to database" screen... – emersonthis Aug 20 '15 at 19:24
  • Does Wordpress work? Does it connect to MySQL database? – Nikita Zernov Aug 20 '15 at 19:27
  • I think so. Just added **Update 2** above, which elaborates – emersonthis Aug 20 '15 at 19:28
  • 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.php` – Nikita Zernov Aug 20 '15 at 19:33
  • 1452: `mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags ); ` – emersonthis Aug 20 '15 at 19:35
  • Maybe this can help: http://stackoverflow.com/a/7264459/2938258, so you will need to change `$socket` variable. – Nikita Zernov Aug 20 '15 at 19:37
  • Should wp-cli be connecting to MAMPs mysql? Or OSXs? Or both? – emersonthis Aug 20 '15 at 19:38
  • 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. – Nikita Zernov Aug 20 '15 at 19:40
  • 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? – emersonthis Aug 20 '15 at 19:42
  • It's also worth noting that I don't have `msyql.sock` in either of the locations mentioned in that other SO question – emersonthis Aug 20 '15 at 19:42
  • I am sorry about that. As your MySQL is running under MAMP, socket is located is here: `/Applications/MAMP/tmp/mysql/mysql.sock` – Nikita Zernov Aug 20 '15 at 19:44
  • It's weird. I don't have `mysql.sock` in that location either. I just upgraded to MAMP Pro 3.4 – emersonthis Aug 20 '15 at 19:46
  • 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. – emersonthis Aug 20 '15 at 19:50
  • Sorry I was unclear. Wordpress sites are connection to MySQL without problem. But still not the wp-cli unit tests. `phpunit` – emersonthis Aug 20 '15 at 19:52
  • Can you update your question with your php unit test file? – Nikita Zernov Aug 20 '15 at 19:54
  • Done. **Update 3** above – emersonthis Aug 20 '15 at 20:22
  • I just discovered that I DO have `/Applications/MAMP/tmp/mysql/mysql.sock`! It was hidden though. I'm not sure why – emersonthis Aug 20 '15 at 20:59
  • 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! – emersonthis Aug 20 '15 at 21:40
  • I checked and those settings were already properly set in the php.ini file. – emersonthis Aug 21 '15 at 12:38
  • I think we almost have this figured out... see **Update 4** above – emersonthis Aug 21 '15 at 13:21
  • Sure here's what I found: http://www.dolinaj.net/software-installation/mac/how-to-install-phpunit-with-mamp-on-mac/ – Nikita Zernov Aug 21 '15 at 13:23
  • I saw that too, but when I try to follow those instruction, pear installation fails for phpunit. I copied the output in **Update 5** above – emersonthis Aug 21 '15 at 13:31
  • 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? – emersonthis Aug 21 '15 at 13:43
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!

emersonthis
  • 32,822
  • 59
  • 210
  • 375
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

I hope it help.

  • 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? – emersonthis Aug 21 '15 at 12:46