How to configure a MongoDB replica set

Source: Internet
Author: User
Tags auth install mongodb

MongoDB has become the most well-known NoSQL database on the market. MongoDB is document-oriented and its modeless design makes it popular among a wide variety of Web applications. One of the most favorite features is its replica set (Replica set), which places multiple copies of the same data on a set of Mongod nodes for redundancy and high availability of data.

This tutorial will show you how to configure a MongoDB replica set.

The most common configuration of a replica set requires one master node and multiple secondary nodes. The replication behavior that starts after this occurs from this primary node to the other secondary nodes. The replica set not only protects the database against unexpected hardware failures and downtime events, but also increases the throughput of database client data reads by providing more nodes.

Configuring the Environment

In this tutorial, we will configure a replica set that includes a primary node and two secondary nodes.

To achieve this, we used 3 virtual machines running on the VirtualBox. I will install Ubuntu 14.04 on these virtual machines and install the MongoDB official package.

I'll configure the desired environment on a virtual machine instance and then clone it to another instance of the virtual machine. Therefore, select a virtual machine named Master to perform the following installation procedures.

First, we need to add a MongoDB key to apt:

$ sudo apt-key adv--keyserver hkp://keyserver.ubuntu.com:80--recv 7F0CEB10

Then, add the official MongoDB repository to the source.list:

$ sudo su

# echo "Debhttp://repo.mongodb.org/apt/ubuntu" $ (LSB_RELEASE-SC) "/mongodb-org/3.0 Multiverse" | sudo tee/etc/apt/sources.list.d/mongodb-org-3.0.list

Next, update the APT warehouse and install MongoDB.

$ sudo apt-get update

$ sudo apt-get install-ymongodb-org

Now make some changes to/etc/mongodb.conf

Auth = True

Dbpath=/var/lib/mongodb

Logpath=/var/log/mongodb/mongod.log

Logappend=true

Keyfile=/var/lib/mongodb/keyfile

Replset=myreplica

The purpose of the first line is to show that our database needs to be validated before it can be used. The keyfile configures a key file for the replication behavior between MongoDB nodes. Replset set a name for the replica set.

Next we create a key file for all instances.

$ echo-n "Myrandomstringforreplicaset" | md5sum > KeyFile

This will create a key file with the MD5 string, but because it contains some noise, we need to clean it up before we can formally use it in MongoDB.

$ echo-n "Myreplicasetkey" | Md5sum|grep-o "[0-9a-z]\+" >keyfile

grep The purpose of the command is to print out the MD5 string after filtering out the whitespace and other things we don't want.

Now let's do some work on the key file to make it really usable.

$ sudo cp keyfile/var/lib/mongodb

$ sudo chown mongodb:nogroupkeyfile

$ sudo chmod keyfile

Next, turn off this virtual machine. Clone their Ubuntu system to another virtual machine.

This is the clone of sub-node 1 and sub-node 2. Confirm that you have re-initialized their MAC address and cloned the entire hard drive.

Note that three virtual machine samples need to be in the same network to communicate with each other. So we need them to get to the Internet.

It is recommended that you set a static IP address for each virtual machine instead of using DHCP. This way they do not lose connectivity when DHCP assigns IP addresses to them.

Edit the/etc/networks/interfaces file for each virtual machine as follows.

On the master node:

Auto Eth1

Iface eth1 inet Static

Address 192.168.50.2

Netmask 255.255.255.0

On sub-node 1:

Auto Eth1

Iface eth1 inet Static

Address 192.168.50.3

Netmask 255.255.255.0

On sub-node 2:

Auto Eth1

Iface eth1 inet Static

Address 192.168.50.4

Netmask 255.255.255.0

Since we do not have a DNS service, we need to set up/etc/hosts this file, manually put the host name into this file.

On the master node:

127.0.0.1 localhost Primary

192.168.50.2 Primary

192.168.50.3 Secondary1

192.168.50.4 Secondary2

On sub-node 1:

127.0.0.1 Localhostsecondary1

192.168.50.2 Primary

192.168.50.3 Secondary1

192.168.50.4 Secondary2

On sub-node 2:

127.0.0.1 Localhostsecondary2

192.168.50.2 Primary

192.168.50.3 Secondary1

192.168.50.4 Secondary2

Use the ping command to check the connections between each node.

$ ping Primary

$ ping Secondary1

$ ping Secondary2

Configuring replica Sets

After verifying that the individual nodes are connected properly, we can create a new administrator user for subsequent replica set operations.

On the master node, open the/etc/mongodb.conf file and comment out the auth and replset two items.

Dbpath=/var/lib/mongodb

Logpath=/var/log/mongodb/mongod.log

Logappend=true

#auth = True

Keyfile=/var/lib/mongodb/keyfile

#replSet =myreplica

Before configuring any user or replica set on a newly installed MongoDB, you need to comment out the Auth line. By default, MongoDB does not create any users. And if you enable Auth before you create a user, you won't be able to do anything. You can enable Auth again after creating a user.

After modifying/etc/mongodb.conf, restart the mongod process.

$ sudo service mongod restart

Connect to MongoDB Master now:

$ mongo<master-ip-address>:27017

After you connect to MongoDB, create a new administrator user.

> Use admin

> Db.createuser ({

User: "Admin",

PWD: "

})

To restart MongoDB:

$ sudo service mongod restart

Connect to MongoDB again and use the next command to add the secondary node 1 and the secondary node 2 nodes to our replica set.

> Use admin

>db.auth ("admin", "Myreallyhardpassword")

> Rs.initiate ()

> Rs.add ("secondary1:27017")

>rs.add ("secondary2:27017")

Now that the replica set is available, we can start our project. Refer to the official driver documentation to learn how to connect to a replica set. If you want to use the Shell to request data, then you need to connect to the main node to insert or request data, the secondary node does not. If you're determined to try a replica set, the following error message pops up to greet you.

Myreplica:secondary>

Myreplica:secondary> showdatabases

2015-05-10t03:09:24.131+0000e QUERY error:listdatabases failed:{"note": "From ExecCommand", "OK": 0, "errmsg": "Not Master "}

At Error ()

At Mongo.getdbs (src/mongo/shell/mongo.js:47:15)

At Shellhelper.show (src/mongo/shell/utils.js:630:33)

At Shellhelper (src/mongo/shell/utils.js:524:36)

At (SHELLHELP2): 1:1 at src/mongo/shell/mongo.js:47

If you want to connect from the shell to the entire replica set, you can install the following command. Failover in the replica set is automatic.

$ mongoprimary,secondary1,secondary2:27017/?replicaset=myreplica

If you use other driver languages (for example, JavaScript, Ruby, and so on), the format may be different.

I hope this tutorial will help you. You can use vagrant to get your local environment configuration done and speed up your code.

free pick up brother even it education original Cloud Computing Training video/Detailed Linux tutorials, details of the website customer service: http://www.lampbrother.net/linux/ or hooking up with q2430675018~.

Welcome to the Linux Communication Group 478068715


How to configure a MongoDB replica set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.