- 服务分配
服务器110 | 服务器120 | 服务器130 |
---|---|---|
mongos | mongos | mongos |
config | config | config |
shard 1 主节点 | shard1 副节点 | shard1 仲裁 |
shard 1 仲裁 | shard1 主节点 | shard1 副节点 |
shard 1 副节点 | shard1 仲裁 | shard1 主节点 |
- 端口分配
mongos | 20000 |
---|---|
config | 21000 |
shard1 | 27001 |
shard2 | 27002 |
shard3 | 27003 |
初始化环境
关闭firewalld和selinux
1
2
3
4systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i "s/SELINUX\=enforcing/SELINUX\=disabled/" /etc/selinux/config配置环境变量
1
2
3
4
5vim /etc/profile
export MONGO_HOME=/home/kookserver/mongodb
export PATH=$PATH:$MONGO_HOME/bin
source /etc/profile主机110/120/130 分别为mongos和config、3个shard创建log目录和data目录
1 | mkdir -p /home/kookserver/mongodb/conf |
配置config server
在110/120/130 3台主机上配置config server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20vim /home/kookserver/mongodb/conf/config.conf
## 配置文件内容
pidfilepath = /home/kookserver/mongodb/config/log/configsrv.pid
dbpath = /home/kookserver/mongodb/config/data
logpath = /home/kookserver/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
#declare this is a config db of a cluster;
configsvr = true
#副本集名称
replSet=configs
#设置最大连接数
maxConns=20000启动config server
1 | mongod -f /home/kookserver/mongodb/conf/config.conf |
- 在任意一台主机登录初始化配置副本集
1
2
3
4
5
6
7
8
9
10mongo --port 21000
config = {
_id : "configs",
members : [
{_id : 0, host : "192.168.1.110:21000" },
{_id : 1, host : "192.168.1.120:21000" },
{_id : 2, host : "192.168.1.130:21000" }
]
}
rs.initiate(config)
配置第一个分片集
三台主机分别配置 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23vim /home/kookserver/mongodb/conf/shard1.conf
pidfilepath = /home/kookserver/mongodb/shard1/log/shard1.pid
dbpath = /home/kookserver/mongodb/shard1/data
logpath = /home/kookserver/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
#打开web监控
httpinterface=true
rest=true
#副本集名称
replSet=shard1
#declare this is a shard db of a cluster;
shardsvr = true
#设置最大连接数
maxConns=20000启动3个shard1
1
mongod -f /home/kookserver/mongodb/conf/shard1.conf
登录任意一台服务器,初始化副本集
1
2
3
4
5
6
7
8
9
10
11mongo --port 27001
use admin
config = {
_id : "shard1",
members : [
{_id : 0, host : "192.168.1.110:27001" },
{_id : 1, host : "192.168.1.120:27001" },
{_id : 2, host : "192.168.1.130:27001", arbiterOnly: true}
]
}
rs.initiate(config);
配置第二个分片集
三台主机分别配置 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23vim /home/kookserver/mongodb/conf/shard2.conf
pidfilepath = /home/kookserver/mongodb/shard2/log/shard2.pid
dbpath = /home/kookserver/mongodb/shard2/data
logpath = /home/kookserver/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
#打开web监控
httpinterface=true
rest=true
#副本集名称
replSet=shard2
#declare this is a shard db of a cluster;
shardsvr = true
#设置最大连接数
maxConns=20000启动3个shard2
1
mongod -f /home/kookserver/mongodb/conf/shard2.conf
登录任意一台服务器,初始化副本集
1
2
3
4
5
6
7
8
9
10
11mongo --port 27002
use admin
config = {
_id : "shard2",
members : [
{_id : 0, host : "192.168.1.110:27002", arbiterOnly: true},
{_id : 1, host : "192.168.1.120:27002" },
{_id : 2, host : "192.168.1.130:27002" }
]
}
rs.initiate(config);
配置第三个分片集
三台主机分别配置 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23vim /home/kookserver/mongodb/conf/shard3.conf
pidfilepath = /home/kookserver/mongodb/shard3/log/shard3.pid
dbpath = /home/kookserver/mongodb/shard3/data
logpath = /home/kookserver/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
#打开web监控
httpinterface=true
rest=true
#副本集名称
replSet=shard3
#declare this is a shard db of a cluster;
shardsvr = true
#设置最大连接数
maxConns=20000启动3个shard3
1
mongod -f /home/kookserver/mongodb/conf/shard3.conf
登录任意一台服务器,初始化副本集
1
2
3
4
5
6
7
8
9
10
11mongo --port 27003
use admin
config = {
_id : "shard3",
members : [
{_id : 0, host : "192.168.1.110:21003"},
{_id : 1, host : "192.168.1.120:21003", arbiterOnly: true },
{_id : 2, host : "192.168.1.130:21003" }
]
}
rs.initiate(config);
配置mongos
三台机器配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16vim /home/kookserver/mongodb/conf/mongos.conf
#内容
pidfilepath = /home/kookserver/mongodb/mongos/log/mongos.pid
logpath = /home/kookserver/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
configdb = configs/192.168.1.110:21000,192.168.1.120:21000,192.168.1.130:21000
#设置最大连接数
maxConns=20000启动3台服务器的mongos
1
mongos -f /home/kookserver/mongodb/conf/mongos.conf
启用分片 登录任意一台mongos
1
2
3
4
5
6mongo --port 20000
use admin
sh.addShard("shard1/192.168.1.110:27001,192.168.1.120:27001,192.168.1.130:27001")
sh.addShard("shard2/192.168.1.110:27002,192.168.1.120:27002,192.168.1.130:27002")
sh.addShard("shard3/192.168.1.110:27003,192.168.1.120:27003,192.168.1.130:27003")
sh.status()测试分片 在任意一台mongos上
1
2
3
4
5
6
7mongo 127.0.0.1:20000
use admin
db.runCommand( { enablesharding :"testdb"});
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
use testdb
for (var i = 1; i <= 100000; i++) db.table1.save({id:i,"test1":"testval1"});
db.table1.stats();
初始化
建立msg表分片,将下面代码保存到脚本,执行输出的语句在mongo中执行
1
2
3
4
5
6
7
8
9
10
11
printf "use admin\n"
printf "db.runCommand( { enablesharding :\"msg\"});\n"
for i in `seq 0 99`;
do
if [ $i -lt 10 ];then
printf "db.runCommand({ shardcollection:\"msg.single_0$i\",key:{tg: 'hashed'}})\n"
else
printf "db.runCommand({ shardcollection:\"msg.single_$i\",key:{tg: 'hashed'}})\n"
fi
done创建mongo索引
1
2
3python /home/kookserver/script/setup/mongodb/mongo_ensureIndex.py
cd /home/kookserver/script/setup/mongodb/4.0/
for i in `ls *mongo`;do cat $i|mongo 127.0.0.1:20000;done
创建systemctl 管理mongo进程,
创建systemd文件,下面👇分别保持到文件(文件名为#部分)
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#mongos.service
[Unit]
Description=Mongo Mongos
Documentation=https://www.ccwork.com.cn
After=network-online.target
[Service]
User=root
Group=root
LimitNOFILE=65536
ExecStart=/home/kookserver/mongodb/bin/mongos -f /home/kookserver/mongodb/conf/mongos.conf
#ExecStop=/home/kookserver/mongodb/bin/mongos --shutdown --dbpath /home/kookserver/mongodb/mongos/data
KillMode=control-group
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=influxd.service
#mongoconfig
[Unit]
Description=Mongo Config
Documentation=https://www.ccwork.com.cn
After=network-online.target
[Service]
User=root
Group=root
LimitNOFILE=65536
ExecStart=/home/kookserver/mongodb/bin/mongod -f /home/kookserver/mongodb/conf/config.conf
ExecStop=/home/kookserver/mongodb/bin/mongod --shutdown --dbpath /home/kookserver/mongodb/config/data/
KillMode=control-group
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=influxd.service
#mongoshard1.service
[Unit]
Description=Mongo Shard1
Documentation=https://www.ccwork.com.cn
After=network-online.target
[Service]
User=root
Group=root
LimitNOFILE=65536
ExecStart=/home/kookserver/mongodb/bin/mongod -f /home/kookserver/mongodb/conf/shard1.conf
ExecStop=/home/kookserver/mongodb/bin/mongod --shutdown --dbpath /home/kookserver/mongodb/shard1/data/
KillMode=control-group
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=influxd.service
#mongoshard2.service
[Unit]
Description=Mongo Shard2
Documentation=https://www.ccwork.com.cn
After=network-online.target
[Service]
User=root
Group=root
LimitNOFILE=65536
ExecStart=/home/kookserver/mongodb/bin/mongod -f /home/kookserver/mongodb/conf/shard2.conf
ExecStop=/home/kookserver/mongodb/bin/mongod --shutdown --dbpath /home/kookserver/mongodb/shard2/data/
KillMode=control-group
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=influxd.service
#mongoshard3.service
[Unit]
Description=Mongo Shard3
Documentation=https://www.ccwork.com.cn
After=network-online.target
[Service]
User=root
Group=root
LimitNOFILE=65536
ExecStart=/home/kookserver/mongodb/bin/mongod -f /home/kookserver/mongodb/conf/shard3.conf
ExecStop=/home/kookserver/mongodb/bin/mongod --shutdown --dbpath /home/kookserver/mongodb/shard3/data/
KillMode=control-group
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=influxd.service拷贝到/usr/lib/systemd/system/
1
scp *service /usr/lib/systemd/system/
systemctl 管理服务
1
2
3
4
5
6
7
8# 启动
systemctl start mongoconfig mongoshard1 mongoshard2 mongoshard3 mongos
# 查看状态
systemctl status mongoconfig mongoshard1 mongoshard2 mongoshard3 mongos
# 停止
systemctl stop mongoconfig mongoshard1 mongoshard2 mongoshard3 mongos
# 重启
systemctl restart mongoconfig mongoshard1 mongoshard2 mongoshard3 mongos
恢复mongo数据
- 解压恢复mongo备份包
1
2
3
4
5cd /srv/20180919
//解压
for i in `ls`;do cd $i;for x in `ls`;do gzip -d $x;done;cd -;done
//恢复
for i in `ls`;do cd $i;mongorestore -h 10.128.30.246:20000 --drop -d $i . --batchSize=1;cd -;done
- 本文作者: ChuLinx
- 本文链接: http://yoursite.com/2019/11/21/mongo 分片副本集配置/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!