MongoDB entry practice - Installation

This article mainly introduces the installation of MongoDB and the software versions used: MongoDB 5.0.5 and Centos 7.6.

1. Stand alone installation

1.1. Download and unzip MongoDB

Download address: https://www.mongodb.com/try/download/community

Decompression: tar zxvf mongodb Linux x86_ 64-rhel70-5.0.5. tgz

1.2. Create data and log directory

cd $MONGODB_HOME

mkdir data
mkidr logs

1.3 startup

cd $MONGODB_HOME/bin

./mongod --bind_id 10.40.96.10,127.0.0.1 --dbpath ../data --logpath ../logs/mongod.log --fork

--dbpath: data storage directory
--logpath: log file

More parameters of the mongod command can be viewed using mongod -h.

1.4. Stop

Connect MongoDB service through mongo, and then execute the stop command.

Copy code
cd $MONGODB_HOME/bin
./mongo

> use admin
switched to db admin
> db.shutdownServer()
Copy code

2. Master slave installation

When installing in the master-slave mode, start the host node first and then the standby node; The startup method is as follows:

./mongod --master --bind_ip <ip> --dbpath <dbpath> --logpath <logapth>  #Master node  
  
./mongod --slave --bind_ip <ip> --source <masterip:masterport> --dbpath <dbpath> --logfile <logfile>  #Standby node

This deployment mode is no longer supported in the new version of MongoDB (the runtime will prompt: Master/slave replication is no longer supported), which will not be described in detail here.

3. Replica Set replica set installation

3.1 planning

ipportroleMongoDB installation directory
10.49.196.1027017Primary/home/hadoop/app/mongodb-5.0.5
10.49.196.1127017Secondary/home/hadoop/app/mongodb-5.0.5
10.49.196.1227017Secondary/home/hadoop/app/mongodb-5.0.5
10.49.196.1227018Arbiter/home/hadoop/app/mongodb-5.0.5-27018

3.2 configuration

Use the configuration file to start MongoDB. The configuration file (mongod.conf) of each MongoDB instance is as follows:

10.49.196.10:27017

dbpath=/home/hadoop/app/mongodb-5.0.5/data
logpath=/home/hadoop/app/mongodb-5.0.5/logs/mongo.log
fork=true
replSet=rs0
bind_ip=127.0.0.1,10.49.196.10

10.49.196.11:27017

dbpath=/home/hadoop/app/mongodb-5.0.5/data
logpath=/home/hadoop/app/mongodb-5.0.5/logs/mongo.log
fork=true
replSet=rs0
bind_ip=127.0.0.1,10.49.196.11

10.49.196.12:27018

dbpath=/home/hadoop/app/mongodb-5.0.5/data
logpath=/home/hadoop/app/mongodb-5.0.5/logs/mongo.log
fork=true
replSet=rs0
bind_ip=127.0.0.1,10.49.196.12

10.49.196.12:27018

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27018/data
logpath=/home/hadoop/app/mongodb-5.0.5-27018/logs/mongo.log
fork=true
replSet=rs0
port=27018
bind_ip=127.0.0.1,10.49.196.12
 Copy code

Note: data directory and log directory need to be created by yourself.

3. Startup

Use the following command on each machine to start MongoDB:

cd $MONGODB_HMOE/bin

./mongod --config ./mongod.conf

3.4. Initialize} Replica Set

Connect any node of the Primary and Secondary nodes and perform initialization operations:

> use admin
> cfg = {_id: "rs0",members:[{_id: 0,host: '10.49.196.10:27017',priority: 3},{_id: 1,host: '10.49.196.11:27017',priority: 2},{_id: 2,host: '10.49.196.12:27017',priority: 1},{_id: 3,host: '10.49.196.12:27018',arbiterOnly: true}]};
> rs.initiate(cfg)

Outermost_ id refers to the name of Replica Set, and members contain the addresses and priorities of all nodes; The highest priority node becomes the master node. For the arbitration node, you need to set: arbiterOnly:true.

3.5. Viewing status

rs0:PRIMARY> rs.status()

3.6 make Secondary readable

By default, the Secondary is not writable or readable; You can make # Secondary readable by executing # rs.slaveOk() (the new version has expired) or # rs.secondaryOk():

