Question

I'm working on a local build consisting of Magento 2.0 and PHP 7. I've got everything up and running, but when I try to load the site I get the following error:

SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.2.102' (13) "/var/www/html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Mysql.php(111): Zend_Db_Adapter_Pdo_Abstract->_connect()

I realize that's a pretty straight forward error, but I can't figure out why I'm getting it because everything I've checked would seem to say that it would work.

I've confirmed that the pdo_mysql and PDO extensions are installed.
I've confirmed that PDO and pdo_mysql are enabled and showing correctly in phpinfo();
I've confirmed that PDO has pdo_mysql listed as a driver for it in phpinfo();
I've confirmed that I can connect to the DB using mysql on the command line
I've confirmed that the DB credentials in Magento's config are correct

I noticed that mysqli was installed instead of mysql when I do 'yum install php70w-mysql'. Did some research on php.net and found that it had been removed because it was crazy old and replaced with mysqli. I tried disabling mysqli since I want to use pdo_mysql and was wondering if there was a conflict,but got the same exception message about not being able to connect.

Any thoughts on why Magento can't connect to the DB?

System specs are:
Centos 6.7
Apache 2.2.15-47
PHP 7 w/ mod_fastci
PHP 7 FPM
Mysql 5.7
Magento 2.0

Relevant parts of phpinfo();

mysqli

MysqlI Support => enabled
Client API library version => 5.1.72
Active Persistent Links => 0
Inactive Persistent Links => 0
Active Links => 0
Client API header version => 5.1.73
MYSQLI_SOCKET => /var/lib/mysql/mysql.sock

Directive => Local Value => Master Value
mysqli.allow_local_infile => On => On
mysqli.allow_persistent => On => On
mysqli.default_host => no value => no value
mysqli.default_port => 3306 => 3306
mysqli.default_pw => no value => no value
mysqli.default_socket => no value => no value
mysqli.default_user => no value => no value
mysqli.max_links => Unlimited => Unlimited
mysqli.max_persistent => Unlimited => Unlimited
mysqli.reconnect => Off => Off
mysqli.rollback_on_cached_plink => Off => Off

PDO

PDO support => enabled
PDO drivers => mysql, sqlite

pdo_mysql

PDO Driver for MySQL => enabled
Client API version => 5.1.72

Directive => Local Value => Master Value
pdo_mysql.default_socket => /var/lib/mysql/mysql.sock =>     /var/lib/mysql/mysql.sock

EDIT 1: I wrote a test script to see if I can connect via PHP to the database and it worked perfectly. However, I dropped the test script into my doc root and called it in my browser and got the same can't connect error. Would that be an issue with FastCGI or with FPM and any ideas what it's doing? Test script is below.

<?php

    $host = '192.168.2.102';
    $user = 'ezp2_user';
    $pass = 'admin123';
    $dbname = 'klein';

    try{
        $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $query = $conn->prepare("select * from core_config_data limit 10");
        $query->execute();

        print_r(json_encode($query->fetchALL()) . "\n");
    } catch(PDOException $e){
        echo "Error: " . $e->getMessage();
    }

    $conn = null;

?>

EDIT 2: I changed the SAPI that I was using from FastCGI over to mod_php and got the same error. So I think I can rule out FastCGI and FPM as a source of the problem. So I'm thinking that leaves PHP as the final culprit. Thoughts?

Was it helpful?

Solution

I figured out what was going on. SELinux was enabled and enforcing and that was blocking Apache/PHP from connecting to MySQL. I set SELinux to permissive and everything started working like a charm.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top