Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize master-slave replication in mongodb

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Mongodb in how to achieve master-slave replication, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Environment description:

System: CentOS6.4_x64

Master (Master): 192.168.2.2

From (Slave): 192.168.2.3

I. installation and basic configuration

# cd / usr/local

# tar-zxvf mongodb-linux-x86_64-2.2.6.tgz

# mv mongodb-linux-x86_64-2.2.6.tgz mongodb

The mongodb configuration file is in / usr/local/mongodb/conf

Master and slave database directories are all in / usr/local/mongodb/data

Log directories are all in / usr/local/mongodb/logs

# mkdir-p / usr/local/mongodb/conf

# mkdir-p / usr/local/mongodb/data

# mkdir-p / usr/local/mongodb/logs

II. One master and one slave configuration

Key points:

1) in the database cluster, it is necessary to know exactly who is the master server. There is only one master server.

2) the slave server needs to know its own data source, that is, who the master server is for itself.

3) master is used to determine the master server, slave is used to control the slave server, and source determines the data source of the slave service.

192.168.2.2 Master

Create a new file mongodb.conf under conf

Add as follows:

Port=27017

Fork=true

Logpath=/usr/local/mongodb/logs/mongodb.log

Logappend=true

Dbpath=/usr/local/mongodb/data

MaxConns=1024

Master=true

OplogSize=2048

192.168.2.3 from

Port=27017

Fork=true

Logpath=/usr/local/mongodb/logs/mongodb.log

Logappend=true

Dbpath=/usr/local/mongodb/data

MaxConns=1024

Slave=true

Source=192.168.2.2:27017

Autoresync=true

3. Start MONGODB

[root@localhost ~] # echo "PATH=$PATH:/usr/local/mongodb/bin" > > / etc/profile

[root@localhost ~] # source / etc/profile

[root@localhost] # mongod-f / usr/local/mongodb/conf/mongod.conf

Enlighten the Lord first and then follow.

Take a look at the master log mongodb.log

You can see that the main database allows 192.168.2.3 to connect from 59172

Fourth, test master-slave replication

Master: 192.168.2.2

Create a database test, the collection name is also test, and insert a field AGE:18. Then to execute show dbs; from the database to see that it has been synchronized.

[root@localhost conf] # mongo

MongoDB shell version: 2.2.6

Connecting to: test

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

Http://docs.mongodb.org/

Questions? Try the support group

Http://groups.google.com/group/mongodb-user

> show dbs

Local4.013671875GB

> use test

Switched to db test

> db.test.save ({"AGE": 18})

> db.test.find ()

{"_ id": ObjectId ("534f592ce7b706845c58740b"), "AGE": 18}

> show dbs

Local4.013671875GB

Test0.203125GB

From: 192.168.2.3

[root@localhost conf] # mongo

MongoDB shell version: 2.2.6

Connecting to: test

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

Http://docs.mongodb.org/

Questions? Try the support group

Http://groups.google.com/group/mongodb-user

> show dbs

Local4.013671875GB

Test0.203125GB

And look at the log on the library, you can see the behavior of synchronization

Fifth, thinking and supplement:

1. How to specify the libraries to be synchronized

By default, all libraries are synchronized. If you need to specify that only one library is synchronized, you can specify it in the configuration file of the slave library.

Method: only=test

It means to synchronize only the TEST library

2. Other parameters that can be set from the library

Slavedelay slave node sets the delay of synchronizing data in the master database (in seconds)

The fastsync slave node starts the slave database with the node snapshot of the master database as the node

Autoresync automatically synchronizes the database if it is not synchronized

3. Check the synchronization status from the server

> db.printReplicationInfo ()

This is a slave, printing slave replication info.

Source: 192.168.2.2:27017

SyncedTo: Thu Apr 17 2014 01:38:02 GMT-0700 (PDT)

= 10 secs ago (0.04hrs)

4. Backup and recovery

Any database needs to be backed up and restored, you know.

Backup:

> mongodump-h dbhost-d dbname-o dbdirectory

-the address of the server where the h:MongDB is located, for example: 192.168.2.2. Of course, you can also specify the port number: 192.168.2.2 h:MongDB 27017

-d: database instance that needs to be backed up, for example: test

-o: the location of the backup data, such as / data/dump. Of course, this directory needs to be established in advance. After the backup is completed, the system automatically creates a test directory under the dump directory, in which the backup data of the database instance is stored.

Restore:

> mongorestore-h dbhost-d dbname-- directoryperdb dbdirectory

-address of the server where the h:MongoDB is located

-d: the database instance to be restored, such as test. Of course, this name can also be different from that of backup, such as test2.

-- directoryperdb: where the backup data is located, for example: / data/dump/test, why add an extra test here instead of the dump at the time of backup? check the tips for yourself!

-- drop: when restoring, delete the current data first, and then restore the backed-up data. That is to say, after recovery, the modified data added after backup will be deleted, so use it cautiously!

5. Monitoring

You can use mongodb with its own port 28017 for graphical monitoring

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report