I. Environmental preparedness
1. This example uses 3 Linux hosts, with IP addresses as follows:
Click (here) to collapse or open the Server a:192.168.1.231
Server b:192.168.1.232
Server c:192.168.1.233
2. According to the need, open the corresponding host firewall related ports. This time need to use 3 hosts, so open the following ports of these 3 hosts:
Click (here) to collapse or open-A input-m State--state new-m tcp-p TCP--dport 27017-j ACCEPT
-A input-m state--state new-m tcp-p TCP--dport 27018-j ACCEPT
-A input-m state--state new-m tcp-p TCP--dport 27019-j ACCEPT
-A input-m state--state new-m tcp-p TCP--dport 20000-j ACCEPT
second, build distributed cluster
Using MongoDB replication set (Replica Sets) + fragmentation (Sharding) can realize MongoDB distributed high availability cluster.
Fragmentation (sharding) can add more machines to cope with ever-increasing loads and data, and does not affect applications.
The replica set (Replica Sets) ensures that each fragment node has automatic backup, automatic failback capability.
1. The environment of the cluster
Schema diagram:
Host Design:
2. Configure Replica Sets
To create a data directory:
Click (here) to collapse or open mkdir-p/data/{shard1-1,shard2-1,config} #在serverA上
Mkdir-p/data/{shard1-2,shard2-2,config} #在serverB上
Mkdir-p/data/{shard1-3,shard2-3,config} #在serverC上
2.1 Configure shard1 Replica Sets :
#在serverA上启动shard1-1
Click (here) to collapse or open./mongod--fork--shardsvr--port 27018--dbpath/data/shard1-1/-- Logappend--replset Shard1--directoryperdb
# Start the shard1-2 on the ServerB
Click (here) to collapse or open./mongod--fork--shardsvr--port 27018--dbpath/data/shard1-2/-- Logappend--replset Shard1--directoryperdb
# Start the shard1-3 on the ServerC
Click (here) to collapse or open./mongod--fork--shardsvr--port 27018--dbpath/data/shard1-3/-- Logappend--replset Shard1--directoryperdb
Initialize the replica set shard1 using MONGO to connect the 27018-port Mongod of one of the hosts:
./mongo--port 27018
Click (here) to fold or open >config={_id: ' Shard1 ', members:[
{_id:0,host: ' 192.168.1.231:27018 ', priority:2},
{_id:1,host: ' 192.168.1.232:27018 ', priority:1},
{_id:2,host: ' 192.168.1.233:27018 ', arbiteronly:true}]
}
>rs.initiate (config)
2.2 configuration shard2 replica Sets
#在serverA上启动shard2-1
Click (here) to collapse or open./mongod--fork--shardsvr--port 27019--dbpath/data/shard2-1/-- Logappend--replset Shard2--directoryperdb
# Start the shard2-2 on the ServerB
Click (here) to collapse or open./mongod--fork--shardsvr--port 27019--dbpath/data/shard2-2/-- Logappend--replset Shard2--directoryperdb
# Start the shard2-3 on the ServerC
Click (here) to collapse or open./mongod--fork--shardsvr--port 27019--dbpath/data/shard2-3/-- Logappend--replset Shard2--directoryperdb
Initializes the replica set Shard2 with a MONGO connection to the 27019-port Mongod of one of the hosts (recommended on ServerB):
./MONGO--port 27019
Click (here) to collapse or open >config={_id: ' Shard2 ', members:[
{_id:0,host: ' 192.168.1.231:27019 ', arbiteronly:true},
{_ Id:1,host: ' 192.168.1.232:27019 ', priority:1},
{_id:2,host: ' 192.168.1.233:27019 ', priority:2}]
>rs.initiate (config)