本文为mongoDB sharding 4.0 分片的详细搭建配置文档。

## 服务器规划

  • 10.204.11.124
  • 10.204.11.125
  • 10.204.11.126

目录规划

mongos 27017
/data0/mongodb/mongos
configsvr 28017
/data0/mongodb/configsvr
shard1 29017
/data0/mongodb/shard1
shard2 29018
/data0/mongodb/shard2
shard3 29019
/data0/mongodb/shard3

mongodb
├── configsvr
│   └── 28017_configsvr
│   ├── data
│   ├── keyFile
│   ├── log
│   └── mongod_28017.pid
├── mongos
│   └── 27017_mongos
│   ├── data
│   ├── keyFile
│   ├── log
│   └── mongod_27017.pid
├── shard1
│   └── 29017_shard
│   ├── data
│   ├── keyFile
│   ├── log
│   └── mongod_29017.pid
├── shard2
│   └── 29018_shard
│   ├── data
│   ├── keyFile
│   ├── log
│   └── mongod_29018.pid
└── shard3
└── 29019_shard
├── data
├── keyFile
├── lk
├── log
└── mongod_29019.pid

集群规划

ServerName Role1 Role2 Role3 Role4 Role5
server2 shard1-secondary shard2-secondary shard3-secondary mongos-2 configsvr-2
server1 shard1-primary shard2-primary shard3-primary mongos-1 configsvr-1
server3 shard1-secondary shard2-secondary shard3-secondary mongos-2 configsvr-2

安装mongodb

