php安装oracle扩展折腾手记

安装oracle client

下载rpm包 oracle client x86_64

  • oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
  • oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
  • oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm (不是必须)

转换成deb包

1
2
3
sudo alien oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
sudo oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
sudo alien oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm

安装

1
2
3
sudo dpkg -i oracle-instantclient11.2-basic_11.2.0.4.0-2_amd64.deb
sudo dpkg -i oracle-instantclient11.2-devel_11.2.0.4.0-2_amd64.deb
sudo dpkg -i oracle-instantclient11.2-sqlplus_11.2.0.4.0-2_amd64.deb

创建软链接

32位软链(64位系统必须,遗留的一个bug)

1
2
sudo ln -s /usr/lib/oracle/11.2/client64 /usr/lib/oracle/11.2/client
sudo ln -s /usr/include/oracle/11.2/client64 /usr/include/oracle/11.2/client

oracle 10g软链(pdo_oci可能不支持oracle11g)

1
2
sudo ln -s /usr/include/oracle/11.2 /usr/include/oracle/10.2.0.1
sudo ln -s /usr/lib/oracle/11.2 /usr/lib/oracle/10.2.0.1

定义环境变量

1
2
3
4
5
export ORACLE_BASE=/usr/lib/oracle/11.2
export ORACLE_HOME=$ORACLE_BASE/client64
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
export TNS_NAMES=$ORACLE_HOME/network/admin

源码安装oci8、pdo_oci

下载oci8、pdo_oci扩展

安装

1
sudo ./configure --prefix=/usr/local/php5.3.28/ --enable-fpm --with-mysql --with-oci8 --with-pdo-oci=instantclient,/usr,10.2.0.1 --with-pdo-mysql

测试

php -m

php-m-oci

oracle连接测试

1
2
3
4
5
6
7
8
9
10
<?php
$tns = '(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST =10.6.219.184)(PORT =1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ORCL)
)
);charset=AL32UTF8';
$dbh = new PDO('oci:dbname='.$tns, 'tongji', 'tongji123');
echo 'Success';

遇到的问题

  • Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000]: OCIEnvNlsCreate: Check the character set is valid and that PHP has access to Oracle libraries and NLS data (/home/vagrant/php-5.3.28/ext/pdo_oci/oci_driver.c:610)’ in /mfs/product/yidong/web/trunk/test2.php:9 Stack trace: #0 /mfs/product/yidong/web/trunk/test2.php(9): PDO->__construct(‘oci:dbname=(DES…’, ‘tongji’, ‘tongji123’) #1 {main} thrown in /mfs/product/yidong/web/trunk/test2.php on line 9

在php-fpm.conf文件中加入LD_LIBRARY_PATHORACLE_HOME

php-oracle-add-env

明明设置了LD_LIBRARY_PATHORACLE_HOME这两个环境变量,为什么会读取不到呢?

–原来使用source导入/etc/profile的用户是vagrant,而启动php-fpm使用了sudo

参考文档

坚持原创技术分享,您的支持将鼓励我继续创作!