安装参考:https://www.cnblogs.com/pfnie/articles/6759105.html
mkdir /data1/mongo/log -p
tar -zxvf /data1/software/mongodb-linux-x86_64-3.4.2.tgz /usr/local/
mv /usr/local/mongodb-linux-x86_64-3.4.2 /usr/local/mongodb
mkdir /usr/local/mongodb/etc
vi /usr/local/mongodb/etc/mongodb.conf,内容如下:
dbpath=/data1/mongodb logpath=/data1/mongodb/log/mongodb.log logappend=true port=27017 fork=true auth=true pidfilepath=/usr/local/mongodb/mongo.pid replSet=dsj_dbtest01 #副本集名称 keyFile=/usr/local/mongodb/etc/mongo.pass #群集节点间通信秘钥
ln /usr/local/mongodb/bin/mongod /usr/bin/mongod
启动mongodb:mongod -f /usr/local/mongodb/etc/mongodb.conf
验证:访问 http://mongodb_ip:27017/ 可以打开则表示成功
添加开机启动项:
vim /etc/init.d/mongod,内容如下:
ulimit -SHn 655350 #!/bin/sh # chkconfig: - 64 36 # description:mongod case $1 in start) /usr/local/mongodb/bin/mongod --maxConns 20000 --config /usr/local/mongodb/etc/mongodb.conf ;; stop) /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/etc/mongodb.conf --shutdown ;; status) /usr/local/mongodb/bin/mongo 127.0.0.1:27017/admin --eval "db.stats()" ;; restart) /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/etc/mongodb.conf --shutdown /usr/local/mongodb/bin/mongod --maxConns 20000 --config /usr/local/mongodb/etc/mongodb.conf ;; esac
chmod +x /etc/init.d/mongod
chkconfig mongod on
service mongod start
mongodb配置副本集群集:
1.生成keyfile:
openssl rand -base64 745 > /usr/local/mongodb/etc/mongo.pass
chmod 600 /usr/local/mongodb/etc/mongo.pass
2.mongodb.conf添加keyfile:
vim /usr/local/mongodb/etc/mongodb.conf,添加keyfile,如下:
keyFile=/usr/local/mongodb/etc/mongo.pass
3.进入任一 一台mongo实例配置副本集:
mongo -p 10.10.201.76:27017
use admin
conf = {
_id: "dsj_dbtest01", #此为mogodb.conf文件中配置的replset名称
version: 1,
members: [
{ _id: 0, host : "10.10.21.76:27017" },
{ _id: 1, host : "10.10.21.76:27018" },
{ _id: 2, host : "10.10.21.76:27019" }
]
}
rs.initiate(conf)
rs.slaveOk() #设置副本集可读,不是永久生效,永久生效方法如下:
sudo vim /root/.mongorc.js在文件里面添加一行:rs.slaveOk(); #设置副本集可读
参考:https://blog.csdn.net/pengjunlee/article/details/84101732