MongoDB多实例配置基于下方单机部署环境配置多实例环境
👇👇👇👇👇👇👇👇👇👇👇👇
👆👆👆👆👆👆👆👆👆👆👆👆
创建多实例目录:
1 2 |
mkdir -p /mongodb/280{17..20}/{conf,data,log} |
编辑28017实例配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
vim /mongodb/28017/conf/mongod.conf systemLog: destination: file path: "/mongodb/28017/log/mongodb.log" logAppend: true storage: journal: enabled: true dbPath: "/mongodb/28017/data" directoryPerDB: true #engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: fork: true net: bindIp: 10.0.0.20 port: 28017 replication: oplogSizeMB: 2048 replSetName: my_repl |
拷贝配置文件到其他实例:
1 2 3 4 |
cp /mongodb/28017/conf/mongod.conf /mongodb/28018/conf/ cp /mongodb/28017/conf/mongod.conf /mongodb/28019/conf/ cp /mongodb/28017/conf/mongod.conf /mongodb/28020/conf/ |
修改其他节点目录及端口配置:
1 2 3 4 |
sed -i "s#28017#28018#g" /mongodb/28018/conf/mongod.conf sed -i "s#28017#28019#g" /mongodb/28019/conf/mongod.conf sed -i "s#28017#28020#g" /mongodb/28020/conf/mongod.conf |
授权目录:
1 2 |
chown -R mongod.mongod /mongodb/ |
配置systemd管理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
vim /lib/systemd/system/mongodb-28017.service [Unit] Description=mongodb After=network.target remote-fs.target nss-lookup.target [Service] User=mongod Type=forking ExecStart=/mongodb/bin/mongod -f /mongodb/28017/conf/mongod.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/mongodb/bin/mongod -f /mongodb/28017/conf/mongod.conf --shutdown PrivateTmp=true [Install] WantedBy=multi-user.target |
拷贝启动文件:
1 2 3 4 |
cp /lib/systemd/system/mongodb-28017.service /lib/systemd/system/mongodb-28018.service cp /lib/systemd/system/mongodb-28017.service /lib/systemd/system/mongodb-28019.service cp /lib/systemd/system/mongodb-28017.service /lib/systemd/system/mongodb-28020.service |
修改其他节点目录配置:
1 2 3 |
sed -i "s#28017#28018#g" /lib/systemd/system/mongodb-28018.service sed -i "s#28017#28019#g" /lib/systemd/system/mongodb-28019.service sed -i "s#28017#28020#g" /lib/systemd/system/mongodb-28020.service |
启动多实例:
1 2 3 4 5 |
systemctl daemon-reload systemctl start mongodb-28017.service && systemctl enable mongodb-28017.service systemctl start mongodb-28018.service && systemctl enable mongodb-28018.service systemctl start mongodb-28019.service && systemctl enable mongodb-28019.service systemctl start mongodb-28020.service && systemctl enable mongodb-28020.service |