设为首页 收藏本站
查看: 786|回复: 0

[经验分享] bdb及其在php下扩展的安装 & bdb性能测试

[复制链接]

尚未签到

发表于 2017-4-11 08:24:44 | 显示全部楼层 |阅读模式
  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/
  -----------------------------
BerkeleyDB性能测试

table { padding: 2px; border: 1px solid rgb(170, 170, 170); border-collapse: collapse; }td { padding: 2px; border: 1px solid rgb(170, 170, 170); border-collapse: collapse; }BerkeleyDB的cache可以采用Hash和BTree两种索引方式,今天测试了这两种方式的存取速度,测试方法是:向bdb中写100万条记录,key是1至100万的数字编号,value分别是32、64、128、256字节的字符串,然后再顺序查询这100万条记录,分别统计写和读的时间,其中bdb版本为4.2。
当cache开到1G大小时,数据如下(其中大小单位为字节,时间单位为秒):

Cache为1G时
value大小(字节)Hash写时间Hash读时间BTree写时间BTree读时间
3212643
648443
1287453
2567463

由于cache非常大,几乎所有的数据都可以放在里面,所以两种索引方式的存取效率基本一样。
如果把cache改为32M,测试结果又如下:

Cache为32M时
value大小(字节)Hash写时间Hash读时间BTree写时间BTree读时间
32116539
64205621750
1284442
25618651

其中“缺”字表示测试无法进行,因为当数据太大时,用Hash索引运行bdb几乎把机器搞死,我不得不中断测试。这组测试看得出:如果使用BTree,随着数据的翻倍,写的时间也近乎翻倍,但读的时间非常稳定,对于数据量大、写少读多的应用(比如bbs、图片服务等)应该尽量使用BTree索引。
总的来看,BerkeleyDB的Hash索引表现古怪,只在小cache小数据量的情况下查询速度快于BTree,可现在哪会有这种情况?还是用BTree索引保险一些。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-363139-1-1.html 上篇帖子: php 汉字转拼音及获取拼音首字母 下篇帖子: [收藏]PHP给图片增加水印得类
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表