①、解压
[root@jianshu ~]# tar -zxvf mongodb-linux-x86_64-4.0.13.tgz -C /usr/local/
[root@jianshu ~]# mv mongodb-linux-x86_64-4.0.13 /usr/local/
[root@jianshu ~]# ln -s /usr/local/mongodb/bin/* /usr/bin/
②、创建伪集群存放目录mdb1、mdb2、mdb3和其子目录logs、data/db、key
[root@jianshu ~]# mkdir -pv /data/mongodbs/
[root@jianshu ~]# cd /data/mongodbs/
[root@jianshu ~]# mkdir -pv mkdir -pv mdb{1,2,3}/{logs,data/db,key}
③、生成key(集群之间的安全认证,3个需一致)
[root@jianshu ~]# openssl rand -base64 745 > /data/mongodbs/mdb1/key/keyfile
[root@jianshu ~]# chmod 600 /data/mongodbs/mdb1/key/keyfile
[root@jianshu ~]# cp /data/mongodbs/mdb1/key/keyfile /data/mongodbs/mdb2/key/
[root@jianshu ~]# cp /data/mongodbs/mdb1/key/keyfile /data/mongodbs/mdb3/key/
④、配置
集群1配置:
[root@jianshu ~]# cat /data/mongodbs/mdb1/mongodb.conf
systemLog:
destination: file
path: "/data/mongodbs/mdb1/logs/mongod.log"
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongodbs/mdb1/mongod.pid"
net:
bindIp: 0.0.0.0
port:27018
maxIncomingConnections: 10000
storage:
dbPath: "/data/mongodbs/mdb1/data/db"
journal:
enabled: true
engine: wiredTiger
directoryPerDB: true
replication:
oplogSizeMB: 50
replSetName: "mdbcluster"
#注:如果配置security的keyFile,则开启了授权认证,则不能添加用户,如果要添加用户可以先暂时关闭
security:
keyFile: "/data/mongodbs/mdb1/key/keyfile"
clusterAuthMode: "keyFile"
集群2配置:
[root@jianshu ~]# cat /data/mongodbs/mdb2/mongodb.conf
systemLog:
destination: file
path: "/data/mongodbs/mdb2/logs/mongod.log"
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongodbs/mdb2/mongod.pid"
net:
bindIp: 0.0.0.0
port:27019
maxIncomingConnections: 10000
storage:
dbPath: "/data/mongodbs/mdb2/data/db"
journal:
enabled: true
engine: wiredTiger
directoryPerDB: true
replication:
oplogSizeMB: 50
replSetName: "mdbcluster"
#注:如果配置security的keyFile,则开启了授权认证,则不能添加用户,如果要添加用户可以先暂时关闭
security:
keyFile: "/data/mongodbs/mdb3/key/keyfile"
clusterAuthMode: "keyFile"
集群3配置:
[root@jianshu ~]# cat /data/mongodbs/mdb3/mongodb.conf
systemLog:
destination: file
path: "/data/mongodbs/mdb3/logs/mongod.log"
logAppend: true
processManagement:
fork: true
pidFilePath: "/data/mongodbs/mdb3/mongod.pid"
net:
bindIp: 0.0.0.0
port:27020
maxIncomingConnections: 10000
storage:
dbPath: "/data/mongodbs/mdb3/data/db"
journal:
enabled: true
engine: wiredTiger
directoryPerDB: true
replication:
oplogSizeMB: 50
replSetName: "mdbcluster"
#注:如果配置security的keyFile,则开启了授权认证,则不能添加用户,如果要添加用户可以先暂时关闭
security:
keyFile: "/data/mongodbs/mdb3/key/keyfile"
clusterAuthMode: "keyFile"
⑤、启动3个集群集群
[root@jianshu ~]# /usr/local/mongodb/bin/mongod -f /data/mongodbs/mdb1/mongodb.conf
[root@jianshu ~]# /usr/local/mongodb/bin/mongod -f /data/mongodbs/mdb2/mongodb.conf
[root@jianshu ~]# /usr/local/mongodb/bin/mongod -f /data/mongodbs/mdb3/mongodb.conf
⑥、查看启动状态
[root@jianshu ~]# netstat -lnpt | egrep "270"
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 10762/mongod
tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 10827/mongod
tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN 10869/mongod
⑦、配置集群
[root@jianshu ~]# mongo -port 27018
MongoDB shell version v4.0.13
connecting to: mongodb://127.0.0.1:27018/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("da5e47a0-319d-4081-ae47-2c58ace5add2") }
MongoDB server version: 4.0.13
>use admin
switched to db admin
>config={ _id:"mdbcluster", members:[ {"_id" : 0, "host" : "127.0.0.1:27018"}, {"_id" : 1, "host" : "127.0.0.1:27019"}, {"_id" : 2, "host" : "127.0.0.1:27020"} ] }
{
"_id" : "mdbcluster",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27018"
},
{
"_id" : 1,
"host" : "127.0.0.1:27019"
},
{
"_id" : 2,
"host" : "127.0.0.1:27020"
}
]
}
>rs.initiate(config);
{ "ok" : 1 }
mdbcluster:SECONDARY>rs.status();
{
"set" : "mdbcluster",
"date" : ISODate("2020-10-23T06:34:39.614Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1603434879, 5),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1603434879, 5),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1603434879, 5),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1603434879, 5),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1603434879, 1),
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 110,
"optime" : {
"ts" : Timestamp(1603434879, 5),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-10-23T06:34:39Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1603434878, 1),
"electionDate" : ISODate("2020-10-23T06:34:38Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 12,
"optime" : {
"ts" : Timestamp(1603434867, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1603434867, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2020-10-23T06:34:27Z"),
"optimeDurableDate" : ISODate("2020-10-23T06:34:27Z"),
"lastHeartbeat" : ISODate("2020-10-23T06:34:38.161Z"),
"lastHeartbeatRecv" : ISODate("2020-10-23T06:34:39.323Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 12,
"optime" : {
"ts" : Timestamp(1603434867, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1603434867, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2020-10-23T06:34:27Z"),
"optimeDurableDate" : ISODate("2020-10-23T06:34:27Z"),
"lastHeartbeat" : ISODate("2020-10-23T06:34:38.161Z"),
"lastHeartbeatRecv" : ISODate("2020-10-23T06:34:39.324Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1603434879, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1603434879, 5),
"signature" : {
"hash" : BinData(0,"La+1SisigTTB+RW6X6nguiyVx5U="),
"keyId" : NumberLong("6886700366570717185")
}
}
}
⑧、附:登录mongo脚本和加入系统命令
[root@jianshu ~]# cat /root/script/mongo-login-27018.sh
/usr/local/mongodb/bin/mongo -port 27018
[root@jianshu ~]# cat /root/script/mongo-login-27019.sh
/usr/local/mongodb/bin/mongo -port 27019
[root@jianshu ~]# cat /root/script/mongo-login-27020.sh
/usr/local/mongodb/bin/mongo -port 27020
[root@jianshu ~]# ln -s /root/script/mongo-login-27018.sh /usr/bin/27018mongo
[root@jianshu ~]# ln -s /root/script/mongo-login-27019.sh /usr/bin/27019mongo
[root@jianshu ~]# ln -s /root/script/mongo-login-27020.sh /usr/bin/27020mongo
⑨、附:创建用户、创建管理员用户、添加数据
样例1,创建普通用户:
> use test
switched to db test
> db.createUser(
... {
... user: "test",
... pwd: "test",
... roles: [ "readWrite"]
... }
... );
Successfully added user: { "user" : "member", "roles" : [ "readWrite" ] }
>
样例2,创建管理员用户:
> use admin
switched to db admin
> db.createUser(
{
user: "root",
pwd: "root",
roles: ["dbAdmin"]
}
);
Successfully added user: { "user" : "root", "roles" : [ "dbAdmin" ] }
>
样例3,添加表数据:
> use test
>db.createCollection('table1');
>db.table1.insert({name:'amu',age:22})
> db.table1.find();
网友评论