rs0:SECONDARY> rs.secondaryOk()

4. Sharding split installation

4.1 planning

ipportroleMongoDB installation directory
10.49.196.1027011Router(mongos)/home/hadoop/app/mongodb-5.0.5-27011-mongos
27012Config Server(config Primary)/home/hadoop/app/mongodb-5.0.5-27012-config
27013Shard(shard1 Secondary)/home/hadoop/app/mongodb-5.0.5-27013-shard1
27014Shard(shard2  Secondary)/home/hadoop/app/mongodb-5.0.5-27014-shard2
10.49.196.1127011Router(mongos)/home/hadoop/app/mongodb-5.0.5-27011-mongos
27012Config Server(config Secondary)/home/hadoop/app/mongodb-5.0.5-27012-config
27013Shard(shard1 Primary)/home/hadoop/app/mongodb-5.0.5-27013-shard1
27014Shard(shard2 Secondary)/home/hadoop/app/mongodb-5.0.5-27014-shard2
10.49.196.1227011Router(mongos)/home/hadoop/app/mongodb-5.0.5-27011-mongos
27012Config Server(config Secondary)/home/hadoop/app/mongodb-5.0.5-27012-config
27013Shard(shard1 Secondary)/home/hadoop/app/mongodb-5.0.5-27013-shard1
27014Shard(shard2 Primary)/home/hadoop/app/mongodb-5.0.5-27014-shard2

4.2. Start Config Server

A. Start each node of config with the following command:

./mongod --config ./mon.conf

Configuration file of each node mon The contents of conf are as follows:

10.49.196.10:27012

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27013-config/data
logpath=/home/hadoop/app/mongodb-5.0.5-27012-config/logs/mon.log
fork=true
replSet=config
bind_ip=127.0.0.1,10.49.196.10
port=27012
configsvr=true
 Copy code

10.49.196.11:27012

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27013-config/data
logpath=/home/hadoop/app/mongodb-5.0.5-27012-config/logs/mon.log
fork=true
replSet=config
bind_ip=127.0.0.1,10.49.196.11
port=27012
configsvr=true
 Copy code

10.49.196.12:27012

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27013-config/data
logpath=/home/hadoop/app/mongodb-5.0.5-27013-config/logs/mon.log
fork=true
replSet=config
bind_ip=127.0.0.1,10.49.196.12
port=27012
configsvr=true
 Copy code

B. Initialize replica set config

Log in to any node (. / mong --port 27012) and execute the following command:

> use admin
> cfg = {_id: "config",members:[{_id: 1,host: '10.49.196.10:27012',priority: 3},{_id: 2,host: '10.49.196.11:27012',priority: 1},{_id: 3,host: '10.49.196.12:27012',priority: 2}]};
> rs.initiate(cfg)

4.3. Start Shard

4.3.1 start shard1

A. Start each node of shard1 with the following command:

./mongod --config ./mon.conf

Configuration file of each node mon The contents of conf are as follows:

10.49.196.10:27013

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27013-shard1/data
logpath=/home/hadoop/app/mongodb-5.0.5-27013-shard1/logs/mon.log
fork=true
replSet=shard1
bind_ip=127.0.0.1,10.49.196.10
port=27013
shardsvr=true
 Copy code

10.49.196.11:27013

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27013-shard1/data
logpath=/home/hadoop/app/mongodb-5.0.5-27013-shard1/logs/mon.log
fork=true
replSet=shard1
bind_ip=127.0.0.1,10.49.196.11
port=27013
shardsvr=true
 Copy code

10.49.196.12:27013

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27013-shard1/data
logpath=/home/hadoop/app/mongodb-5.0.5-27013-shard1/logs/mon.log
fork=true
replSet=shard1
bind_ip=127.0.0.1,10.49.196.12
port=27013
shardsvr=true
 Copy code

B. Initialize shard1

Log in to any node (. / mong --port 27013) and execute the following command:

> use admin
> cfg = {_id: "shard1",members:[{_id: 1,host: '10.49.196.10:27013',priority: 2},{_id: 2,host: '10.49.196.11:27013',priority: 3},{_id: 3,host: '10.49.196.12:27013',priority: 1}]};
> rs.initiate(cfg)

