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

I’m trying to run PHPUnit to unittest a WordPress plugin, but the error in the title keeps showing up.

I used WP-CLI to setup the unittests, but also WP-CLI throws a similar error when I try to run it.

Read More

I use MAMP to run the database.

I have setup WP-CLI and PHPUnit as phars, that are aliased in ~/.bash-profile, and ran with the default “php” supplied by OS X.
Changing this, and running WP-CLI and PHPUnit with the latest PHP version supplied by MAMP fixed WP-CLI(It was running and connecting to the database just fine) but PHPUnit was still throwing the same error.

I have tried editing the wp-config.php file, and setting the host to “:/path/to/mamp/mysql.socket”, “localhost:/path/to/mamp/mysql.socket” and “127.0.0.1”, none of which helped.

I’m totally stuck, and don’t know what to try next.

Related posts

7 comments

  1. I just ran into this error – have you checked the schema exists that your wp-config.php is specifying?

    In my case I’d outright forgotten to create it, so the fix was as simple as a CREATE DATABASE wordpress.

    I’ve also run into this error when the wp-config.php database host is wrong (try swapping localhost and 127.0.0.1).

  2. First, be sure that MySql is actually running. It won’t create a socket file if the process hasn’t started.

    netstat -tulpn | grep mysql
    

    or

    ps -e | grep mysql
    

    If MySql is running, changing your database host from localhost to 127.0.0.1 in wp-config.php works, but it’s only a workaround.

    When you specify localhost, the mysqli_real_connect() function tries to connect to your database through a Unix socket that it cannot find (hence the “no such file” error.) When you specify 127.0.0.1, it attempts to connect to your database using the default TCP port (typically 3306) which works.

    That does not fix the problem that PHP does not know where to find your MySql socket. You need to configure the following options in your php.ini. The location to the socket will vary depending on your OS, but you can typically find it by running locate mysql.sock. Here’s the settings that work for me on CentOS 6.8.

    php.ini

    pdo_mysql.default_socket = /var/lib/mysql/mysql.sock
    mysqli.default_socket = /var/lib/mysql/mysql.sock
    

    Other systems commonly use /tmp/mysqld.sock. You can verify the configured location by checking the MySql configuration file my.cfg.

    Once you’ve configured PHP to point to the correct socket location, restart Apache/nginx so that it picks up the new settings.

    Now you can use localhost as intended.

  3. Just go to your your phpMyAdmin, click on your database and copy the ip where is running the service and replace on your wp-config.php file:

    /** MySQL hostname */
    define(‘DB_HOST’, ‘localhost’);

    /** MySQL hostname */
    define(‘DB_HOST’, ‘ip_where_is_running_mysqlserver’);

    Replace Localhost to ip of running MySQL Server

  4. It made me confused but I solved eventually.

    My MySQL port is 3308, so I change “127.0.0.1” to “localhost:3308” in

    $cfg['Servers'][$i]['host'].
    

    It works!

    In addition, I set

    $cfg['Servers'][$i]['controluser'] = '';
    
    $cfg['Servers'][$i]['controlpass'] = '';
    

    And…

    $cfg['Servers'][$i]['auth_type'] = 'cookie';
    

    But I think the most key is port changed.

  5. Creating symbolic for wp-cli to find is more efficient and remove the issue of editing many files and keeping track with your wp-config file.

    I did this for the MAC Sierra and MAMP PRO,

    Create symbolic link of the mysql socket file after locating it using netstat

    netstat -a | grep mysql
    

    Create file in var folder.

    cd /var 
    sudo mkdir mysql
    sudo chmod 755 mysql
    cd mysql
    sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
    

    wp plugin list from your WordPress installed folder works

    /path/to/wordpress/installation $ wp plugin list
    -------------------------------------------------+----------+-----------+---------+
    | name                                            | status   | update    | version |
    +-------------------------------------------------+----------+-----------+---------+
    | advanced-custom-fields                          | active   | none      | 5.9.0   |
    | akismet                                         | active   | none      | 4.1.6   |
    | bbp-voting                                      | inactive | available | 1.3.5   |
    | breadcrumb-navxt                                | active   | none      | 6.5.0   |
    | contact-form-7                                  | active   | none      | 5.2.2   |
    | flamingo                                        | active   | none      | 2.2     |
    | keydesign-addon                                 | active   | none      | 3.2     |
    | post-my-contact-form-7                          | active   | none      | 4.1.8   |
    
  6. I’m using Mac, and for the local environment, using MAMP. I’ve used the solution shared above that changed the localhost to 127.0.0.1, but it didn’t work for me.

    I use the default ports (8888/8889) of MAMP, not 80/3306.

    Also, I’ve checked the mySQL sock file as per the second answer but got it correctly set up.

    Finally, the solution that worked for me is the following:

    I’ve replaced the localhost with 127.0.0.1:8889 in the wp-config.php file.

    Thanks to Jeff for writing this article with the solution.

  7. On the reported line in /wp-includes/wp-db.php (Line 1489 in WordPress v 4.5) the code reads:

    mysqli_real_connect( $this->duh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    

    The issue goes away by adding a @ in front. So the code should read:

    @mysqli_real_connect( $this->duh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    

    Add the missing @, save and upload the modified file to get the warning to disappear.

Comments are closed.