配置mongodb副本集
主机信息:
[root@my2 db]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.56.101 my1.ml.com my1
192.168.56.102 my2.ml.com my2
192.168.56.103 my3.ml.com my3
关闭大页内存:
echo never >> /sys/kernel/mm/transparent_hugepage/enabled
echo never >> /sys/kernel/mm/transparent_hugepage/defrag
在每个主机上面创建目录:
mkdir /data/db -p
在每个主机上面编辑配置文件:
vim /data/db/MongoDB.conf
storage:
dbPath: "/data/db"
directoryPerDB: true
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
systemLog:
destination: file
path: "/data/db/mongodb.log"
logRotate: rename
logAppend: true
timeStampFormat: iso8601-local
operationProfiling:
slowOpThresholdMs: 200
mode: slowOp
replication:
oplogSizeMB: 10240
replSetName: "repsetzhou"
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27017
security:
keyFile: "/data/db/mongodb-keyfile"
authorization: "enabled"
生成认证文件:
[root@my1 db]# openssl rand -base64 800 > /data/db/mongodb-keyfile
scp到其他节点:
[root@my1 db]# scp /data/db/mongodb-keyfile 192.168.56.102:/data/db/
mongodb-keyfile
100% 1390 1.4KB/s 00:00
[root@my1 db]# scp /data/db/mongodb-keyfile 192.168.56.103:/data/db/
mongodb-keyfile
启动三个节点上的mongodb:
/usr/local/mongodb/bin/mongod --config /data/db/mongodb.conf
登录192.168.56.101上的mongodb进行配置初始化复制集:
/usr/local/mongodb/bin/mongo
> use admin
switched to db admin
配置复制集:
config = {
_id : "repsetzhou",
members : [
{_id : 0, host : "192.168.56.101:27017"},
{_id : 1, host : "192.168.56.102:27017"},
{_id : 2, host : "192.168.56.103:27017"},
]
}
初始化配置集:
rs.initiate(config)
操作:
> config = {
... _id : "repsetzhou",
... members : [
... {_id : 0, host : "192.168.56.101:27017"},
... {_id : 1, host : "192.168.56.102:27017"},
... {_id : 2, host : "192.168.56.103:27017"},
... ]
... }
{
"_id" : "repsetzhou",
"members" : [
{
"_id" : 0,
"host" : "192.168.56.101:27017"
},
{
"_id" : 1,
"host" : "192.168.56.102:27017"
},
{
"_id" : 2,
"host" : "192.168.56.103:27017"
}
]
}
>
> rs.initiate(config)
{ "ok" : 1 }
登录确认:
repsetzhou:PRIMARY> use admin
switched to db admin
repsetzhou:PRIMARY> rs.config()
{
"_id" : "repsetzhou",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "192.168.56.101:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.56.102:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.56.103:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5b9724ff670e7d5d4d9c6c7e")
}
}
repsetzhou:PRIMARY>
添加db认证:
use admin
db.createUser(
{
user:"admin",
pwd:"admin123",
roles:[{role:"userAdminAnyDatabase",db:"admin"}]
}
);
repsetzhou:PRIMARY> use admin
switched to db admin
repsetzhou:PRIMARY> db.createUser(
... {
... user:"admin",
... pwd:"admin123",
... roles:[{role:"userAdminAnyDatabase",db:"admin"}]
... }
... );
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
授权:
db.grantRolesToUser("admin", ["clusterAdmin"])
db.grantRolesToUser("admin", ["readWriteAnyDatabase"])
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站建设、网站制作、龙马潭网络推广、成都微信小程序、龙马潭网络营销、龙马潭企业策划、龙马潭品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供龙马潭建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
网页标题:配置mongodb副本集
本文来源:http://pwwzsj.com/article/jhciji.html