当前位置: 首页 > 工具软件 > Berkeley Yacc > 使用案例 >

Berkeley 相关

施冠玉
2023-12-01

安装Berkeley DB(4.5.20


因为:由于openldap需要用Berkeley DB来存放数据,所以要先安装所以需先安装Berkeley 数据库. 下载地址:http://www.oracle.com/technology/software/products/berkeley-db/htdocs/popup/db/4.5.20/db-targz.html

# tar zxvf db-4.5.20.tar.tar # cd db-4.5.20/build_unix/ # ../dist/configure # make # make install

# vi /etc/ld.so.conf 加入 /usr/local/BerkeleyDB.4.5/lib # /sbin/ldconfig

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/psprite/archive/2008/06/25/2585213.aspx
========================================================
Berkeley DB XML for PHP 方法总结

附:安装PHP的dbxml扩展(windows下的暂时没有找到)

  下载 wget http://download.oracle.com/berkeley-db/dbxml-2.2.13.tar.gz

  tar zxvf dbxml-2.2.13.tar.gz

  cd dbxml-2.2.13

  ./buildall.sh

  编译 Berkeley DB for php 模块

  cd dbxml-2.2.13/db-4.3.29/php_db4

  phpize

  ./configure –with-db4

  make

  make install

  编译 Berkeley DB XML for php 模块

  cd dbxml-2.2.13/dbxml/src/php

  phpize

  make

  make install

  修改php.ini

  extension=db4.so

  extension=dbxml.so

  重启 apache


========================================================

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

<?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

<?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();
?>

========================================================

现在可以编写一个简单的程序来测试一下了。

#include <stdio.h>
#include <stdlib.h>
#include <db.h>

#define DATABASE "test.db"

typedef struct _data_struct {
    int data_id;
    char data[20];
} data_struct;

int main()
{
    DBT key, data;
    DB *dbp;
    int ret;
    data_struct my_data;

    ret = db_create(&dbp, NULL, 0);  // create the DB handle
    if (ret != 0)
    {
        perror("create");
        return 1;
    }

    ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0);  // open the database
    if (ret != 0)
    {
        perror("open");
        return 1;
    }


    my_data.data_id = 1;
    strcpy(my_data.data, "some data");

    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));

    key.data = &(my_data.data_id);
    key.size = sizeof(my_data.data_id);
    data.data = &my_data;
    data.size = sizeof(my_data);

    ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);  // add the new data into the database
    if (ret != 0)
    {
        printf("Data ID exists/n");
    }

    dbp->close(dbp, 0);   // close the database

    return 0;
}
编译程序:
$ gcc -I /usr/local/BerkeleyDB.4.7/include/ -o testdb testdb.c  -L /usr/local/BerkeleyDB.4.7/lib/ -ldb
运行程序:
$ ./testdb

可以看到目录下生成了test.db文件,并对该文件进行了编码,简单用cat命令可以查看其内容(实际中当然是用dbp->get()函数了):
$ cat test.db
b1        ????    ????    some data?Bп??

可以看到"some data"已经被记录了.
========================================================
berkeley db果然是很快的

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://syre.blogbus.com/logs/12250299.html

据说 berkeley db 在存储key-value形式的数据时速度很快,就实验了一下,拿bdb和mysql myisam比较了一下。

测试代码:

$max = 100000;
ini_set('display_errors', 1);
error_reporting(E_ALL);

$conn = mysql_connect('localhost', 'test', '.....');
mysql_select_db('test', $conn);
mysql_query('truncate table test_kv', $conn);

$t = microtime(true);
for($i = 0; $i < $max; $i++){
    $key = 'key' . $i;
    mysql_query("insert into test_kv(`key`,`value`) values('$key','$i')", $conn);
}
echo microtime(true) - $t, "/n";

$t = microtime(true);
for($i = 0; $i < $max; $i++){
    $key = 'key' . $i;
    $rs = mysql_query("select `value` from test_kv where `key`='$key'");
    $row = mysql_fetch_row($rs);
}
echo microtime(true) - $t, "/n";


dl('dba.so');
//print_r(dba_handlers(i));
$db = dba_open('test.db', 'n', 'db4');

$t = microtime(true);
for($i = 0; $i < $max; $i++){
    $key = 'key' . $i;
    dba_insert($key, $i, $db);
}
echo microtime(true) - $t, "/n";

$t = microtime(true);
for($i = 0; $i < $max; $i++){
    $key = 'key' . $i;
    dba_fetch($key, $db);
}
echo microtime(true) - $t, "/n";

结果

12.7905659676
58.7637891769
1.14525485039
0.541149139404

前面两行是mysql的结果,后面两行是berkeley db 4的结果,分别是插入100000条和读取100000条记录消耗的时间。 如果是存取key-value的数据,又不需要在服务器间共享的话可以考虑使用berkeley db。


========================================================

 

 类似资料: