相对于传统的结构化数据库而言,MongoDB 是一个基于分布式文件存储的数据库。
MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。
在高负载的情况下,添加更多的节点,可以保证服务器性能。
MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。
MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。
但针对于C/C++的开发人员而言其安装和调用有点复杂,主要是编译环境不同导致的。
下面根据安装体会写一下安装步骤,希望给己用,方便别人用。(之前安装时卡壳了,询问了好久都没有人理我,最后在慢慢摸索下终于安装成功了)
系统:centos7.6
软件:mongo-c-driver-1.17.2
mongo-cxx-driver-r3.6.2
python 2.7.5/python3.6.6
(官网说For mongocxx-3.6.x, libmongoc 1.17.0 or later is required)
1、yum install -y git gcc gcc-c++ openssl openssl-devel
先安装配套的软件,主要是安装git软件。
2、查看cmake版本,官网上有介绍,需要安装3.2及以上的版本。我安装的是cmake-3.21.2版本。具体如下:
yum remove cmake
wget https://www.cmake.org/files/v3.21/cmake-3.21.2.tar.gz
tar -xvf cmake-3.21.2.tar.gz
cd cmake-3.21.2
./bootstrap
./configure
make && make install
ln -s /usr/local/bin/cmake /usr/bin
cd ..
3、安装mongo-c-driver-1.17.2。我安装的是1.17.2版本不算最新的但是可以用,因为官网说For mongocxx-3.6.x, libmongoc 1.17.0 or later is required.
具体如下:(可以将sudo安装一下)
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.17.2/mongo-c-driver-1.17.2.tar.gz
tar -zxvf mongo-c-driver-1.17.2.tar.gz
cd mongo-c-driver-1.17.2
mkdir cmake-build
cd cmake-build
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..
make
sudo make install
此处的python的版本可以是2.7.5(centos7.6自带)
4、升级python的版本到3.0以上,因为后续安装CXX的时候需要高版本。
具体安装如下:
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel(如果已安装可以不更新)
mkdir /usr/local/python3 # 安装目录
mkdir /opt/python3 # 编译目录
tar xvJf Python-3.6.6.tar.xz -C /opt/python3
/opt/python3/configure --prefix=/usr/local/python3
备份
mv /usr/bin/python /usr/bin/python2.7.bak
如果不想删除老版本可以进行备份,再启用是反向修改即可
修改软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
配置YUM
[root@centos7 bin]# vim /usr/bin/yum
#!/usr/bin/python2.7
import sys
try:
import yum
同时修改/usr/libexec/urlgrabber-ext-down中的配置。
[root@centos7 bin]# vim /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python2.7
# A very simple external downloader
# Copyright 2011-2012 Zdenek Pavlas
不然的话yum会报错。
5、安装mongo-cxx-driver-r3.6.2
wget https://github.com/mongodb/mongo-cxx-driver/archive/r3.6.2.tar.gz
tar -xvf r3.6.2.tar.gz
cd mongo-cxx-driver-r3.6.2/build/
cmake -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_MNMLSTC=1 -DCMAKE_INSTALL_PREFIX=/usr/local ..
sudo make EP_mnmlstc_core
make
sudo make install
如果出错如下:
错误1:
[root@localhost build]# cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
-- The CXX compiler identification is GNU 4.8.5
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: /usr/bin/python (found version "2.7.5")
fatal: Not a git repository (or any of the parent directories): .git
Traceback (most recent call last):
File "etc/calc_release_version.py", line 171, in <module>
RELEASE_VER = main()
File "etc/calc_release_version.py", line 108, in main
head_tag_ver = check_head_tag()
File "etc/calc_release_version.py", line 68, in check_head_tag
tags = check_output(['git', 'tag', '-l']).split()
File "etc/calc_release_version.py", line 46, in check_output
return subprocess.check_output(args)
File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['git', 'tag', '-l']' returned non-zero exit status 128
CMake Error at CMakeLists.txt:162 (message):
BUILD_VERSION not specified and could not be calculated (script invocation
failed); specify in CMake command, -DBUILD_VERSION=<version>
-- Configuring incomplete, errors occurred!
See also "/home/mongo-cxx-driver-r3.6.2/build/CMakeFiles/CMakeOutput.log".
说明python版本过低,升级python版本见上述4操作。
错误2:
[root@localhost build]# cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
-- The CXX compiler identification is GNU 4.8.5
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: /usr/bin/python (found version "3.6.6")
fatal: Not a git repository (or any of the parent directories): .git
Traceback (most recent call last):
File "etc/calc_release_version.py", line 171, in <module>
RELEASE_VER = main()
File "etc/calc_release_version.py", line 108, in main
head_tag_ver = check_head_tag()
File "etc/calc_release_version.py", line 68, in check_head_tag
tags = check_output(['git', 'tag', '-l']).split()
File "etc/calc_release_version.py", line 46, in check_output
return subprocess.check_output(args)
File "/usr/local/python3/lib/python3.6/subprocess.py", line 336, in check_output
**kwargs).stdout
File "/usr/local/python3/lib/python3.6/subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['git', 'tag', '-l']' returned non-zero exit status 128.
CMake Error at CMakeLists.txt:162 (message):
BUILD_VERSION not specified and could not be calculated (script invocation
failed); specify in CMake command, -DBUILD_VERSION=<version>
-- Configuring incomplete, errors occurred!
See also "/home/mongo-cxx-driver-r3.6.2/build/CMakeFiles/CMakeOutput.log".
需要修改“"/usr/local/python3/lib/python3.6/subprocess.py", line 336”这个文件的336行“
return run(*popenargs, stdout=PIPE, timeout=timeout, check=False,
**kwargs).stdout”
将“check=true”改为“check=False”即可编译通过。
例子:
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
#include <bsoncxx/builder/stream/helpers.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/stream/array.hpp>
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
using namespace std;
int main()
{
// 建立链接
mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://xxx.xxx.xxx.xxx:27017");
mongocxx::client client(uri);
// 创建数据源
mongocxx::database db = client["mydb"];
// 创建集合或者表
mongocxx::collection coll = db["test"];
// 循环插入多条数据
std::vector<bsoncxx::document::value> documents;
for(int j = 0; j < 100; j++){
documents.clear();
for(int i = 0; i < 100; i++) {
documents.push_back(
bsoncxx::builder::stream::document{} << "sigID" << "qwrqwerqwerqwer12345" << "cdrID" <<"asdfasdfasdfasdf1234" << "alarmID" << "" << "reqTimeSec" << 1671524976 << "dataType" << "0" << "dataLen" << 255 << "data" <<"yriewuqiroeuwofhskdhfwouiehfdsuafhewafudshfeiwufdsfhueiwfhjkshfuiewhfaesdjkhfuehafjdkshauefhwiaue123yriewuqiroeuwofhskdhfwouiehfdsuafhewafudshfeiwufdsfhueiwfhjkshfuiewhfaesdjkhfuehafjdkshauefhwiaue123qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdf" << finalize);
}
coll.insert_many(documents);
}
return 0;
}
编译:
c++ --std=c++11 -g test4.cpp -o test4 -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/bsoncxx/v_noabi -I/usr/local/include/libbson-1.0 -L/usr/local/lib64 -lmongocxx -lbsoncxx -Wl,-rpath,/usr/local/lib64
安装完成!