Introduced
In the era of big data, the database application of massive data and throughput has caused a great pressure on the performance of single machine, which will occur such as CPU exhaustion, storage pressure, exhausted resources and so on.
New technologies, sharding technologies, are emerging. It is a method used by MongoDB to split large collections into different servers, almost automatically doing everything, and by telling MongoDB what data to allocate, it can automatically maintain the data to the balanced storage between different servers.
The Shard mechanism provides three advantages:
- Abstract the cluster so that the cluster is "invisible"
- Ensure that the cluster is always readable and writable
- Makes clustering easy to scale
Shard Cluster Architecture:
config server: storage of all nodes of the cluster, shard data routing information, the default need to configure 3 Config server node;
Mongos: provide access to external applications, all operations are performed through Mongos, generally have multiple Mongos nodes, data migration and data automatic balance;
Mongod: Storage Application data records, generally have multiple Mongod nodes, to achieve the purpose of data fragmentation.
System environment:
1. Three servers are configured as follows:
IP Address |
Routing Server |
Configure the server |
Shard1 |
Shard2 |
Shard3 |
192.168.96.10 |
20000 |
21000 |
27001 |
27002 |
27003 |
192.168.96.11 |
20000 |
21000 |
27001 |
27002 |
27003 |
192.168.96.12 |
20000 |
21000 |
27001 |
27002 |
27003 |
2. Operating system: CentOS 7
3. Turn off firewall and SELinux features
4. Package: mongodb-linux-x86_64-rhel70-3.6.6.tgz Password: 4h77
Start deployment first three server installation and configuration 1. Unpacking the installation package
TAR-ZXVF mongodb-linux-x86_64-rhel70-3.6.6.tgz-c/usr/local
2. Switch to the software catalog
Cd/usr/local
3. Renaming directory names
MV mongodb-linux-x86_64-rhel70-3.6.6 MongoDB
4. Create routing, configuration, shard server configuration, data, log management directory
mkdir -p /data/mongodb/confmkdir -p /data/mongodb/mongos/logmkdir -p /data/mongodb/config/datamkdir -p /data/mongodb/config/log#循环创建分片服务器目录for i in 1 2 3do mkdir -p /data/mongodb/shard$i/data mkdir -p /data/mongodb/shard$i/logdone
5. Create an administrative user
Useradd-m-s/sbin/nologin MONGO
6. Modify Directory Permissions
Chown-r Mongo.mongo/usr/local/mongodb
Chown-r Mongo.mongo/data/mongodb
7. Add to System environment variables
echo "Path=/usr/local/mongodb/bin: $PATH" >>/etc/profile
8. Reload Environment variables
Source/etc/profile
9. System memory Optimization
ulimit -n 25000ulimit -u 25000sysctl -w vm.zone_reclaim_mode=0echo never > /sys/kernel/mm/transparent_hugepage/enabledecho never > /sys/kernel/mm/transparent_hugepage/defrag
Special Reminder: Please check if all three server configurations have been configured and started.
The second part configures the server (three configuration steps are consistent) 1. Create a configuration file
Vim/data/mongodb/conf/config.conf
#配置文件内容pidfilepath = /data/mongodb/config/log/config-srv.piddbpath = /data/mongodb/config/datalogpath = /data/mongodb/config/log/config-srv.loglogappend = truebind_ip = 0.0.0.0port = 21000fork = true#declare this is a config db of a cluster;configsvr = true#复制集名称replSet=configs#设置最大连接数maxConns=20000
2. Create a startup script:
Vim/usr/lib/systemd/system/mongo-config.service
[Unit]Description=mongodb-configAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingUser=mongoGroup=mongoExecStart=/usr/local/mongodb/bin/mongod -f /data/mongodb/conf/config.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/config.confPrivateTmp=true[Install]WantedBy=multi-user.target
3. Start the CONFIGSRV server
systemctl daemon-reloadsystemctl enable mongo-configsystemctl start mongo-config
Special reminder: After the three servers are configured, the following initialization steps are performed
4. Initialize the replica set (log in to any server to initialize the replica set)
MONGO--port 21000
//配置> config={"_id":"configs",members:[{"_id":0,"host":"192.168.96.10:21000"},{"_id":1,"host":"192.168.96.11:21000"},{"_id":2,"host":"192.168.96.12:21000"}]}//初始化> rs.initiate(config)//查看状态> rs.status()
Part Three Shard server one, Shard1 Shard server 1. Create a configuration file
Vim/data/mongodb/conf/shard1.conf
pidfilepath = /data/mongodb/shard1/log/shard1.piddbpath = /data/mongodb/shard1/datalogpath = /data/mongodb/shard1/log/shard1.loglogappend = truejournal = truequiet = truebind_ip = 0.0.0.0port = 27001fork = true#复制集名称replSet=shard1#declare this is a shard db of a cluster;shardsvr = true#设置最大连接数maxConns=20000
2. Create a startup script
Vim/usr/lib/systemd/system/mongo-shard1.service
[Unit]Description=mongodb-shard1After= mongo-config.target network.target[Service]Type=forkingUser=mongoGroup=mongoExecStart=/usr/local/mongodb/bin/mongod -f /data/mongodb/conf/shard1.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/shard1.confPrivateTmp=true[Install]WantedBy=multi-user.target
3. Start the service
systemctl daemon-reloadsystemctl enable mongo-shard1systemctl start mongo-shard1
Special reminder: After the three servers are configured, the following initialization steps are performed
4. Initialize the replica set (log in to any server to initialize the replica set)
MONGO--port 27001
> use admin> config={"_id":"shard1",members:[{"_id":0,"host":"192.168.96.10:27001",arbiterOnly:true},{"_id":1,"host":"192.168.96.11:27001"},{"_id":2,"host":"192.168.96.12:27001"}]}> rs.initiate(config)> rs.status()
Second, Shard2 Shard server 1. Create a configuration file
Vim/data/mongodb/conf/shard2.conf
pidfilepath = /data/mongodb/shard2/log/shard2.piddbpath = /data/mongodb/shard2/datalogpath = /data/mongodb/shard2/log/shard2.loglogappend = truejournal = truequiet = truebind_ip = 0.0.0.0port = 27002fork = true#复制集名称replSet=shard2#declare this is a shard db of a cluster;shardsvr = true#设置最大连接数maxConns=20000
2. Create a startup script
Vim/usr/lib/systemd/system/mongo-shard2.service
[Unit]Description=mongodb-shard2After= mongo-config.target network.target[Service]Type=forkingUser=mongoGroup=mongoExecStart=/usr/local/mongodb/bin/mongod -f /data/mongodb/conf/shard2.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/shard2.confPrivateTmp=true[Install]WantedBy=multi-user.target
3. Start the service
systemctl daemon-reloadsystemctl enable mongo-shard2systemctl start mongo-shard2
Special reminder: After the three servers are configured, the following initialization steps are performed
4. Initialize the replica set (log in to any server to initialize the replica set)
MONGO--port 27002
> use admin> config={"_id":"shard2",members:[{ "_id":0,"host":"192.168.96.10:27002"}, {"_id":1,"host":"192.168.96.11:27002",arbiterOnly:true},{"_id":2,"host":"192.168.96.12:27002"}]}> rs.initiate(config);> rs.status()
Shard3 Shard server 1. Creating a configuration file
Vim/data/mongodb/conf/shard3.conf
pidfilepath = /data/mongodb/shard3/log/shard3.piddbpath = /data/mongodb/shard3/datalogpath = /data/mongodb/shard3/log/shard3.loglogappend = truejournal = truequiet = truebind_ip = 0.0.0.0port = 27003fork = true#复制集名称replSet=shard3#declare this is a shard db of a cluster;shardsvr = true#设置最大连接数maxConns=20000
2. Create a startup script
Vim/usr/lib/systemd/system/mongo-shard3.service
"[Unit]
Description=mongodb-shard3
after= Mongo-config.target Network.target
[Service]
Type=forking
User=mongo
Group=mongo
Execstart=/usr/local/mongodb/bin/mongod-f/data/mongodb/conf/shard3.conf
Execreload=/bin/kill-s HUP $MAINPID
Execstop=/usr/local/mongodb/bin/mongod--shutdown-f/data/mongodb/conf/shard3.conf
Privatetmp=true
[Install]
Wantedby=multi-user.target
#### 3.创建启动脚本
Systemctl Daemon-reload
Systemctl Enable Mongo-shard3
Systemctl Start Mongo-shard3
**特别提醒:三台服务器都配置完毕后,再进行以下初始化步骤**#### 4.初始化复制集(登录任意一台服务器进行初始化复制集)> mongo --port 27003
Use admin
config={"_id": "Shard3", members:[{"_id": 0, "host": "192.168.96.10:27003"}, {"_id": 1, "host": "192.168.96.11:27003"}, {"_id": 2, "host": "192.168.96.12:27003", Arbiteronly:true}]}
Rs.initiate (config)
Rs.status ()
Part IV MONGOs routing server 1. Creating a configuration file
Vim/data/mongodb/conf/mongos.conf
pidfilepath = /data/mongodb/mongos/log/mongos.pidlogpath = /data/mongodb/mongos/log/mongos.loglogappend = truebind_ip = 0.0.0.0port = 20000fork = true#监听的配置服务器的地址:端口,(重要、重要、重要)configdb = configs/192.168.96.10:21000,192.168.96.11:21000,192.168.96.12:21000#设置最大连接数maxConns=20000
2. Create a startup script
Vim/usr/lib/systemd/system/mongos.service
[Unit]Description=Mongo Router ServiceAfter=mongo-config.service[Service]Type=forkingUser=mongoGroup=mongoExecStart=/usr/local/mongodb/bin/mongos --config /data/mongodb/conf/mongos.confRestart=on-failure[Install]WantedBy=multi-user.target
3. Start the service
systemctl daemon-reloadsystemctl enable mongossystemctl start mongos
Special reminder: After the three servers are configured, follow these steps
4. Start the Shard function (log in to any server)
MONGO--port 20000
> use admin> sh.addShard("shard1/192.168.96.10:27001,192.168.96.11:27001,192.168.96.12:27001")sh.addShard("shard2/192.168.96.10:27002,192.168.96.11:27002,192.168.96.12:27002")sh.addShard("shard3/192.168.96.10:27003,192.168.96.11:27003,192.168.96.12:27003")> sh.status()
5. Check if the Shard is successful
> use school> for(i=1;i<=1000;i++){db.user.insert({"id":i,"name":"jack"+i})}//开启school数据库分片功能> db.runCommand({enablesharding:"school"}};//指定school数据库需要分片的集合和片键> db.runCommand({shardcollection:"school.user",key:{id:1}})//查看状态> db.user.stats()
MongoDB3.6 Shard Cluster