PHPのMySQL用PDO Extensionである、pdo_mysqlの導入の実施記録です。pdo_mysqlの導入に必要なPEAR、PECLも導入しています。Mac OS Xにて特有と思われるPath違いエラーへの対処法も説明しています。
■ 前提
■ 前提環境の準備
【PHPの所在とバージョンの確認】-----(ここから)-----$ which php/usr/bin/php$ php -vCopyright (c) 1997-2013 The PHP GroupZend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies-----(ここまで)-----
php.iniのデフォルト設定例が下記パスにありました。
MySQLを事前に導入しておきます。
※MySQLの導入についてはこちらで説明しています。
→《MySQLの導入と最も基本的なコマンドの説明》
mysql_config
※ここでは以下のようなsymlinkを作りました。
/usr/local/bin/mysql_config -> /usr/local/mysql-5.6.16-osx10.7-x86_64/bin/mysql_config
※"/usr/local/bin"が環境変数PATHに含まれています。
※これを忘れると、以降のpdo_mysqlの導入で下記のようなエラーがでます。
-----(ここから)-----・・・(略)・・・checking for mysql_config... not found-----(ここまで)-----
http://pear.php.net/manual/ja/about.pear.php
1. ターミナルを開き、カレントディレクトリを"/usr/local"に移動します。
$ cd /usr/local
$ sudo curl -o go-pear.phar http://pear.php.net/go-pear.phar
※http://pear.php.net/go-pearから得られる"go-pear.php"では、PHP 5.3に対して古すぎるとエラーになりました。
◆注意◆・「1. Installation base」を"/usr/local/pear"に変更しました。・他はデフォルトのまま受入れています。
【PEAR導入の実行ログ】
-----(ここから)-----
$ sudo php -q go-pear.pharPassword:Below is a suggested file layout for your new PEAR installation. Tochange individual locations, type the number in front of thedirectory. Type 'all' to change all of them or simply press Enter toaccept these locations.1. Installation base ($prefix) : /usr・・・(略)・・・11. Name of configuration file : /private/etc/pear.conf1-11, 'all' or Enter to continue: 1(Use $prefix as a shortcut for '/usr', etc.)Installation base ($prefix) [/usr] : /usr/local/pearBelow is a suggested file layout for your new PEAR installation. Tochange individual locations, type the number in front of thedirectory. Type 'all' to change all of them or simply press Enter toaccept these locations.1. Installation base ($prefix) : /usr/local/pear2. Temporary directory for processing : /tmp/pear/install3. Temporary directory for downloads : /tmp/pear/install4. Binaries directory : /usr/local/pear/bin6. Documentation directory : /usr/local/pear/docs7. Data directory : /usr/local/pear/data8. User-modifiable configuration files directory : /usr/local/pear/cfg9. Public Web Files directory : /usr/local/pear/www10. Tests directory : /usr/local/pear/tests11. Name of configuration file : /private/etc/pear.conf1-11, 'all' or Enter to continue:
Beginning install...PHP Warning: date(): It is not safe to rely on the system's timezone settings.・・・(略)・・・Configuration written to /private/etc/pear.conf...Initialized registry...Preparing to install...・・・(略)・・・******************************************************************************WARNING! The include_path defined in the currently used php.ini does notIf the specified directory is also not in the include_path used byyour scripts, you will have problems getting any PEAR packages working.
Current include path : .:Press Enter to continue:** WARNING! Old version found at /usr/local/pear/bin, please remove it or be sure to use the new /usr/local/pear/bin/pear command** The 'pear' command is not currently in your PATH, so you need to** '/usr/local/pear/bin' to your PATH environment variable.Run it without parameters to see the available actions, try 'pear list'to see what packages are installed, or 'pear help' for help.For more information about PEAR, see:Thanks for using go-pear!-----(ここまで)-----
3. 下記のsymlinkを作成しました。
/usr/local/bin/pear -> /usr/local/pear/bin/pear
/usr/local/bin/pecl -> /usr/local/pear/bin/pecl
1. 下記コマンドを実行します。
-----(ここから)-----
$ sudo pecl install pdo_mysqlPassword:WARNING: "pecl/PDO_MYSQL" is deprecated in favor of "channel://http://svn.php.net/viewvc/php/php-src/trunk/ext/pdo_mysql//ext/pdo_mysql"WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to updateWARNING: "pecl/PDO" is deprecated in favor of "channel://http://svn.php.net/viewvc/php/php-src/trunk/ext/pdo//ext/PDO"downloading PDO_MYSQL-1.0.2.tgz ...Starting to download PDO_MYSQL-1.0.2.tgz (14,778 bytes).....done: 14,778 bytesdownloading PDO-1.0.3.tgz ...Starting to download PDO-1.0.3.tgz (52,613 bytes)...done: 52,613 bytes12 source files, buildingrunning: phpizeConfiguring for:Zend Module Api No: 20090626Zend Extension Api No: 220090626configure.in:3: warning: prefer named diversionsconfigure.in:3: warning: prefer named diversionsrunning: /private/tmp/pear/install/PDO/configurechecking for egrep... /usr/bin/grep -E・・・(略)・・・checking whether to build static libraries... nocreating libtoolappending configuration tag "CXX" to libtoolconfigure: creating ./config.statusconfig.status: creating config.hrunning: makeBuild complete.Don't forget to run 'make test'.running: make INSTALL_ROOT="/private/tmp/pear/install/pear-build-rootjUcOqf/install-PDO-1.0.3" installInstalling shared extensions: /private/tmp/pear/install/pear-build-rootjUcOqf/install-PDO-1.0.3/usr/lib/php/extensions/no-debug-non-zts-20090626/cp: modules/*: No such file or directorymake: *** [install-modules] Error 1ERROR: `make INSTALL_ROOT="/private/tmp/pear/install/pear-build-rootjUcOqf/install-PDO-1.0.3" install' failed7 source files, buildingrunning: phpizeConfiguring for:Zend Module Api No: 20090626Zend Extension Api No: 220090626configure.in:3: warning: prefer named diversions・・・(略)・・・config.m4:104: the top level・・・(略)・・・・・・(略)・・・running: make・・・(略)・・・/private/tmp/pear/install/PDO_MYSQL/mysql_statement.c:176:81: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]if (S->fields[i].max_length == 0 && S->bound_result[i].buffer_length < 128 && MYSQL_TYPE_VAR_STRING) {^ ~~~~~~~~~~~~~~~~~~~~~if (S->fields[i].max_length == 0 && S->bound_result[i].buffer_length < 128 && MYSQL_TYPE_VAR_STRING) {^~&/private/tmp/pear/install/PDO_MYSQL/mysql_statement.c:176:81: note: remove constant to silence this warningif (S->fields[i].max_length == 0 && S->bound_result[i].buffer_length < 128 && MYSQL_TYPE_VAR_STRING) {~^~~~~~~~~~~~~~~~~~~~~~~~/private/tmp/pear/install/PDO_MYSQL/mysql_statement.c:309:11: warning: 5 enumeration values not handled in switch: 'PDO_PARAM_EVT_FREE', 'PDO_PARAM_EVT_EXEC_POST', 'PDO_PARAM_EVT_FETCH_PRE'... [-Wswitch]switch (event_type) {^/private/tmp/pear/install/PDO_MYSQL/mysql_statement.c:422:21: warning: assigning to 'long *' from 'unsigned long *' converts between pointers to integer types with different sign [-Wpointer-sign]S->current_lengths = mysql_fetch_lengths(S->result);^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~3 warnings generated.・・・(略)・・・----------------------------------------------------------------------Libraries have been installed in:If you ever happen to want to link against installed librariesin a given directory, LIBDIR, you must either use libtool, andspecify the full pathname of the library, or use the `-LLIBDIR'flag during linking and do at least one of the following:- add LIBDIR to the `DYLD_LIBRARY_PATH' environment variableduring executionSee any operating system documentation about shared libraries formore information, such as the ld(1) and ld.so(8) manual pages.----------------------------------------------------------------------Build complete.Don't forget to run 'make test'.running: make INSTALL_ROOT="/private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.2" installInstalling shared extensions: /private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.2/usr/lib/php/extensions/no-debug-non-zts-20090626/running: find "/private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.2" | xargs ls -dils31764657 0 drwxr-xr-x 3 root wheel 102 3 6 15:00 /private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.231765084 0 drwxr-xr-x 3 root wheel 102 3 6 15:00 /private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.2/usr31765085 0 drwxr-xr-x 3 root wheel 102 3 6 15:00 /private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.2/usr/lib31765086 0 drwxr-xr-x 3 root wheel 102 3 6 15:00 /private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.2/usr/lib/php31765087 0 drwxr-xr-x 3 root wheel 102 3 6 15:00 /private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.2/usr/lib/php/extensions31765088 0 drwxr-xr-x 3 root wheel 102 3 6 15:00 /private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.2/usr/lib/php/extensions/no-debug-non-zts-2009062631765089 64 -rwxr-xr-x 1 root wheel 31660 3 6 15:00 /private/tmp/pear/install/pear-build-rootCVPpY5/install-PDO_MYSQL-1.0.2/usr/lib/php/extensions/no-debug-non-zts-20090626/pdo_mysql.soBuild process completed successfully-----(ここまで)-----
2. pdo_mysql.soが作成されているのを確認します。
-----(ここから)-----
$ ls -l /usr/lib/php/extensions/no-debug-non-zts-20090626/total 224-rw-r--r-- 1 root wheel 31660 3 6 15:00 pdo_mysql.so-rwxr-xr-x 1 root wheel 202736 9 24 15:08 xdebug.so-----(ここまで)-----
【php.ini追記内容】
-----(ここから)----------(ここまで)-----
4. もし、PHP処理系起動時、下記のようなエラーが発生する場合、環境変数DYLD_LIBRARY_PATHの設定をするか、libmysqlclient.18.dylibへのsymlinkを作成してください。
【発生し得るエラー】
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20090626/pdo_mysql.so' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20090626/pdo_mysql.so, 9): Library not loaded: libmysqlclient.18.dylib\n Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20090626/pdo_mysql.so\n Reason: image not found in Unknown on line 0
【環境変数DYLD_LIBRARY_PATHを設定する場合の内容】-----(ここから)-----export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql/lib-----(ここまで)-----
【libmysqlclient.18.dylibへのsymlinkを作る場合の内容】/usr/lib/libmysqlclient.18.dylib -> /usr/local/mysql/lib/libmysqlclient.18.dylib
【発生し得るエラー】
※次のような、PDOオブジェクトを構築してDB接続を得ようというコードにて、上記実行時エラーが発生し得ます。
-----(ここから)-----$con = new PDO($dsn, &uid, $pwd);-----(ここまで)-----
【symlink作成手順】-----(ここから)-----$ sudo mkdir /var/mysql-----(ここまで)-----
◆以上
関連記事