PHP安装 OCI8 和 PDO_OCI 扩展和PDO、swoole连接池配置示例

PHP安装 OCI8 和 PDO_OCI 扩展和PDO、swoole连接池配置示例

本文环境:服务器(centos7.9)PHP版本(7.2.33)

安装Oracle客户端

  • 下载 Oracle Instantclient ,包括2个文件:Basic、SDK 。下载你机器对应的版本

https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html

instantclient-basic-linux.x64-21.6.0.1.0.zip
instantclient-sdk-linux.x64-21.6.0.1.0.zip
  • 配置解压 Oracle Instantcient ,并创建必要的软连接(注意你下载的文件的版本,可能与文章的不同)
mkdir -p /opt/oracle
mv ./instantclient-*.zip /opt/oracle/
cd /opt/oracle
unzip instantclient-basic-linux.x64-21.6.0.1.0.zip
unzip instantclient-sdk-linux.x64-21.6.0.1.0.zip

mv instantclient_21.6 instantclient
cd instantclient
ln -s /opt/oracle/instantclient/libclntsh.so.21.1 /opt/oracle/instantclient/libclntsh.so
ln -s /opt/oracle/instantclient/libocci.so.21.1 /opt/oracle/instantclient/libocci.so
ln -s /opt/oracle/instantclient/ /opt/oracle/instantclient/lib

mkdir -p include/oracle/21.6/
cd include/oracle/21.6/
ln -s ../../../sdk/include client
cd -

mkdir -p lib/oracle/21.6/client
cd lib/oracle/21.6/client
ln -s ../../../ lib
cd -

mkdir -p /opt/oracle/instantclient/lib/oracle/21.6
ln -s /opt/oracle/instantclient/sdk  /opt/oracle/instantclient/lib/oracle/21.6/client
ln -s /opt/oracle/instantclient  /opt/oracle/instantclient/lib/oracle/21.6/client/lib

echo /opt/oracle/instantclient/ | sudo tee -a /etc/ld.so.conf.d/oracle.conf
ldconfig

安装 oci8 和 pdo_oci 扩展

  • 扩展包从php对应版本的源码包下ext下获取,记得切换到对应的版本分支,我的php版本为7.2.33

https://github.com/php/php-src/tree/PHP-7.2.33

  • 安装oci8

    cd oci8
    phpize
    ./configure --with-oci8=instantclient,/opt/oracle/instantclient/ --with-php-config=/www/server/php/72/bin/php-config
    make
    make install
    
  • 安装pdo_oci

    cd pdo_oci
    phpize
    ./configure --with-pdo-oci=instantclient,/opt/oracle/instantclient,21.6 --with-php-config=/www/server/php/72/bin/php-config
    make
    make install
    
  • 打开phpini,在文本尾部添加,重启就OK了

    [oci8]
    extension = oci8.so
    [pdo_oci]
    extension = pdo_oci.so
    

下载地址:php7.2.33扩展和Oracle21.6客户端源码

https://zhuoxin.lanzout.com/id22607juowd 密码:cho7

参考:https://www.jianshu.com/p/dae94faabe6a

使用PDO连接Oracle

  • PDO连接

    语法

    $dsn = "oci:dbname=//IP地址:端口(默认1521)/数据库名";

    new PDO(user, $pwd);

    $dsn="oci:dbname=//127.0.0.1:1521/user'";
    $user="root";
    $pwd="123456"
    $pdo = new PDO($dsn, $user, $pwd);
    
  • 使用swoole连接池,前提安装swoole扩展

    $PDOConfig = (new PDOConfig)
                    ->withDriver('oci')
                    ->withDbname('//127.0.0.1:1521/user')
                    ->withCharset('utf8')
                    ->withUsername('root')
                    ->withPassword('123456');
    // 初始化连接池
    $pool = new PDOPool($PDOConfig);
    

你可能感兴趣的:(PHP安装 OCI8 和 PDO_OCI 扩展和PDO、swoole连接池配置示例)