专注系统运维、网络架构,研究技术凯发app官方网站的解决方案,记录我的思想轨迹、工作学习、生活和关注的领域
分类: linux
2011-03-31 19:57:08
bdb安装程序:
1、安装berkeley db
# cd /usr/local/src
# wget
# 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用于光标处理。具体使用可参考
/usr/local/src/db-4.6.18/php_db4/samples目录下提供了2个简单的例子simple_counter.php和transactional_counter.php。
simple_counter.php
// create a new db4 instance
$db = new db4();
// open it outside a db4env environment with datafile/var/lib/db4
// and database name "test"
$db->open(null, "/var/tmp/db4", "test");
// get the current value of "counter"
$counter = $db->get("counter");
print "counter value is $counter\n";
// increment $counter and put() it.
$db->put("counter", $counter 1);
// sync to be certain, since we're leaving the handle open
$db->sync();
?>
transactional_counter.php
// open a new db4env
$dbenv = new db4env();
$dbenv->set_data_dir("/var/tmp/dbhome");
$dbenv->open("/var/tmp/dbhome");
// open a database in $dbenv. note that even though
// we pass null in as the transaction, db4 forces this
// operation to be transactionally protected, so php
// will force auto-commit internally.
$db = new db4($dbenv);
$db->open(null, 'a', 'foo');
$counter = $db->get("counter");
// create a new transaction
$txn = $dbenv->txn_begin();
if($txn == false) {
print "txn_begin failed";
exit;
}
print "current value of counter is $counter\n";
// increment and reset counter, protect it with $txn
$db->put("counter", $counter 1, $txn);
// commit the transaction, otherwise the above put() will rollback.
$txn->commit();
// sync for good measure
$db->sync();
// this isn't a real close, use _close() for that.
$db->close();
?>