ubuntu 下按照官网教程安装 mongodb和c++ driver
2015-06-09 21:43
561 查看
ubuntu 下按照官网教程安装 mongodb和c++ driver
官网教程地址:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
1.安装mongodb(版本3.0.3):
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
1.1.启动mongodb:
sudo service mongod start
可在
/var/log/mongodb/mongod.log 中查看是否启动成功,若其中一行是:
“[initandlisten] waiting for connections on port <port> (<port>,默认为27017)”,则表明mongodb
开启成功
2.安装c++ driver
本文是按照“Legacy Driver Documentation”教程来安装c++ driver的,地址是
https://github.com/mongodb/mongo-cxx-driver/wiki/Download-and-Compile-the-Legacy-Driver
安装c++ driver前需要先安装boost(>=1.49)、python(2.x)、scons、git。
python(2.x)、scons、git都比较好安装,Ubuntu自带python,下面只讲一下boost的安装。
注意:安装编译boost前,得先安装gcc,使用命令:sudo apt-get install g++即可安装。
2.1.安装编译boost:
tar -zxvf boost_1_58_0.tar.gz
cd boost_1_58_0
编译前需要配置,输入下面的命令:
sudo ./bootstrap.sh
然后会提示你使用b2(老版本是使用bjam编译)编译,这里选择完全安装,自定义安装以后再研究,输入命令:
sudo ./b2 install
编译安装完成后,会把boost头文件拷贝到/usr/local/include/目录下,库文件在/usr/local/lib/下
2.2.安装编译c++ driver
获取源代码:
git clone https://github.com/mongodb/mongo-cxx-driver.git
选择分支:
cd mongo-cxx-driver
git checkout legacy
编译driver(SConstruct在mongo-cxx-driver文件夹里,需在mongo-cxx-driver目录下执行,否则提示没有SConstruct):
将driver安装在$HOME/mongo-client-install(也可以在/opt/local, /usr/local):
y@ubuntu:~/mongo-cxx-driver$ scons --prefix=$HOME/mongo-client-install install
启用ssl:
y@ubuntu:~/mongo-cxx-driver$ scons --prefix=$HOME/mongo-client-install --ssl install(没有成功,提示找不到boost,没在意,略过了)
启用sasl:
y@ubuntu:~/mongo-cxx-driver$ scons --prefix=$HOME/mongo-client-install --use-sasl-client install(没有成功,提示需要安装ssl,没在意,略过了)
建立driver的一个共享库版本:
scons --prefix=$HOME/mongo-client-install --sharedclient install
建立库的调试版本:
y@ubuntu:~/mongo-cxx-driver$ scons --prefix=$HOME/mongo-client-install --dbg=on --opt=on install
到此算是安装结束了,教程中的一些不合适的内容或命令就没跟着做,下面的教程“Configuring the Legacy Driver”只是看了一下,然后看的是教程“Tutorial”的connecting部分。
3.连接测试:
建立文件:
touch tutorial.cpp
然后将教程中的代码复制到tutorial.cpp中。
我在linux中用的是gcc,编译tutorial.cpp的命令如下(用I和L指出mongo及boost的头文件及库的位置,I用于指定头文件,L用于指定库):
y@ubuntu:~$ g++ -I/home/y/mongo-client-install/include -L/home/y/mongo-client-install/lib -I/usr/local/include -L/usr/local/lib -pthread -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options -lboost_system -lboost_regex tutorial.cpp -o tutorial
运行后出现提示:
/tmp/ccnElcEW.o: In function `main':
tutorial.cpp:(.text+0xe2): undefined reference to `mongo::client::Options::Options()'
tutorial.cpp:(.text+0xf8): undefined reference to `mongo::client::initialize(mongo::client::Options const&)'
/tmp/ccnElcEW.o: In function `__static_initialization_and_destruction_0(int, int)':
tutorial.cpp:(.text+0x223): undefined reference to `boost::system::generic_category()'
tutorial.cpp:(.text+0x22f): undefined reference to `boost::system::generic_category()'
tutorial.cpp:(.text+0x23b): undefined reference to `boost::system::system_category()'
/tmp/ccnElcEW.o:(.gcc_except_table+0x1b8): undefined reference to `typeinfo for mongo::DBException'
/tmp/ccnElcEW.o: In function `mongo::DBException::DBException(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
tutorial.cpp:(.text._ZN5mongo11DBExceptionC2ERKSsi[_ZN5mongo11DBExceptionC5ERKSsi]+0x27): undefined reference to `vtable for mongo::DBException'
/tmp/ccnElcEW.o: In function `mongo::DBException::~DBException()':
tutorial.cpp:(.text._ZN5mongo11DBExceptionD2Ev[_ZN5mongo11DBExceptionD5Ev]+0x16): undefined reference to `vtable for mongo::DBException'
/tmp/ccnElcEW.o: In function `mongo::DBException::addContext(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tutorial.cpp:(.text._ZN5mongo11DBException10addContextERKSs[mongo::DBException::addContext(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x24): undefined reference to `mongo::causedBy(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)'
/tmp/ccnElcEW.o: In function `mongo::UserException::UserException(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tutorial.cpp:(.text._ZN5mongo13UserExceptionC2EiRKSs[_ZN5mongo13UserExceptionC5EiRKSs]+0x30): undefined reference to `vtable for mongo::UserException'
/tmp/ccnElcEW.o: In function `mongo::UserException::~UserException()':
tutorial.cpp:(.text._ZN5mongo13UserExceptionD2Ev[_ZN5mongo13UserExceptionD5Ev]+0x13): undefined reference to `vtable for mongo::UserException'
/tmp/ccnElcEW.o: In function `mongo::DBClientConnection::DBClientConnection(bool, mongo::DBClientReplicaSet*, double)':
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x23): undefined reference to `mongo::DBClientBase::DBClientBase()'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x2e): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x3a): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0xbe): undefined reference to `mongo::HostAndPort::HostAndPort()'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x10d): undefined reference to `mongo::DBClientConnection::_numConnections'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x1a6): undefined reference to `mongo::DBClientBase::~DBClientBase()'
/tmp/ccnElcEW.o: In function `mongo::DBClientConnection::~DBClientConnection()':
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x14): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x20): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x2a): undefined reference to `mongo::DBClientConnection::_numConnections'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0xa7): undefined reference to `mongo::DBClientBase::~DBClientBase()'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x161): undefined reference to `mongo::DBClientBase::~DBClientBase()'
/tmp/ccnElcEW.o: In function `mongo::DBClientConnection::connect(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tutorial.cpp:(.text._ZN5mongo18DBClientConnection7connectERKSs[mongo::DBClientConnection::connect(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x51): undefined reference to `mongo::HostAndPort::HostAndPort(mongo::StringData
const&)'
/tmp/ccnElcEW.o:(.rodata._ZTVN5mongo16ConnectExceptionE[vtable for mongo::ConnectException]+0x30): undefined reference to `mongo::UserException::appendPrefix(std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&) const'
......
collect2: ld returned 1 exit status
网上查了许多资料,百思不得其解,最后改了一个心中认为出错的地方(改变一些命令或参数的位置),命令如下:
y@ubuntu:~$ g++ tutorial.cpp -pthread -I/home/y/mongo-client-install/include -L/home/y/mongo-client-install/lib -I/usr/local/include -L/usr/local/lib -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options -lboost_system -lboost_regex -o tutorial
结果没有报错,tutorial.cpp编译成功。
接着执行命令:
y@ubuntu:~$ ./tutorial
出现错误提示:
./tutorial: error while loading shared libraries: libmongoclient.so: cannot open shared object file: No such file or directory
网上查找资料,原因是共享库文件libmongoclient.so安装位置有问题,自己电脑上的libmongoclient.so在/home/y/mongo-client-install/lib目录下。把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 命令如下:
# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
# echo "/home/y/mongo-client-install/lib" >> /etc/ld.so.conf
# ldconfig
y@ubuntu:~$ ./tutorial
出现connected ok
证明连接成功!
注:本人只是一个初学者,能力有限,所写文章水平也不高,有什么问题或建议还望不吝赐教。
官网教程地址:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
1.安装mongodb(版本3.0.3):
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
1.1.启动mongodb:
sudo service mongod start
可在
/var/log/mongodb/mongod.log 中查看是否启动成功,若其中一行是:
“[initandlisten] waiting for connections on port <port> (<port>,默认为27017)”,则表明mongodb
开启成功
2.安装c++ driver
本文是按照“Legacy Driver Documentation”教程来安装c++ driver的,地址是
https://github.com/mongodb/mongo-cxx-driver/wiki/Download-and-Compile-the-Legacy-Driver
安装c++ driver前需要先安装boost(>=1.49)、python(2.x)、scons、git。
python(2.x)、scons、git都比较好安装,Ubuntu自带python,下面只讲一下boost的安装。
注意:安装编译boost前,得先安装gcc,使用命令:sudo apt-get install g++即可安装。
2.1.安装编译boost:
tar -zxvf boost_1_58_0.tar.gz
cd boost_1_58_0
编译前需要配置,输入下面的命令:
sudo ./bootstrap.sh
然后会提示你使用b2(老版本是使用bjam编译)编译,这里选择完全安装,自定义安装以后再研究,输入命令:
sudo ./b2 install
编译安装完成后,会把boost头文件拷贝到/usr/local/include/目录下,库文件在/usr/local/lib/下
2.2.安装编译c++ driver
获取源代码:
git clone https://github.com/mongodb/mongo-cxx-driver.git
选择分支:
cd mongo-cxx-driver
git checkout legacy
编译driver(SConstruct在mongo-cxx-driver文件夹里,需在mongo-cxx-driver目录下执行,否则提示没有SConstruct):
将driver安装在$HOME/mongo-client-install(也可以在/opt/local, /usr/local):
y@ubuntu:~/mongo-cxx-driver$ scons --prefix=$HOME/mongo-client-install install
启用ssl:
y@ubuntu:~/mongo-cxx-driver$ scons --prefix=$HOME/mongo-client-install --ssl install(没有成功,提示找不到boost,没在意,略过了)
启用sasl:
y@ubuntu:~/mongo-cxx-driver$ scons --prefix=$HOME/mongo-client-install --use-sasl-client install(没有成功,提示需要安装ssl,没在意,略过了)
建立driver的一个共享库版本:
scons --prefix=$HOME/mongo-client-install --sharedclient install
建立库的调试版本:
y@ubuntu:~/mongo-cxx-driver$ scons --prefix=$HOME/mongo-client-install --dbg=on --opt=on install
到此算是安装结束了,教程中的一些不合适的内容或命令就没跟着做,下面的教程“Configuring the Legacy Driver”只是看了一下,然后看的是教程“Tutorial”的connecting部分。
3.连接测试:
建立文件:
touch tutorial.cpp
然后将教程中的代码复制到tutorial.cpp中。
我在linux中用的是gcc,编译tutorial.cpp的命令如下(用I和L指出mongo及boost的头文件及库的位置,I用于指定头文件,L用于指定库):
y@ubuntu:~$ g++ -I/home/y/mongo-client-install/include -L/home/y/mongo-client-install/lib -I/usr/local/include -L/usr/local/lib -pthread -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options -lboost_system -lboost_regex tutorial.cpp -o tutorial
运行后出现提示:
/tmp/ccnElcEW.o: In function `main':
tutorial.cpp:(.text+0xe2): undefined reference to `mongo::client::Options::Options()'
tutorial.cpp:(.text+0xf8): undefined reference to `mongo::client::initialize(mongo::client::Options const&)'
/tmp/ccnElcEW.o: In function `__static_initialization_and_destruction_0(int, int)':
tutorial.cpp:(.text+0x223): undefined reference to `boost::system::generic_category()'
tutorial.cpp:(.text+0x22f): undefined reference to `boost::system::generic_category()'
tutorial.cpp:(.text+0x23b): undefined reference to `boost::system::system_category()'
/tmp/ccnElcEW.o:(.gcc_except_table+0x1b8): undefined reference to `typeinfo for mongo::DBException'
/tmp/ccnElcEW.o: In function `mongo::DBException::DBException(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
tutorial.cpp:(.text._ZN5mongo11DBExceptionC2ERKSsi[_ZN5mongo11DBExceptionC5ERKSsi]+0x27): undefined reference to `vtable for mongo::DBException'
/tmp/ccnElcEW.o: In function `mongo::DBException::~DBException()':
tutorial.cpp:(.text._ZN5mongo11DBExceptionD2Ev[_ZN5mongo11DBExceptionD5Ev]+0x16): undefined reference to `vtable for mongo::DBException'
/tmp/ccnElcEW.o: In function `mongo::DBException::addContext(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tutorial.cpp:(.text._ZN5mongo11DBException10addContextERKSs[mongo::DBException::addContext(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x24): undefined reference to `mongo::causedBy(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)'
/tmp/ccnElcEW.o: In function `mongo::UserException::UserException(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tutorial.cpp:(.text._ZN5mongo13UserExceptionC2EiRKSs[_ZN5mongo13UserExceptionC5EiRKSs]+0x30): undefined reference to `vtable for mongo::UserException'
/tmp/ccnElcEW.o: In function `mongo::UserException::~UserException()':
tutorial.cpp:(.text._ZN5mongo13UserExceptionD2Ev[_ZN5mongo13UserExceptionD5Ev]+0x13): undefined reference to `vtable for mongo::UserException'
/tmp/ccnElcEW.o: In function `mongo::DBClientConnection::DBClientConnection(bool, mongo::DBClientReplicaSet*, double)':
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x23): undefined reference to `mongo::DBClientBase::DBClientBase()'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x2e): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x3a): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0xbe): undefined reference to `mongo::HostAndPort::HostAndPort()'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x10d): undefined reference to `mongo::DBClientConnection::_numConnections'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionC2EbPNS_18DBClientReplicaSetEd[_ZN5mongo18DBClientConnectionC5EbPNS_18DBClientReplicaSetEd]+0x1a6): undefined reference to `mongo::DBClientBase::~DBClientBase()'
/tmp/ccnElcEW.o: In function `mongo::DBClientConnection::~DBClientConnection()':
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x14): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x20): undefined reference to `vtable for mongo::DBClientConnection'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x2a): undefined reference to `mongo::DBClientConnection::_numConnections'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0xa7): undefined reference to `mongo::DBClientBase::~DBClientBase()'
tutorial.cpp:(.text._ZN5mongo18DBClientConnectionD2Ev[_ZN5mongo18DBClientConnectionD5Ev]+0x161): undefined reference to `mongo::DBClientBase::~DBClientBase()'
/tmp/ccnElcEW.o: In function `mongo::DBClientConnection::connect(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tutorial.cpp:(.text._ZN5mongo18DBClientConnection7connectERKSs[mongo::DBClientConnection::connect(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x51): undefined reference to `mongo::HostAndPort::HostAndPort(mongo::StringData
const&)'
/tmp/ccnElcEW.o:(.rodata._ZTVN5mongo16ConnectExceptionE[vtable for mongo::ConnectException]+0x30): undefined reference to `mongo::UserException::appendPrefix(std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&) const'
......
collect2: ld returned 1 exit status
网上查了许多资料,百思不得其解,最后改了一个心中认为出错的地方(改变一些命令或参数的位置),命令如下:
y@ubuntu:~$ g++ tutorial.cpp -pthread -I/home/y/mongo-client-install/include -L/home/y/mongo-client-install/lib -I/usr/local/include -L/usr/local/lib -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options -lboost_system -lboost_regex -o tutorial
结果没有报错,tutorial.cpp编译成功。
接着执行命令:
y@ubuntu:~$ ./tutorial
出现错误提示:
./tutorial: error while loading shared libraries: libmongoclient.so: cannot open shared object file: No such file or directory
网上查找资料,原因是共享库文件libmongoclient.so安装位置有问题,自己电脑上的libmongoclient.so在/home/y/mongo-client-install/lib目录下。把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 命令如下:
# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
# echo "/home/y/mongo-client-install/lib" >> /etc/ld.so.conf
# ldconfig
y@ubuntu:~$ ./tutorial
出现connected ok
证明连接成功!
注:本人只是一个初学者,能力有限,所写文章水平也不高,有什么问题或建议还望不吝赐教。
相关文章推荐
- mongodb笔记之update
- 【MongoDB for Java】Java操作MongoDB
- mongoDB 入门指南、示例
- mongoDB 介绍(特点、优点、原理)
- 关于mongoDb
- 安装mongodb记录,mongo基本命令记录
- MongoDB常用命令
- 用mongoose实现mongodb增删改查
- 方法说---mac下安装homebrew与mongodb
- mongodb执行js脚本(一)---shell执行
- mongodb执行js脚本(一)---shell执行
- MongoDB JAVA API高级应用
- mongodb 执行js脚本(二)---mongovue执行
- mongodb 执行js脚本(二)---mongovue执行
- MongoDB开发学习(1)开天辟地,经典入门
- mongodb安装与使用
- Windows安装MongoDB全过程
- MongoDB 多条件组合查询
- Java实现对MongoDB的AND、OR和IN操作
- mongodb 数据库操作--备份 还原 导出 导入