In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to configure MongoDB replica sets under Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
MongoDB replication is the process of synchronizing data on multiple servers. Replication provides redundant backup of data, and stores copies of data on multiple servers, which improves the availability of data and ensures the security of data. Replication also allows you to recover data from hardware failures and service disruptions.
Configure the environment
In this tutorial, we will configure a replica set that includes one primary node and two secondary nodes.
To achieve this, we used three virtual machines running on VirtualBox. I will install Ubuntu 14.04 on these virtual machines and install the MongoDB official package.
I will configure the required environment on one virtual machine instance and then clone it to another virtual machine instance. Therefore, select a virtual machine named master and perform the following installation procedure.
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 source.list:
$sudo su# echo "deb http://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 repository and install MongoDB.
$sudo apt-get update$ sudo apt-get install-y mongodb-org
Now make some changes to / etc/mongodb.conf
Auth = truedbpath=/var/lib/mongodblogpath=/var/log/mongodb/mongod.loglogappend=truekeyFile=/var/lib/mongodb/keyFilereplSet=myReplica
The purpose of the first line is to indicate that our database needs to be verified before it can be used. Keyfile configures the key file used for replication between MongoDB nodes. ReplSet sets 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 that contains MD5 strings, but because it contains some noise, we need to clean them up before we can officially use them in MongoDB.
$echo-n "MyReplicaSetKey" | md5sum | grep-o "[0-9a-z]\ +" > keyFile
The function of the grep command is to print out the MD5 string after filtering out spaces and other unwanted content.
Now let's do something with the key file to make it really available.
$sudo cp keyFile / var/lib/mongodb$ sudo chown mongodb:nogroup keyFile$ sudo chmod 400 keyFile
Next, shut down the virtual machine. Clone its Ubuntu system to another virtual machine.
This is the cloned secondary node 1 and secondary node 2. Make sure you have reinitialized their MAC addresses and cloned the entire hard drive.
Note that the three virtual machine examples need to be on the same network in order to communicate with each other. Therefore, we need them to get on the Internet.
It is recommended that you set a static IP address for each virtual machine instead of using DHCP. So they don't lose connectivity when DHCP assigns them an IP address.
Edit the / etc/networks/interfaces file for each virtual machine as follows.
On the primary node:
Auto eth2 iface eth2 inet static address 192.168.50.2 netmask 255.255.255.0
On secondary node 1:
Auto eth2 iface eth2 inet static address 192.168.50.3 netmask 255.255.255.0
On secondary node 2:
Auto eth2 iface eth2 inet static address 192.168.50.4 netmask 255.255.255.0
Since we don't have a DNS service, we need to set up the / etc/hosts file and manually put the host name in this file.
On the primary node:
127.0.0.1 localhost primary192.168.50.2 primary192.168.50.3 secondary1192.168.50.4 secondary2
On secondary node 1:
127.0.0.1 localhost secondary1192.168.50.2 primary192.168.50.3 secondary1192.168.50.4 secondary2
On secondary node 2:
127.0.0.1 localhost secondary2192.168.50.2 primary192.168.50.3 secondary1192.168.50.4 secondary2
Use the ping command to check the connectivity between the nodes.
$ping primary$ ping secondary1 $ping secondary2 configuration copy set
After verifying that the nodes are connected properly, we can create a new administrator user for subsequent replica set operations.
On the main node, open the / etc/mongodb.conf file and comment out the auth and replSet.
Dbpath=/var/lib/mongodblogpath=/var/log/mongodb/mongod.loglogappend=true#auth = truekeyFile=/var/lib/mongodb/keyFile#replSet=myReplica
Before configuring any users or replica sets on a newly installed MongoDB, you need to comment out the auth lines. By default, MongoDB does not create any users. And if you enable auth before you create a user, you can't do anything. You can enable auth again after creating a user.
After modifying / etc/mongodb.conf, restart the mongod process.
$sudo service mongod restart
Now connect to MongoDB master:
$mongo: 27017
After connecting to MongoDB, create a new administrator user.
> use admin > db.createUser ({user: "admin", pwd: "})
Restart MongoDB:
$sudo service mongod restart
Connect to MongoDB again and add the secondary node 1 and secondary node 2 nodes to our replica set with the following command.
> use admin > db.auth ("admin", "myreallyhardpassword") > rs.initiate () > rs.add ("secondary1:27017") > rs.add ("secondary2:27017")
Now that we have the copy set, we can start our project. Refer to the official driver documentation to learn how to connect to a copy set. If you want to use Shell to request data, you need to connect to the primary node to insert or request data, not the secondary node. If you insist on trying to operate with a copy set, the following error message pops up to greet you.
MyReplica:SECONDARY > myReplica:SECONDARY > show databases2015-05-10T03:09:24.131+0000 E 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 to the entire replica set from shell, you can install the following command. Failed switching in the replica set is automatic.
$mongo primary,secondary1,secondary2:27017/?replicaSet=myReplica
If you use other driver languages (for example, JavaScript, Ruby, etc.), the format may be different.
Thank you for reading! This is the end of this article on "how to configure MongoDB replica set under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.