Home | Mirror | SearchITEYE 博客 | OSChina 博客 | 51CTO 博客

7.4. Database Extensions

7.4.1. sqlite

		
mkdir /srv/php-5.2.17/etc/conf.d
/srv/php-5.2.17/bin/phpize
./configure --with-php-config=/srv/php-5.2.17/bin/php-config
make && make install
		
		
		
cat > /srv/php-5.2.17/etc/conf.d/sqlite.ini <<EOF
extension=sqlite.so
extension=pdo_sqlite.so
EOF
		
		

7.4.2. mysqli

		
mkdir /srv/php-5.2.17/etc/conf.d
/srv/php-5.2.17/bin/phpize
./configure --with-php-config=/srv/php-5.2.17/bin/php-config
make && make install
		
		
		
cat > /srv/php-5.2.17/etc/conf.d/mysqli.ini <<EOF
extension=mysqli.so
EOF
		
		

7.4.3. MongoDB

sudo pecl install mongo
		
vim /srv/php/etc/conf.d/mongo.ini
extension=mongo.so
		

7.4.4. php-redis.x86_64 : Extension for communicating with the Redis key-value store

7.4.4.1. pecl 安装

pecl install redis
			

7.4.4.2. yum安装

https://github.com/nicolasff/phpredis

yum search redis

php-redis.x86_64 : Extension for communicating with the Redis key-value store
python-redis.noarch : A Python client for redis
redis.x86_64 : A persistent key-value database
			

7.4.4.3. 源码编译安装

安装git版本控制客户端

yum install git
			

从github仓库中克隆一份代码到本地

git clone git://github.com/nicolasff/phpredis.git
			

编译安装phpredis; 我暂时没有找到 pecl的phpredis源

			
cd phpredis
phpize
./configure --with-php-config=/srv/php-5.4.9/bin/php-config
make && make install
			
			

创建配置文件

7.4.4.4. 配置 redis.ini

			
cat > /srv/php-5.4.9/etc/conf.d/redis.ini <<EOF
extension=redis.so
EOF
			
			

查看安装情况

# php -m | grep redis
redis
			

7.4.4.5. Session 配置

使用 Redis 存储 Session 数据

			
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
或者使用多个redis
session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2"
			
			

7.4.5. php5-pgsql - PostgreSQL module for php5

$ sudo apt-get install php5-pgsql
		

7.4.6. PHP connect SQL Server under unix like

Sql Server 支持由Freetds提供

主页:http://www.freetds.org/

cd /usr/local/src/
wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
tar zxvf freetds-stable.tgz
cd freetds-0.64/

./configure --prefix=/usr/local/freetds-0.64
make
make install

ln -s /usr/local/freetds-0.64 /usr/local/freetds
		

configure

./configure --prefix=/usr/local/php-5.2.3 \
--with-config-file-path=/usr/local/php-5.2.3/etc \
--enable-fastcgi \
--enable-force-cgi-redirect \
--with-curl \
--with-gd \
--with-ldap \
--enable-zip \
--enable-exif \
--enable-pcntl \
--with-mssql=/usr/local/freetds

make
make test
make install
		

MSSQL在PHP中的配置如下

/usr/local/freetds/etc/freetds.conf

[MyServer2k]
       host = 10.10.10.11
       port = 3433
       tds version = 8.0
		

mssql.php 测试文件

		
<?php
$conn = mssql_pconnect('MyServer2k', 'u_mobile', 'kEyt+_Zf.$P6');
mssql_select_db('D3_Mobile', $conn);
$query = mssql_query ('select * from dbo.MobileCommand where id=1');
$result = mssql_fetch_array ($query);
echo '<pre>';
print_r($result);
echo '</pre>';
?>
		
		
[注意]mssql_pconnect

resource mssql_connect ( [string servername [, string username [, string password [, bool new_link]]]] )

servername 指的是freetds.conf中定义服务器名

测试

php -q mssql.php

7.4.7. MySQL

$ sudo apt-get install php5-mysql
		

7.4.8. oracle

pdo_oci/oci8

http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

export ORACLE_HOME=/usr/lib/oracle/11.1/client64/
export LD_LIBRARY_PATH=/usr/lib/oracle/11.1/client64:$LD_LIBRARY_PATH
export  NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
		

cd /usr/local/src/php-5.2.14/ext/pdo_oci
/usr/local/php-5.2.14/bin/phpize
./configure --with-php-config=/usr/local/php-5.2.14/bin/php-config --with-oci8=instantclient,/usr/lib/oracle/11.1/client64/lib
    make
    make install
		
   安装后生成/usr/local/php/lib/php/extensions/no-debug-non-zts-yyyymmdd/oci8.so
   把oci8.so 移动到/usr/local/php/lib/php/extensions/目录下
    mv /usr/local/php/lib/php/extensions/no-debug-non-zts-yyyymmdd/oci8.so ../

    extension_dir = /usr/local/php/lib/php/extensions/

   添加
     extension = pdo_oci.so
     extension = oci8.so
		

例 7.3. oracle

			
ln -s /usr/lib/oracle/10.2.0.3/client64 /usr/lib/oracle/10.2.0.3/client
ln -s /usr/include/oracle/10.2.0.3/client64    /usr/include/oracle/10.2.0.3/client

export ORACLE_HOME=/usr/lib/oracle/10.2.0.3/client64/
export LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.3/client64:$LD_LIBRARY_PATH
export  NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
# /usr/local/php-5.2.14/bin/phpize
# ./configure --with-php-config=/usr/local/php-5.2.14/bin/php-config  --with-pdo-oci=instantclient,/usr,10.2.0.3
# make && make install
			
			

php.ini

extension=pdo_oci.so
			

oci.php

			
    <?php
    $pdo= new PDO("oci:dbname=//localhost:1521/mydbname;charset=utf-8,username,password);

    $sql="select table_name as tname from user_tables";
    $query = $pdo->prepare($sql);
    $query->execute();

    for($i=0; $row = $query->fetch(); $i++){
      #print_r($row);
      echo $i." - ".$row[0]."<br/>";
      echo $i." - ".$row['TNAME']."<br/>";
    }
    ?>
			
			

comments powered by Disqus