C语言操作mongodb

MongoDB是一个基于分布式文件存储的数据库,旨在为WEB应用提供可扩展的高性能数据存储解决方案。

安装openssl:

1
2
3
$ brew install openssl
$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl/include"

安装mongodb:

1
$ brew install mongodb

下载并安装mongodb的C语言驱动:

1
2
3
4
5
6
7
8
$ curl -LO https://github.com/mongodb/mongo-c-driver/releases/download/1.3.0/mongo-c-driver-1.3.0.tar.gz
$ tar xzf mongo-c-driver-1.3.0.tar.gz
$ cd mongo-c-driver-1.3.0
// 安装编译工具
$ brew install git automake autoconf libtool pkgconfig
$ ./configure
$ make
$ sudo make install

生成帮助文档:

1
2
3
$ brew install yelp-xsl yelp-tools
$ ./configure --enable-html-docs --enable-man-pages
$ make man html

测试环境:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 连接数据库
$ mongo --host localhost --port 27017

// 提示错误
MongoDB shell version: 3.2.0
connecting to: localhost:27017/test
2016-05-24T15:20:59.736+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2016-05-24T15:20:59.736+0800 E QUERY [thread1] Error: couldn't connect to server localhost:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:226:14
@(connect):1:6

exception: connect failed

// 原因
mongodb服务没有启动

// 解决方案
$ brew services start mongodb
$ mongo --host localhost --port 27017

其他平台的安装过程:

其他平台的安装过程: http://api.mongodb.com/c/current/installing.html

,