如何在SSL下从PDO连接MySQL

时间:2015-11-13 20:13:31

标签: php mysql ssl pdo

我在ubuntu服务器中配置了bin/run-proj SSL。

MySQL

PHP脚本中的当前PDO连接是这样的:

show variables like "%ssl%";
+---------------+----------------------------+
| Variable_name | Value                      |
+---------------+----------------------------+
| have_openssl  | YES                        |
| have_ssl      | YES                        |
| ssl_ca        | /etc/mysql/ca-cert.pem     |
| ssl_capath    |                            |
| ssl_cert      | /etc/mysql/server-cert.pem |
| ssl_cipher    |                            |
| ssl_key       | /etc/mysql/server-key.pem  |
+---------------+----------------------------+

我尝试连接添加此数组。但是,它不起作用。

try {
    $this->_conn = $this->dbh = new PDO('mysql:host=' . DB_SERVER . ';dbname='. DB_NAME, DB_USER, DB_PASS);
    $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Couldn't connect to database. Please try again!");
}

现在,问题是:如何在安全连接(SSL)下连接MySQL数据库?我需要做些哪些更改才能使其安全?

我已经生成了客户端和服务器证书:

array(PDO::MYSQL_ATTR_SSL_KEY  => '/etc/mysql/client-key.pem',
      PDO::MYSQL_ATTR_SSL_CERT => '/etc/mysql/client-cert.pem',
      PDO::MYSQL_ATTR_SSL_CA   => '/etc/mysql/ca-cert.pem'
     );

错误消息

  

SQLSTATE [HY000] [2026] SSL连接错误:无法获取私钥

0 个答案:

没有答案