一台电脑(双系统windows和linux)或者一台电脑(windows 下装虚拟机)。
Windows版本:win7 旗舰版 64位。
虚拟机操作系统配置:红帽6.7_x86_64版本。
通过ping命令能够互相ping通,保持网络通常。
mongodb-linux-x86_64-rhel62-3.2.10.tgz
下载地址:链接:https://pan.baidu.com/s/1yr9MwuBjldsNoEGn94DA0w 密码:cw83
首先,切换至root用户。
解压:
tar xvf mongodb-linux-x86_64-rhel62-3.2.10.tgz
重命名:
mv mongodb-linux-x86_64-rhel62-3.2.10.tgz mongodb
移动
mv mongodb /usr/local
进入目录
cd /usr/local/mongodb
新建data和logs目录
mkdir data logs
新建配置文件mongodb.conf
vim mongodb.conf
添加我们需要的选项
dbpath=/usr/local/mongodb/data
port=27017
logpath=/usr/local/mongodb/logs/mongodb.log
logappend=true
fork=true
1.数据库目录
2.端口
3.日志路径文件
4.日志追加末尾
5.后台子进程运行
启动:
cd /usr/local/mongodb/bin
./mongod -f ../mongodb.conf
至此,mongodb应该是可以启动了,如有问题,请看错误处理部分。
切换成普通用户,在/home/username/
这一级目录下vim .bash_profile
在最后添加一句
export PATH=/usr/local/mongodb/bin:$PATH
保存退出,在终端输入,使环境变量生效
source .bash_profile
mongo
即可进入mongodb的命令行界面。
写入
db.collName.save({name: 'zhangsan', age: 25});
修改
db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true);
查询
db.users.find({age: 132});
删除
db.users.remove({age: 132});
创建表
db.createCollection("collName", {name:'haha', age:17});
删除表
db.collection.drop()
mongo-c-driver-1.12.0.tar.gz
Cmake版本不能低于3.1(默认是2.8)
下载地址:
mongo-c-driver-1.12.0.tar.gz
Cmake3.5
云盘地址:
链接:https://pan.baidu.com/s/1yr9MwuBjldsNoEGn94DA0w 密码:cw83
将压缩包下载下来,su进入root用户,
tar xzf cmake-3.5.2.tar.gz
cd cmake-3.5.2
./bootstrap
make
make install
cd到 压缩包目录下,执行
tar xzf mongo-c-driver-1.12.0.tar.gz
cd mongo-c-driver-1.12.0
mkdir cmake-build
cd cmake-build
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..
make
make install
连接数据库测试,代码如下:
#include
#include
#include
int
main (int argc,
char *argv[])
{
mongoc_client_t *client;
mongoc_database_t *database;
mongoc_collection_t *collection;
bson_t *command,
reply,
*insert;
bson_error_t error;
char *str;
bool retval;
/*
* Required to initialize libmongoc's internals
*/
mongoc_init ();//初始化libmongoc驱动
/*
* Create a new client instance
*/
client = mongoc_client_new ("mongodb://localhost:27017");//创建连接对象
/*
* Get a handle on the database "db_name" and collection "coll_name"
*/
database = mongoc_client_get_database (client, "db_name");//获取数据库
collection = mongoc_client_get_collection (client, "db_name", "coll_name");//获取指定数据库和集合
/*
* Do work. This example pings the database, prints the result as JSON and
* performs an insert
*/
command = BCON_NEW ("ping", BCON_INT32 (1));
retval = mongoc_client_command_simple (client, "admin", command, NULL, &reply, &error);//执行命令
if (!retval) {
fprintf (stderr, "%s\n", error.message);
return EXIT_FAILURE;
}
str = bson_as_json (&reply, NULL);
printf ("%s\n", str);
insert = BCON_NEW ("hello", BCON_UTF8 ("world"));//字段为hello,值为world字符串
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, insert, NULL, &error)) {//插入文档
fprintf (stderr, "%s\n", error.message);
}
bson_destroy (insert);
bson_destroy (&reply);
bson_destroy (command);
bson_free (str);
/*
* Release our handles and clean up libmongoc
*/
mongoc_collection_destroy (collection);//释放表对象
mongoc_database_destroy (database);//释放数据库对象
mongoc_client_destroy (client);//释放连接对象
mongoc_cleanup ();//释放libmongoc驱动
return 0;
}
编译:
gcc -o test test.c -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0/ -lmongoc-1.0 -lbson-1.0
注意,这里直接执行./test 会报错:
./test: error while loading shared libraries: libmongoc-1.0.so.0: cannot open shared object file: No such file or directory
如何解决:
ldd test
查看缺少的链接库文件,
会发现缺少的链接库文件
linux-vdso.so.1 => (0x00007ffe4e089000)
libmongoc-1.0.so.0 => not found
libbson-1.0.so.0 => not found
libc.so.6 => /lib64/libc.so.6 (0x0000003c2fe00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c2f600000)
查看缺少的文件在哪儿:find / -name libmongoc-1.0.so.0
/home/work/ssd/tp/mongoc/lib/libmongoc-1.0.so.0
/usr/local/lib64/libmongoc-1.0.so.0
/usr/local/mongodb/mongo-c-driver-1.12.0/cmake-build/src/libmongoc/libmongoc-1.0.so.0
可以看到在/usr/local/lib64文件夹下面,故在执行的时候,这样执行
LD_LIBRARY_PATH=/usr/local/lib64/ ./test
问题解决,但是我们总不能每次编译都要打这么长一行吧?那肯定是不行的,我们只需要这样:
cd /etc/ld.so.conf.d/
vim mongoc.conf
将LD_LIBRARY_PATH=/usr/local/lib64/写入,保存退出。运行程序,完美解决。
参考文献:https://blog.csdn.net/warrior_zhang/article/details/50503406
其它版本的linux可以参考官方的安装教程:传送门
报错:
./mongod: /usr/lib64/libssl.so.10: no version information available (required by ./mongod)
./mongod: /usr/lib64/libcrypto.so.10: no version information available (required by ./mongod)
./mongod: /usr/lib64/libcrypto.so.10: no version information available (required by ./mongod)
./mongod: relocation error: ./mongod: symbol TLSv1_2_server_method, version libssl.so.10 not defined in file libssl.so.10 with link time reference
解决:
rpm -ivh openssl-1.0.1e-57.el6.x86_64.rpm --nodeps
再启动mongodb。
注:此问题我并没有遇到,如有小伙伴遇到了,请参考此方法解决,但不一定成功。
查资料的过程中看到有人说遇到过这个问题,但因为我之前配置过,所以安装mongodb的过程中也没有遇到。安装过程中如有遇到,请自行百度解决。
[MDBS@ding ~]$ mongo
MongoDB shell version: 3.2.3
connecting to: test
2018-08-15T13:57:40.669+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2018-08-15T13:57:40.698+0800 E QUERY [thread1] Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:224:14
@(connect):1:6
exception: connect failed
这是因为咱们的配置文件没有启用,参考前面的配置文件的书写和启用再走一遍,然后,mongo
,就可以了。
参考资料:
https://www.cnblogs.com/chenglee/p/8510288.html
http://mongoc.org/
http://www.runoob.com/mongodb/mongodb-tutorial.html
https://blog.csdn.net/warrior_zhang/article/details/50503406