Home mail me! Syndicate this site using RSS

Howto Install Oracle Berkeley DB on Linux

Berkeley DB是由美国Sleepycat Software公司开发的一套开放源代码的嵌入式数据库管理系统(已被Oracle收购),它为应用程序提供可伸缩的、高性能的、有事务保护功能的数据管理服务。

Berkeley DB为许多编程语言提供了实用的api接口,包括c、c++、java、perl、tcl、python和php等。所有同数据库相关的操作都由Berkeley DB函数库负责统一完成。

官方地址为:http://www.oracle.com/technology/products/berkeley-db/db/index.html

本文就先讲一下如何在CentOS下安装Berkeley DB数据库(其他系统类似)。

1、安装Berkeley DB

# cd /usr/local/src
# wget http://download.oracle.com/berkeley-db/db-4.6.18.tar.gz
# tar -zxvf db-4.6.18.tar.gz
# cd db-4.6.18
# 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/目录。

至此,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
# ./configure --with-db4=/usr/local/berkeleydb/
# make
# make install

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

Enjoy it!

DbRunas - Howto Install Oracle Berkeley DB on Linux said,

August 22, 2007 @ 22:57:11

[...] Berkeley DB on Linux, and looks at howto compile and use the php_db4 extension.Articulo completo: http://www.sofee.cn/blog/2007/08/18/100/ [...]

Artur Bier said,

June 24, 2008 @ 21:09:02

Thank You for this post.

extension berkeley said,

June 29, 2008 @ 23:33:29

[...] howto install Oracle berkeley DB on Linux, and looks at howto compile and use the php_db4 extension.http://www.sofee.cn/blog/2007/08/18/100/UC BERKELEY EXTENSION – BERKELEY, CA – 1297500 in Defense …UC berkeley extension – BERKELEY, CA – [...]

4399 said,

October 13, 2008 @ 12:35:20

第一次接触berkeley db ,有问题过来再问

Gonzo said,

January 27, 2009 @ 23:54:53

Thank you very much for this!

PHP对Berkeley DB游标遍历的方法 | 屈伟 said,

February 4, 2009 @ 16:07:54

[...] PHP操作Berkeley DB有两种方法,一是通过DBA系列函数来实现,具体看php手册dba_open,二是直接通过使用Berkeley DB自带的PHP API。 安装Berkeley DB的PHP扩展的方法可以看这篇博客。 [...]

Alex said,

October 14, 2009 @ 22:10:19

Tnx! Can I build Apache2.2 + php5.** + BerkeleyDB4.8.** ???

Tweets that mention Howto Install Oracle Berkeley DB on Linux « Justin’s Tech Blog -- Topsy.com said,

April 28, 2010 @ 17:06:29

[...] This post was mentioned on Twitter by Tsendsuren. Tsendsuren said: http://www.sofee.cn/blog/2007/08/18/100/ berkeleyDB [...]

Suzuki Samurai Csf Radiator, Samurai Warriors 2 River Sanada said,

May 22, 2010 @ 17:17:16

[...] 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263Suzuki Samurai Csf Radiator, Samurai Warriors 2 River SanadaSuzuki Samurai Csf Radiator, Samurai Warriors 2 River Sanadasamurai code way of the warrior suzuki samurai diesel radiator page vitara parts midlands samurai suzuki suzuki samurai bulb radiator carb suzuki samurai detroit antique japanese samurai swords samurai hid kit samurai lighting turn signal samurai fashion 1988 suzuki samurai review samurai radiator fuel pump grand vitara samurai party fuel pump used suzuki samurai california austin healey last samurai movie download quality samurai alternative keyword elite postcode samurai warriors 2 suzuki samurai lockers ring and pinion samurai auto parts custom roll cage suzuki samurai offensive weapons samurai swords ancient samurai battle descargar samurai warriors 2 para pc sidekick part samurai grand suzuki forenza 7 samurai bandits toshiro mifune samurai them just samurai sword parts suzuki samurai shirts 7 samurai janus films samurai prints used classified 1987 suzuki samurai bushido code samurai book samurai 2 radiator big mutha truckers warriors orochi piezas suzuki samurai samurai sword sets samurai weapons full tang 1992 suzuki samurai pictures samurai jack posters 1990 samurai radiator grand vitara 1992 suzuki samurai parts radiator hose 4×4 suzuki samurai vendre asientos suzuki samurai caldes de montbui samurai used car samurai warriors 2 xtreme legends neoseeker 1992 1995 suzuki samurai radiator brake caliper 1994 suzuki samurai jl suzuki samurai firing order sierra 1988 suzuki samurai parts radiator hose samurai warriors 2 bulb saturn l100 suzuki samurai 2002 diesel japanese samurai warrior armor suzuki samurai 4wd conversion samurai warriors 2 manual samurai swords in india best samurai sword katana samurai sword pictures samurai warriors 2 full game wrangler parts jeep soft tops suzuki samurai cargo racks ehs samurai shark x 90 parts oem suzuki samurai lock box suzuki samurai parts samurai warriors 2 coolant samurai boys toys suzuki samurai modificat off road discount samurai warriors 2 empires suzuki samurai epa mileage ww2 samurai swords authentic samurai sword collection hard top suzuchi samurai samurai specifications samurai toys japan samurai hard tops suzuki samurai supercharger return of the six samurai deck samurai jack ps2 7 samurai films toshiro mifune seven samurai blu ray magnificent b boy samurai handmade samurai katana sword samurai episode fuel pump six samurai kamon samurai warriors 2 offer samurai for sale vancouver suzuki swift swords samurai armor vinyl version 7 samurai book of the six samurai inch high samurai samurai jack blog jj abrams suzuki samurai parts oregon japanese samurai battles toyotomi hideyoshi samurai champloo shirts 1998 sidekick egr valve 1988 suzuki samurai sale suzuki samurai rock crawler warn buy samurai swords information samurai swords last samurai sword honor seven samurai movie download ultimate sale samurai swords paul chen suzuki samurai sale suzuki samurai bulb clutch sae samurai warriors 2 hileleri E60 Amg Headlight | Chevrolet P25 Discount | Vigorelli Accessories Taillight | Nassau Substitute Manhasset | G1500 Promotion S15 | [...]

