跟redis相比,网上MongoDB的文章比较少,MongoDB集群的搭建大部分是多机器部署,为了测试方便,需要在本地机器部署MongoDB集群,参考了很多文章,终于搭建成功,记录下相关步骤。
需要注意的是,MongoDB更新很快,很多文章的语法过时,建议参考看官网文档,
环境版本:
mongodb 3.4.6
Centos 7.5
1、安装mongodb
#解压
tar -xzvf mongodb-linux-x86_-3.4.6.tgz
#改名
mv mongodb-linux-x86_-3.4.6 ../mongodb
#配置环境变量
vim /etc/profile
export MONGODB_HOME=/opt/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
source /etc/profile
mkdir -p /opt/mongodb/conf
mkdir -p /opt/mongodb/config1/data
mkdir -p /opt/mongodb/config1/log
mkdir -p /opt/mongodb/config2/data
mkdir -p /opt/mongodb/config2/log
mkdir -p /opt/mongodb/config3/data
mkdir -p /opt/mongodb/config3/log
mkdir -p /opt/mongodb/mongos1/log
mkdir -p /opt/mongodb/mongos2/log
mkdir -p /opt/mongodb/mongos3/log
mkdir -p /opt/mongodb/shard1a/data
mkdir -p /opt/mongodb/shard1a/log
mkdir -p /opt/mongodb/shard1b/data
mkdir -p /opt/mongodb/shard1b/log
mkdir -p /opt/mongodb/shard1c/data
mkdir -p /opt/mongodb/shard1c/log
mkdir -p /opt/mongodb/shard2a/data
mkdir -p /opt/mongodb/shard2a/log
mkdir -p /opt/mongodb/shard2b/data
mkdir -p /opt/mongodb/shard2b/log
mkdir -p /opt/mongodb/shard2c/data
mkdir -p /opt/mongodb/shard2c/log
mkdir -p /opt/mongodb/shard3a/data
mkdir -p /opt/mongodb/shard3a/log
mkdir -p /opt/mongodb/shard3b/data
mkdir -p /opt/mongodb/shard3b/log
mkdir -p /opt/mongodb/shard3c/data
mkdir -p /opt/mongodb/shard3c/log
2、config server配置服务器
mongodb3.4以后要求配置服务器也创建副本集,不然集群搭建不成功。
添加配置文件
vi /opt/mongodb/conf/config1.conf
## 配置文件内容
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/config1/log/congigsrv.log
storage:
dbPath: /opt/mongodb/config1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongodb/config1/log/configsrv.pid
net:
port: 21001
bindIp: 0.0.0.0
replication:
replSetName: configs
sharding:
clusterRole: configsvr
## 配置文件end
vi /opt/mongodb/conf/config2.conf
## 配置文件内容
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/config2/log/congigsrv.log
storage:
dbPath: /opt/mongodb/config2/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongodb/config2/log/configsrv.pid
net:
port: 21002
bindIp: 0.0.0.0
replication:
replSetName: configs
sharding:
clusterRole: configsvr
## 配置文件end
vi /opt/mongodb/conf/config3.conf
## 配置文件内容
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/config3/log/congigsrv.log
storage:
dbPath: /opt/mongodb/config3/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongodb/config3/log/configsrv.pid
net:
port: 21003
bindIp: 0.0.0.0
replication:
replSetName: configs
sharding:
clusterRole: configsvr
## 配置文件end
启动三台服务器的config server
mongod -f /opt/mongodb/conf/config1.conf
mongod -f /opt/mongodb/conf/config2.conf
mongod -f /opt/mongodb/conf/config3.conf
登录任意一台配置