主机信息:
[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"])
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。