序
在debian上安装mongo社区版,官方给了文档。
Install MongoDB Community Edition on Debian
现在mongo版本为3.4
装完mongod, 再装mongo-c-driver, mongo-c-driver. 可能会缺很多其他依赖,找资料,一个一个装,心碎了。
如果下的单独的deb, 按照deb下载页说明, 将安装源加入/etc/apt/sources.list.
执行apt-get update.
如果一个deb装时有依赖,执行2次命令。
dpkg -i ./x.deb // first install
apt-get -f install // if install error, second install, auto install depends component.
实验
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.4 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo service mongod start
成功信息
[ ok ] Starting database: mongod apparently already running.
用内建客户端连上去看看是否能操作mongo
mongo
成功信息
MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-05-20T11:08:27.680+0800 I STORAGE [initandlisten]
2017-05-20T11:08:27.680+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-05-20T11:08:27.680+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-05-20T11:08:27.865+0800 I CONTROL [initandlisten]
2017-05-20T11:08:27.865+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-05-20T11:08:27.865+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-05-20T11:08:27.865+0800 I CONTROL [initandlisten]
2017-05-20T11:08:27.865+0800 I CONTROL [initandlisten]
2017-05-20T11:08:27.865+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-05-20T11:08:27.865+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-05-20T11:08:27.865+0800 I CONTROL [initandlisten]
>
远程mongod的连接设置
刚装完的mongod只能在本地用mongo连接。
如果用robomongo-1.0.0-rc1-windows-x86_64-496f5c2连接虚拟机里面的mongod(ip and port are ok), 显示网络不可达.
这时,要去改mongod的配置文件,然后重启mongod.
要屏蔽掉mongod.cond中的bindIp, 或在bindIp中加入允许访问的IP.
ref : How to connect to your remote MongoDB server
root@debian750:~# more /etc/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
# bindIp: 127.0.0.1
#processManagement:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
安装mongo-c-driver
mongo-cxx-driver依赖mongo-c-driver, 正式发布的mongo-c-driver版本为1.6.2
下载mongo-c-driver-1.6.2.tar.gz,解压 tar -xzvf
因为要带口令登录mongod, 需要使sslss生效
./configure --enable-sasl=yes
make clean
make all
make install
测试mongoc是否正确编译和安装
编译后的源码目录有些测试程序,执行一下。
./test-libmongoc
安装mongo-cxx-driver
如果缺少git, 运行apt-get install git
git clone https://github.com/mongodb/mongo-cxx-driver.git --branch releases/stable --depth 1
如果缺少cmake, 运行 apt-get install cmake
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
如果cmake版本低了
sudo apt-get install build-essential
wget http://www.cmake.org/files/v3.2/cmake-3.2.2.tar.gz
tar xf cmake-3.2.2.tar.gz
cd cmake-3.2.2
./configure
make
make install
进入mongo-cxx-driver/src目录,开始编译
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
报错,说CMAKE_ROOT没找到
执行 ~/.bashrc, 增加CMAKE_ROOT
export CMAKE_ROOT=/usr/share/cmake-3.2
cd cmake-3.2
./bootstrap –prefix=/usr
make
make install
最后验证一下cmake是否可用
root@debian750:/home/lostspeed/tmp/cmake-3.2.2# cmake --version
cmake version 3.2.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
去mongo-cxx-driver/src, 继续执行cmake
```
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
这版的mongo-cxx-driver需要GCC4.8.2, 现在是4.7.x, 升级gcc
Insufficient GCC version - GCC 4.8.2+ required
下载gcc_4.9.2-2_amd64.deb到虚拟机,
dpkg -i ./gcc_4.9.2-2_amd64.deb
apt-get -f install
去mongo-cxx-driver/src, 继续执行cmake
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
cmake编译git版的mongo-stable-3.0.x,不支持mongo3.4, 也不支持说明上的cmake命令行。
直接去下3.1.x的tar包。
如果缺curl, apt-get install curl
curl -OL https://github.com/mongodb/mongo-cxx-driver/archive/r3.1.1.tar.gz
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
在mongo-cxx-driver/src中,cmake通过。
make EP_mnmlstc_core
make install
这次,mongo-cxx-driver 3.1.1编译过了,心碎。
mongo-cxx-driver测试
mongo-cxx官方给了测试代码,随便找一个就行。
但是makefile和测试代码执行时,还会有点小问题,主要是路径包含和共享库的连接名称。
# ==============================================================================
# makefile
# lostspeed 2017-05-21
# command list:
# make clean
# make
# make rebuild
# ==============================================================================
#
# 必须用gcc + -lstdc++
# 否则报错 : undefined reference to `std::allocator<char>::allocator()'
#
# $@ is the label
# $< is x.cpp
# $^ is x.cpp y.cpp ...
#
# show cur dir's all files
# find .
#
# echo show somthing by echo off
# @echo xx
#
# use ldd view elf depends's so
# ldd ./testcase1.out
# linux-vdso.so.1 (0x00007ffdd7993000)
# libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fea69430000)
# libmongocxx.so._noabi => not found
# libbsoncxx.so._noabi => not found
# libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fea6912f000)
# libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fea68f19000)
# libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fea68cfc000)
# libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fea68951000)
# /lib64/ld-linux-x86-64.so.2 (0x00007fea6973b000)
#
# when run, error while loading shared libraries: libmongocxx.so._noabi: \
# cannot open shared object file: No such file or directory
#
# use ln -s create link file name
# ln -s real_file_name link_file_name
#
# /usr/local/lib/libmongocxx.so._noabi -> libmongocxx.so.3.1.1
# /usr/local/lib/libbsoncxx.so._noabi -> libbsoncxx.so.3.1.1
# /usr/local/lib/libmongocxx.so.3.1.1
# /usr/local/lib/libbsoncxx.so.3.1.1
#
# rm /usr/local/lib/libmongocxx.so._noabi
# rm /usr/local/lib/libbsoncxx.so._noabi
# ln -s /usr/local/lib/libmongocxx.so.3.1.1 /usr/local/lib/libmongocxx.so._noabi
# ln -s /usr/local/lib/libbsoncxx.so._noabi /usr/local/lib/libbsoncxx.so._noabi
#
# use ldconfig reload config
# ldconfig
#
# now elf output depends ok
# ldd ./testcase1.out
# linux-vdso.so.1 (0x00007ffc487ef000)
# libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7f1e455000)
# libmongocxx.so._noabi => /usr/local/lib/libmongocxx.so._noabi (0x00007f7f1e200000)
# libbsoncxx.so._noabi => /usr/local/lib/libbsoncxx.so._noabi (0x00007f7f1dfe6000)
# libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7f1dce5000)
# libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7f1dacf000)
# libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7f1d8b2000)
# libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f1d507000)
# /lib64/ld-linux-x86-64.so.2 (0x00007f7f1e760000)
# libmongoc-1.0.so.0 => /usr/local/lib/libmongoc-1.0.so.0 (0x00007f7f1d2ab000)
# libsasl2.so.2 => /usr/lib/x86_64-linux-gnu/libsasl2.so.2 (0x00007f7f1d090000)
# libssl.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 (0x00007f7f1ce2f000)
# libcrypto.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007f7f1ca35000)
# librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7f1c82d000)
# libbson-1.0.so.0 => /usr/local/lib/libbson-1.0.so.0 (0x00007f7f1c5fd000)
# libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7f1c3f9000)
# libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f7f1c1e2000)
# libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f7f1bfcb000)
# ------------------------------------------------------------------------------
CC = g++
CFLAGS = --std=c++11 \
-Wall \
-g
BIN = testcase1.out
INC = -I. \
-I./3rd \
-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
LIBS = -lstdc++ \
-pthread \
-lmongocxx \
-lbsoncxx
LIBPATH = /usr/local/lib
SUBDIR = $(shell ls -d */)
ROOTSRC = $(wildcard *.cpp)
ROOTOBJ = $(ROOTSRC:.cpp=.o)
SUBSRC = $(shell find $(SUBDIR) -name '*.cpp')
SUBOBJ = $(SUBSRC:.cpp=.o)
help:
clear
@echo make help
@echo command list:
@echo make clean
@echo make all
@echo make rebuild
clean:
clear
@echo make clean
rm -f $(BIN) $(ROOTOBJ) $(SUBOBJ)
all:$(BIN)
@echo make all
find . -name "*.o"
find . -name $(BIN)
$(BIN) : $(ROOTOBJ) $(SUBOBJ)
$(CC) $(CFLAGS) -o $@ $^ -L$(LIBPATH) $(LIBS)
.cpp.o:
$(CC) -c $(CFLAGS) $^ -o $@ $(INC)
rebuild:
make clean
make all
// @file main.cpp
// @brief ...
#include <stdlib.h>
#include <stdio.h>
#include "mongo_opt/mongo_opt.h"
void test();
int main(int argc, char* argv[])
{
printf(">> main\n");
test();
printf("THE END\n");
return EXIT_SUCCESS;
}
void test()
{
bool b_rc = false;
class_mongo_opt opt;
b_rc = opt.test();
printf("[%s] = opt.test()\n", (b_rc ? "true" : "false"));
}
// @file mongo_opt/mongo_opt.h
// @brief ...
#ifndef __MONGO_OPT_H__
#define __MONGO_OPT_H__
#include <stdlib.h>
#include <stdio.h>
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/stdx/make_unique.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/exception/exception.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 mongocxx::v_noabi::instance;
using mongocxx::v_noabi::uri;
using namespace mongocxx;
class class_mongo_opt
{
public:
class_mongo_opt();
virtual ~class_mongo_opt();
bool test();
};
#endif // #ifndef __MONGO_OPT_H__
// @file mongo_opt/mongo_opt.cpp
// @brief ...
#include <string>
#include "mongo_opt/mongo_opt.h"
class_mongo_opt::class_mongo_opt()
{
}
class_mongo_opt::~class_mongo_opt()
{
}
bool class_mongo_opt::test()
{
bool b_rc = false;
std::string errmsg;
try {
mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(uri);
b_rc = true;
}
catch(std::exception& ex) {
errmsg = ex.what();
printf("exception : %s\n", errmsg.c_str());
b_rc = false;
}
return b_rc;
}