1.下载和解压
在官网下载:
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.22.tgz
上传到服务器,解压,然后配置环境变量:
2.参数设置如下:
bind_ip = 0.0.0.0
port = 27017
fork = true
pidfilepath = /data0/mongodb/data/mongodb.pid
logpath = /data0/mongodb/log/mongodb.log
dbpath = /data0/mongodb/data
journal = true
storageEngine=wiredTiger
httpinterface = true
#auth=true
#keyFile= /data0/mongodb/keyfile/keyfile_cms
#replSet=cms
#rest=true
#logappend=true
#oplogSize=16
3.在所有节点添加副本集启动 --replSet cms
/data0/mongodb/bin/mongod --fork --replSet cms --config /data0/mongodb/mongod.conf
/data/software/mongodb/bin/mongod --fork --replSet icloud --config /data/apps/mongodb/27017/config/mongod.conf
/data/software/mongodb/bin/mongod --fork --replSet icloud --config /data/apps/mongodb/27018/config/mongod.conf
/data/software/mongodb/bin/mongod --fork --replSet icloud --config /data/apps/mongodb/27019/config/mongod.conf
下面是在其中一台服务器执行命令:
config = { _id:"cms", members:
[
{_id:0,host:"10.0.4.94:27017"},
{_id:1,host:"10.0.4.95:27017"},
{_id:2,host:"10.0.4.96:27017"}
]
}
rs.initiate(config);
mongodb3.4必须使用下面命令,上面的命令会错误:
config = { _id:“icloud”, members: [ {_id:0,host:“172.16.16.202:27017”}, {_id:1,host:“172.16.16.202:27018”}, {_id:2,host:“172.16.16.202:27019”} ] }
4.创建用户和赋予权限:
use admin
db.createUser(
{
user: "admin",
pwd: "123456",
roles:
[
{ role: "readWrite", db: "config" },
"clusterAdmin"
]
}
)
db.grantRolesToUser( "admin",[ { role: "root", db: "admin" } ])
5.配置副本集
config = { _id:"cms", members:
[
{_id:0,host:"10.0.4.94:27017"},
{_id:1,host:"10.0.4.95:27017"},
{_id:2,host:"10.0.4.96:27017"}
]
}
rs.initiate(config);
下面命令生成密钥:
openssl rand -base64 745 >/data0/mongodb/keyfile/keyfile_cms
openssl rand -base64 745 >/data/apps/mongodb/27017/keyfile/keyfile_icloud
把密码拷贝到其他机器:
scp …
6.关闭数据库实例,先停止从库,然后停止主库,配置文件去除下面注释:
auth=true
keyFile= /data0/mongodb/keyfile/keyfile_cms
replSet=cms
rest=true
logappend=true
7.启动数据库
mongod --fork --config /data0/mongodb/mongod.conf
参考文章:
https://docs.mongodb.com/v3.2/reference/method/db.currentOp/index.html