In other words, the virtual machine will report various errors, and it will not be so painful in the real environment. I thought about the cause of the error in the virtual machine. Machine card. If the processing time is long, an error will be thrown. If the space provided by the virtual machine is too small, an exception will also be reported. Here is my general idea. First, choose three servers. It is better to design three servers instead of virtual ones.
In other words, the virtual machine will report various errors, and it will not be so painful in the real environment. I thought about the cause of the error in the virtual machine. Machine card. If the processing time is long, an error will be thrown. If the space provided by the virtual machine is too small, an exception will also be reported. Here is my general idea. First, choose three servers. It is better to design three servers instead of virtual ones.
In other words, the virtual machine will report various errors, and it will not be so painful in the real environment.
I thought about the cause of the error in the virtual machine. Machine card. An error will be thrown if the processing time is long.
If the space provided by the virtual machine is too small, an exception is reported.
Here is my general idea.
First, choose three servers, preferably not virtual
2. design the IP addresses, ports, and roles of the three servers.
Third, check whether the firewall is disabled and whether the time is synchronized.
If you can do the above, you can perform the operation.
Of course, the mongodb version and mongodb should be built on the premise.
Wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.4.tgz
Decompress mongodb files
The version number of host 1 is different. My website is 2.0.4.
[Root @ mongodb40 ~] # Tar zxvfmongodb-linux-x86_64-2.4.7.tgz
[Root @ mongodb40 ~] # Mvmongodb-linux-x86_64-2.4.7/home/mongodb
Host 2
[Root @ mongodb41 ~] # Tar zxvfmongodb-linux-x86_64-2.4.7.tgz
[Root @ mongodb41 ~] # Mvmongodb-linux-x86_64-2.4.7/home/mongodb
Host 3
[Root @ mongodb250 ~] # Tar zxvfmongodb-linux-x86_64-2.4.7.tgz
[Root @ mongodb250 ~] # Mvmongodb-linux-x86_64-2.4.7/home/mongodb
Mongo cluster sharding practice
Three servers
HOST 1 (172.16.0.40) Host 2 (172.16.0.41) host 3 (172.16.0.250)
Arbitration of the first (11731) Primary copy
Second (11732) Arbitration master copy
Arbitration master for the third (11733) Copy
30000 configServer config Server
60000 RouteProcess Route Process
Create Database directory
HOST 1 (172.16.0.40) configuration file
[Root @ mongodb115 ~] # Mkdir-p/home/data/shard1_1
[Root @ mongodb115 ~] # Mkdir-p/home/data/shard2_1
[Root @ mongodb115 ~] # Mkdir-p/home/data/shard3_1
[Root @ mongodb115 ~] # Mkdir-p/home/data/config # config server Directory
[Root @ mongodb115 ~] # Mkdir-p/home/config # Put the configuration file
[Root @ mongodb115 ~] # Mkdir-p/home/data/logs
All the configuration directories in this place are stored in the/home/config directory. Remember to remember, because I have suffered a loss here and I do not know it once.
Part 1
[Root @ mongodb40 config] # catshard=1.conf
Port = 11731
Dbpath =/home/data/shard1_1
Logpath =/home/data/logs/shard1_1.log
Directoryperdb = true
Logappend = true
ReplSet = shard1
# Bind_ip = 172.16.0.40
Shardsvr = true
Fork = true
Pidfilepath =/home/data/shard1_1.pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
Part 2
[Root @ mongodb40 config] # catshard2_1.conf
Port = 11732
Dbpath =/home/data/shard2_1
Logpath =/home/data/logs/shard2_1.log
Directoryperdb = true
Logappend = true
ReplSet = shard2
# Bind_ip = 172.16.0.40
Shardsvr = true
Fork = true
Pidfilepath =/home/data/shard2_1.pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
Part 3
[Root @ mongodb40 config] # catshard3_1.conf
Port = 11733
Dbpath =/home/data/shard3_1
Logpath =/home/data/logs/shard3_1.log
Directoryperdb = true
Logappend = true
ReplSet = shard3
# Bind_ip = 172.16.0.40
Shardsvr = true
Fork = true
Pidfilepath =/home/data/shard3_1.pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
Config server configuration file
[Root @ mongodb40 config] # catconfig. conf
Port = 30000
Dbpath =/home/data/config
Logpath =/home/data/logs/config. log
Directoryperdb = true
Configsvr = true
Logappend = true
# Bind_ip = 172.16.0.40
Fork = true
Pidfilepath =/home/data/config. pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
RouteProces configuration file
[Root @ mongodb40 config] # catmongos. conf
Port = 60000
Logpath =/home/data/logs/mongos. log
Logappend = true
Configdb = 172.16.0.40: 30000,172.16 .0.41: 30000,172.16 .0.250: 30000
# Bind_ip = 172.16.0.40
Fork = true
Pidfilepath =/home/data/mongos. pid
ChunkSize = 5
MaxConns = 20000
Nohttpinterface = true
Host 2 (172.16.0.41) configuration file
[Root @ mongodb114 ~] # Mkdir-p/home/data/shard1_2
[Root @ mongodb114 ~] # Mkdir-p/home/data/shard2_2
[Root @ mongodb114 ~] # Mkdir-p/home/data/shard3_2
[Root @ mongodb114 ~] # Mkdir-p/home/data/config # config server Directory
[Root @ mongodb114 ~] # Mkdir-p/home/config # Put the configuration file
[Root @ mongodb114 ~] # Mkdir-p/home/data/logs
Part 1
[Root @ mongodb41 config] # catshard1_2.conf
Port = 11731
Dbpath =/home/data/shard1_2
Logpath =/home/data/logs/shard1_2.log
Directoryperdb = true
Logappend = true
ReplSet = shard1
# Bind_ip = 172.16.0.41
Shardsvr = true
Fork = true
Pidfilepath =/home/data/shard1_2.pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
Part 2
[Root @ mongodb41 config] # catshard2_2.conf
Port = 11732
Dbpath =/home/data/shard2_2
Logpath =/home/data/logs/shard2_2.log
Directoryperdb = true
Logappend = true
ReplSet = shard2
# Bind_ip = 172.16.0.41
Shardsvr = true
Fork = true
Pidfilepath =/home/data/shard2_2.pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
Part 3
[Root @ mongodb41 config] # catshard3_2.conf
Port = 11733
Dbpath =/home/data/shard3_2
Logpath =/home/data/logs/shard3_2.log
Directoryperdb = true
Logappend = true
ReplSet = shard3
# Bind_ip = 172.16.0.41
Shardsvr = true
Fork = true
Pidfilepath =/home/data/shard3_2.pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
Config server configuration file
[Root @ mongodb41 config] # catconfig. conf
Port = 30000
Dbpath =/home/data/config
Logpath =/home/data/logs/config. log
Directoryperdb = true
Configsvr = true
Logappend = true
# Bind_ip = 172.16.0.41
Fork = true
Pidfilepath =/home/data/config. pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
RouteProces configuration file
[Root @ mongodb41 config] # catmongos. conf
Port = 60000
Logpath =/home/data/logs/mongos. log
Logappend = true
Configdb = 172.16.0.40: 30000,172.16 .0.41: 30000,172.16 .0.250: 30000
# Bind_ip = 172.16.0.41
Fork = true
Pidfilepath =/home/data/mongos. pid
ChunkSize = 5
MaxConns = 20000
Nohttpinterface = true
Host 3 (172.16.0.250) configuration file
[Root @ mongodb103 ~] # Mkdir-p/home/data/shard1_3
[Root @ mongodb103 ~] # Mkdir-p/home/data/shard2_3
[Root @ mongodb103 ~] # Mkdir-p/home/data/shard3_3
[Root @ mongodb103 ~] # Mkdir-p/home/data/config # config server Directory
[Root @ mongodb103 ~] # Mkdir-p/home/config # Put the configuration file
[Root @ mongodb103 ~] # Mkdir-p/home/data/logs
Part 1
[Root @ mongodb250 config] # catshard#3.conf
Port = 11731
Dbpath =/home/data/shard1_3
Logpath =/home/data/logs/shard1_3.log
Directoryperdb = true
Logappend = true
ReplSet = shard1
# Bind_ip = 172.16.0.250
Shardsvr = true
Fork = true
Pidfilepath =/home/data/shard1_3.pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
Part 2
[Root @ mongodb250 config] # catshard2_3.conf
Port = 11732
Dbpath =/home/data/shard2_3
Logpath =/home/data/logs/shard2_3.log
Directoryperdb = true
Logappend = true
ReplSet = shard2
# Bind_ip = 172.16.0.250
Shardsvr = true
Fork = true
Pidfilepath =/home/data/shard2_3.pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
Part 3
[Root @ mongodb250 config] # catshard3_3.conf
Port = 11733
Dbpath =/home/data/shard3_3
Logpath =/home/data/logs/shard3_3.log
Directoryperdb = true
Logappend = true
ReplSet = shard3
# Bind_ip = 172.16.0.250
Shardsvr = true
Fork = true
Pidfilepath =/home/data/shard3_3.pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
Config server configuration file
[Root @ mongodb250 config] # catconfig. conf
Port = 30000
Dbpath =/home/data/config
Logpath =/home/data/logs/config. log
Directoryperdb = true
Configsvr = true
Logappend = true
# Bind_ip = 172.16.0.250
Fork = true
Pidfilepath =/home/data/config. pid
MaxConns = 20000
OplogSize = 5000
Noprealloc = true
Nohttpinterface = true
RouteProces configuration file
[Root @ mongodb250 config] # catmongos. conf
Port = 60000
Logpath =/home/data/logs/mongos. log
Logappend = true
Configdb = 172.16.0.40: 30000,172.16 .0.41: 30000,172.16 .0.250: 30000
# Bind_ip = 172.16.0.250
Fork = true
Pidfilepath =/home/data/mongos. pid
ChunkSize = 5
MaxConns = 20000
Nohttpinterface = true
Start shard1, shard2, shard3
HOST 1
[Root @ mongodb115 ~] #/Home/mongodb/bin/mongod-f/home/config/shard1_1.conf
[Root @ mongodb115 ~] #/Home/mongodb/bin/mongod-f/home/config/shard2_1.conf
[Root @ mongodb115 ~] #/Home/mongodb/bin/mongod-f/home/config/shard3_1.conf
Host 2
[Root @ mongodb114 ~] #/Home/mongodb/bin/mongod-f/home/config/shard1_2.conf
[Root @ mongodb114 ~] #/Home/mongodb/bin/mongod-f/home/config/shard2_2.conf
[Root @ mongodb114 ~] #/Home/mongodb/bin/mongod-f/home/config/shard3_2.conf
Host 3
[Root @ mongodb103 ~] #/Home/mongodb/bin/mongod-f/home/config/shard1_3.conf
[Root @ mongodb103 ~] #/Home/mongodb/bin/mongod-f/home/config/shard2_3.conf
[Root @ mongodb103 ~] #/Home/mongodb/bin/mongod-f/home/config/shard3_3.conf
Configure ReplicaSets
Configure Part 1 on 172.16.0.40
[Root @ mongodb115 ~] #/Home/mongodb/bin/mongo -- port 11731
MongoDB shell version: 2.0.4
Connecting to: 127.0.0.1: 11731/test
> Config = {_ id: 'shard1 ', members: [{_ id: 0, host: '123. 16.0.40: 100', priority: 2}, {_ id: 1, host: '123. 16.0.41: 11731 '}, {_ id: 2, host: '2017. 16.0.250: 11731 ', arbiterOnly: true}]}
Rs. initiate (config)
Configure Part 2 on 172.16.0.41
[Root @ mongodb114 ~] #/Home/mongodb/bin/mongo -- port 11732
MongoDB shell version: 2.0.4
Connecting to: 127.0.0.1: 11732/test
> Config = {_ id: 'shard2 ', members: [{_ id: 0, host: '123. 16.0.40: 11732 ', arbiterOnly: true}, {_ id: 1, host: '100. 16.0.41: 11732 ', priority: 2}, {_ id: 2, host: '2017. 16.0.250: 11732 '}]}
Rs. initiate (config)
Configure Part 3 to configure the priority value in members on 172.16.0.250 as the master node, and add arbiterOnly: true to the arbitration point.
[Root @ mongodb103 ~] #/Home/mongodb/bin/mongo -- port 11733
MongoDB shell version: 2.0.4
Connecting to: 127.0.0.1: 11733/test
> Config = {_ id: 'shard3 ', members: [{_ id: 0, host: '123. 16.0.40: 11733 '}, {_ id: 1, host: '2017. 16.0.41: 11733 ', arbiterOnly: true}, {_ id: 2, host: '100. 16.0.25: 11733 ', priority: 2}]}
Rs. initiate (config)
Start 3 configservers
HOST 1
[Root @ mongodb40 ~] #/Home/mongodb/bin/mongod-f/home/config. conf
Host 2
[Root @ mongodb41 ~] #/Home/mongodb/bin/mongod-f/home/config. conf
Host 3
[Root @ mongodb250 ~] #/Home/mongodb/bin/mongod-f/home/config. conf
Start 3 Route Processes
HOST 1
[Root @ mongodb40 ~] #/Home/mongodb/bin/mongos-f/home/config/mongos. conf
Host 2
[Root @ mongodb41 ~] #/Home/mongodb/bin/mongos-f/home/config/mongos. conf
Host 3
[Root @ mongodb250 ~] #/Home/mongodb/bin/mongos-f/home/config/mongos. conf
Configure shardCluser
[Root @ mongodb115 ~] #/Home/mongodb/bin/mongo -- port 60000
MongoDB shell version: 2.0.4
Connecting to: 127.0.0.1: 60000/test
Mongos> use admin
Switched to db admin
Mongos> db. runCommand ({addshard: "shard1/172.16.0.40: 11731,172.16 .0.41: 11731,172.16 .0.250: 11731 "})
Mongos> db. runCommand ({addshard: "shard2/172.16.0.40: 11732,172.16 .0.41: 11732,172.16 .0.250: 11732 "})
Mongos> db. runCommand ({addshard: "shard3/172.16.0.40: 11733,172.16 .0.41: 11733,172.16 .0.250: 11733 "})
Next, activate the shard, as shown in the following code: Use the hash shard
Db. runCommand ({enablesharding: "test "})
Db. runCommand ({shardcollection: "test. users", key: {id: "hashed "}})
The above error will be reported. My version does not support hashed sharding.