<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/pdo.drivers.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'ref.pdo-mysql.php',
    1 => 'MySQL PDO ドライバ',
    2 => 'MySQL PDO ドライバ (PDO_MYSQL)',
  ),
  'up' => 
  array (
    0 => 'pdo.drivers.php',
    1 => 'PDO ドライバ',
  ),
  'prev' => 
  array (
    0 => 'ref.pdo-informix.connection.php',
    1 => 'PDO_INFORMIX DSN',
  ),
  'next' => 
  array (
    0 => 'ref.pdo-mysql.connection.php',
    1 => 'PDO_MYSQL DSN',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/pdo_mysql/reference.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/ref.pdo-mysql.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="ref.pdo-mysql" class="reference">
  
  <h1 class="title">MySQL PDO ドライバ (PDO_MYSQL)</h1>
  
  <div class="partintro">

   <div class="section" id="ref.pdo-mysql.intro">
   <h2 class="title">はじめに</h2>
    <p class="para">
     PDO_MYSQL は、PHP から MySQL データベースへのアクセスを可能にするための
     <a href="book.pdo.php#intro.pdo" class="link">PHP Data
     Objects (PDO) インターフェイス</a> を実装したドライバです。
    </p>
    <p class="para">
     PDO_MYSQLは、
     デフォルトでエミュレートされたプリペアドステートメントを使います。
    </p>

    <p class="para">
      <strong>MySQL 8</strong>
    </p>

    <p class="para">
     PHP 7.1.16 より前のバージョン、もしくは PHP 7.2.4 より前の 7.2系の PHP では、
     MySQL 8 サーバーのデフォルトパスワードプラグインを
     <em>mysql_native_password</em> に設定するようにしてください。
     さもないと、
     <em>The server requested authentication method unknown to the client [caching_sha2_password]</em>
     のようなエラーを見ることになります。たとえあなたが
     <em>caching_sha2_password</em> を使っていなくても、です。
    </p>
    <p class="para">
     これは MySQL 8 のデフォルトが 古い PHP (mysqlnd) 
     のリリースが認識していないプラグイン caching_sha2_password だからです。
     かわりに、<var class="filename">my.cnf</var> の設定を
     <code class="literal">default_authentication_plugin=mysql_native_password</code>
     と変更してください。
     <em>caching_sha2_password</em> プラグインは、
     PHP 7.4.4 以降で完全にサポートされました。
     これより前のバージョンでは、
     <a href="book.mysql-xdevapi.php" class="link">mysql_xdevapi</a>
     拡張モジュールでサポートされています。
    </p>

    <div class="warning"><strong class="warning">警告</strong>
     <p class="para">
      注意: MySQL のテーブル型 (ストレージエンジン) の中には、
      トランザクションをサポートしていないものがあります。
      トランザクションをサポートしていないテーブル型を使用して
      トランザクションを使用するコードを書くと、
      MySQL はトランザクションが正常に使用できたかのように振舞います。
      さらに、DDL クエリを実行する際には、
      実行中のトランザクションが暗黙的にコミットされます。
     </p>
    </div>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      MySQL ドライバは、
      <span class="methodname"><a href="pdostatement.bindparam.php" class="methodname">PDOStatement::bindParam()</a></span> メソッドで
      <strong><code><a href="pdo.constants.php#pdo.constants.param-input-output">PDO::PARAM_INPUT_OUTPUT</a></code></strong>
      を適切にサポートしていません。使うことはできますが、
      値が更新されません。(つまり、出力が無視されています)
     </p>
    </p></blockquote>
   </div>
   



<div class="section" id="ref.pdo-mysql.installation">
 <h2 class="title">インストール手順</h2>
 <p class="para">
  一般的な Unix ディストリビューションには、バイナリ版の PHP がインストールされています。
  これらのバイナリ版は、通常は MySQL 拡張モジュールを有効にしてビルドされています。
  しかし、拡張モジュールのライブラリ自身を別パッケージでインストールする必要があるでしょう。
  使用しているディストリビューションのパッケージマネージャで確認してみましょう。
 </p>
 
 <p class="para">
  たとえば Ubuntu なら、<code class="literal">php5-mysql</code> パッケージをインストールすれば、
  ext/mysql、ext/mysqli、そして pdo_mysql をインストールできます。CentOS の場合は
  <code class="literal">php-mysql</code> パッケージを使えば、同じものをインストールできます。
 </p>

 <p class="para">
  この拡張モジュールを自分でコンパイルすることもできます。
  PHP をソースからビルドすれば、使いたい MySQL 拡張モジュールを指定したり
  各拡張モジュールが使用するクライアントライブラリを選択したりすることができます。
 </p>

 <p class="para">
  <strong class="option configure">--with-pdo-mysql[=DIR]</strong> で
  PDO MySQL 拡張モジュールをインストールします。オプションの <code class="literal">[=DIR]</code>
  は、MySQL ベースライブラリの場所です。 
  <a href="book.mysqlnd.php" class="link">Mysqlnd</a>
  がデフォルトのベースライブラリです。
  どのライブラリを選べばいいのかについての詳細は
  <a href="mysqlinfo.library.choosing.php" class="link">MySQL ライブラリの選択</a>
  を参照ください。
 </p>
 <p class="para">
  オプションの <strong class="option configure">--with-mysql-sock[=DIR]</strong> で
  MySQL unix ソケットの場所を指定することができます。これは PDO_MYSQL を含むすべての
  MySQL 拡張モジュールで利用します。省略した場合はデフォルトの場所を検索します。
 </p>
 <p class="para">
  オプションの <strong class="option configure">--with-zlib-dir[=DIR]</strong> で、
  libz のインストールプレフィックスを指定することができます。
 </p>
 <p class="para">
  <div class="example-contents screen">
<div class="cdata"><pre>
$ ./configure --with-pdo-mysql --with-mysql-sock=/var/mysql/mysql.sock
</pre></div>
  </div>
 </p>
 <p class="para">
  <abbr title="Secure Sockets Layer">SSL</abbr> サポートを有効にするには、適切な
  <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-ssl-key">Pdo\Mysql::ATTR_SSL_<span class="replaceable">*</span></a></code></strong>
  を指定します。これは <a href="https://dev.mysql.com/doc/c-api/8.4/en/mysql-ssl-set.html" class="link external">&raquo;&nbsp;MySQL C API 関数 mysql_ssl_set()</a>
  をコールするのと同じ意味になります。また、SSL を <span class="methodname"><a href="pdo.setattribute.php" class="methodname">PDO::setAttribute()</a></span>
  で有効にすることはできません。というのも、この時点ではすでに接続が確立されてしまっているからです。
  MySQL のドキュメントで <a href="https://dev.mysql.com/doc/en/using-encrypted-connections.html" class="link external">&raquo;&nbsp;SSL を使った
  MySQL への接続</a> について確認しましょう。
 </p>

</div>



   



<div class="section" id="ref.pdo-mysql.constants">
 <h2 class="title">定義済み定数</h2>
 <p class="simpara">このドライバでは以下の定数が定義されて
います。これは拡張モジュールが PHP に組み込まれているか、実行時に動的にロード
されている場合のみ使用可能です。さらに、これらのドライバ固有の定数は
そのドライバを使用している場合にのみ使用されます。
あるドライバ固有の属性を別のドライバで使うと、予期せぬ結果を引き起こします。
もし複数のドライバを使用しているコードを実行している場合、
<span class="function"><a href="pdo.getattribute.php" class="function">PDO::getAttribute()</a></span> で <strong><code><a href="pdo.constants.php#pdo.constants.attr-driver-name">PDO::ATTR_DRIVER_NAME</a></code></strong>
属性を使用することで、使用中のドライバ名を調べることが可能です。</p>
 <dl>
  
   <dt id="pdo.constants.mysql-attr-use-buffered-query">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-use-buffered-query">PDO::MYSQL_ATTR_USE_BUFFERED_QUERY</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-use-buffered-query">Pdo\Mysql::ATTR_USE_BUFFERED_QUERY</a></code></strong> のエイリアス
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-local-infile">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-local-infile">PDO::MYSQL_ATTR_LOCAL_INFILE</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-local-infile">Pdo\Mysql::ATTR_LOCAL_INFILE</a></code></strong> のエイリアス
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-local-infile-directory">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-local-infile-directory">PDO::MYSQL_ATTR_LOCAL_INFILE_DIRECTORY</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-local-infile-directory">Pdo\Mysql::ATTR_LOCAL_INFILE_DIRECTORY</a></code></strong> のエイリアス
     PHP 8.1.0 以降で利用可能です。
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-init-command">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-init-command">PDO::MYSQL_ATTR_INIT_COMMAND</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-init-command">Pdo\Mysql::ATTR_INIT_COMMAND</a></code></strong> のエイリアス
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-read-default-file">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-read-default-file">PDO::MYSQL_ATTR_READ_DEFAULT_FILE</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-read-default-file">Pdo\Mysql::ATTR_READ_DEFAULT_FILE</a></code></strong> のエイリアス
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-read-default-group">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-read-default-group">PDO::MYSQL_ATTR_READ_DEFAULT_GROUP</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-read-default-group">Pdo\Mysql::ATTR_READ_DEFAULT_GROUP</a></code></strong> のエイリアス
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-max-buffer-size">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-max-buffer-size">PDO::MYSQL_ATTR_MAX_BUFFER_SIZE</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-max-buffer-size">Pdo\Mysql::ATTR_MAX_BUFFER_SIZE</a></code></strong> のエイリアス
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-direct-query">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-direct-query">PDO::MYSQL_ATTR_DIRECT_QUERY</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="pdo.constants.php#pdo.constants.attr-emulate-prepares">PDO::ATTR_EMULATE_PREPARES</a></code></strong> のエイリアス
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-found-rows">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-found-rows">PDO::MYSQL_ATTR_FOUND_ROWS</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-found-rows">Pdo\Mysql::ATTR_FOUND_ROWS</a></code></strong> のエイリアス
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-ignore-space">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-ignore-space">PDO::MYSQL_ATTR_IGNORE_SPACE</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-ignore-space">Pdo\Mysql::ATTR_IGNORE_SPACE</a></code></strong> のエイリアス
    </span>
   </dd>
  
  
   <dt id="pdo.constants.mysql-attr-compress">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-compress">PDO::MYSQL_ATTR_COMPRESS</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-compress">Pdo\Mysql::ATTR_COMPRESS</a></code></strong> のエイリアス
    </span>
   </dd>
  

  
   <dt id="pdo.constants.mysql-attr-server-public-key">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-server-public-key">PDO::MYSQL_ATTR_SERVER_PUBLIC_KEY</a></code></strong>
    (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-server-public-key">Pdo\Mysql::ATTR_SERVER_PUBLIC_KEY</a></code></strong> のエイリアス
    </span>
   </dd>
  

  
   <dt id="pdo.constants.mysql-attr-ssl-ca">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-ca">PDO::MYSQL_ATTR_SSL_CA</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-ssl-ca">Pdo\Mysql::ATTR_SSL_CA</a></code></strong> のエイリアス
    </span>
   </dd>
  

  
   <dt id="pdo.constants.mysql-attr-ssl-capath">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-capath">PDO::MYSQL_ATTR_SSL_CAPATH</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-ssl-capath">Pdo\Mysql::ATTR_SSL_CAPATH</a></code></strong> のエイリアス
    </span>
   </dd>
  

  
   <dt id="pdo.constants.mysql-attr-ssl-cert">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-cert">PDO::MYSQL_ATTR_SSL_CERT</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-ssl-cert">Pdo\Mysql::ATTR_SSL_CERT</a></code></strong> のエイリアス
    </span>
   </dd>
  

  
   <dt id="pdo.constants.mysql-attr-ssl-cipher">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-cipher">PDO::MYSQL_ATTR_SSL_CIPHER</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-ssl-cipher">Pdo\Mysql::ATTR_SSL_CIPHER</a></code></strong> のエイリアス
    </span>
   </dd>
  

  
   <dt id="pdo.constants.mysql-attr-ssl-key">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-key">PDO::MYSQL_ATTR_SSL_KEY</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-ssl-key">Pdo\Mysql::ATTR_SSL_KEY</a></code></strong> のエイリアス
    </span>
   </dd>
  

  
   <dt id="pdo.constants.mysql-attr-ssl-verify-server-cert">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-verify-server-cert">PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-ssl-verify-server-cert">Pdo\Mysql::ATTR_SSL_VERIFY_SERVER_CERT</a></code></strong> のエイリアス
      PHP 7.0.18 および、PHP 7.1.4 以降で利用可能です。
    </span>
   </dd>
  

  
   <dt id="pdo.constants.mysql-attr-multi-statements">
    <strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-multi-statements">PDO::MYSQL_ATTR_MULTI_STATEMENTS</a></code></strong>
     (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <span class="simpara">
     <strong><code><a href="class.pdo-mysql.php#pdo-mysql.constants.attr-multi-statements">Pdo\Mysql::ATTR_MULTI_STATEMENTS</a></code></strong> のエイリアス
    </span>
   </dd>
  

 </dl>
</div>



   


<div class="section" id="pdo-mysql.configuration">
 <h2 class="title">実行時設定</h2>
 <p class="simpara">
<var class="filename">php.ini</var> の設定により動作が変化します。
</p>
 <p class="para">
  <table class="doctable table">
   <caption><strong>PDO_MYSQL 設定オプション</strong></caption>
   
    <thead>
     <tr>
      <th>名前</th>
      <th>デフォルト</th>
      <th>変更可能</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td><a href="ref.pdo-mysql.php#ini.pdo-mysql.default-socket" class="link">pdo_mysql.default_socket</a></td>
      <td>&quot;/tmp/mysql.sock&quot;</td>
      <td><strong><code><a href="info.constants.php#constant.ini-system">INI_SYSTEM</a></code></strong></td>
     </tr>

     <tr>
      <td><a href="ref.pdo-mysql.php#ini.pdo-mysql.debug" class="link">pdo_mysql.debug</a></td>
      <td>NULL</td>
      <td><strong><code><a href="info.constants.php#constant.ini-system">INI_SYSTEM</a></code></strong></td>
     </tr>

    </tbody>
   
  </table>

  INI_* モードの詳細および定義については
<a href="configuration.changes.modes.php" class="xref">どこで設定を行うのか</a> を参照してください。
 </p>

 <p class="para">以下に設定ディレクティブに関する
簡単な説明を示します。</p>

 <p class="para">
  <dl>
   
    <dt id="ini.pdo-mysql.default-socket">
     <code class="parameter">pdo_mysql.default_socket</code>
     <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
    </dt>
    <dd>
     <p class="para">
      Unix ドメインソケットを設定します。
      configure の際にドメインソケットが見つかれば、
      この値はコンパイル時にも設定されます。この ini 設定は Unix でのみ使えます。
     </p>
    </dd>
   
   
    <dt id="ini.pdo-mysql.debug">
     <code class="parameter">pdo_mysql.debug</code>
     <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>
    </dt>
    <dd>
     <p class="para">
      PDO_MYSQL のデバッグ機能を有効にします。
      この設定が有効になるのは、mysqlnd を使って PDO_MYSQL をでコンパイルし、
      かつ PDO がデバッグモードであるときだけです。
     </p>
    </dd>
   
  </dl>
 </p>
</div>



  </div>

  

 <h2>目次</h2><ul class="chunklist chunklist_reference"><li><a href="ref.pdo-mysql.connection.php">PDO_MYSQL DSN</a> — MySQL データベースに接続する</li></ul>
</div>
<?php manual_footer($setup); ?>