Non-relational database MongoDB: (2) Replica set deployment instructions, data migration, memory limit, and mongo authentication enabled

Table of contents

One: mongo replica set deployment instructions

1.1 Single node deployment

1.2 Multi-node deployment

Two: mongoDB data migration (mongodump mode)

2.1 Data export

2.2 Data transmission

2.3 Data import

Three: Limit MongoDB memory usage (RHEL7)

3.1 Modify the mongoDB configuration file

3.2 Configure MongoDB to start using systemctl

3.3 Limit MongoDB memory usage

3.4 Start MongoDB

Four: Enable mongo authentication

4.1 Log in to mongDB and create an administrator account under the admin library

4.2 Modify the MongoDB configuration file, enable user authentication (add the last two lines), and restart MongoDB

4.3 Log in to the administrator account and create a business user

Preface: First familiarize yourself with the characteristics, advantages, deployment, and basic commands of the mongoDB database. Look at the non-relational database MongoDB: (1) Understanding, advantages, construction, and basic commands
 


One: mongo replica set deployment instructions

1.1 Single node deployment

The premise of this part is that you have downloaded and installed MongoDB, and it can run normally. Running a replica set on a single node can only be used for development and testing, not as a production environment.

Below we create a replica set named rs0. The configuration environment is ubuntu 12.10 and MongoDB 2.4.

step:

1. Create three folders with the following commands

mkdir -p /srv/mongodb/rs0-0 /srv/mongodb/rs0-1 /srv/mongodb/rs0-2

2. Start three mongod processes. Note that the port numbers cannot be the same and are not occupied. You can use three terminals to connect to the same Linux, and then run a command in each terminal to simulate the use of three servers at a time.

Here are the commands to start the three mongod processes:

mongod --port 27017 --dbpath /srv/mongodb/rs0-0 --replSet rs0 --smallfiles --oplogSize 128 --logpath /var/log/mongod1.log --fork
mongod --port 27018 --dbpath /srv/mongodb/rs0-1 --replSet rs0 --smallfiles --oplogSize 128--logpath /var/log/mongod2.log --fork
mongod --port 27019 --dbpath /srv/mongodb/rs0-2 --replSet rs0 --smallfiles --oplogSize 128--logpath /var/log/mongod3.log --fork

The replSet parameter is followed by the replica set name;

smallfiles and oplogSize are used to reduce disk usage and are only used for testing here, not recommended for production environments;

The logpath parameter specifies the path where the log is saved;

--fork means to run this process as a background process.

3. Now start a mongo process and connect to one of the three processes just started:

mongo --port 27017

4. Create a replica set configuration object for initializing the replica set. The object contents are as follows:

rsconf = {
_id: "rs0",
members: [
{
_id: 0,
host: "<hostname>:27017"
}
]
}

Where hostname refers to your server name, or ip address. Because it is local, you can use localhost.

5. Use the rs.initiate() method to initialize the replica set:

rs.initiate( rsconf )

6. After running the last command, you will be prompted to wait for a while before you can see the result. Now use rs.conf() to view replica cluster information:

rs.conf()

You will find that there is only one node member, which is the node you are currently operating on.

7. Now use the rs.add() command to add the remaining two mongod processes to the replica set:

rs.add("<hostname>:27018")
rs.add("<hostname>:27019")

Be careful to replace hostname with your server name. Wait a moment.

Then use rs.status() to view the current replica cluster information, and you will find that there are now three replica set members. At this point, the deployment of the single-node replica set is complete.

You can try to insert some test data first, and then shut down one of the mongod processes to simulate a node failure. Then use mongo to connect to the new master node, and then try to insert or read information into the database to see if it can read and write normally.

1.2 Multi-node deployment

The multi-node production environment is not much different from the single-node test development environment deployment, except that mongod runs on different servers, but it is necessary to ensure that each node can be resolved by name. In order to prevent adding a lot of parameters every time you run mongod, you can save the parameters in a file, and use --config to specify the configuration file at startup .

step

1. First specify the names of the three servers and add them to the hostname file. The IP address and name of the server can be modified according to your actual environment.

Server 1: mdb1, 10.6.12.79

Server 2: mdb2, 10.6.12.80

Server 3: mdb3, 10.6.12.81

Server 4: mdb4, 10.6.12.82

After configuration, use the ping command to test each other whether they can resolve the server name.

2. Edit the test file and customize the file name, here it is named config.conf. The content is as follows:

port = 27017
#需要监听的客户端IP地址,即只接受来自这部分的IP连接,提高了安全性。(默认为接受所有的IP请求)
bind_ip = 10.6.12.79
dbpath = /srv/mongodb
fork = true
replSet = rs0

3. Use the scp command to copy the configuration file to the other two servers, and use the following command to start the mongod process:

mongod --config /src/config.conf 

