MongoDB tutorial

MongoDb tutorial

1, Installing MongoDB on Linux platform

MongoDB provides 32-bit and 64 bit installation packages on linux platform. You can download the installation package on the official website.
Download address: https://www.mongodb.com/download-center#community
1. Find the corresponding download address

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.10.tgz

2. Unzip after downloading

tar zxvf mongodb-linux-x86_64-rhel62-3.2.10.tgz

2, Start Mongodb service

  1. Create directory and assign permissions
mkdir -p /data/mongo/db
mkdir -p /data/mongo/log
chmod 777 /data/mongo/db
chmod 777 /data/mongo/log

2. Start service

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongod --dbpath /data/mongo/db --logpath /data/mongo/log/mongod.log --fork

3. Test whether the service is normal

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongo  #Enter MongoDB background management Shell
db.runoob.insert({x:10})  #Test insert a piece of data
db.runoob.find()   #Test query data

4. Stop mongo service

./mongod /data/mongo/db --logpath /data/mongo/log/mongod.log --shutdown #There is a problem with the test. It is recommended to directly kill the process number

3, Slice

There is another kind of cluster in mongodb, namely, sharding technology, which can meet the demand of large growth of mongodb data volume.

When MongoDB stores a large amount of data, one machine may not be enough to store data, or it may not be enough to provide acceptable read and write throughput. At this time, we can split the data on multiple machines, so that the database system can store and process more data.
Why use shards

  • Copy all writes to the master node
  • The delayed sensitive data will be queried at the master node
  • A single replica set is limited to 12 nodes
  • When the amount of requests is huge, there will be insufficient memory.
  • Insufficient local disk
  • Vertical expansion is expensive

Three main components:

  • Shard:

    It is used to store actual data blocks. In the actual production environment, a shard server role can be assumed by a relica set of several machine groups to prevent a single point of failure of the host

  • Config Server:

    mongod instance, which stores the entire ClusterMetadata, including chunk information.

  • Query Routers:

    Front end routing enables the client to access, and makes the whole cluster look like a single database. Front end applications can be used transparently.
    Slice instance
    The port distribution of slice structure is as follows:

    Shard Server 1: 27020
    Shard Server 2: 27021
    Shard Server 3: 27022
    Shard Server 4: 27023
    Config Server : 27100
    Route Process: 40000
    

Step 1: start Shard Server

mkdir -p /data/mongo/shard/s0
mkdir -p /data/mongo/shard/s1
mkdir -p /data/mongo/shard/s2
mkdir -p /data/mongo/shard/s3
mkdir -p /data/mongo/shard/log

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongod --port 27020 --dbpath=/data/mongo/shard/s0 --logpath=/data/mongo/shard/log/s0.log --logappend --fork

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongod --port 27021 --dbpath=/data/mongo/shard/s1 --logpath=/data/mongo/shard/log/s1.log --logappend --fork

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongod --port 27022 --dbpath=/data/mongo/shard/s2 --logpath=/data/mongo/shard/log/s2.log --logappend --fork

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongod --port 27023 --dbpath=/data/mongo/shard/s3 --logpath=/data/mongo/shard/log/s3.log --logappend --fork

Step 2: start Config Server

mkdir -p /data/mongo/shard/config

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongod --port 27100 --dbpath=/data/mongo/shard/config --logpath=/data/mongo/shard/log/config.log --logappend --fork --configsvr

Step 3: start Route Process

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongos --port 40000 --configdb 127.0.0.1:27100 --fork --logpath=/data/mongo/shard/log/route.log --chunkSize 500 -f /data/mongo/conf/mongos.config 

In mongos startup parameters, chunkSize is used to specify the size of chunks. The unit is MB, and the default size is 200MB

Step 4: configure Sharding
Next, we use the MongoDB Shell to log in to mongos and add the Shard node

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongo admin --port 40000

mongos> db.runCommand({ addshard:"127.0.0.1:27020" })  #Add node 1
mongos> db.runCommand({ addshard:"127.0.0.1:27021" })  #Add node 2
mongos> db.runCommand({ addshard:"127.0.0.1:27022" })  #Add node 3
mongos> db.runCommand({ addshard:"127.0.0.1:27023" })  #Add node 4

mongos> db.runCommand({ enablesharding:"test" }) #Select one of two databases for slice storage
mongos> sh.enableSharding("test")  #Select one of two databases for slice storage

mongos> db.runCommand({ shardcollection: "test.log", key: { id:1,time:1}})#Select one of two keys for setting slice
mongos> sh.shardCollection("test.log",{id:"hashed"}) #Select one of two keys for setting slice

Step 5: without much change in the program code, directly connect the database to the access interface 40000 as connecting to the ordinary mongo database

#View cluster status
> sh.status()
> 

Step 6: create a user and start password access

/data/tools/mongodb-linux-x86_64-rhel62-3.2.10/bin/mongo admin --port 40000

> use admin
> db.createUser(
  {
    user: "admin",
    pwd: "123456",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
> db.auth('admin', '123456')   #Test whether the password is valid. Return 1

Keywords: MongoDB

Added by adamhhh1 on Wed, 29 Dec 2021 20:43:22 +0200