Install MongoDB and configure Replica Set

Source: Internet
Author: User
Tags install mongodb

MongoDB environments mainly include StandAlone, Replication, and Sharding.

  • StandAlone: StandAlone environment, usually used for development and testing.
  • Replication: master-slave structure. One Primary and multiple Secondary may have Arbitry.
    • After the Primary fails, a Secondary is elected as the Primary, similar to zookeeper.
    • Arbitry does not store data, just to collect data. The election algorithm requires that the number of nodes must be an odd number. If Primary + Secondary is not an odd number, Arbitry should be used to count.
    • Write data can only be in Primary, and read data is also in Primary by default. You can configure to read data from Secondary and select the nearest node.
    • After the data is successfully written on Primary, the operation will be recorded in oplog, and Secondary will copy the oplog, and then follow the operation again, there will be data.
    • The data above Primary and Secondary ensures final consistency. You can configure write concern for the write operation. There are several levels: After writing the data on Primary, the write operation is considered successful; after writing the data to oplog, the write operation is considered successful; write to one/multiple/One/several Secondary and think the write is successful.
  • Sharding: share nothing structure. Each machine stores only a part of data. The mongod server stores data. The mongos server is responsible for routing read/write requests, and metadata is stored in the config database.

Because of the data volume and machine volume, the project eventually uses a Primary, a Secondary, and an Arbitry. My own development environment is Ubuntu and the test environment is CentOS. The 64-bit MongoDB is installed.

Install on Ubuntu
sudo apt-get install mongodb-10gen
Installation on CentOS

Configure the yum source and create the file:/etc/yum. repos. d/mongodb. repo.

[mongodb]name=MongoDB Repositorybaseurl=http://downloads-distro.mongodb.org/repo/RedHat/os/x86_64/gpgcheck=0enabled=1

Installation command:

yum install mongo-10gen mongo-10gen-server
Configuration

The configuration file/etc/initd. conf on each machine is changed to the following:

# Database file location (default) dbpath =/var/lib/mongo # log location (default) logpath =/var/log/mongo/mongod. log # pid location (default) pidfilepath =/var/run/mongodb/mongod. pid # keyFile location. The generation method is later (ADD) keyFile =/var/lib/mongo/key # port (default) port = 27017 # logs are appended after each start, logappend = true # Start (ADD) in deamon mode fork = true # open operation logs for fault recovery and persistence (default) journal = true # replica set Name (ADD) replSet = test-set

Run on each machine:

sudo mongod -f /etc/mongod.conf

In my environment, Primary ip: 192.168.1.1, Secondary ip: 192.168.1.2, and Arbitary ip: 192.168.1.3. You can set multiple mongodb ports on a single machine. I have tested it.

Run "mongo" on Primary, open the command line, and Set the Replica Set:

rs.initiate(    {"_id" : "test-set",     "members" : [        {"_id" : 1, "host" : "192.168.1.1"},        {"_id" : 2, "host" : "192.168.1.2"},        {"_id" : 3, "host" : "192.168.1.3", "arbiterOnly" : true}    ]});{ "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 }

After the history is successful, wait for a while. He will elect Primary and then check the status of the Replica Set:

test-set:PRIMARY> rs.status(){        "set" : "test-set",        "date" : ISODate("2014-02-21T10:28:55Z"),        "myState" : 7,        "members" : [                {                        "_id" : 1,                        "name" : "192.168.1.1:27017",                        "health" : 1,                        "state" : 1,                        "stateStr" : "PRIMARY",                        "uptime" : 4086,                        "optime" : Timestamp(1392972480, 1),                        "optimeDate" : ISODate("2014-02-21T08:48:00Z"),                        "lastHeartbeat" : ISODate("2014-02-21T10:28:54Z"),                        "lastHeartbeatRecv" : ISODate("2014-02-21T10:28:53Z"),                        "pingMs" : 0                },                {                        "_id" : 2,                        "name" : "192.168.1.2:27017",                        "health" : 1,                        "state" : 2,                        "stateStr" : "SECONDARY",                        "uptime" : 3997,                        "optime" : Timestamp(1392972480, 1),                        "optimeDate" : ISODate("2014-02-21T08:48:00Z"),                        "lastHeartbeat" : ISODate("2014-02-21T10:28:54Z"),                        "lastHeartbeatRecv" : ISODate("2014-02-21T10:28:55Z"),                        "pingMs" : 0,                        "syncingTo" : "192.168.131.15:27017"                },                {                        "_id" : 3,                        "name" : "192.168.1.3:27017",                        "health" : 1,                        "state" : 7,                        "stateStr" : "ARBITER",                        "uptime" : 5781,                        "self" : true                }        ],        "ok" : 1}

In this way, the entire replica set is successfully configured, which is relatively simple.

MongoDB details: click here
MongoDB: click here

MongoDB backup and recovery

CentOS compilation and installation of MongoDB

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

How to create a new database and set in MongoDB

MongoDB beginners must read (both concepts and practices)

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

  • 1
  • 2
  • Next Page

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.