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安裝教程

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