Where /src/config.conf is the path of the configuration file.

4. Use mongo to connect to one of the mongod processes, and use rs.initiate() to initialize the replica set:

> rs.initiate()
{
	"info2" : "no configuration explicitly specified -- making one",
	"me" : "mdb2:27017",
	"info" : "Config now saved locally.  Should come online in about a minute.",
	"ok" : 1
}

5. Use rs.conf() to view the current status:

> rs.conf()
{
	"_id" : "rs0",
	"version" : 1,
	"members" : [
		{
			"_id" : 0,
			"host" : "mdb2:27017"
		}
	]
}

6. Use rs.add() to add the other three nodes to the replica set:

> rs.add("mdb1:27017")
{ "ok" : 1 }
rs0:PRIMARY> rs.add("mdb3:27017")
{ "ok" : 1 }

At the same time, add another arbitration node:

rs0:PRIMARY> rs.addArb("mdb4:27017")
{ "ok" : 1 }

From the above content, we can see that rs0:PRIMARY did not appear at the beginning. When a member is added, the node becomes the primary node. This actually means that the node you added has been successfully added to the replica set.

At this point, the cluster node deployment is complete. But in practical applications, because of the need to operate back and forth on different servers, it is recommended to use scripts to automatically start the deployment process.


Two: mongoDB data migration (mongodump mode)

Log in to the machine mapped by lx-mgodb in the original cluster environment. 
This example takes the ZJJQ project as an example. The operation process in other environments is the same, pay attention to the replacement of parameters

2.1 Data export

Check the list of db to export

/opt/lxxn/services/mongodb/bin/mongo --port 27017
show dbs;

Substitute the other db names in the displayed list except local into the parameter after -d in the following export command, and you can define the parameter value of -o yourself. This parameter value is the directory /tmp/dumpfiles where the exported file is located. This directory needs to exist,

Example:

sudo /opt/lxxn/services/mongodb/bin/mongodump --host=localhost --port=27017 -d orgcontact -o='/tmp/dumpfiles/'

Parameter explanation:
–host: server name or IP:PORT
-u: username
-p: password
-d: database dbname
-o: storage path of the exported file

–authenticationDatabase admin: Add –authenticationDatabase admin parameter to formulate authentication database

If there are multiple dbs, modify the parameters after -d, and then execute it multiple times, such as:

sudo /opt/xxin/services/mongodb/bin/mongodump --host=localhost --port=27017 -d orgcontact -o='/tmp/dumpfiles/'
sudo /opt/lxxn/services/mongodb/bin/mongodump --host=localhost --port=27017 -d zjjq-lanxin -o='/tmp/dumpfiles/'
sudo /opt/laxx/services/mongodb/bin/mongodump --host=localhost --port=27017 -d logdb -o='/tmp/dumpfiles/'

2.2 Data transmission

Pack and transfer /tmp/dumpfiles (according to the export parameter value defined by yourself)
to the server designated to perform the import (the client tool of mongodb needs to be installed on the server)

2.3 Data import

To import data in the new environment, replace the parameter value after -d and the absolute path described by the last parameter value by yourself. At the same time, –host points to the ip of the imported mongodb server. The server that executes the import requires the installation of the mongodb client tool.

sudo /opt/xxx/mongo/bin/mongorestore --host=localhost --port=27017 -d orgcontact --drop /tmp/dumpfiles/orgcontact
sudo /opt/xxxx/mongo/bin/mongorestore --host=localhost --port=27017 -d xxx --drop /tmp/dumpfiles/zjjq-lanxin
sudo /opt/xx/mxxongo/bin/mongorestore --host=localhost --port=27017 -d logdb --drop /tmp/dumpfiles/logdb

Data Recovery (Database)

Command: mongorestore -h localhost -u root -p root123 --db admin dump/test_jia/ --authenticationDatabase admin
-h: server name or IP:PORT
-u: username
-p: password
–db: database dbname, needs to be restored The database name
dump/test_jia/: the storage path of the restored file
–authenticationDatabase admin: Add –authenticationDatabase admin parameter to formulate the authentication database


Three: Limit MongoDB memory usage (RHEL7)

3.1 Modify the mongoDB configuration file

vim /opt/xx/conf/mongo/mongo_standalone.conf

Add MongoDB parameter: storage.wiredTiger.engineConfig.cacheSizeGB

systemLog:
   destination: file
   path: "/opt/logs/mongo/mongo_standalone.log"
   logAppend: true
storage:
   dbPath: "/opt/db/mongo/mongo_standalone"
   journal:
      enabled: true
   engine: wiredTiger
   wiredTiger:
      engineConfig:
         cacheSizeGB: 14
processManagement:
   fork: true
   pidFilePath: "/opt/run/mongo/mongo_standalone.pid"
