一. 添加Repo源
创建/etc/yum.repos.d/mongodb-org-3.4.repo
添加如下内容
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
二.安装
sudo yum install -y mongodb-org
- 修改/etc/mongod.conf配置
修改端口,bindIp,添加replication配置
net:
port: 10336
bindIp: 127.0.0.1,192.168.177.141
Listen to local interface only, comment to listen on all interfaces.
replication:
oplogSizeMB: 2048
replSetName: mcluster
2.在各个节点启动mongod服务
/etc/init.d/mongod start
3.在第一个节点进入mongo shell
mongo
输入以下内容
rs.initiate()
rs.conf()
rs.add(“192.168.177.142:10336”)
rs.add(“192.168.177.143:10336”)
结果如下
{“ok” : 1}
表示成功
rs.conf()
可以查看集群的状态
rs.status()
三.创建管理员账户
mongo
use admin
db.createUser({ user: “shao”, pwd: “shaodong”, roles: [ { role: “root”, db: “admin” } ] })
四.生产keyfile
推出mongo shell
mkdir -p /usr/local/mongodb/conf
openssl rand -base64 741 > /usr/local/mongodb/conf/mongodb-keyfile
将keyfile拷贝至相应位置,同时也必须拷贝至所有节点
chmod 600 /usr/local/mongodb/conf/mongodb-keyfile
chown -R mongod.mongod /usr/local/mongodb/conf/mongodb-keyfile
五.修改配置文件,在/etc/mongod.conf中添加,所有节点
security:
authorization: enabled
keyFile: /usr/local/mongodb/conf/mongodb-keyfile
六.重启mongod
/etc/init.d/mongo restart