博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bdb及其在php下扩展的安装
阅读量:4167 次
发布时间:2019-05-26

本文共 4479 字,大约阅读时间需要 14 分钟。

1、安装Berkeley DB

# cd /usr/local/src
# wget http://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
// 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();
?>

需要补充的是

db4.so文件

cp modules/db4.so /usr/local/php5/ext/
这里需要你先设置你的php的扩展目录的在
在php.ini里面
通过设extension_dir
最后一不是你在php.ini文件中打开这个扩展
extension=jinzhesheng_module.so

http://cms.nn200.com/html/system/201011/1117992/

转载地址:http://vtgxi.baihongyu.com/

你可能感兴趣的文章
商务智能-基本方法-数据钻取
查看>>
C++程序员技术需求规划(发展方向)
查看>>
如何判断变量在内存中如何放置的?低位在前还是高位在前
查看>>
c语言中通过指针将数值赋值到制定内存地址
查看>>
64位与32位linux c开发时默认字节对齐值
查看>>
malloc(malloc在32位编译系统中分配的地址会8字节对齐,64为编译系统中会8或者16字节对齐)
查看>>
初始化时共享内存的key值和信号量初始化的key值可以一样
查看>>
linux创建线程之pthread_create
查看>>
pthread_attr_init线程通俗举例讲解与线程属性
查看>>
进程和线程的区别
查看>>
int main(int argc,char* argv[])详解,以及与int main()有什么区别
查看>>
SourceInsight全工程查找替换方法
查看>>
C语言chdir()函数:改变当前的工作目录
查看>>
Linux下的函数执行时间的统计方法(测试某个函数的执行时间)
查看>>
调整内核printk的打印级别(启动脚本中运行 echo 0 4 0 7 > /proc/sys/kernel/printk 关闭所有内核打印)
查看>>
临时关闭打开console办法
查看>>
Linux中gmtime和localtime的区别(time_t格式转换为tm格式)
查看>>
如果函数传递的是结构体,小心在调用的参数中给指针重新赋值(拿tm结构体举例)
查看>>
使用nm命令获取linux的可执行文件里或动态库中的所有函数名称
查看>>
动态库编写 头文件.h注意事项
查看>>