connect to remote wpengine mysql service with php/pdo

Following the instructions here:

https://wpengine.com/support/setting-remote-database-access/

I’m able to connect to mysql from my command line with:

mysql -uuser -ppassword -h instance.sftp.wpengine.com -P13306 -D database –default-auth=mysql_native_password

But when I try the same parameters through php/pdo:

$dbh = new PDO('mysql:host='.$host.';port='.$port.';dbname='.$db.';default-auth=mysql_native_password', $user, $pass);

I get this error:

PHP Fatal error: Uncaught PDOException: PDO::__construct(): MySQL server has gone away

Add Comment
1 Answer(s)

You need to add options and specify the .pem key.

You can download the pem key from the instructions in the question:

https://wpengine.com/support/setting-remote-database-access/ – save wpengine_root_ca.pem to the same directory as your script.

    $user = '';     $pass = '';     $db = '';     $port = '13306';     $host = 'instance.sftp.wpengine.com';  //put your account name instead of 'instance' here     $options = ['default-auth'=>'mysql_native_password',     PDO::MYSQL_ATTR_SSL_CA=>__DIR__.'/wpengine_root_ca.pem',     PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false];          $dbh = new PDO('mysql:host='.$host.';port='.$port.';dbname='.$db.';default-auth=mysql_native_password', $user, $pass, $options); 

You should read over the directions carefully and also make sure that WP Engine has whitelisted your ip address.

Answered on September 1, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.