4.3.2 start shard2

A. Start each node of shard2 with the following command:

./mongod --config ./mon.conf

Configuration file of each node mon The contents of conf are as follows:

10.49.196.10:27014

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27014-shard2/data
logpath=/home/hadoop/app/mongodb-5.0.5-27014-shard2/logs/mon.log
fork=true
replSet=shard2
bind_ip=127.0.0.1,10.49.196.10
port=27014
shardsvr=true
 Copy code

10.49.196.11:27014

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27014-shard2/data
logpath=/home/hadoop/app/mongodb-5.0.5-27014-shard2/logs/mon.log
fork=true
replSet=shard2
bind_ip=127.0.0.1,10.49.196.11
port=27014
shardsvr=true
 Copy code

10.49.196.12:27014

Copy code
dbpath=/home/hadoop/app/mongodb-5.0.5-27014-shard2/data
logpath=/home/hadoop/app/mongodb-5.0.5-27014-shard2/logs/mon.log
fork=true
replSet=shard2
bind_ip=127.0.0.1,10.49.196.12
port=27014
shardsvr=true
 Copy code

B. Initialize shard2

Log in to any node (. / mong --port 27014) and execute the following command:

> use admin
> cfg = {_id: "shard2",members:[{_id: 1,host: '10.49.196.10:27014',priority: 1},{_id: 2,host: '10.49.196.11:27014',priority: 2},{_id: 3,host: '10.49.196.12:27014',priority: 3}]};
> rs.initiate(cfg)

4.3. Start Route

Here, take starting a Route as an example to demonstrate that other routes are similar operations.

1. Start Route with the following command:

./mongos -f ./mon.conf

Configuration file mon The contents of conf are as follows:

logpath=/home/hadoop/app/mongodb-5.0.5-27011-mongos/logs/mon.log
fork=true
bind_ip=127.0.0.1,10.49.196.10
port=27011
configdb=config/10.49.196.10:27012,10.49.196.11:27012,10.49.196.12:27012  #Replica set corresponding to the configuration

2. Configuring Sharding

./mongo --port 27011

mongos> use admin
mongos> sh.addShard("shard1/10.49.196.10:27013,10.49.196.12:27013,10.49.196.12:27013");
mongos> sh.addShard("shard2/10.49.196.10:27014,10.49.196.12:27014,10.49.196.12:27014");

3. View cluster status

mongos> sh.status()

4. View slice information

mongos> use config
mongos> db.shards.find()

5. View chunk information

mongos> use config
mongos> db.chunks.find()

6. Specifies the database that uses sharding

mongos> sh.enableSharding("testdb")

7. hash fragmentation example

Specify the set and key of shards first

mongos> use testdb
mongos> db.createCollection('col1')
mongos> sh.enableSharding("testdb")
mongos> sh.shardCollection("testdb.col1", {"name":"hashed"})

Insert data into the collection:

mongos> for(i=1;i<=1000;i++){db.col1.insert({"id":i,"name":"test" + i})};

To view statistics for a collection:

Copy code
mongos> db.col1.find()

...
"shards" : {
                "shard2" : {
                        "ns" : "testdb.col1",
                        "size" : 24274,
                        "count" : 468,
                        "avgObjSize" : 51,
                        "storageSize" : 28672,
                        "freeStorageSize" : 0,
                        "capped" : false,
...
                "shard1" : {
                        "ns" : "testdb.col1",
                        "size" : 27619,
                        "count" : 532,
                        "avgObjSize" : 51,
                        "storageSize" : 32768,
...
Copy code

It can be seen that there are 468 records on shard2 and 532 records on shard1.

8. Range slicing example

Specify the set and key of shards first

mongos> use testdb
mongos> db.createCollection('col2')
mongos> sh.enableSharding("testdb")
mongos> sh.shardCollection("testdb.col2", {"x":1})

Insert data into the collection:

mongos> for(i=1;i<=1000;i++){db.col2.insert({"x":i,"name":"test" + i})};

Keywords: Java Database MongoDB architecture nosql

Added by Jamesm on Sat, 26 Feb 2022 08:55:06 +0200