MongoDB以及C++驱动在Ubuntu上的安装

最近在学习数据库MongoDB,将安装过程记录下来,方便以后查看。
安装环境:主机是win10系统,在VM虚拟机上安装了Ubuntu16.04LTS系统,通过Xshell软件连接Ubuntu进行远程操作

1.在Ubuntu16.04LTS上安装MongoDB 4.2 Community Edition

1.1 导入包管理系统使用的公钥

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
lzj@lzj-virtual-machine:/etc/apt$ wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
[sudo] password for lzj:
OK

1.2 为MongoDB创建一个列表文件
Ubuntu 16.04(Xenial)创建/etc/apt/sources.list.d/mongodb-org-4.2.list文件

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

以上指令是从mongodb的官网下载,会导致后面下载速度很慢,这时需要设置国内的镜像源,阿里云镜像地址:http://mirrors.aliyun.com/。设置阿里云镜像源的教程可以参考该博客
此时为MongoDB创建的列表文件也需要修改为阿里云镜像源,设置如下:

echo "deb [ arch=amd64 ] http://mirrors.aliyun.com/mongodb/apt/ubuntu xenial/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

重新更新本地的包。

sudo apt-get update

1.3 安装MongoDB软件包
安装最新的稳定版

sudo apt-get install -y mongodb-org

1.4 运行MongoDB

  1. 启动MongoDB,sudo service mongod start
  2. 确定MongoDB启动成功,sudo service mongod status
  3. 停止MongoDB,sudo service mongod stop
  4. 重启MongoDB,sudo service mongod restart
  5. 使用MongoDB,mongo
    运行结果:
lzj@lzj-virtual-machine:~/Desktop/mongodb$ mongo
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("97664d6d-9363-4b6e-bf07-7b3528ddf622") }
MongoDB server version: 4.2.3
Server has startup warnings: 
2020-02-03T11:01:45.665+0800 I  STORAGE  [initandlisten] 
2020-02-03T11:01:45.665+0800 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-02-03T11:01:45.665+0800 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-02-03T11:01:46.253+0800 I  CONTROL  [initandlisten] 
2020-02-03T11:01:46.253+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-02-03T11:01:46.253+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-02-03T11:01:46.253+0800 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 

到这里,MongoDB就已经安装完成了,就可以开始学习MongoDB的增删改查的基本操作了,但是如果要使用C++代码对MongoDB数据库进行操作,还需要安装C++驱动。

2.MongoDB的C++驱动安装

step 1: 安装相关的依赖

sudo apt-get update
sudo apt-get install -y cmake gcc git libsasl2-dev libssl-dev libsnappy-dev make pkg-config tar wget

step 2: 安装MongoDB C驱动
Installing the MongoDB C Driver官方文档

  • 使用软件包管理器安装libmongoc
sudo apt-get install libmongoc-1.0-0
  • 使用软件包管理器安装libbson
sudo apt-get install libbson-1.0
  • 在Ubuntu上构建C驱动
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.16.0/mongo-c-driver-1.16.0.tar.gz
tar xzf mongo-c-driver-1.16.0.tar.gz
cd mongo-c-driver-1.16.0/
mkdir cmake-build
cd cmake-build
sudo apt install cmake
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..

最后一行的输出

-- Build files have been written to: /home/lzj/Downloads/mongo-c-driver-1.16.0/cmake-build
  • 安装MongoDB的C驱动
sudo make install

step 3: 生成并安装MongoDB CXX驱动程序

cd $SOURCE_ROOT
git clone https://github.com/mongodb/mongo-cxx-driver.git
cd mongo-cxx-driver
git checkout r3.4.0
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
  • 使用sudo安装polyfill
sudo make EP_mnmlstc_core
  • 生成并安装驱动程序
make
sudo make install

step 4: 测试C++驱动是否安装成功

  • 新建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("mongodb://localhost:27017")};

    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;
    }
}
  • 设置环境变量
export LD_LIBRARY_PATH=/usr/local/lib
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
  • 编译运行test.cpp程序
c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
./test

运行结果:

lzj@lzj-virtual-machine:~/Desktop/mongodb$ ./test
{ "_id" : { "$oid" : "5e37c40518bc083a6a12d692" }, "hello" : "world" }

3.参考文档

[1] Install MongoDB Community Edition on Ubuntu官方安装文档
[2] Installing the mongocxx driver驱动官方安装文档
[3] Building CXX MongoDB Driver驱动github安装教程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章