• notice
  • Congratulations on the launch of the Sought Tech site

Install mongodb replica set

The steps to install mongodb replica set are as follows:

1. Enter the download page of the official website https://www.mongodb.com.

2. Put the file into the linux server (downloaded here is Red Hat 6.X or CentOS6.X) installation file: mongodb-linux-x86_64-rhel62-3.4.4.tgz

3. Decompress the file: tar xvfz mongodb-linux-x86_64-rhel62-3.4.4.tgz

4. Rename: mv mongodb-linux-x86_64-rhel62-3.4.4 mongodb

5. Prepare the replication set node; create four directories plus a configuration file.

Prepare the replication set: mongodb_node1, create the mongodb_node1 directory and create four directories under this directory: data log conf bin

6. Prepare to start the configuration file. Set configuration startup parameters under conf: mongod.conf.

vim conf/mongod.conf

7. Use keyFile user authentication. Prepare the keyFile file under data. And set permissions to 600.

openssl rand -base64 753 >keyFile

chmod 600 data/keyFile

8. Copy the startup command.

cp /home/mongodb/mongodb/bin/mongod /home/mongodb/mongodb_node1/bin/

cp /home/mongodb/mongodb/bin/mongo /home/mongodb/mongodb_node1/bin/

9. Prepare the replica set node mongodb_node2 according to points 5 to 9, in which only the port in conf/mongod.conf is changed to port 12346.

10. Start two replica sets of mongod.

 /home/mongodb/mongodb_node1/bin/mongod -f /home/mongodb/mongodb_node1/conf/mongod.conf

 /home/mongodb/mongodb_node2/bin/mongod -f /home/mongodb/mongodb_node2/conf/mongod.conf

12. Start the client. Set up a replica set. And create a user admin.




config = {"_id":"slnodes","members":[{"_id":0,"host":""},{"_id":1,"host":"192.168. 6.155:12346"}]}


13. Then add a set of data on the primary node to see if the secondary node can see it. If you see it, it means that the creation of mongodb replication set is successful;

master node:

Secondary node:

It should be noted here that both the primary and secondary nodes need to log in with the user created on your network, and the secondary node must use: rs.slaveOk(true); to view the collection data when viewing data.

Then: If you want to use an external client to access the mongodb service, you need to open the above service port: add the following two lines to iptables, and then restart the firewall.

vi /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 12345 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 12346 -j ACCEPT

service iptales restart


Technical otaku

Sought technology together

Related Topic


Leave a Reply