您的位置:首页 > 数据库 > Mongodb

mongodb学习笔记

2011-09-12 00:28 441 查看
0. 官方网站:
http://www.mongodb.org/
1. 入门学习文档:

The Little MongoDB Book.pdf

MongoDB权威指南.pdf

2. 安装mongodb:

2.1 ubuntu可以直接使用apt-get安装;

sudo apt-get install mongodb

在安装mongodb的时候,系统会安装mongodb的很多依赖包,安装运行mongodb,会提示:

mongod: error while loading shared libraries: libmozjs.so: cannot open shared object file: No such file or directory

解决方法是:

sudo apt-get install xulrunner-dev

查找libmozjs.so文件

find /usr/lib | grep libmozjs

可以看到

 /usr/lib/xulrunner-1.9.2.15/libmozjs.so

 /usr/lib/libmozjs.so

 /usr/lib/xulrunner-devel-1.9.2.15/sdk/lib/libmozjs.so

给库做一个链接:

sudo ln -s /usr/lib/xurunner-1.9.2.15/libmozjs.so /usr/lib

重启mongodb的服务:

sudo service mongodb start

查看进程,确认下mongodb进程:

ps aux | grep mongodb

mongodb 1027 0.0 0.1 79576 3720  Ssl Mar23 0:00 /usr/lib/mongodb/mongod --config /etc/mongodb.conf

root 2066 0.0 0.0 1864 740 pts/0 S+ 10:23 0:00 grep --color=auto mongodb

这表示mongodb服务已经启动,现在可以链接mongodb:

/usr/lib/mongodb/mongo

MongoDB shell version: 1.2.2

 url: test

 connecting to: test

 type "exit" to exit

 type "help" for help

 >

这就表示已经成功安装和启动了。

2.2 也可参考官方网站:
http://www.mongodb.org/display/DOCS/Quickstart+Unix
压缩包的安装和启动方法

首先下载linux包:http://www.mongodb.org/downloads,下载linux 32-bit;

上传到服务器:/usr/lib

解压:tar xzfmongodb-linux-i686-1.8.0.tar 重命名下,改为mongodb(我这里是为了好看起见)

现在可以测试下:

启动mongodb服务:/usr/lib/mongodb/bin/mongod (注意默认情况下mongodb的数据文件是指定到/data/db中的,所以在启动前,新建此文件夹,也可以自己定义数据库文件路径,启动方式是:/usr/lib/mongodb/bin/mongod --dbpath 你所建的数据库文件路径)

链接数据库:/usr/lib/mongodb/bin/mongo 

要想在服务器启动时自动启动mongodb,解决方法是:

在/etc/rc.local文件中加入/usr/lib/mongodb/bin/mongod --dbpath /data/db,

reboot,直接连接数据库,你会发现不用启动mongodb服务就可以连接了,这说明上面自动启动mongodb服务添加成功!

程序目录:

/usr/lib/mongodb

配置文件:

/etc/mongodb.conf

sudo service mongodb start

sudo service mongodb stop

3. C++连接数据库:

下载全量的代码包,用root用户编译安装。
http://www.mongodb.org/pages/viewpage.action?pageId=133415
Installing the Driver Library and Headers

A good source for general information about setting up a MongoDB development environment on various operating systems is the building page.

The normal database distribution used to include the C++ driver, but there were many problems with library version mismatches so now you have to build from source. You can either get the full source code for the database and just build the C++ driver or download
the driver separately and build it.

sudo apt-get install scons

scons - a software construction tool

The  scons  utility builds software (or other files) by determining which component pieces must be rebuilt and executing the necessary commands to rebuild them.

By default, scons searches for a file named SConstruct, Sconstruct, or sconstruct (in that order)  in  the  current  directory  and reads its configuration from the first file found.  An alternate file name may be specified via the -f option.

Compiling

The C++ drivers requires the pcre and boost libraries (with headers) to compile.? Be sure they are in your include and lib paths. You can usually install them from your OS's package manager if you don't already have them.

需要安装boost库:

sudo apt-get install libboost*
http://www.boost.org/
Welcome to Boost.org!

Boost provides free peer-reviewed portable C++ source libraries.

We emphasize libraries that work well with the C++ Standard Library. Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications. The Boost license encourages both commercial and non-commercial use.

下载C++驱动后安装、编译:
http://www.mongodb.org/pages/viewpage.action?pageId=133409
cd mongo-cxx-driver-nightly/

scons

报错问题解决:

g++ -o client/examples/authTest.o -c -O3 -D_SCONS -DMONGO_EXPOSE_MACROS -Imongo client/examples/authTest.cpp

In file included from mongo/client/dbclient.h:23:0,

                 from client/examples/authTest.cpp:20:

mongo/client/../pch.h:80:33: fatal error: boost/thread/once.hpp: No such file or directory

compilation terminated.

scons: *** [client/examples/authTest.o] Error 1

scons: building terminated because of errors.

安装libboost,之后:

g++ -o mongo/client/dbclient.o -c -O3 -D_SCONS -DMONGO_EXPOSE_MACROS -Imongo mongo/client/dbclient.cpp

In file included from mongo/client/../db/cursor.h:23:0,

                 from mongo/client/../db/pdfile.h:411,

                 from mongo/client/dbclient.cpp:19:

mongo/client/../db/matcher.h:24:21: fatal error: pcrecpp.h: No such file or directory

compilation terminated.

scons: *** [mongo/client/dbclient.o] Error 1

scons: building terminated because of errors.

PCRE - Perl-compatible regular expressions

解决办法:
http://groups.google.com/group/mongodb-user/browse_thread/thread/824c7c8c5c05f3c1
apt-get -y install tcsh git-core scons g++

apt-get -y install libpcre++-dev libboost-dev libreadline-dev xulrunner-1.9.2-dev

apt-get -y install libboost-program-options-dev libboost-thread-dev libboost-filesystem-dev libboost-date-time-dev
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息