一、环境准备
linux:centos7
备注:如果大家要买ECS的话建议还是买阿里云,笔者用腾讯云安装mongodb分片集群的时候遇到了很多问题,花了很长时间解决。阿里这方面做得比较好,无论是yum源还是相关配置都已经设置好了。
mongodb:3.2.9(3.2以后的版本分片集群的搭建方式有改动,具体请参照官网)
由于笔者只买了一台ECS服务器,所以本次集群的方式可以说是一个伪集群.所有的节点都是部署在同一台机器。
二、安装
下载mongodb-linux-x86_64-ubuntu1404-3.2.9.tgz.
其它版本可以访问:https://www.mongodb.org/dl/linux/
cd /usr/local/software
tar -zxvf mongodb-linux-x86_64-ubuntu1404-3.2.9.tgz
mv mongodb-linux-x86_64-ubuntu1404-3.2.9 mongodb3.2.9
二、配置
//建立数据存储目录
cd / 进入根目录
mkdir -p /data/config
mkdir -p /data/shard1
mkdir -p /data/shard2
mkdir -p /data/shard3
//建立日志目录:
mkdir logs
//配置configsrv启动文件
//(/usr/local/software/mongodb3.2.9/config.d/configsrv.conf)
logpath=/logs/configsrv.log
pidfilepath=/logs/configsrv.pid
logappend=true
fork=true
port=27000
dbpath=/data/config
configsvr=true
//配置路由节点启动文件
//(/usr/local/software/mongodb3.2.9/config.d/router.conf)
logpath=/logs/mongos_router.log
pidfilepath=/logs/mongos_router.pid
logappend=true
fork=true
port=27010
configdb=172.27.0.14:27000 //指定的是configsrv的ip和端口(3.2版本以后configdb只能用于副本集集群模式-亲测)
三、启动服务
1.启动配置服务器(configsrv)
./mongod -f ../config.d/configsrv.conf
2.启动路由服务器(router)
./mongos -f ../config.d/router.conf 注意:用mongos启动
3.分别启动分片1,2,3.用不同的端口27001,27002,27003区分:
./mongod --dbpath /data/shard1/ --logpath /logs/shard1.log --fork --port 27001
./mongod --dbpath /data/shard2/ --logpath /logs/shard2.log --fork --port 27002
./mongod --dbpath /data/shard3/ --logpath /logs/shard3.log --fork --port 27003
4.用ps查看一下
[root@VM_0_14_centos bin]# ps -ef|grep mongo
root 2391 1 0 20:00 ? 00:00:08 ./mongod -f ../config.d/configsrv.conf
root 2760 1 0 20:06 ? 00:00:00 ./mongos -f ../config.d/router.conf
root 2975 1 0 20:10 ? 00:00:01 ./mongod --dbpath /data/shard1/ --logpath /logs/shard1.log --fork --port 27001
root 3054 1 0 20:12 ? 00:00:00 ./mongod --dbpath /data/shard2/ --logpath /logs/shard2.log --fork --port 27002
root 3183 1 0 20:13 ? 00:00:00 ./mongod --dbpath /data/shard3/ --logpath /logs/shard3.log --fork --port 27003
root 3267 2330 0 20:15 pts/2 00:00:00 grep --color=auto mongo
[root@VM_0_14_centos bin]#
说明已经正常启动了,一个configsrv节点,一个路由几点,3个分片节点。
5.启动异常说明
笔者启动的时候老提示"Error while loading shared libraries: libssl.so.6: cannot open shared object file:"
解决办法:
下载openssl-1.0.1e(https://www.openssl.org/source/old/1.0.1/openssl-1.0.1e.tar.gz),解压后再通过./config shared zlib-dynamic命令生成MakeFile,然后执行make重新生成libssl.so.1.0.0、libcrypto.so.1.0.0,再将这两个文件copy到/usr/lib64下面。然后执行:
ln -s libssl.so.1.0.0 libssl.so.10
ln -s libcrypto.so.1.0.0 libcrypto.so.10
如果中途make的时候出现gcc,zib.h缺失直接yum install安装即可。如果ln -s的时候提示软链接已存在,则将原来的软链接删掉重新执行上述命令即可。
6.添加分片
[root@VM_0_14_centos bin]# ./mongo --port 27000
./mongo: /lib64/libcrypto.so.1.0.0: no version information available (required by ./mongo)
./mongo: /lib64/libssl.so.1.0.0: no version information available (required by ./mongo)
MongoDB shell version: 3.2.9
connecting to: 127.0.0.1:27000/test
Server has startup warnings:
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten]
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten]
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten]
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten]
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 7281 processes, 100001 files. Number of processes should be at least 50000.5 : 0.5 times number of files.
2018-05-12T20:00:53.255+0800 I CONTROL [initandlisten]
configsvr>
//注意添加分片的时候必须连到路由节点的端口:
[root@VM_0_14_centos bin]# ./mongo --port 27010
./mongo: /lib64/libcrypto.so.1.0.0: no version information available (required by ./mongo)
./mongo: /lib64/libssl.so.1.0.0: no version information available (required by ./mongo)
MongoDB shell version: 3.2.9
connecting to: 127.0.0.1:27010/test
Server has startup warnings:
2018-05-12T20:06:43.668+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2018-05-12T20:06:43.668+0800 I CONTROL [main]
mongos> use admin
switched to db admin
mongos> sh.addShard("172.27.0.14:27001");
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("172.27.0.14:27002");
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.addShard("172.27.0.14:27003");
{ "shardAdded" : "shard0002", "ok" : 1 }
mongos>
一个分片集群就已经搭建完成了。
四、总结
要想深入了解mongodb,就必要搭建一套集群环境.mongodb其实有分片,主从,副本集的集群方式,但是企业一般都是用的分片集群,所以搭建一套集群环境对以后深入了解mongodb是非常有必要的。
参考文档:
https://blog.csdn.net/qq43748322/article/details/78327389
https://blog.csdn.net/shmnh/article/details/50465862