1
2
3
4
5
6
tar zxvf /data0/packages/percona-server-mongodb-3.6.8-2.0-centos6-x86_64.tar.gz  -C /usr/local/;
ln -sf /usr/local/percona-server-mongodb-3.6.8-2.0-centos6-x86_64 /usr/local/mongodb;
ln -sf /usr/local/mongodb/bin/* /usr/bin/;

mkdir /data0/mongodb/{mongos,configsvr,shard1,shard2,shard3} -p ;

搭建 shard

搭建三个shard(副本集)

s1 s2 s3 分别创建对应文件夹

1
2
3
mkdir /data0/mongodb/shard1/29017_shard/{data,log} -p;
mkdir /data0/mongodb/shard2/29018_shard/{data,log} -p;
mkdir /data0/mongodb/shard3/29019_shard/{data,log} -p;

生成配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
cat << EOF >/etc/mongodb_shard1_29017.conf 
## base on mongodb4.0
# https://docs.mongodb.com/manual/reference/configuration-options/#systemlog-options
#Author xxj
#Date: 2018-11-16

systemLog:
verbosity: 0 #default 0 ,debug:1-5
quiet: false
traceAllExceptions: true
#syslogFacility: <string>
path: /data0/mongodb/shard1/29017_shard/log/mongod.log
logAppend: true
logRotate: rename #default
destination: file
timeStampFormat: iso8601-local #default
component:
accessControl:
verbosity: 0 #default 0 ,debug:1-5
command:
verbosity: 0
control:
verbosity: 0
ftdc:
verbosity: 0
geo:
verbosity: 0
index:
verbosity: 0
network:
verbosity: 0
query:
verbosity: 0
replication:
verbosity: 0
heartbeats:
verbosity: 0
rollback:
verbosity: 0
sharding:
verbosity: 0
storage:
verbosity: 0
journal:
verbosity: 0
#recovery: #new in 4.0
#verbosity: 0 #new in 4.0
write:
verbosity: 0
processManagement:
fork: true
pidFilePath: /data0/mongodb/shard1/29017_shard/mongod_29017.pid
#timeZoneInfo: <string>
#cloud: #New in version 4.0. Available for MongoDB Community Edition.
# monitoring:
# free:
# state: runtime #runtime(default) ,on ,off
# #tag: <string>
net:
port: 29017
bindIp: 127.0.0.1, 10.204.11.124
#bindIpAll: false #false(default)
maxIncomingConnections: 50000
wireObjectCheck: true
#ipv6: <boolean>
unixDomainSocket:
enabled: true #true(default)
pathPrefix: /tmp #default: /tmp
filePermissions: 0700 #default: 0700
#ssl:
#sslOnNormalPorts: <boolean> # deprecated since 2.6
#mode: <string>
#PEMKeyFile: <string>
#PEMKeyPassword: <string>
#certificateSelector: <string>
#clusterCertificateSelector: <string>
#clusterFile: <string>
#clusterPassword: <string>
#CAFile: <string>
#clusterCAFile: <string>
#CRLFile: <string>
#allowConnectionsWithoutCertificates: <boolean>
#allowInvalidCertificates: <boolean>
#allowInvalidHostnames: <boolean>
#disabledProtocols: <string>
#FIPSMode: <boolean>
compression:
compressors: snappy #snappy(default),zlib
serviceExecutor: synchronous #synchronous(default),adaptive
security:
keyFile: /data0/mongodb/shard1/29017_shard/keyFile
clusterAuthMode: keyFile #keyFile sendKeyFile sendX509 x509
authorization: enabled #disabled(default) enabled
transitionToAuth: false #false(default)
javascriptEnabled: true #true(default)
#redactClientLogData: false # false(default),Available in MongoDB Enterprise only.
storage:
dbPath: /data0/mongodb/shard1/29017_shard/data
#indexBuildRetry: true #true(default) cannot be used in conjunction with replication.replSetName
#repairPath: <string> #for MMAPv1 only
journal:
enabled: true #true(default)
commitIntervalMs: 100 #wt default:100
directoryPerDB: true
syncPeriodSecs: 60 #default 60,Do not change this value on production systems
engine: wiredTiger
#mmapv1:
# preallocDataFiles: <boolean>
# nsSize: <int>
# quota:
# enforced: <boolean>
# maxFilesPerDB: <int>
# smallFiles: <boolean>
# journal:
# debugFlags: <int>
# commitIntervalMs: <num>
wiredTiger:
engineConfig:
cacheSizeGB: 1 #10 for prod
journalCompressor: snappy #none snappy(default) zlib
directoryForIndexes: false #false(default)
collectionConfig:
blockCompressor: snappy #none snappy(default) zlib
indexConfig:
prefixCompression: true #true(default)
#inMemory:
# engineConfig:
# inMemorySizeGB: <number>
operationProfiling:
mode: slowOp # off(default) slowOp all
slowOpThresholdMs: 1000 #default 100
#slowOpSampleRate: <double> #0-1,default 1
replication:
oplogSizeMB: 20480 #20G
replSetName: repl_shard1
#secondaryIndexPrefetch: <string> #for mmapv1 only
enableMajorityReadConcern: true #true(default) from 3.6 ,In general,avoid disable it
sharding:
clusterRole: shardsvr # configsvr shardsvr
archiveMovedChunks: false #Starting in 3.2,false(default)
#auditLog:
# destination: <string>
# format: <string>
# path: <string>
# filter: <string>
#snmp:
# subagent: <boolean>
# master: <boolean>

##### mongos-only Options begin #####
#replication:
# localPingThresholdMs: 15 #default: 15
#sharding:
# configDB: <string> #<configReplSetName>/cfg1.example.net:27017, cfg2.example.net:27017,...
##### mongos-only Options end #####
EOF


创建 keyFile

(分片集群中所有节点的keyfile要一致)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 cat << EOF > /data0/mongodb/shard1/29017_shard/keyFile 
o6N9mXF59nASL2WVpnjSkI9mW4q3TUE7gadL35POm29fbHHlcOdo47KYuVENEKr9
p7iuymjlz1/pxSXZoFTAGv7Mn7fLNeInlF4EQyVjMsW68f4E0acQlArZ47uUublm
63NM6Bc6awHzgWd/blJqLzL+GhZ9tngvopHkGTT5hW64MP8YscrQmjsWvJp4AzA6
coakn98MG2VvOnDWNIkPjFf56Zhw26JuDpMDQWl5Gob6M8AtqObaL3apjm+MH4uN
F87DwqoQjue53YPSH7GxXe+cZ7ywruR0DZ3vAoFP3LgjnLh8dupz7zzjPi3n+idM
FNaOzJeBadYItvrdxsUyD/Q8PG3fmNiN+cM8tkLvhuSRdRbaoJ3QVw86BXRsqjlY
Njxf9oRb/CcN/HYrRq67jLcqRC5H6t6aVCTqKxPv8wRK3Exn+G08K/UukQ3Tru8f
tNtE4/+B33XNEaqYpl/Gpe8wS+xUG4QFWRbOoQAun4hGllIQObGAalklATZYPnIB
LtIBcH04myTIYY9lrtTvisSP39pkXbjVkdhMn/LBvZpEpiz1MKwK3+gCvurMshYk
Q/gdaddyLvdZYFiWCkNFvHmvCRt5BlBDFmofhUWI30qgoRPeflx+E0MH8VYmNBCM
zmK1omF4oVfZei1POLg0OT6r5K9KcHLkePMSYLkrJLjoaKnUy+GAMsLwuJqmgHZR
sbtnWrk+D3lxVogVAg4hNbpH5a7RjtH30DEFVkiUTK43LkQm9c5BbCT/C3b26KHO
D7z43Yr87h7iMSFkNWwOn+YGw950dolEtVZcj2FdMu3dYZsVlUSweJtSAt2CuBYN
jGYsr5OGNb+Haht4GFDrZUGT4YNT42JVwYC/7hNuvWx9ZcwpgDA4RNetvGaR0lpq
6ii29RLF5s1ge9Ec1U7FlTbVbaAdeB8vBFqCvdx8PygvKLhjGYj1gabaAdPCbJsX
b/txg5KecwpVrpSLs1bRiWUEu+55
EOF

或者如下生成

1
2
3
openssl rand -base64 756 >  /data0/mongodb/shard1/29017_shard/keyFile 

chmod 400 /data0/mongodb/shard1/29017_shard/keyFile

其他两个节点如法炮制

三节点启动mongod

1
mongod -f /etc/mongodb_shard1_29017.conf 

在任一节点上执行(一般在想要成为primary的节点操作)

1
2
3
4
5
6
7
mongo --host localhost --port 29017  
config = { _id:"repl_shard1", members:[
{_id:0,host:"10.204.11.124:29017"},
{_id:1,host:"10.204.11.125:29017"},
{_id:2,host:"10.204.11.126:29017"}]
}
rs.initiate(config)

#或者

1
2
3
4
5
6
7
8
rs.initiate( {
_id : "repl_shard1",
members: [
{ _id: 0, host: "10.204.11.124:29017" },
{ _id: 1, host: "10.204.11.125:29017" },
{ _id: 2, host: "10.204.11.126:29017" }
]
})

创建账号

1
mongo --host localhost --port 28017 ;

管理员账号创建

1
2
use admin;
db.createUser({user:'root',pwd:'******', roles:[{role:'root', db:'admin'}]})

用户用户(需要在指定的db下创建)

1
2
3
4
5
6
7
8
9
10
use bch_hcrm_abc_com;
db.demo.insert({"age":1});

db.createUser(
{
user:"hcrm_bch_user",
pwd:"ms8Gb5xmO9ceF6FyV",
roles:[{role:"readWrite",db:"bch_hcrm_abc_com"}]
}
)

搭建 configservr

s1 s2 s3 分别创建对应文件夹

1
mkdir /data0/mongodb/configsvr/28017_configsvr/{data,log} -p;

生成配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 cat << EOF >/etc/mongodb_shard1_29017.conf 
## base on mongodb4.0
# https://docs.mongodb.com/manual/reference/configuration-options/#systemlog-options
#Author xxj
#Date: 2018-11-16

systemLog:
verbosity: 0 #default 0 ,debug:1-5
quiet: false
traceAllExceptions: true
#syslogFacility: <string>
path: /data0/mongodb/shard1/29017_shard/log/mongod.log
logAppend: true
logRotate: rename #default
destination: file
timeStampFormat: iso8601-local #default
component:
accessControl:
verbosity: 0 #default 0 ,debug:1-5
command:
verbosity: 0
control:
verbosity: 0
ftdc:
verbosity: 0
geo:
verbosity: 0
index:
verbosity: 0
network:
verbosity: 0
query:
verbosity: 0
replication:
verbosity: 0
heartbeats:
verbosity: 0
rollback:
verbosity: 0
sharding:
verbosity: 0
storage:
verbosity: 0
journal:
verbosity: 0
#recovery: #new in 4.0
#verbosity: 0 #new in 4.0
write:
verbosity: 0
processManagement:
fork: true
pidFilePath: /data0/mongodb/shard1/29017_shard/mongod_29017.pid
#timeZoneInfo: <string>
#cloud: #New in version 4.0. Available for MongoDB Community Edition.
# monitoring:
# free:
# state: runtime #runtime(default) ,on ,off
# #tag: <string>
net:
port: 29017
bindIp: 127.0.0.1, 10.204.11.124
#bindIpAll: false #false(default)
maxIncomingConnections: 50000
wireObjectCheck: true
#ipv6: <boolean>
unixDomainSocket:
enabled: true #true(default)
pathPrefix: /tmp #default: /tmp
filePermissions: 0700 #default: 0700
#ssl:
#sslOnNormalPorts: <boolean> # deprecated since 2.6
#mode: <string>
#PEMKeyFile: <string>
#PEMKeyPassword: <string>
#certificateSelector: <string>
#clusterCertificateSelector: <string>
#clusterFile: <string>
#clusterPassword: <string>
#CAFile: <string>
#clusterCAFile: <string>
#CRLFile: <string>
#allowConnectionsWithoutCertificates: <boolean>
#allowInvalidCertificates: <boolean>
#allowInvalidHostnames: <boolean>
#disabledProtocols: <string>
#FIPSMode: <boolean>
compression:
compressors: snappy #snappy(default),zlib
serviceExecutor: synchronous #synchronous(default),adaptive
security:
keyFile: /data0/mongodb/shard1/29017_shard/keyFile
clusterAuthMode: keyFile #keyFile sendKeyFile sendX509 x509
authorization: enabled #disabled(default) enabled
transitionToAuth: false #false(default)
javascriptEnabled: true #true(default)
#redactClientLogData: false # false(default),Available in MongoDB Enterprise only.
storage:
dbPath: /data0/mongodb/shard1/29017_shard/data
#indexBuildRetry: true #true(default) cannot be used in conjunction with replication.replSetName
#repairPath: <string> #for MMAPv1 only
journal:
enabled: true #true(default)
commitIntervalMs: 100 #wt default:100
directoryPerDB: true
syncPeriodSecs: 60 #default 60,Do not change this value on production systems
engine: wiredTiger
#mmapv1:
# preallocDataFiles: <boolean>
# nsSize: <int>
# quota:
# enforced: <boolean>
# maxFilesPerDB: <int>
# smallFiles: <boolean>
# journal:
# debugFlags: <int>
# commitIntervalMs: <num>
wiredTiger:
engineConfig:
cacheSizeGB: 1 #10 for prod
journalCompressor: snappy #none snappy(default) zlib
directoryForIndexes: false #false(default)
collectionConfig:
blockCompressor: snappy #none snappy(default) zlib
indexConfig:
prefixCompression: true #true(default)
#inMemory:
# engineConfig:
# inMemorySizeGB: <number>
operationProfiling:
mode: slowOp # off(default) slowOp all
slowOpThresholdMs: 1000 #default 100
#slowOpSampleRate: <double> #0-1,default 1
replication:
oplogSizeMB: 20480 #20G
replSetName: repl_shard1
#secondaryIndexPrefetch: <string> #for mmapv1 only
enableMajorityReadConcern: true #true(default) from 3.6 ,In general,avoid disable it
sharding:
clusterRole: configsvr # configsvr shardsvr
archiveMovedChunks: false #Starting in 3.2,false(default)
#auditLog:
# destination: <string>
# format: <string>
# path: <string>
# filter: <string>
#snmp:
# subagent: <boolean>
# master: <boolean>

##### mongos-only Options begin #####
#replication:
# localPingThresholdMs: 15 #default: 15
#sharding:
# configDB: <string> #<configReplSetName>/cfg1.example.net:27017, cfg2.example.net:27017,...
##### mongos-only Options end #####
EOF

创建 keyFile

(分片集群中所有节点的keyfile要一致)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 cat << EOF > /data0/mongodb/configsvr/28017_configsvr/keyFile 
o6N9mXF59nASL2WVpnjSkI9mW4q3TUE7gadL35POm29fbHHlcOdo47KYuVENEKr9
p7iuymjlz1/pxSXZoFTAGv7Mn7fLNeInlF4EQyVjMsW68f4E0acQlArZ47uUublm
63NM6Bc6awHzgWd/blJqLzL+GhZ9tngvopHkGTT5hW64MP8YscrQmjsWvJp4AzA6
coakn98MG2VvOnDWNIkPjFf56Zhw26JuDpMDQWl5Gob6M8AtqObaL3apjm+MH4uN
F87DwqoQjue53YPSH7GxXe+cZ7ywruR0DZ3vAoFP3LgjnLh8dupz7zzjPi3n+idM
FNaOzJeBadYItvrdxsUyD/Q8PG3fmNiN+cM8tkLvhuSRdRbaoJ3QVw86BXRsqjlY
Njxf9oRb/CcN/HYrRq67jLcqRC5H6t6aVCTqKxPv8wRK3Exn+G08K/UukQ3Tru8f
tNtE4/+B33XNEaqYpl/Gpe8wS+xUG4QFWRbOoQAun4hGllIQObGAalklATZYPnIB
LtIBcH04myTIYY9lrtTvisSP39pkXbjVkdhMn/LBvZpEpiz1MKwK3+gCvurMshYk
Q/gdaddyLvdZYFiWCkNFvHmvCRt5BlBDFmofhUWI30qgoRPeflx+E0MH8VYmNBCM
zmK1omF4oVfZei1POLg0OT6r5K9KcHLkePMSYLkrJLjoaKnUy+GAMsLwuJqmgHZR
sbtnWrk+D3lxVogVAg4hNbpH5a7RjtH30DEFVkiUTK43LkQm9c5BbCT/C3b26KHO
D7z43Yr87h7iMSFkNWwOn+YGw950dolEtVZcj2FdMu3dYZsVlUSweJtSAt2CuBYN
jGYsr5OGNb+Haht4GFDrZUGT4YNT42JVwYC/7hNuvWx9ZcwpgDA4RNetvGaR0lpq
6ii29RLF5s1ge9Ec1U7FlTbVbaAdeB8vBFqCvdx8PygvKLhjGYj1gabaAdPCbJsX
b/txg5KecwpVrpSLs1bRiWUEu+55
EOF

或者如下生成

1
2
3
openssl rand -base64 756 >  /data0/mongodb/configsvr/28017_configsvr/keyFile 

chmod 400 /data0/mongodb/configsvr/28017_configsvr/keyFile

启动mongod

1
mongod -f  /etc/mongodb_configsvr_28017.conf 

#在任一节点上执行(一般在想要成为primary的节点操作)

1
2
3
4
5
6
7
8
9
rs.initiate( {
_id : "repl_configsvr",
configsvr: true,
members: [
{ _id: 0, host: "10.204.11.124:28017" },
{ _id: 1, host: "10.204.11.125:28017" },
{ _id: 2, host: "10.204.11.126:28017" }
]
})

创建账号

1
mongo --host localhost --port 28017 ;

管理员账号创建

1
2
use admin;
db.createUser({user:'root',pwd:'******', roles:[{role:'root', db:'admin'}]})

搭建 mongos

s1 s2 s3 分别创建对应文件夹

1
mkdir  /data0/mongodb/mongos/27017_mongos/{data,log} -p;

生成配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
 cat << EOF >/etc/mongodb_mongos_27017.conf 
## base on mongodb4.0
# https://docs.mongodb.com/manual/reference/configuration-options/#systemlog-options
#Author xxj
#Date: 2018-11-16

systemLog:
verbosity: 0 #default 0 ,debug:1-5
quiet: false
traceAllExceptions: true
#syslogFacility: <string>
path: /data0/mongodb/mongos/27017_mongos/log/mongod.log
logAppend: true
logRotate: rename #default
destination: file
timeStampFormat: iso8601-local #default
component:
accessControl:
verbosity: 0 #default 0 ,debug:1-5
command:
verbosity: 0
control:
verbosity: 0
ftdc:
verbosity: 0
geo:
verbosity: 0
index:
verbosity: 0
network:
verbosity: 0
query:
verbosity: 0
replication:
verbosity: 0
heartbeats:
verbosity: 0
rollback:
verbosity: 0
sharding:
verbosity: 0
storage:
verbosity: 0
journal:
verbosity: 0
#recovery: new in 4.0
#verbosity: 0 # new in 4.0
write:
verbosity: 0
processManagement:
fork: true
pidFilePath: /data0/mongodb/mongos/27017_mongos/mongod_27017.pid
#timeZoneInfo: <string>
#cloud: #New in version 4.0. Available for MongoDB Community Edition.
# monitoring:
# free:
# state: runtime #runtime(default) ,on ,off
# #tag: <string>
net:
port: 27017
bindIp: 127.0.0.1, 10.204.11.124
#bindIpAll: false #false(default)
maxIncomingConnections: 50000
wireObjectCheck: true
#ipv6: <boolean>
unixDomainSocket:
enabled: true #true(default)
pathPrefix: /tmp #default: /tmp
filePermissions: 0700 #default: 0700
#ssl:
#sslOnNormalPorts: <boolean> # deprecated since 2.6
#mode: <string>
#PEMKeyFile: <string>
#PEMKeyPassword: <string>
#certificateSelector: <string>
#clusterCertificateSelector: <string>
#clusterFile: <string>
#clusterPassword: <string>
#CAFile: <string>
#clusterCAFile: <string>
#CRLFile: <string>
#allowConnectionsWithoutCertificates: <boolean>
#allowInvalidCertificates: <boolean>
#allowInvalidHostnames: <boolean>
#disabledProtocols: <string>
#FIPSMode: <boolean>
compression:
compressors: snappy #snappy(default),zlib
serviceExecutor: synchronous #synchronous(default),adaptive
security:
keyFile: /data0/mongodb/mongos/27017_mongos/keyFile
clusterAuthMode: keyFile #keyFile sendKeyFile sendX509 x509
transitionToAuth: false #false(default)
#redactClientLogData: false # false(default),Available in MongoDB Enterprise only.
#operationProfiling:
#slowOpThresholdMs: 1000 #default 100 #<if 4.0 available for mongod only,>=4.0 available for mongod and mongos
#slowOpSampleRate: <double> #0-1,default 1
#replication:
#oplogSizeMB: 20480 #20G
#replSetName: repl_configsrv
#secondaryIndexPrefetch: <string> #for mmapv1 only
#enableMajorityReadConcern: true #true(default) from 3.6 ,In general,avoid disable it
#sharding:
#clusterRole: configsvr # configsvr shardsvr
#archiveMovedChunks: false #Starting in 3.2,false(default)
#auditLog:
# destination: <string>
# format: <string>
# path: <string>
# filter: <string>
#snmp:
# subagent: <boolean>
# master: <boolean>

##### mongos-only Options begin #####
replication:
localPingThresholdMs: 15 #default: 15
sharding:
#<configReplSetName>/cfg1.example.net:27017, cfg2.example.net:27017,...
configDB: repl_configsvr/10.204.11.124:28017,10.204.11.125:28017,10.204.11.126:28017
#configDB: repl_configsvr/d15070009.grid.house.sina.com.cn:28017,d15070007.grid.house.sina.com.cn:28017,d15070011.grid.house.sina.com.cn:28017
##### mongos-only Options end #####
EOF

创建 keyFile

(分片集群中所有节点的keyfile要一致)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 cat << EOF > /data0/mongodb/mongos/27017_mongos/keyFile 
o6N9mXF59nASL2WVpnjSkI9mW4q3TUE7gadL35POm29fbHHlcOdo47KYuVENEKr9
p7iuymjlz1/pxSXZoFTAGv7Mn7fLNeInlF4EQyVjMsW68f4E0acQlArZ47uUublm
63NM6Bc6awHzgWd/blJqLzL+GhZ9tngvopHkGTT5hW64MP8YscrQmjsWvJp4AzA6
coakn98MG2VvOnDWNIkPjFf56Zhw26JuDpMDQWl5Gob6M8AtqObaL3apjm+MH4uN
F87DwqoQjue53YPSH7GxXe+cZ7ywruR0DZ3vAoFP3LgjnLh8dupz7zzjPi3n+idM
FNaOzJeBadYItvrdxsUyD/Q8PG3fmNiN+cM8tkLvhuSRdRbaoJ3QVw86BXRsqjlY
Njxf9oRb/CcN/HYrRq67jLcqRC5H6t6aVCTqKxPv8wRK3Exn+G08K/UukQ3Tru8f
tNtE4/+B33XNEaqYpl/Gpe8wS+xUG4QFWRbOoQAun4hGllIQObGAalklATZYPnIB
LtIBcH04myTIYY9lrtTvisSP39pkXbjVkdhMn/LBvZpEpiz1MKwK3+gCvurMshYk
Q/gdaddyLvdZYFiWCkNFvHmvCRt5BlBDFmofhUWI30qgoRPeflx+E0MH8VYmNBCM
zmK1omF4oVfZei1POLg0OT6r5K9KcHLkePMSYLkrJLjoaKnUy+GAMsLwuJqmgHZR
sbtnWrk+D3lxVogVAg4hNbpH5a7RjtH30DEFVkiUTK43LkQm9c5BbCT/C3b26KHO
D7z43Yr87h7iMSFkNWwOn+YGw950dolEtVZcj2FdMu3dYZsVlUSweJtSAt2CuBYN
jGYsr5OGNb+Haht4GFDrZUGT4YNT42JVwYC/7hNuvWx9ZcwpgDA4RNetvGaR0lpq
6ii29RLF5s1ge9Ec1U7FlTbVbaAdeB8vBFqCvdx8PygvKLhjGYj1gabaAdPCbJsX
b/txg5KecwpVrpSLs1bRiWUEu+55
EOF

或者如下生成

1
2
3
openssl rand -base64 756 >  /data0/mongodb/mongos/27017_mongos/keyFile 

chmod 400 /data0/mongodb/mongos/27017_mongos/keyFile

启动mongod

1
mongos -f /etc/mongodb_mongos_27017.conf 

添加分片信息

1
2
3
4
mongo --host localhost --port 27017 -uroot -p"******" --authenticationDatabase admin;
sh.addShard( "repl_shard1/10.204.11.124:29017"); #每个分片只要指定一个机器就可以
sh.addShard( "repl_shard2/10.204.11.124:29018");
sh.addShard( "repl_shard3/10.204.11.124:29019");

查看集群状态

sh.status()

启用库的分片

sh.enableSharding( “test” )

#Create an index on the shard key
db.test_collection1.createIndex( { number : 1 } )

#Shard the collection.
范围片键
sh.shardCollection(“库名.集合名”,key : {分片键: 1})
哈希片键
admin > sh.shardCollection( “数据库名.集合名”, { 片键: “hashed” } )
sh.shardCollection( “test.test_collection1”,key : { “number” : 1 } )

#Confirm the shard is balancing
use test
db.stats()
或者
db.printShardingStatus()

写入测试

#test_collection1 先声明分片,再写数据

1
2
3
4
5
6
7
8
9
10
use test
var bulk = db.test_collection1.initializeUnorderedBulkOp();
people = ["Marc", "Bill", "George", "Eliot", "Matt", "Trey", "Tracy", "Greg", "Steve", "Kristina", "Katie", "Jeff"];
for(var i=0; i<1000000; i++){
user_id = i;
name = people[Math.floor(Math.random()*people.length)];
number = Math.floor(Math.random()*10001);
bulk.insert( { "user_id":user_id, "name":name, "number":number });
}
bulk.execute();

#test_collection2 不分片,只写数据

1
2
3
4
5
6
7
8
9
10
use test
var bulk = db.test_collection2.initializeUnorderedBulkOp();
people = ["Marc", "Bill", "George", "Eliot", "Matt", "Trey", "Tracy", "Greg", "Steve", "Kristina", "Katie", "Jeff"];
for(var i=0; i<1000000; i++){
user_id = i;
name = people[Math.floor(Math.random()*people.length)];
number = Math.floor(Math.random()*10001);
bulk.insert( { "user_id":user_id, "name":name, "number":number });
}
bulk.execute();

#test_collection3 先写数据,再声明分片

1
2
3
4
5
6
7
8
9
10
use test
var bulk = db.test_collection3.initializeUnorderedBulkOp();
people = ["Marc", "Bill", "George", "Eliot", "Matt", "Trey", "Tracy", "Greg", "Steve", "Kristina", "Katie", "Jeff"];
for(var i=0; i<10000000; i++){
user_id = i;
name = people[Math.floor(Math.random()*people.length)];
number = Math.floor(Math.random()*10001);
bulk.insert( { "user_id":user_id, "name":name, "number":number });
}
bulk.execute();

分片管理

查看是否分片

​ db.collection.stats()
​ config.locks集合里的state表示均衡器是否找正在运行 0表示非活动状态,2表示正在均衡

configserver
均衡器工作状态
sh.getBalancerState()
sh.isBalancerRunning()

sh.setBalancerState(false)  #关闭自动均衡器,手动均衡,打开:sh.setBalancerState(true)
db.settings.find()          #查看均衡器状态
均衡时间窗口:activeWindow
     db.settings.update({"_id":"balancer"},{"$set":{"activeWindow":{"start":"08:00","stop":"02:00"}}},true)
     db.settings.find({"_id":"balancer"})
     均衡器是以块的数量作为迁移指标,而非数据大小,块的大小默认是64M,可以修改:(config.settings)

删除分片

​ db.runCommand({“removeshard”:”mmm”})

刷新配置服务器

​ db.adminCommand({“flushRouterConfig”:1})

查看下分片成员

​ db.runCommand({ listshards : 1 })

权重配置

​ PRIMARY> config=rs.conf()
​ PRIMARY>config.members[3].priority = 3
​ PRIMARY> rs.reconfig(config)

添加用户

​ (需要在指定的db下创建),直接在mongos操作,会写入configserver内
​ 查看已有用户 mongos–> db.system.users.find()

1
2
3
4
5
6
7
8
9
    use test;
db.demo.insert({"age":1});
db.createUser(
{
user:"test",
pwd:"123",
roles:[{role:"readWrite",db:"test"}]
}
)

测试用户
mongo –host 10.204.11.124 –port 27017 -utest -p”123” –authenticationDatabase test;

锁定数据库

​ db.fsyncLock()
​ db.fsyncUnlock()

备份 从mongos 入口进入
​ mongodump –host=10.204.11.124 -uroot -p”******” –authenticationDatabase=admin –port 27017 -o /data0/backup/

节点启动顺序

​ 配置服务器 –> 分片 –> mongos

关闭集群

​ killall mongod
​ killall mongos

判断是否Shard集群

​ admin> db.runCommand({ isdbgrid : 1})

列出所有分片信息

​ admin> db.runCommand({ listshards : 1})

迁移primary shard

​ db.adminCommand( { movePrimary: , to: } )
​ db.adminCommand( { movePrimary : “test”, to : “shard0001” } ) #将指定库中未分片col迁移到指定shard中
​ 注意点有二:1. 迁移完,要么重启mongos,要么刷新配置服务器 db.adminCommand({“flushRouterConfig”:1})
​ 2.迁移过程中,不能读写未分片col
​ 如果不注意上述两点,有丢数据的风险,实测2中的情况确实存在。实际表现为,迁移过程中写入的可以直接读出来,但等迁移结束后,就读不到写写入的数据了。