下载
https://www.mongodb.com/
节点关系
192.168.181.138 | 192.168.181.139 | 192.168.181.137 | 192.168.180.132 |
---|---|---|---|
mongos 27017 | mongos 27017 | mongos 27017 | 无 |
配置17017节点 | 分片shard1节点37017 | 分片shard2节点47017 | 分片shard3节点57017 |
配置17087节点 | 分片shard1节点37019 | 分片shard2节点47018 | 分片shard3节点57018 |
配置17089节点 | 分片shard1节点37019 | 分片shard2节点47019 | 分片shard3节点57019 |
无 | 分片shard1节点37020仲裁节点 | 分片shard2节点47020仲裁节点 | 分片shard3节点57020仲裁节点 |
配置节点没有仲裁节点
安装配置节点
建立config文件用于放配置文件
- config-17017.conf
#数据库文件地址
dbpath=/data/mongo/config17017
#日志文件位置
logpath=/data/mongo/logs/config17017.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
bind_ip=0.0.0.0
port = 17017
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
- config-17018.conf
#数据库文件地址
dbpath=/data/mongo/config17018
#日志文件位置
logpath=/data/mongo/logs/config17018.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
bind_ip=0.0.0.0
port = 17018
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
- config-17019.conf
#数据库文件地址
dbpath=/data/mongo/config17019
#日志文件位置
logpath=/data/mongo/logs/config17019.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork = true
bind_ip=0.0.0.0
port = 17019
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
注意配置节点不能有仲裁节点的
- 启动
./bin/mongod -f config/config-17017.conf
./bin/mongod -f config/config-17018.conf
./bin/mongod -f config/config-17019.conf
或者新建文件config.sh角本
./bin/mongod -f config/config-17017.conf
./bin/mongod -f config/config-17018.conf
./bin/mongod -f config/config-17019.conf
chmode 750
- 登录17017
./bin/mongo --port 27017
MongoDB shell version v4.1.3
connecting to: mongodb://127.0.0.1:27017/
2020-09-29T10:50:29.804+0800 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed
[root@localhost mongodb-config]# ./bin/mongo --port 17017
MongoDB shell version v4.1.3
connecting to: mongodb://127.0.0.1:17017/
Implicit session: session { "id" : UUID("c34e56d9-e518-468c-bab0-07844ad20312") }
MongoDB server version: 4.1.3
Server has startup warnings:
2020-09-29T10:50:07.197+0800 I CONTROL [initandlisten]
2020-09-29T10:50:07.197+0800 I CONTROL [initandlisten] ** NOTE: This is a development version (4.1.3) of MongoDB.
2020-09-29T10:50:07.197+0800 I CONTROL [initandlisten] ** Not recommended for production.
2020-09-29T10:50:07.197+0800 I CONTROL [initandlisten]
2020-09-29T10:50:07.197+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-09-29T10:50:07.197+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-09-29T10:50:07.197+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-09-29T10:50:07.197+0800 I CONTROL [initandlisten]
2020-09-29T10:50:07.198+0800 I CONTROL [initandlisten]
2020-09-29T10:50:07.198+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-09-29T10:50:07.198+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-09-29T10:50:07.198+0800 I CONTROL [initandlisten]
2020-09-29T10:50:07.198+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-09-29T10:50:07.198+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-09-29T10:50:07.198+0800 I CONTROL [initandlisten]
> rs.status()
{
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
>
- 配置复制集
var cfg ={"_id":"configsvr",
... "protocolVersion" : 1,
... "members":[
... {"_id":1,"host":"192.168.181.138:17017"},
... {"_id":2,"host":"192.168.181.138:17018"},
... {"_id":3,"host":"192.168.181.138:17019"}]
... };
> rs.initiate(cfg)
- 查看
rs.status()
{
"set" : "configsvr",
"date" : ISODate("2020-09-29T03:04:23.046Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1601348662, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1601348662, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1601348662, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1601348662, 1),
"t" : NumberLong(1)
}
},
"lastStableRecoveryTimestamp" : Timestamp(1601348642, 1),
"lastStableCheckpointTimestamp" : Timestamp(1601348642, 1),
"members" : [
{
"_id" : 1,
"name" : "192.168.181.138:17017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 857,
"optime" : {
"ts" : Timestamp(1601348662, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-09-29T03:04:22Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1601348641, 1),
"electionDate" : ISODate("2020-09-29T03:04:01Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.181.138:17018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 33,
"optime" : {
"ts" : Timestamp(1601348647, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1601348647, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-09-29T03:04:07Z"),
"optimeDurableDate" : ISODate("2020-09-29T03:04:07Z"),
"lastHeartbeat" : ISODate("2020-09-29T03:04:21.419Z"),
"lastHeartbeatRecv" : ISODate("2020-09-29T03:04:21.583Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.181.138:17017",
"syncSourceHost" : "192.168.181.138:17017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.181.138:17019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 33,
"optime" : {
"ts" : Timestamp(1601348647, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1601348647, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-09-29T03:04:07Z"),
"optimeDurableDate" : ISODate("2020-09-29T03:04:07Z"),
"lastHeartbeat" : ISODate("2020-09-29T03:04:21.419Z"),
"lastHeartbeatRecv" : ISODate("2020-09-29T03:04:21.581Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.181.138:17017",
"syncSourceHost" : "192.168.181.138:17017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(1601348629, 1),
"electionId" : ObjectId("7fffffff0000000000000001")
},
"lastCommittedOpTime" : Timestamp(1601348662, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1601348662, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1601348662, 1)
}
192.168.181.139分片一
- shard1-37017.conf
dbpath=/data/mongo/shard37017
bind_ip=0.0.0.0
port=37017
fork=true
logpath=/data/mongo/logs/shard1-37017.log
replSet=shard1
shardsvr=true
- shard1-37018.conf
dbpath=/data/mongo/shard37018
bind_ip=0.0.0.0
port=37018
fork=true
logpath=/data/mongo/logs/shard1-37018.log
replSet=shard1
shardsvr=true
- shard1-37019.conf
dbpath=/data/mongo/shard37019
bind_ip=0.0.0.0
port=37019
fork=true
logpath=/data/mongo/logs/shard1-37019.log
replSet=shard1
shardsvr=true
- shard1-37020.conf
dbpath=/data/mongo/shard37020
bind_ip=0.0.0.0
port=37020
fork=true
logpath=/data/mongo/logs/shard1-37020.log
replSet=shard1
shardsvr=true
- 启动
vim shard1.sh
./bin/mongod -f shard/shard1-37017.conf
./bin/mongod -f shard/shard1-37018.conf
./bin/mongod -f shard/shard1-37019.conf
./bin/mongod -f shard/shard1-37020.conf
chmod 750 shard1.sh
./shard1.sh
- 登录37017配置
./bin/mongo --port 37017
MongoDB shell version v4.1.3
connecting to: mongodb://127.0.0.1:37017/
Implicit session: session { "id" : UUID("61e5e0fc-bc2e-48fe-b207-6a3f67ff516f") }
MongoDB server version: 4.1.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2020-09-29T11:20:01.291+0800 I CONTROL [initandlisten]
2020-09-29T11:20:01.291+0800 I CONTROL [initandlisten] ** NOTE: This is a development version (4.1.3) of MongoDB.
2020-09-29T11:20:01.291+0800 I CONTROL [initandlisten] ** Not recommended for production.
2020-09-29T11:20:01.291+0800 I CONTROL [initandlisten]
2020-09-29T11:20:01.291+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-09-29T11:20:01.291+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-09-29T11:20:01.291+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-09-29T11:20:01.291+0800 I CONTROL [initandlisten]
2020-09-29T11:20:01.292+0800 I CONTROL [initandlisten]
2020-09-29T11:20:01.292+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-09-29T11:20:01.292+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-09-29T11:20:01.292+0800 I CONTROL [initandlisten]
2020-09-29T11:20:01.292+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-09-29T11:20:01.292+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-09-29T11:20:01.292+0800 I CONTROL [initandlisten]
> rs.status()
{
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
>
- 配置复制集
var cfg ={"_id":"shard1",
"protocolVersion" : 1,
"members":[
{"_id":1,"host":"192.168.181.139:37017","priority":10},
{"_id":2,"host":"192.168.181.139:37018"},
{"_id":3,"host":"192.168.181.139:37019"},
{"_id":4,"host":"192.168.181.139:37020",arbiterOnly:true}
]};
rs.initiate(cfg)
#rs.reconfig(cfg)
rs.status()
rs.initiate(cfg)
rs.staus()
rs.status()
{
"set" : "shard1",
"date" : ISODate("2020-09-29T05:58:47.421Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1601359124, 1),
"t" : NumberLong(2)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1601359124, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1601359124, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1601359124, 1),
"t" : NumberLong(2)
}
},
"lastStableRecoveryTimestamp" : Timestamp(1601359071, 1),
"lastStableCheckpointTimestamp" : Timestamp(1601359071, 1),
"members" : [
{
"_id" : 1,
"name" : "192.168.181.139:37017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 110,
"optime" : {
"ts" : Timestamp(1601359124, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-09-29T05:58:44Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1601359029, 1),
"electionDate" : ISODate("2020-09-29T05:57:09Z"),
"configVersion" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.181.139:37018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 104,
"optime" : {
"ts" : Timestamp(1601359124, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1601359124, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-09-29T05:58:44Z"),
"optimeDurableDate" : ISODate("2020-09-29T05:58:44Z"),
"lastHeartbeat" : ISODate("2020-09-29T05:58:46.074Z"),
"lastHeartbeatRecv" : ISODate("2020-09-29T05:58:47.108Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 3,
"name" : "192.168.181.139:37019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 102,
"optime" : {
"ts" : Timestamp(1601359124, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1601359124, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-09-29T05:58:44Z"),
"optimeDurableDate" : ISODate("2020-09-29T05:58:44Z"),
"lastHeartbeat" : ISODate("2020-09-29T05:58:46.074Z"),
"lastHeartbeatRecv" : ISODate("2020-09-29T05:58:47.110Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 4,
"name" : "192.168.181.139:37020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 3,
"lastHeartbeat" : ISODate("2020-09-29T05:58:46.102Z"),
"lastHeartbeatRecv" : ISODate("2020-09-29T05:58:46.136Z"),
"pingMs" : NumberLong(4),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 2
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1601359124, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1601359124, 1)
}
- 从库查看
rs.slaveOk()
192.168.181.137-shard2分片
- shard2-47017.conf
dbpath=/data/mongo/share47017
bind_ip=0.0.0.0
port=47017
fork=true
logpath=/data/mongo/logs/shard2-47017.log
replSet=shard2
shardsvr=true
- shard2-47018.conf
dbpath=/data/mongo/share47018
bind_ip=0.0.0.0
port=47018
fork=true
logpath=/data/mongo/logs/shard2-47018.log
replSet=shard2
shardsvr=true
- shard2-47019.conf
dbpath=/data/mongo/share47019
bind_ip=0.0.0.0
port=47019
fork=true
logpath=/data/mongo/logs/shard2-47019.log
replSet=shard2
shardsvr=true
- shard2-47020.conf -仲裁节点
dbpath=/data/mongo/share47020
bind_ip=0.0.0.0
port=47020
fork=true
logpath=/data/mongo/logs/shard2-47020.log
replSet=shard2
shardsvr=true
- 配置角本
vim shard2.sh
./bin/mongod -f shard/shard2-47017.conf
./bin/mongod -f shard/shard2-47018.conf
./bin/mongod -f shard/shard2-47019.conf
./bin/mongod -f shard/shard2-47020.conf
chmod 750 shard2.sh
- 启动
./shard2.sh
- 登录 47017 配置复制集
./bin/mongo --port 47017
var cfg ={"_id":"shard2",
"protocolVersion" : 1,
"members":[
{"_id":1,"host":"192.168.181.137:47017","priority":10},
{"_id":2,"host":"192.168.181.137:47018"},
{"_id":3,"host":"192.168.181.137:47019"},
{"_id":4,"host":"192.168.181.137:47020",arbiterOnly:true}
]};
rs.initiate(cfg)
rs.status()
- 查看
rs.status()
{
"set" : "shard2",
"date" : ISODate("2020-09-29T06:14:38.071Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1601360072, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1601360072, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1601360072, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1601360072, 1),
"t" : NumberLong(1)
}
},
"lastStableRecoveryTimestamp" : Timestamp(1601360042, 1),
"lastStableCheckpointTimestamp" : Timestamp(1601360042, 1),
"members" : [
{
"_id" : 1,
"name" : "192.168.181.137:47017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 191,
"optime" : {
"ts" : Timestamp(1601360072, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-09-29T06:14:32Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1601360041, 1),
"electionDate" : ISODate("2020-09-29T06:14:01Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.181.137:47018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1601360072, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1601360072, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-09-29T06:14:32Z"),
"optimeDurableDate" : ISODate("2020-09-29T06:14:32Z"),
"lastHeartbeat" : ISODate("2020-09-29T06:14:37.048Z"),
"lastHeartbeatRecv" : ISODate("2020-09-29T06:14:37.162Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.181.137:47017",
"syncSourceHost" : "192.168.181.137:47017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "192.168.181.137:47019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 48,
"optime" : {
"ts" : Timestamp(1601360072, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1601360072, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-09-29T06:14:32Z"),
"optimeDurableDate" : ISODate("2020-09-29T06:14:32Z"),
"lastHeartbeat" : ISODate("2020-09-29T06:14:37.048Z"),
"lastHeartbeatRecv" : ISODate("2020-09-29T06:14:37.162Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.181.137:47017",
"syncSourceHost" : "192.168.181.137:47017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 4,
"name" : "192.168.181.137:47020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 48,
"lastHeartbeat" : ISODate("2020-09-29T06:14:37.048Z"),
"lastHeartbeatRecv" : ISODate("2020-09-29T06:14:38.037Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1601360072, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1601360072, 1)
}
192.168.181.132 分片3
- 配置
shard3-57017.conf
dbpath=/data/mongo/shard57017
bind_ip=0.0.0.0
port=57017
fork=true
logpath=/data/mongo/logs/shard3-57017.log
replSet=shard3
shardsvr=true
shard3-57018.conf
dbpath=/data/mongo/shard57018
bind_ip=0.0.0.0
port=57018
fork=true
logpath=/data/mongo/logs/shard3-57018.log
replSet=shard3
shardsvr=true
shard3-57019.conf
dbpath=/data/mongo/shard57019
bind_ip=0.0.0.0
port=57019
fork=true
logpath=/data/mongo/logs/shard3-57019.log
replSet=shard3
shardsvr=true
shard3-57020.conf
dbpath=/data/mongo/shard57020
bind_ip=0.0.0.0
port=57020
fork=true
logpath=/data/mongo/logs/shard3-57020.log
replSet=shard3
shardsvr=true
- 启动
./bin/mongod -f shard/shard3-57017.conf
./bin/mongod -f shard/shard3-57018.conf
./bin/mongod -f shard/shard3-57019.conf
./bin/mongod -f shard/shard3-57020.conf
配置路由节点-138,139,137路由节点
3台机器上都建立
- route-27017.conf
port=27017
bind_ip=0.0.0.0
fork=true
logpath=/data/mongo/route/route.log
configdb=configsvr/192.168.181.138:17017,192.168.181.138:17018,192.168.181.138:17019
- 在3台机器上分别启动
./bin/mongos -f route/route-27017.conf
登录mongos节点
mongo --port 27017
- 查看状态
sh.status() - 添加分片
sh.addShard("shard1/192.168.181.139:37017,192.168.181.139:37018,192.168.181.139:37019,192.168.181.139:37020");
sh.addShard("shard2/192.168.181.137:47017,192.168.181.137:47018,192.168.181.137:47019,192.168.181.137:47020");
sh.addShard("shard3/192.168.181.132:57017,192.168.181.132:57018,192.168.181.132:57019,192.168.181.132:57020");
仲裁节点会自动被去掉
开启分片功能
#为数据库开启分片功能
sh.enableSharding("my_test")
#为指定集合开启分片功能
sh.shardCollection("my_test.my_test_datas",{"name":"hashed"})
测试
use my_test
for(var i=1;i<= 100;i++){
db.my_test_datas.insert({"name":"test"+i,
salary:(Math.random()*20000).toFixed(2)});
}
139 上查看
- 137上查看
安全认证
登录任何一个mongos节点
必须要有admin用户,不然为指定数据库创建的用户一样没效果
use admin
创建管理员帐号
user admin
db.createUser(
{
user:"root",
pwd:"123456",
roles:[{role:"root",db:"admin"}]
})
- 创建指定数据库的用户
lg_resume
db.createUser(
{
user:"lg_resume",
pwd:"abc321",
roles:[{role:"readWrite",db:"lg_resume"}]
})
查看用户
user admin
db.system.users.find().pretty()
创建key
在任意一台上面
生成密钥文件 并修改权限
openssl rand -base64 756 > /data/mongo/key/myKeyFile.file
chmod 600 /data/mongo/key/myKeyFile.file
将这个key复制到其它的所有服务上对应的目录
并设置权限
chmod 600 /data/mongo/key/myKeyFile.file
其它
安装psmisc
yum install psmisc
安装完之后可以使用killall 命令 快速关闭多个进程
killall mongod
killall mongos
所有的配置文件都增加
auth=true
keyFile=/data/mongo/key/myKeyFile.file
路由节点只增加
keyFile=/data/mongo/key/myKeyFile.file
重启
- 配置节点
- shard1节点
- shard2节点
- shard3节点
- 各个mongos节点
登录mongos节点
use admin
db.auth("root","123456")
use lg_resume
db.createUser(
{
user:"lagou_gx",
pwd:"abc321",
roles:[{role:"readWrite",db:"lg_resume"}]
})
sh.enableSharding("lg_resume")
#为指定集合开启分片功能
sh.shardCollection("lg_resume.lg_resume_datas",{"name":"hashed"})
- 测试
for(var i=1;i<= 10;i++){
db.lg_resume_datas.insert({"name":"test"+i,
salary:(Math.random()*20000).toFixed(2)});
}
查看数据分布
db.lg_resume_datas.getShardDistribution()