MongoDB's distributed configuration

Source: Internet
Author: User

MongoDB has three levels of distributed configuration: master-slave configuration, cluster configuration, and cluster shard configuration, we recommend using cluster configuration.

Master-Slave configuration
    1. Start the primary server: Mongod.exe--dbpath. \db--port 27017–master
    2. Start a number of slave servers: Mongod.exe--slave--source 127.0.0.1:27017--dbpath. \DB2--port 27018,source is followed by the address and port number of the primary server.

This method is simple and flexible, it can be used for backup, fault recovery, read extension. In order to balance the load, generally through the read and write separation mode, that is, the main library to write, read from the library. The downside is that if the main library is down, it cannot be written and will not be restored automatically.

Cluster configuration
    1. Start three instances: Mongod.exe–replset rs--dbpath. \db--port 27017,mongod.exe–replset rs--dbpath. \db--port 27018,mongod.exe–replset rs--dbpath. \db--port 27019. The next step is to initialize the replica sets environment
    2. Log in to any one instance: Mongo.exe--port 27018–host localhost
    3. config_rs={_id: ' rs ', Members:[{_id:0,host: ' 127.0.0.1:27017 '},{_id:1,host: ' 127.0.0.1:27018 '},{_id:2,host: ' 127.0.0.1:27019 '}]}

4. Execute rs.initiate (config_rs) to see the configured cluster after successful execution of Rs.status (). "Statestr" For all nodes is "Primary" or "secondary" and the cluster is synchronized well.

5. Then if you want to add or remove nodes, use Rs.add (HOSTPORTSTR), Rs.remove (HOSTPORTSTR)

The cluster is divided into two types of nodes, one primary and multiple secondary (using Rs.ismaster () to see the type of node), primary can read and write, and secondary can read only. When primary goes wrong, the remaining secondary will nominate one to become primary, which will automatically be added to the cluster and become secondary when the wrong primary returns to normal.

6. Mongodb.driver's link configuration:

Mongodb://localhost:3000,localhost:3001,localhost:3002/?replicaset=rs&readpreference=primarypreferred, If you need better performance, you can use readpreference= secondarypreferred for read-write separation.

Cluster Shard Configuration
    1. Create a cluster shard-a (process in the same cluster configuration)

Mongod.exe--shardsvr--replset shard-a--dbpath ". \data\rs-a-1 "--port 3000

Mongod.exe--shardsvr--replset shard-a--dbpath ". \data\rs-a-2 "--port 3001

Mongod.exe--shardsvr--replset shard-a--dbpath ". \data\rs-a-3 "--port 3002

config_rsa={_id: ' Shard-a ', members:[{_id:0,host: ' 127.0.0.1:3000 '},{_id:1,host: ' 127.0.0.1:3001 '},{_id:2,host: ' 127.0.0.1:3002 '}]}

Rs.initiate (CONFIG_RSA)

2. Create a cluster shard-b

Mongod.exe--shardsvr--replset shard-b--dbpath ". \data\rs-b-1 "--port 30100

Mongod.exe--shardsvr--replset shard-b--dbpath ". \data\rs-b-2 "--port 30101

Mongod.exe--shardsvr--replset shard-b--dbpath ". \data\rs-b-3 "--port 30102

config_rsb={_id: ' Shard-b ', members:[{_id:0,host: ' 127.0.0.1:30100 '},{_id:1,host: ' 127.0.0.1:30101 '},{_id:2,host: ' 127.0.0.1:30102 '}]}

Rs.initiate (CONFIG_RSB)

3. Start the configuration server

Mongod.exe--configsvr--dbpath ". \data\config-1 "--port 27019

Mongod.exe--configsvr--dbpath ". \data\config-2 "--port 27020

Mongod.exe--configsvr--dbpath ". \data\config-3 "--port 27021

4. Start MONGOs (can also be multiple)

Mongos.exe--configdb localhost:27019,localhost:27020,localhost:27021--port 40000

Add the Shard shard-a, shard-b to the Shard cluster: (using Mongo.exe login MONGOs)

Sh.addshard ("shard-a/127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002")

Sh.addshard ("shard-b/127.0.0.1:30100,127.0.0.1:30101,127.0.0.1:30102")

5. Turn on the Shard collection (Login to MONGOs)

Sh.enablesharding ("usersubscribe_db")

Sh.shardcollection ("usersubscribe_db. Sector ", {_id:1})

6. View MONGOs Status

Sh.status

Db.getsiblingdb ("config"). Databases.find (): View settings for the Shard database

Db.getsiblingdb ("config"). Collections.find (): View settings for a shard collection

7. Mongodb.driver's link configuration

Mongodb://localhost:40000/

8. For scenarios where non-fragmented data is already in use, then the Shard collection

First export the existing data, then configure the Shard cluster based on the above process and open the Shard for the library (select the appropriate key for sharding), and finally import the data via MONGOs. This can be accessed through MONGOs.

MongoDB's distributed configuration

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.