扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
主机信息:
[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"])
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流