bdb安裝程式:
1、安裝Berkeley DB
# cd /usr/local/src
# wgethttp://download.oracle.com/berkeley-db/db-4.6.21.tar.gz
# tar -zxvf db-4.6.21.tar.gz
# cd db-4.6.21
# cd build_unix
Berkeley DB預設是安裝在/usr/local/BerkeleyDB.4.6目錄下,其中4.6就是版本号,你也可以指定–prefix參數來設定安裝目錄
# ../dist/configure --prefix=/usr/local/berkeleydb --enable-cxx
其中–enable-cxx就是編譯C++庫,這樣才能編譯Berkeley DB資料庫的PHP擴充php_db4
# make
# make install
# echo '/usr/local/berkeleydb/lib/' >> /etc/ld.so.conf
# ldconfig
這2句的作用就是通知系統Berkeley DB的動态連結庫在/usr/local/berkeleydb/lib/目錄。
如果沒有系統提示ldconfig指令,則用whereis ldconfig找一下在哪,一般可用 # /sbin/ldconfig
至此,Berkeley DB資料庫已經安裝完成。
2、安裝Berkeley DB的PHP擴充
雖然PHP裡已經自帶了php_db和php_dba兩個擴充都支援Berkekey DB,但是畢竟支援的有限,是以還是編譯Berkeley DB自帶的php_db4擴充好。
# cd /usr/local/src/db-4.6.18/php_db4/
# phpize(/usr/local/php/bin/phpize)# ./configure --with-db4=/usr/local/berkeleydb/ --with-php-config=/usr/local/php/bin/php-config
# make
# make install
說明:這裡configure的時候可能會提示你找不到php-config,你可以找到你的php安裝PATH,然後增加--with-php-config=PATH
至此db4已編譯好在/usr/lib64/php/modules/db4.so目錄(具體跟你的系統有關)
echo 'extension=db4.so' > /etc/php.d/db4.ini
重起WEB伺服器(Apache等)
至此php_db4擴充的安裝也完成了,執行php -m即可看到db4擴充已經加載了。
3、測試php_db4擴充php_db4提供了下面4個類:
class Db4Env {
function Db4Env($flags = 0) {}
function close($flags = 0) {}
function dbremove($txn, $filename, $database = null, $flags = 0) {}
function dbrename($txn, $file, $database, $new_database, $flags = 0) {}
function open($home, $flags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, $mode = 0666) {}
function remove($home, $flags = 0) {}
function set_data_dir($directory) {}
function txn_begin($parent_txn = null, $flags = 0) {}
function txn_checkpoint($kbytes, $minutes, $flags = 0) {}
}
class Db4 {
function Db4($dbenv = null) {} // create a new Db4 object using the optional DbEnv
function open($txn = null, $file = null, $database = null, $flags = DB_CREATE, $mode = 0) {}
function close() {}
function del($key, $txn = null) {}
function get($key, $txn = null, $flags = 0) {}
function pget($key, &$pkey, $txn = null, $flags = 0) {}
function get_type() {} // returns the stringified database type name
function stat($txn = null, $flags = 0) {} // returns statistics as an as
function join($cursor_list, $flags = 0) {}
function sync() {}
function truncate($txn = null, $flags = 0) {}
function cursor($txn = null, flags = 0) {}
}
class Db4Txn {
function abort() {}
function commit() {}
function discard() {
function id() {}
function set_timeout($timeout, $flags = 0) {}
}
class Db4Cursor {
function close() {}
function count() {}
function del() {}
function dup($flags = 0) {}
function get($key, $flags = 0) {}
function pget($key, &$primary_key, $flags = 0) {}
function put($key, $data, $flags = 0) {}
}
從字面上也不難了解,Db4Env設定資料庫環境、Db4操作資料庫、Db4Txn用于事務處理、Db4Cursor用于光标處理。具體使用可參考
http://www.oracle.com/technology/documentation/berkeley-db/db/ref/ext/php.html
/usr/local/src/db-4.6.18/php_db4/samples目錄下提供了2個簡單的例子simple_counter.php和transactional_counter.php。
simple_counter.php
transactional_counter.php