05 Toyota Prius Sale, Prius Shop Manual - 48.computeronlinebingo.com said,

May 25, 2010 @ 13:42:37

[...] 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979805 Toyota Prius Sale, Prius Shop Manual – 48.computeronlinebingo.com05 Toyota Prius Sale, Prius Shop Manualtoyota prius euro tail light prius tax information used 2005 toyota prius prius retail motor rent results toyota prius toyota prius rotors discount warranty sale used toyota prius safety recall toyota prius auto buying toyota prius headlight recall 2007 toyota prius used parts wagon prius nouvelle generation g0c0g prius tickets type battery used prius prius part free shipping all weather floor mats weathertech cargo liners discount toyota prius solar roof toyota prius accessories free shipping all weather floor mats prius card list prius used car sales toyota venza civic hybrid discount prius prius thesaurus sale 2007 toyota prius touring camry solara radiator schedule toyota prius ac condenser charge prius 12v battery prius had free shipping oem parts prius air conditioner filter prius malfunction indicator lamp prius brake energy find 2010 prius future prius hybrid radiator 09 toyota prius replacement latest 2010 toyota prius 2007 toyota prius used manual prius locations prius discount solar roof 5dr prius factory delayed toyota prius comcast tube toyota prius 2009 toyota prius honda insight listen toyota prius used features prius hybrid fuel used toyota prius atlanta part brand toyota prius fit used prius hybrid orlando trucks 2008 prius warranty radiator auto toyota prius parts fan 2004 toyota prius sale toyota prius used mpg tax credit solar roof 2010 toyota prius discount 2010 prius honda insight new toyota prius cost fuel efficiency 2007 parts replacement mirror 2006 toyota prius fog light toyota prius wiring harness prius hid headlight problem hybrid cars prius error used prius new orleans toyota camry prius toyota prius headlight both hybrid 2007 prius headlight failure 100 mpg prius price sale used toyota prius mileage 5dr toyota prius camry hybrid prius aftermarket hid new car cost used toyota prius runaway latest 2008 prius headlight bulb 2008 toyota prius used prius cincinnati beechmont toyota headlight my prius also had toyota 2007 toyota prius used parts headlight 2010 prius key battery 200 headlight prius touring hybrid price toyota prius 2010 prius radiator vehicle oil cooler provide toyota prius parts 2007 sticker price toyota prius prius headlight remove passenger side prius part goth toyota prius 2006 road test motor prius part 2006 prius battery warranty hybrid cars honda civic prius prius chat accessories prius bluetooth hack prius walmart prius radiator wiper motor power 2009 toyota prius 2010 engine specs toyota prius hybrid taillight into toyota prius gas mileage prius increase headlight toyota prius sale 2010 toyota prius future toyota vehicles third generation prius aftermarket stereo prius jbl toyota prius change used seat covers 2010 prius honda prius gas mileage ford fusion hybrid prius package prices headlight my prius too had prius performance interior accessories windshield reflector toyota prius car pictures 2005 4runner discount toyota prius Nickent Golf Nightclubs | Tucson Aftermarket Halo | 2009 Xc70 Mpg | Buick Sportwagon Discount | Echo Radiator Transmission | [...]

RSS feed for comments on this post · TrackBack URI

Leave a Comment

*
To prove that you're not a bot, enter this code
Anti-Spam Image