Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

5.4. Database Extensions

5.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
		
		

5.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
		
		

5.4.3. MongoDB

5.4.3.1. 安装

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

5.4.3.2. GridFS

5.4.3.2.1. 上传文件
			
<?php
$connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" );
$db = $connection->test;

$grid = $db->getGridFS();
$id = $grid->put("/etc/passwd");
			
			
5.4.3.2.2. 读取文件
			
<?php
$connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" );
$db = $connection->test;

$grid = $db->getGridFS();
$id = $grid->put("/etc/passwd");

$oid = new MongoId($id);

$file = $grid->get($oid);

echo $file->getBytes();			
			
			
5.4.3.2.3. storeFile

storeFile 与 put 类似

			
<?php
$connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" );
$db = $connection->test;

$grid = $db->getGridFS();
$storedfile = $grid->storeFile("/etc/passwd", array("date" => new MongoDate()));

// Return newly stored file's Document ID
echo $storedfile;			
			
			
5.4.3.2.4. 查找文件

findOne 与 get 类似,get 只能通过id取出文件,findOne可以查找文件名,日期,尺寸,以及md5值。

			
<?php
$connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" );
$db = $connection->test;

$grid = $db->getGridFS();
$file = $grid->findOne("/etc/passwd");

echo $file->getBytes();

			
			

指定 collections

			
<?php

$images = $db->mydb->getGridFS('images');

$image = $images->findOne('jwage.png');

header('Content-type: image/png;');
echo $image->getBytes();
?>			
			
			
5.4.3.2.5. 遍历文件

findOne一直只返回一条数据,find可以返回结果集,实现遍历文件。

			
<?php
$connection = new MongoClient( "mongodb://neo:chen@192.168.6.1" );
$db = $connection->test;

$grid = $db->getGridFS();
$files = $grid->find();
foreach ($files as $file){
	print_r($file);
}			
			
			

5.4.4. PHP Memcache

参考PHP安装

5.4.4.1. pecl

进入PHP工作目录

cd /usr/local/php/
			

安装 memcache

pecl install

pecl install memcache
			

php.ini memcache 参数

			
cat >> /srv/php/etc/php.ini <<EOF
extension = memcache.so
[memcache]
memcache.allow_failover = 1
memcache.max_failover_attempts=20
memcache.chunk_size =8192
memcache.default_port = 11211
memcache.default_timeout_ms=30
EOF
			
			

5.4.4.2. memcached

# yum install libmemcached-devel			
# pecl install memcached			
			
			
			
			
			

5.4.4.3. apt-get

$ sudo apt-get install php5-memcache
			

memcache.ini 文件

$ cat /etc/php5/conf.d/memcache.ini
; uncomment the next line to enable the module
extension=memcache.so

[memcache]
memcache.dbpath="/var/lib/memcache"
memcache.maxreclevel=0
memcache.maxfiles=0
memcache.archivememlim=0
memcache.maxfilesize=0
memcache.maxratio=0
			

5.4.4.4. example

例 5.2. memcache.php

				
<?php
	$memcache = new Memcache;
	$memcache->connect('localhost', 11211) or die ("Could not connect");

	$memcache->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
	echo $memcache->get('var_key');
?>
				
				

运行测试脚本

php -q memcache.php
				

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

5.4.5.1. pecl 安装

pecl install redis
			

php.ini

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

5.4.5.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
			

5.4.5.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
			
			

创建配置文件

5.4.5.4. 配置 redis.ini

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

查看安装情况

# php -m | grep redis
redis
			

5.4.5.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"
			
			

auth 认证配置

			
session.save_handler = redis
session.save_path = "tcp://redis.example.com:6379?auth=[AUTH_STRING_HERE]"
									
			

数据库选择

			
session.save_path = "unix:///var/run/redis/redis.sock?persistent=1&weight=1&database=0
						
			

5.4.6. php5-pgsql - PostgreSQL module for php5

$ sudo apt-get install php5-pgsql
		

5.4.7. 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

5.4.8. MySQL

$ sudo apt-get install php5-mysql
		

5.4.9. 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
		

例 5.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/>";
    }
    ?>