読者です 読者をやめる 読者になる 読者になる

たなかこういちの資料室

システム開発に携わる筆者があれこれ試したことや学んだことについてのまとめ

PEAR、PECL、pdo_mysqlの導入記録

PHPMySQL用PDO Extensionである、pdo_mysqlの導入の実施記録です。pdo_mysqlの導入に必要なPEARPECLも導入しています。Mac OS Xにて特有と思われるPath違いエラーへの対処法も説明しています。

 
前提
 
- Mac OS X 10.8.5
- 2014年3月6日に実施
 
■ 前提環境の準備
 
 
大前提であるPHPは、Mac OS X 10.8.5に既に導入済みだったものを、そのまま使うこととしました。
 
PHPの所在とバージョンの確認】
-----(ここから)-----
$ which php
/usr/bin/php
$ php -v
PHP 5.3.26 (cli) (built: Jul  7 2013 19:05:08)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
-----(ここまで)-----

php.iniのデフォルト設定例が下記パスにありました。
 
/etc/php.ini.default
 
これをコピーして"php.ini"を作成しました。以降のPEARPECLの導入にて、この"php.ini"に対して変更がなされます。
 
 
MySQLを事前に導入しておきます。
 
MySQLの導入についてはこちらで説明しています。
 
→《MySQLの導入と最も基本的なコマンドの説明
 
MySQLの下記コマンドがサーチPathに含まれるように、環境変数PATHを編集するか、symlinkを作るかしてください。
 
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の導入で下記のようなエラーがでます。
 