net:
   bindIp: 0.0.0.0
   port: 27017

3.2 Configure MongoDB to start using systemctl

Add mongodb.service file:

vim /etc/systemd/system/mongodb.service 
[Unit]
Description=MongoDB_standalone
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/xxxx/run/mongo/mongo_standalone.pid
ExecStart=/opt/xxxx/init.d/mongo start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/xxxx/init.d/mongo stop
#ExecStop=/bin/kill -s QUIT $MAINPID
#ExecStop=/opt/bxixxx/mongodb/bin/mongod --shutdown --config /opt/bxxxix/conf/mongo/mongo_standalone.conf 
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3.3 Limit MongoDB memory usage

In the /etc/systemd/system/mongodb.service file, [Service] section, add MemoryLimit (blue section)

cat /etc/systemd/system/mongodb.service 
[Unit]
Description=MongoDB_standalone
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/xxx/run/mongo/mongo_standalone.pid
ExecStart=/opt/bxxxxix/init.d/mongo start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/opt/xxxx/init.d/mongo stop
PrivateTmp=true
MemoryLimit=20G

[Install]
WantedBy=multi-user.target

3.4 Start MongoDB

Reload systemctl and start MongoDB with systemctl:

# systemctl daemon-reload 

# systemctl start mongodb.service

# systemctl status mongodb
● mongodb.service - MongoDB_standalone
Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2019-04-01 10:00:54 CST; 3 days ago
Process: 29132 ExecStart=/opt/xxxeix/init.d/mongo start (code=exited, status=0/SUCCESS)
Main PID: 29139 (mongod)
Memory: 18.0G (limit: 20.0G)
CGroup: /system.slice/mongodb.service
└─29139 /opt/bxxxx/mongo/bin/mongod --config /opt/xxxix/conf/mongo/mongo_standalone.conf

Apr 01 10:00:53 shcp-bdu04 systemd[1]: Starting MongoDB_standalone...
Apr 01 10:00:53 shcp-bdu04 runuser[29135]: pam_unix(runuser:session): session opened for user xxxeset by (uid=0)
Apr 01 10:00:53 shcp-bdu04 mongo[29132]: about to fork child process, waiting until server is ready for connections.
Apr 01 10:00:53 shcp-bdu04 mongo[29132]: forked process: 29139
Apr 01 10:00:54 shcp-bdu04 mongo[29132]: child process started successfully, parent exiting
Apr 01 10:00:54 shcp-bdu04 runuser[29135]: pam_unix(runuser:session): session closed for user bxxxeset
Apr 01 10:00:54 shcp-bdu04 mongo[29132]: [45B blob data]
Apr 01 10:00:54 shcp-bdu04 systemd[1]: Started MongoDB_standalone.

Four: Enable mongo authentication

4.1 Log in to mongDB and create an administrator account under the admin library

use admin
db.createUser(
{
    user: "xxxadmin",
    pwd: "xxx",
    roles: [ { role: "root", db: "admin" } ]
}
) 

4.2 Modify the MongoDB configuration file, enable user authentication (add the last two lines), and restart MongoDB

# cat /opt/xxix/conf/mongo/mongo_standalone.conf
systemLog:
   destination: file
   path: "/opt/xeixx/logs/mongo/mongo_standalone.log"
   logAppend: true
storage:
   dbPath: "/opt/xxix/db/mongo/mongo_standalone"
   journal:
      enabled: true
processManagement:
   fork: true
   pidFilePath: "/opt/xix/run/mongo/mongo_standalone.pid"
net:
   bindIp: 0.0.0.0
   port: 27017
security:
   authorization: enabled 

4.3 Log in to the administrator account and create a business user

Two ways to log in, verify when logging in or verify after logging in without authentication:

mongo --port 27017 -u "xxx_admin" -p "xxx" --authenticationDatabase "admin"

或

use admin
db.auth("xxx_admin", "xxx" ) 

Create a business database account. The current situation is that the user names and passwords of the three databases are the same, and there are actually three users (MongoDB’s permission setting is based on the database, and the database must be selected first):

use dbdb
db.createUser(
{
    user: "xxx",
    pwd: "xxx",
    roles: [ { role: "readWrite", db: "dbdb" },
               { role: "dbAdmin", db: "dbdbv" } ]
}
) 

use logdb
db.createUser(
{
    user: "xxxn",
    pwd: "xxxLlnxxf",
    roles: [ { role: "readWrite", db: "logdb" },
               { role: "dbAdmin", db: "logdb" } ]
}
) 

use ondtact
db.createUser(
{
    user: "xxxxn",
    pwd: "xxxxnfxx",
    roles: [ { role: "readWrite", db: "ondtact" },
               { role: "dbAdmin", db: "ondtact" } ]

Guess you like

Origin blog.csdn.net/ver_mouth__/article/details/126226028