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 build Master-Slave replication in mongoDB

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

It is believed that many inexperienced people have no idea about how to build master-slave replication in mongoDB. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Explain the building process of mongoDB master-slave replication in detail.

Master 192.168.0.4 from 192.168.0.7

Installation of mongodb

1: download mongodb www.mongodb.org and download the latest stable version

Check which method is suitable for downloading your server (if wget is not available, you can download it in the following way)

Wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.5.tgzcurl-O-L https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.5.tgz

2: the decompressed file does not need to be compiled, it is a compiled binary executable file.

Tar zxvf mongodb-linux-x86_64-rhel62-3.0.5.tgz # Unzip mv mongodb-linux-x86_64-rhel62-3.0.5 / usr/local/mongodb # change the folder name cd / usr/local/mongodb/bin

Create two folders to store data and logs

Mkdir-p / home/map/mongodb/mongo / home/map/mongodb/mlog/

What are the uses of the tools under mongodb's bin:

Mongod: database server, similar to mysqld. Each instance starts a process, which can be run for Daemon by fork.

Mongo: the client command line tool, similar to sqlplus/mysql, is actually a js interpreter that supports js syntax

Mongodump/mongorestore: import data into a file in bson format / restore an bson file to a database, similar to xtracbackup

Mongoexport/mongoimport: export collection to json/csv format data / import data into a database, similar to-mysqldump/mysqlimport

Bsondump: dump files in bson format to data in json format

Mongos: sharded routing. If sharding is used, the application connects to mongos instead of mongod.

Mongofiles:GridFS Management tool mongostat: real-time Monitoring tool

Master-slave server configuration

Vim mongod.conf

The main configuration is as follows

Port=27018 # specify port fork=true # run dbpath=/home/map/mongodb/mongo # specify the location of the database logpath=/home/map/mongodb/mlog/mongodb.log # specify the log file of the database master=true # set the main # bind_ip=127.0.0.1192.168.0.4 # allowed address for security nohttpinterface=true # disable http access

Perform the following steps

. / mongod-f. / mongod.conf # start the database. / mongo # enter the database

All of the above are configured from the server

Port=27018 # specify port fork=true # background run dbpath=/home/map/mongodb/mongo # specify the location of the database logpath=/home/map/mongodb/mlog/mongodb.log # specify the log file of the database slave=true # declare which ip to be subordinate from source=192.168.0.4:27018 # Note: ip is the best address for the master server to use the intranet ip# bind_ip=127.0.0.1192.168.0.4 # allowed address for security nohttpinterface=true # disable http access

Perform the following steps

. / mongod-f. / mongod.conf # start the database. / mongo # enter the database

Test master execution

Use worddb.wsd.save ({name: "wangshudong"}) db.wsd.find ()

Execute from the server

Rs.slaveOk (); show dbs;local 0.078GBword 0.078GB # if the main data indicates that the synchronization is successful use worddb.wsd.find () # check the data that the synchronization is successful {"_ id": ObjectId ("55cc43470278a3d7b1c6f2c0"), "name": "wangshudong"}

For the sake of security, finally pay attention to the prohibition of remote connections

Iptables-I INPUT-s 192.168.0.4-p tcp-- dport 27018-j ACCEPT# accepts ip access from 192.168.0.4 intranet iptables-An INPUT-I eth0-p tcp-- dport 27018-j DROP # rejects all ports from 27018, have you mastered how to build master-slave replication in mongoDB? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Database

Wechat

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

12
Report