MongoDB介绍、安装及mongocxx C++驱动程序的安装

MongoDB介绍

MongoDB 是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。

特点:

  • 面向集合
  • 模式自由
  • 文件存储格式为BSON(一种JSON的扩展)
  • 支持动态查询
  • 支持完全索引

MongoDB百度百科
MongoDB菜鸟教程

centos7 下安装

tar -zxvf mongodb-linux-x86_64-rhel70-3.4.10.tgz -C /home/oracle/app
mv mongodb-linux-x86_64-rhel70-3.4.10 mongodb
cd mongodb
mkdir db
mkdir logs
cd bin
//增加配置文件
vi mongodb.conf
dbpath=/home/oracle/app/mongodb/db/
logpath=/home/oracle/app/mongodb/logs/mongodb.log
port=27017
fork=true
nohttpinterface=true

mongodb启动

./mongodb/bin/mongod –config /home/oracle/app/mongodb/bin/mongodb.conf

./mongodb/bin/mongo

//设置mongod开机自启动
vi /etc/rc.d/rc.local
/home/oracle/app/mongodb/bin/mongod –config /home/oracle/app/mongodb/bin/mongodb.conf

C++驱动安装

准备工作

  • 任何标准的Unix平台,或Windows 7 SP1 +
  • 一个支持C ++ 11(gcc,clang或Visual Studio)的编译器
  • CMake 3.2或更高版本

第1步:安装最新版本的MongoDB C驱动程序

$ sudo yum install pkg-config openssl-devel cyrus-sasl-devel

$ wget https://github.com/mongodb/mongo-c-driver/releases/download/1.8.1/mongo-c-driver-1.8.1.tar.gz
$ tar xzf mongo-c-driver-1.8.1.tar.gz
$ cd mongo-c-driver-1.8.1
$ ./configure --disable-automatic-init-and-cleanup
$ make
$ sudo make install

第2步:选择一个C ++ 17 polyfill
安装mongocxx driver 需要有具有C++17特性的以下接口:

  • MNMLSTC/core(默认非windows系统使用):需安装git、cmake等等等。。。后通过第4步安装
  • Boost(默认windows系统使用,如果用msvc这是唯一选择)

步骤3:下载最新版本的mongocxx驱动程序。

curl -OL https://github.com/mongodb/mongo-cxx-driver/archive/r3.1.3.tar.gz
tar -xzf r3.1.3.tar.gz
cd mongo-cxx-driver-r3.1.3/build

步骤4:配置驱动程序(这里的坑太多,依赖太多一个个装吧)

cd mongo-cxx-driver-r3.1.3/build

PKG_CONFIG_PATH=/usr/local/lib/pkgconfig cmake -DBSONCXX_POLY_USE_MNMLSTC=1 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..

Checking for module ‘libbson-1.0>=1.5.0’
默认绑定安装的libbson版本低
需先安装新版本libbson-1.8.1

wget https://github.com/mongodb/libbson/releases/download/1.8.1/libbson-1.8.1.tar.gz
tar -zxvf libbson-1.8.1.tar.gz
cd libbson-1.8.1/
./configure
make
sudo make install

autoconf与automake的安装(也许不要)

wget http://mirrors.kernel.org/gnu/autoconf/autoconf-2.69.tar.gz
tar -zxvf autoconf-2.69.tar.gz
cd autoconf-2.69/
./configure
make
sudo make install

wget http://ftp.gnu.org/gnu/automake/automake-1.15.1.tar.gz
tar -zxvf automake-1.15.1.tar.gz
cd automake-1.15.1/
./configure
make
sudo make install

m4与libtool的安装(也许不要)

sudo yum install m4

sudo yum install libtool

第5步:构建并安装驱动程序

cd mongo-cxx-driver-r3.1.3/build

sudo make EP_mnmlstc_core

make && sudo make install

第6步:测试您的安装

将以下源文件的文件名保存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;
    }
}

在pkg-config的帮助下编译

先设置PKG_CONFIG_PATH环境变量:在.bash_profile中配置环境变量

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

使用以下命令编译上面的测试程序:

c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)

成功生成test执行程序后执行./test报错
error while loading shared libraries: libmongocxx.so._noabi
解决方法:http://blog.chinaunix.net/uid-26212859-id-3256667.html

# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
#su #切换到root用户
# echo "/usr/local/lib" >> /etc/ld.so.conf
# ldconfig

再次执行./test成功
程序创建testdb数据库及testcollection集合,输出如下:

{ "_id" : { "$oid" : "5a0c27a6e138232bc80c1852" }, "hello" : "world" }

参考:
官方安装文档:https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/#step-3-download-the-latest-version-of-the-mongocxx-driver

http://blog.csdn.net/hhq163/article/details/52056948
http://www.bubuko.com/infodetail-2222544.html

MongoDB图形化界面Robo 3T(Robomongo)安装

wget https://download.robomongo.org/1.1.1/linux/robo3t-1.1.1-linux-x86_64-c93c6b0.tar.gz
tar -zxvf robo3t-1.1.1-linux-x86_64-c93c6b0.tar.gz
cd robo3t-1.1.1-linux-x86_64-c93c6b0/
./bin/robo3t

启动报错:./robo3t: /lib64/libc.so.6: version `GLIBC_2.18’ not found

wget http://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz
tar zxf glibc-2.18.tar.gz 
cd glibc-2.18/
mkdir build
cd build/
../configure --prefix=/usr #注意了,别修改路径
make -j2
sudo make install

参考:http://blog.csdn.net/u010526125/article/details/72626285

猜你喜欢

转载自blog.csdn.net/ts1130/article/details/78515705