您好,登錄后才能下訂單哦!
主機信息:
[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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。