最近做的项目需要使用MongoDB,现学一下。
电脑现有配置:
Ubuntu 16.04
gcc 5.4
cmake 3.5
boost 1.58
安装MongoDB
1. Import the public key used by the package management system.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
2.Create a list file for MongoDB.
Create the /etc/apt/sources.list.d/mongodb-org-3.2.list list file using the command appropriate for your version of Ubuntu:
Ubuntu 16.04
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
3. sudo apt-get update
4. 安装mongodb 3.2.16
sudo apt-get install -y mongodb-org=3.2.16 mongodb-org-server=3.2.16 mongodb-org-shell=3.2.16 mongodb-org-mongos=3.2.16 mongodb-org-tools=3.2.16
如果要安装最新版
sudo apt-get install -y mongodb-org
5.验证安装成功
sudo service mongod start
然后检查/var/log/mongodb/mongod.log文件
有形如这样的一行:
[initandlisten] waiting for connections on port <port>
即为安装成功
一般是27017
6.如果要卸载
三步:
sudo service mongod stop
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
安装MongoDB C++ Driver
官方链接 Installing the MongoDB C++ Driver
参考其中的Building on UNIX中的Ubuntu部分
1.安装MongoDB C Driver
官方链接
我安装MongoDB C Driver的新版本1.8.0
使用命令如下:
首先需要OpenSSL
sudo apt-get install pkg-config libssl-dev libsasl2-dev
然后安装
$ wget https://github.com/mongodb/mongo-c-driver/releases/download/1.8.0/mongo-c-driver-1.8.0.tar.gz
$ tar xzf mongo-c-driver-1.8.0.tar.gz
$ cd mongo-c-driver-1.8.0
$ ./configure --disable-automatic-init-and-cleanup
如果configure的部分执行成功,可以在终端的最后看到:
libmongoc 1.8.0 was configured with the following options:
Build configuration:
Enable debugging (slow) : no
Compile with debug symbols (slow) : no
Enable GCC build optimization : yes
Enable automatic init and cleanup : no
Enable maintainer flags : no
Code coverage support : no
Cross Compiling : no
Fast counters : no
Shared memory performance counters : yes
SASL : sasl2
SSL : openssl
Snappy Compression : bundled
Zlib Compression : bundled
Libbson : bundled
Documentation:
man : no
HTML : no
教训:好像不小心把libbson删掉了,导致Libbson显示的状态是system,又自己琢磨着安了一个libbson
libbson安装 链接
最后,make,install
$ make
$ sudo make install
2. 下载mongocxx driver的最新版本
git clone https://github.com/mongodb/mongo-cxx-driver.git \
--branch releases/stable --depth 1
cd mongo-cxx-driver/build
3.配置你的driver然后安装
第一次安装,先使用默认的路径/usr/local
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
然后
# Only for MNMLSTC polyfill
sudo make EP_mnmlstc_core
make
sudo make install
4.测试安装
test.cpp
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
4.1 使用pkg-config编译
c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
4.2 不使用pkg-config和cmake编译
c++ --std=c++11 test.cpp -o test \
-I/usr/local/include/mongocxx/v_noabi \
-I/usr/local/include/bsoncxx/v_noabi \
-L/usr/local/lib -lmongocxx -lbsoncxx
4.3 使用IDE KDevelop进行测试
因为要和自己的项目有链接,还是使用kdevelop测试一下
从模板建立一个工程,使用CMake
CMakeLists.txt如下:
cmake_minimum_required(VERSION 2.6)
project(mongodbtest)
include_directories(/usr/local/include/bsoncxx/v_noabi /usr/local/include/mongocxx/v_noabi)
add_executable(mongodbtest main.cpp)
install(TARGETS mongodbtest RUNTIME DESTINATION bin)
target_link_libraries(mongodbtest bsoncxx mongocxx snappy)
main.cpp的内容就是test.cpp
注意链接库的部分,libbson.so和libmongo.so……并不是需要的库
我使用的默认安装路径/usr/local
安装在别的地方的需要相应地替换路径。
暂时感觉安装没有问题~不过还不是很懂,新手第一次装,慢慢学吧。