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