-----(ここから)-----
$ sudo pecl install pdo_mysql
・・・(略)・・・
checking for mysql_config... not found
configure: error: Cannot find MySQL header files under
ERROR: `/private/tmp/pear/install/PDO_MYSQL/configure' failed
-----(ここまで)-----
 
■ PEARPECLの導入
 
0. pdo_mysqlを導入するために、まず、PEAR(とPECL)を導入します。
 
PEARPECLについて、下記ページに説明があります。
 
http://pear.php.net/manual/ja/about.pear.php
 
1. ターミナルを開き、カレントディレクトリを"/usr/local"に移動します。
 
$ cd /usr/local
 
2. 下記コマンドでPEAR(とPECL)を導入します。
 
 
http://pear.php.net/go-pearから得られる"go-pear.php"では、PHP 5.3に対して古すぎるとエラーになりました。
 
$ sudo php -q go-pear.phar
 
注意
・「1. Installation base」を"/usr/local/pear"に変更しました。
・他はデフォルトのまま受入れています。
 
PEAR導入の実行ログ】
-----(ここから)-----
$ sudo php -q go-pear.phar
Password:
 
Below is a suggested file layout for your new PEAR installation.  To
change individual locations, type the number in front of the
directory.  Type 'all' to change all of them or simply press Enter to
accept these locations.
 
 1. Installation base ($prefix)                   : /usr
・・・(略)・・・
11. Name of configuration file                    : /private/etc/pear.conf
 
1-11, 'all' or Enter to continue: 1
(Use $prefix as a shortcut for '/usr', etc.)
Installation base ($prefix) [/usr] : /usr/local/pear
 
Below is a suggested file layout for your new PEAR installation.  To
change individual locations, type the number in front of the
directory.  Type 'all' to change all of them or simply press Enter to
accept these locations.
 
 1. Installation base ($prefix)                   : /usr/local/pear
 2. Temporary directory for processing            : /tmp/pear/install
 3. Temporary directory for downloads             : /tmp/pear/install
 4. Binaries directory                            : /usr/local/pear/bin
 5. PHP code directory ($php_dir)                 : /usr/local/pear/share/pear
 6. Documentation directory                       : /usr/local/pear/docs
 7. Data directory                                : /usr/local/pear/data
 8. User-modifiable configuration files directory : /usr/local/pear/cfg
 9. Public Web Files directory                    : /usr/local/pear/www
10. Tests directory                               : /usr/local/pear/tests
11. Name of configuration file                    : /private/etc/pear.conf
 
1-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...
installing phar:///usr/local/go-pear.phar/PEAR/go-pear-tarballs/Archive_Tar-1.3.11.tar...
・・・(略)・・・
install ok: channel://pear.php.net/PEAR-1.9.4
PEAR: Optional feature webinstaller available (PEAR's web-based installer)
PEAR: Optional feature gtkinstaller available (PEAR's PHP-GTK-based installer)
PEAR: Optional feature gtk2installer available (PEAR's PHP-GTK2-based installer)
PEAR: To install optional features use "pear install pear/PEAR#featurename"
 
******************************************************************************
WARNING!  The include_path defined in the currently used php.ini does not
contain the PEAR PHP directory you just specified:
</usr/local/pear/share/pear>
If the specified directory is also not in the include_path used by
your scripts, you will have problems getting any PEAR packages working.
 
 
Would you like to alter php.ini </private/etc/php.ini>? [Y/n] :
 
php.ini </private/etc/php.ini> include_path updated.
 
Current include path           : .:
Configured directory           : /usr/local/pear/share/pear
Currently used php.ini (guess) : /private/etc/php.ini
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 now at your service at /usr/local/pear/bin/pear
 
** The 'pear' command is not currently in your PATH, so you need to
** use '/usr/local/pear/bin/pear' until you have added
** '/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
 
pdo_mysqlの導入(PECLによる)
 
1. 下記コマンドを実行します。
 
$ sudo pecl install pdo_mysql
 
PECLによるpdo_mysqlの導入の実行ログ】
-----(ここから)-----
$ sudo pecl install pdo_mysql
Password:
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 update
WARNING: "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 bytes
downloading PDO-1.0.3.tgz ...
Starting to download PDO-1.0.3.tgz (52,613 bytes)
...done: 52,613 bytes
12 source files, building
running: phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
configure.in:3: warning: prefer named diversions
configure.in:3: warning: prefer named diversions
building in /private/tmp/pear/install/pear-build-rootjUcOqf/PDO-1.0.3
running: /private/tmp/pear/install/PDO/configure
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
・・・(略)・・・
checking whether to build static libraries... no
 
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
running: make
 
Build complete.
Don't forget to run 'make test'.
 
running: make INSTALL_ROOT="/private/tmp/pear/install/pear-build-rootjUcOqf/install-PDO-1.0.3" install
Installing 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 directory
make: *** [install-modules] Error 1
ERROR: `make INSTALL_ROOT="/private/tmp/pear/install/pear-build-rootjUcOqf/install-PDO-1.0.3" install' failed
7 source files, building
running: phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
configure.in:3: warning: prefer named diversions
・・・(略)・・・
config.m4:104: the top level
building in /private/tmp/pear/install/pear-build-rootCVPpY5/PDO_MYSQL-1.0.2
running: /private/tmp/pear/install/PDO_MYSQL/configure
checking for grep that handles long lines and -e... /usr/bin/grep
・・・(略)・・・
checking for mysql_config... /usr/local/bin/mysql_config
・・・(略)・・・
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) {
                                                                                                                   ^  ~~~~~~~~~~~~~~~~~~~~~
/private/tmp/pear/install/PDO_MYSQL/mysql_statement.c:176:81: note: use '&' for a bitwise operation
                                        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 warning
                                        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: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:
   /private/tmp/pear/install/pear-build-rootCVPpY5/PDO_MYSQL-1.0.2/modules
 
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify 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 variable
     during execution
 
See any operating system documentation about shared libraries for
more 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" install
Installing 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 -dils
31764657  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
31765084  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
31765085  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
31765086  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
31765087  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
31765088  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-20090626
31765089 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.so
 
Build process completed successfully
Installing '/usr/lib/php/extensions/no-debug-non-zts-20090626/pdo_mysql.so'
install ok: channel://pecl.php.net/PDO_MYSQL-1.0.2
configuration option "php_ini" is not set to php.ini location
You should add "extension=pdo_mysql.so" to php.ini
-----(ここまで)-----
 
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
-----(ここまで)-----
 
3. php.iniに"extension=pdo_mysql.so"を追記します。
 
php.ini追記内容】
-----(ここから)-----
extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/pdo_mysql.so
-----(ここまで)-----
 
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
 
PHP Moduleを有効にしたApacheの場合、起動時にerror_logにこのようなエラーが出ているか確認してみてください。
 
環境変数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
 
5. もし、PHPプログラム実行時、下記のようなエラーが発生する場合、示すようにmysql.sockへのsymlinkを作成してください。
 
【発生し得るエラー】
PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)
 
※次のような、PDOオブジェクトを構築してDB接続を得ようというコードにて、上記実行時エラーが発生し得ます。
 
-----(ここから)-----
$con = new PDO($dsn, &uid, $pwd);
-----(ここまで)-----
 
【symlink作成手順】
-----(ここから)-----
$ sudo mkdir /var/mysql
$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
-----(ここまで)-----
 
◆以上
 

関連記事