In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
MongoDB has become the best-known NoSQL database on the market. MongoDB is document-oriented, and its schemaless design makes it popular in a variety of WEB applications. One of my favorite features is its Replica Set, which places multiple copies of the same data on a set of mongod nodes, resulting in data redundancy and high availability.
This tutorial will show you how to configure a MongoDB replica set.
The most common configuration of replica sets requires a primary node and multiple secondary nodes. The replication behavior that is initiated after that will go from this primary node to other secondary nodes. Replica sets not only protect the database from unexpected hardware failures and downtime events, but also improve the throughput of database client data reads because it provides more nodes.
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-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 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:nogroupkeyFile
$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 primary
192.168.50.2 primary
192.168.50.3 secondary1
192.168.50.4 secondary2
On secondary node 1:
127.0.0.1 localhostsecondary1
192.168.50.2 primary
192.168.50.3 secondary1
192.168.50.4 secondary2
On secondary 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 connectivity between the nodes.
$ping primary
$ping secondary1
$ping secondary2
Configure 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/mongodb
Logpath=/var/log/mongodb/mongod.log
Logappend=true
# auth = true
KeyFile=/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 > 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 to the entire replica set from shell, you can install the following command. Failed switching in the replica set is automatic.
$mongoprimary,secondary1,secondary2:27017/?replicaSet=myReplica
If you use other driver languages (for example, JavaScript, Ruby, etc.), the format may be different.
I hope this tutorial will be helpful to you. You can use Vagrant to automate your local environment configuration and speed up your code.
Get free IT education original cloud computing training video / detailed linux tutorials, consult the official website customer service: http://www.lampbrother.net/linux/ or hook up Q2430675018 ~
Welcome to linux communication group 478068715.
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.