可以在linode官网购买服务器进行配置
Mongodb 集群的安装 centos6.4
MongoDB is an open-source non-SQL database engine. MongoDB is scalable and an alternative to the standard relational database management system (RDBMS). A replication set is used for redundancy and to provide access to your data in the event of a node failure.
Before installing MongoDB, it is assumed that you have followed our getting started guide. If you are new to Linux server administration, you may want to consult our using Linux document series including the Introduction to Linux Concepts guide and Administration Basics guide.
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you are not familiar with the sudo command, you can review our Users and Groupsguide.
Installing MongoDB
-
Make sure the hostname is set on every member of the replication set by editing the hostname file:
|
|
-
Replace the name in brackets <> with your own hostname. This example uses the Nano text editor. However, you can use the text editor you prefer.
-
/etc/hostname
|
|
-
Create a file to hold the configuration information for the MongoDB repository:
|
|
-
If you are running a 64-bit system, use the following configuration:
|
|
-
For a 32-bit system, use the following configuration:
|
|
-
A 32-bit system is not recommended for production deployments.
-
Install MongoDB using the command:
|
|
Configuring Networking
It is imperative that the networking configurations are set and working properly, or you will not be able to add members to the replication set. This section will provide in detail how to configure three (3) Linodes as a MongoDB replication set.
Before you begin, you will need to obtain all the private IP addresses for each of your Linodes. This information can be found by logging into the Linode Manager. Under the Remote Access tab there is a section called, "Private/LAN Network". Click on the "Add a Private IP" link to assign a private IP address to your Linode. This is the address you will use to configure your hosts file. Again, we are working with a three member replication set so you will need to acquire this information for each member.
Setting the Hosts File
Once you have all your private IPs, you can add them to the hosts file. Use your favorite text editor and add the addresses.
/etc/hosts
|
|
Use your own IP addresses in place of the addresses in the above example. The names of the members in the replication set are also variables, so you may name them what you choose. However, it would be prudent to have some numerical or alphabetic notation as this will make it easier to identify when connecting to the different replication set members.
Replication set member names and the actual server name are different. In this instance, the server name iseuropa, and the replication set members are mongo1, mongo2, and mongo3 respectively.
Set the Network Interfaces
-
Edit your ifcfg-eth0 file to include the public IP address information.
/etc/sysconfig/network-scripts/ifcfg-eth0
|
|
Replace the sample addresses with your own IP information.
-
Next you will need to create a file for your private IP information. You can do this by copying the eth0 file with the name ifcfg-eth0:1:
|
|
-
Now edit your newly created eth0:1 file to reflect your private IP information:
/etc/sysconfig/network-scripts/ifcfg-eth0:1
|
|
Again you will replace the sample addresses with your own IP information.
-
Restart the networking service to ensure your interface changes have taken effect. Use the command:
|
|
Edit the Mongo Conf File
-
Edit the mongod.conf file to add the IP address and port number.
/etc/mongod.conf
|
|
Enter the private IP address of the server you are logged onto in the bind ip section. If bind_ip is not present, you will need to add it. Leave the default port number of 27017, and uncomment the line fork = true.
-
While still in the mongodb.conf file scroll to the bottom and add the replica set information:
/etc/mongod.conf
|
|
In this example, the sample replication set is rs1, however, you may change the name as you choose.
Replication Sets
A replication set will allow your data to be "copied over" or propagated to all other members in the set. It provides redundancy in the event of system failure. It is recommended that an odd number of members be used in a set since it will make elections easier.
Elections are to select which set member will become the primary. Elections take place after the replication set is initiated and when the primary is not available. The primary member is the only one that can accept write operations. In the event the primary is not available, elections take place to select a new primary. This election action allows the set to resume normal operations without manual intervention.
Creating a Replication Set
A mongod.conf file was created during the installation. You will use this configuration file to start the daemon on every member of the replication set.
-
The command is as follows:
|
|
-
Your output should look similar once the daemon has started.
|
|
-
Start MongoDB client on only one member of the replication set with the command:
|
|
-
The variable is the name of replication set member you are working on, in this examplemongo0.
-
At the MongoDB prompt, switch to admin with the command:
|
|
-
You should see the message switched to db admin.
-
Run the rs.initiate() command which will begin creating the replication set with the current member. The output should look similar to the following:
|
|
-
To see the current configuration run the command:
|
|
-
The output should look similar to the following:
|
|
-
To add a member to your replication set use the command:
|
|
-
Below is output for the command rs.add:
|
|
-
To verify that the members have been added correctly run the rs.conf() command again. The output should look similar to the following:
|
|
Verifying Replication
The best way to verify that replication is working and the members are all communicating is to create a new test database. By default, the existing test database is used when you connect to MongoDB. In order to save the new database, data will need to be added. The process for creating and inserting data is as follows:
-
Create a database with the command:
|
|
Replace the variable products with any name you like.
-
Add some data:
|
|
-
If you are not on the primary member in the set, you will receive the message not master. Switch to the primary member and run the commands again. Now use the command:
|
|
-
A list of databases will be displayed. Your new <products> database should appear in the list. Connect to one of the other members of the set and see if the newly created database has propagated.
Adding New Members to an Existing ReplSet
Before you add a new member, its data directory must be empty. Once the new member is added it will copy over all the data from an existing member in the replication set.
Members can be added to the replication set at any time. In order to re-add a removed member or to add a totally new member, you must be connected to the primary member of the replication set. Before you issue the "add" command, you must switch to admin. At the MongoDB prompt issue the command:
|
|
Then use the following command to add a member:
|
|
For this replset configuration, only the hostname was required to add a new member.
An example of the add member process is included for your reference. Make sure to change names and port numbers to reflect your particular configuration.
Use the rs.conf() command to check if the new member is present in the configuration file. In addition, any database should propagate almost immediately (depending on its size) over to the new member.
Database Concepts and Commands
MongoDB is different from SQL in its classification of data as well as its commands. The following sections will provide some basic commands and data descriptions.
Data Classifications
To clarify how data is stored it is important to understand how MongoDB classifies data. The data is classified as follows:
-
A database is the container for collections
-
A collection is a group of documents, it is synonymous with tables
-
A document contains basic units of data
-
Fields are analogous to columns
-
A key is a name (string)
-
A value is basic type such as a string or an array of values
Basic MongoDB Commands
Command |
Description |
help |
displays a short list of help commands |
show dbs |
displays a list of all the databases |
use <db> |
sets the current database |
show collections |
displays the collections for the current database |
show users |
displays the users in the current database |
rs.status |
displays detailed status of each member of the replication set |
rs.conf |
displays the members of the replication set |
db.help |
displays help for database methods |
insert() |
inserts a new document into a collection |
update() |
updates an existing document in a collection |
save |
updates an existing document in a collection or inserts a new document |
remove |
deletes a document from a collection |
drop |
removes a collection completely |
It is important to note that MongoDB uses parentheses () at the end of several commands, comparable to the semicolon in SQL.
MongoDB Server Service
In the event you need to restart, stop or check the status of the MongoDB service, use the following commands:
|
|