Connect through MySQL with SSL

Hi,

How can I connect through MySQL with SSL certificates ?

Thank you !



28.5k

In standard PHP, I will use this :

<?php ini_set ('error_reporting', E_ALL); ini_set ('display_errors', '1'); error_reporting (E_ALL|E_STRICT);

$db = mysqli_init(); mysqli_options ($db, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);

$db->ssl_set('/etc/mysql/ssl/client-key.pem', '/etc/mysql/ssl/client-cert.pem', '/etc/mysql/ssl/ca-cert.pem', NULL, NULL); $link = mysqli_real_connect ($db, 'ip', 'user', 'pass', 'db', 3306, NULL, MYSQLI_CLIENT_SSL); if (!$link) { die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n"); } else { $res = $db->query('SHOW TABLES;'); print_r ($res); $db->close(); } ?>

But, what's about Phalcon ?



96.2k

Phalcon does use PDO as database layer, try something like this:

// Create a connection with PDO options
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
    "host" => "localhost",
    "username" => "root",
    "password" => "sigma",
    "dbname" => "test_db",
    "options" => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES \'UTF8\'",
        PDO::MYSQL_ATTR_SSL_CA => "/path/to/ca"
    )
));

http://php.net/manual/en/ref.pdo-mysql.php



72

Can you guide on how to connect ssl in Phalcon Framework 3.2.4. My connection script looks:

return new \Phalcon\Config(array(

'database' => array(
    'adapter' => 'Mysql',
    'host'     => 'xxx.rds.amazonaws.com',
    'username' => 'dev_user',
    'password' => 'XXXXX',
    'dbname'   => 'dev',
    'name'     => 'dev',
    'charset'  => 'utf8')

    I gave options 

    options" => array(
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES \'UTF8\'",
    PDO::MYSQL_ATTR_SSL_CA => "/path/to/ca"
    )
    But gets access denied. But it connects using core php.