5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PDO_MySQLでSSLクライアント証明書を使う

Posted at

MySQLでSSLクライアント証明書を使うようにしてみたので、PDOから接続してみます。

pdo-mysql-ssl.php
<?php
$db = new PDO(
    'mysql:host=example.jp;dbname=test;charset=utf8',
    'ssluser',
    'pass',
    array(
        PDO::MYSQL_ATTR_SSL_KEY  => '/home/ore/.mysql/client.key',
        PDO::MYSQL_ATTR_SSL_CERT => '/home/ore/.mysql/client.crt',
        PDO::MYSQL_ATTR_SSL_CA   => '/home/ore/.mysql/ca.crt',
    )
);

$stmt = $db->query("select 'ok'");
$row = $stmt->fetchColumn();
var_dump($row);
$ php -v
PHP 5.5.3 (cli) (built: Sep 21 2013 20:49:51)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
    with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

$ php pdo-mysql-ssl.php
string(2) "ok"

ただしこの方法はマニュアルの記載によると PHP 5.3.7 からです。それより前のバージョンだとダメです。

試しに RHEL6/CentOS6 のリポジトリの PHP 5.3.3 で試してみると・・・

$ php -v
PHP 5.3.3 (cli) (built: Jul 12 2013 20:35:47)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

$ php pdo-mysql-ssl.php

Fatal error: Undefined class constant 'MYSQL_ATTR_SSL_KEY' in /home/ore/pdo-mysql-ssl.php on line 7
5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?