• Resolved dlefebvre68

    (@dlefebvre68)


    I have been scratching my head for a while now and can’t figure this problem.

    2 hosts (websrv and dbsrv) both running CentOS 6.6

    I installed WordPress 4.1 on websrv and MySQL 5.1.73 on dbsrv (using yum).

    I created my DB:

    $ mysql -u root -p
    mysql> create database wordpress;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> grant all privileges on wordpress.* to 'wp'@'websrv' identified by 'password';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

    Then I access the main web page of my new web server, I enter the details for the database (DB name, user, password, etc.) and I get :
    Error establishing a database connection

    From the command line on websrv I can successfully connect to the database using:
    mysql -u wp -h dbsrv -p wordpress

    I also successfully connected with a PHP script :

    <?php
    
    $db = @mysql_connect('192.168.200.32', 'wp', 'password');
    if (!$db) echo "connection failed\n";
    else echo "connection succeeded\n";

    But WordPress won’t connect to the database. What the heck am I missing?

Viewing 14 replies - 1 through 14 (of 14 total)
  • Check your “wp-config.php” file to make sure the database credentials are correct.

    You should look at what hostname you have set in your wp-config.php file as that can be an issue. Some servers work with the IP address, some work with the domain and some work with an internal pointer and you’ll need to know which one is allowed from the hosting server.

    If all that seems correct look at the logs on both servers to see where the connection issues are. That will be able to tell you what is failing and where.

    Thread Starter dlefebvre68

    (@dlefebvre68)

    @marc,

    Yep, checked that about 500 times. I used hostname, FQDN, IP; same result. I checked for spaces in the different parameters, nothing wrong (unless I am looking with my “man’s eyes”).

    In fact here’s a snippet of the wp-config.php:

    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define('DB_NAME', 'wordpress');
    
    /** MySQL database username */
    define('DB_USER', 'wp');
    
    /** MySQL database password */
    define('DB_PASSWORD', 'password');
    
    /** MySQL hostname */
    define('DB_HOST', 'dbsrv');
    
    /** Database Charset to use in creating database tables. */
    define('DB_CHARSET', 'utf8');
    
    /** The Database Collate type. Don't change this if in doubt. */
    define('DB_COLLATE', '');

    I don’t get it.

    Thread Starter dlefebvre68

    (@dlefebvre68)

    By the way, dbsrv resolves to the correct IP address.

    Thanks for the help.

    So what do the error logs on both servers show? If could be inthe PHP logs, Apache, HTTP, etc, so there’s a few places to check.

    @dlefebvre68, what type of hosting is your site running on?

    Keith

    (@keithdriscoll)

    I would try connecting using a database utility such as http://www.mysql.com/products/workbench/ to find out if the problem is WordPress specific.

    Thread Starter dlefebvre68

    (@dlefebvre68)

    @marc, this is running in 2 VMs (CentOS 6.6 x86_64) on ESXi 5.1.

    @catacaustic, I don’t know which log files to look at exactly, so I enabled debug in WordPress and this is what I got:

    Warning: mysql_connect(): Can't connect to MySQL server on 'dbsrv' (13) in /var/www/html/wp-includes/wp-db.php on line 1372

    Where are the logs for WordPress? Excuse my ignorance.

    I will look at MySQL now.

    Jon (Kenshino)

    (@kenshino)

    Lord Jon

    Are you sure your MySQL server allows external connections?

    Jon (Kenshino)

    (@kenshino)

    Lord Jon

    Are you sure your MySQL server allows external connections?

    WordPress doesn’t keep it’s own logs, you need to look at the server logs.

    If you don’t know where these are you should ask your hosting company for help. They’ll be able to tell you where the logs are, and will hopefully also be able to tell you if there are any relevant errors on them.

    Like I said before, be aware that the error could be on either side, or both sides, so you’ll need to see what’s happening at both locations.

    Thread Starter dlefebvre68

    (@dlefebvre68)

    @jon, yes absolutely. My wordpress is on websrv and the MySQL database is on dbsrv. From websrv I can run “mysql -u wp -h dbsrv -p wordpress” and once the password is entered I am connected to the right database on the right server.

    @catacaustic, those are my own servers that I run in my home lab for testing purposes.

    To make sure I was able to connect AND create tables (that’s what the setup process in WordPress does), I modified my test script and it can connect to the database on the db server and create a dummy table. So everything works as advertised. Except in WordPress.

    I’ll try to activate the logs in MySQL now to see if the problem is on that side.

    Thanks for all the pointers.

    Thread Starter dlefebvre68

    (@dlefebvre68)

    Well, I finally put my finger on it.

    Security feature in CentOS (SELinux). By default a connection to a remote SQL database from a web page is not permitted. This is to prevent an attacker who has gained some control over a web server to initiate a connection to a database on another host.

    That’s why I could connect from the command line and from a PHP script but not from a PHP script run from the web server.

    To fix this, you need to run :
    setsebool -P httpd_can_network_connect=1
    And that fixed it instantly. More details at http://stackoverflow.com/questions/4078205/php-cant-connect-to-mysql-with-error-13-but-command-line-can

    Thanks for all the suggestions. That made me enable audit logging in MySQL to realize that WordPress never made it to the DB server. That’s when I understood something was happening on the web server.

    Case closed.

    @dlefebvre68
    You are a life saver.. Thanks.. I faced the same issue was able to resolve by running setsebool -P httpd_can_network_connect=1

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Can't connect to a remote database’ is closed to new replies.