Machine: Ubuntu 2 set
Ip:
- 192.168.0.131 host1
- 192.168.0.132 Host2
Installing MongoDB
sudo apt-key adv--keyserver HKP://keyserver.ubuntu.com:80--recv 0C49F3730359A14518585931BC711F9BA15703C6Echo "Deb [ARCH=AMD64] Http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 Multiverse "| sudo tee/etc/apt/sources. list. d/mongodb-org-3.4. List sudo apt-get update sudo apt-get install-y mongodb-org
File directory
Port
Place the startup Mongod configuration file in the/app/mongo/config directory
1.configServer
In 192.168.0.131
mkdir -p/app/mongo/data/config/data mkdir -p/app/mongo/data/config/log /app/mongo/config/configserver.conf
DBPath=/app/MONGO/data/config/data port= 30000 bind_ip=0.0. 0.0 Configsvr = true Replset =conf
Using supervisor to start and stop
[Program:configdb]Command=Mongod-F/App/Mongo/Config/Configserver.confautostart=Trueautorestart=Truestderr_logfile=/App/Mongo/Data/Config/Log/Err.LogStdout_logfile=/App/Mongo/Data/Config/Log/Out.Log
Same operation in 192.168.0.132
Login to any Server Client initialize Config
mongo --port 30000
= {_id: "conf", Members:[ {_id:0, Host: "192.168.0.131:30000"}, {_id:1, Host: " 192.168.0.132:30000 "} ]};rs.initiate (config);
2. Routing MONGOs Configuration
In 192.168.0.132
- /app/MONGO/data/mongos/log
/app/MONGO/config/mongos.conf
Port = 27017
Bind_ip=0.0. 0.0 configdb=conf/192.168. 0.131:30000,192.168. 0.132:30000
Using supervisor to start and stop
[Program:mongos]Command=MONGOs-F/App/Mongo/Config/Mongos.confautostart=Trueautorestart=Truestdout_logfile= /App/Mongo/Data/MONGOs/Log/Out.LogStderr_logfile= /App/Mongo/Data/MONGOs/Log/Err.Logloglevel=Info
3. sharding
In 192.168.0.131
Mkdir-p/app/mongo/data/shard/datamkdir-p/app/mongo/data/shard/logsvim/app/mongo/config/mongod.confdbpath=/app/ mongo/data/shard/dataport=27018 bind_ip=0.0.0.0 Shardsvr=true
Using supervisor to start and stop
# mongod.conf [Program:mongod]command=mongod-f/app/mongo/config/mongod. Confautorestart=trueredirect_stderr=true=/app/mongo/data/shard/ Logs/mongod. Log loglevel=info
Supervisorctl Reload
In 192.168.0.132
mkdir -p/app/mongo/data/shard/datamkdir -p/app/mongo/data/shard/logs
Adding Shard nodes on 4.mongos
Use admindb. RunCommand ({addshard: "192.168.0.131:27018"});d b. RunCommand ({addshard: " 192.168.0.132:27018 "});
View node Information
Db.runcommand ({listshards:1});
Add a shard primary key to the database (take Db.col as an example)
Sh.enablesharding ("DB") db. RunCommand ({"Shardcollection": "Db.col", "key": {"_id": 1}})
mongodb3.4.15 Cluster Construction