MongoDB (1)-replica set and sharding installation Configuration

Source: Internet
Author: User
Tags mongoclient mongodb client mongodb driver

MongoDB official documentation is quite good, and there are also Chinese manuals, although some have not yet been fully translated :) we suggest you read more online manuals to save the trouble of inaccurate online information.

1. installation:

The installation on various operating systems is very simple. You can download the binaries of MongoDB on the corresponding operating system from the Internet and then run it directly. There is no waste of ink here. Please refer to the official Chinese installation Documentation /.

Fqa:

1. Pay attention to the OS architecture of each operating system, that is, the number of operating systems. Download the corresponding MongoDB program running file. You can use the wmic OS get osarchitecture command on widows to view

2. Replica set (replica SEST ):

A. Installation Configuration:

Create a directory for storing data of three MongoDB instances in the test environment:

Mkdir-P/var/database/MongoDB/rs0-0/var/database/MongoDB/rs0-1/var/database/MongoDB/rs0-2

Start three database services instances (as a replication set)

Mongod -- Port 27017 -- dbpath/var/database/MongoDB/rs0-0 -- replset rs0
Mongod -- Port 27018 -- dbpath/var/databasemongodb/rs0-1 -- replset rs0
Mongod -- Port 27019 -- dbpath/var/database/MongoDB/rs0-2 -- replset rs0

Use the MongoDB management client to connect to the MongoDB service at Port 27017 in parallel. After the service is started, perform the replica set operation:

Mongo -- Port 27017

> Rsconf = {
_ ID: "rs0 ",
Members :[
{
_ ID: 0,
HOST: "}
]
}

> Rs. Initiate (rsconf)

> Rs. Add ("> Rs. Add ("

Of course, you can also directly use the configuration method to create:

> Config = {_ ID: 'seta', members: [{_ ID: 0, host: '100. 0.0.1: 10000 '}, {_ ID: 1, host: '2017. 0.0.1: 10001 '}, {_ ID: 2, host: '127.0.0.1: 10002'}]}

> Rs. Initiate (rsconf)

After the configuration is complete, check the status of the current replica set:

> Rs. Status ()

If you use it in the Code, when creating the MongoDB object, you can use all the three MongoDB instances as the instantiation parameters. The MongoDB driver will automatically determine which node is a primiary node, which of the following are slave nodes? By default, the driver will allow connection to read and write the same node (usually primary). You can also set the read/write splitting for this replica set, or set strict read/write consistency for the replica set :). Java code example is provided here:

Publish client publish client = new publish client (arrays. aslist (
New serveraddress ("localhost", 27017 ),
New serveraddress ("localhost", 27018 ),
New serveraddress ("localhost", 27019 )));

Or:

Mongo = new Mongo (arrays. aslist (
New serveraddress ("localhost", 27017 ),
New serveraddress ("localhost", 27018 ),
New serveraddress ("localhost", 27019 )));

We recommend that you use the first mongoclient class because the Mongo class will be replaced by mongoclient in the future.

B. FAQ:

Copy the node election policy in the Set

The set point attribute of the replica set.

For read/write splitting (tag-oriented/non-oriented), write concern indicates read/write consistency.

C. Working Mechanism:

The MongoDB Client recognizes the primary node of the cluster and then reads and writes data to the primary node. If you want to separate the read and write data, you can set it for read and write separation, but the write data is always written to the primarynode, when writing data to the master node, other slave nodes use oplog to copy the data of the master node. Of course, you can also set the chained replica mode, where a slave node reconnects data from the master node, another Server Load balancer node replicates data from this Server Load balancer node. Generally, we recommend that you use at least three nodes in a replica set. If one node fails, the other node must be used as the data source to be restored, affects normal data access, so there are at least three.

3. sharding)

A. Installation Configuration:

1. Create a replica set first, but each database must be started in sharding srever mode. As follows:

$ Mongod -- shardsvr -- Port 27017 -- dbpath/srv/MongoDB/rs0-0 -- replset rs0
$ Mongod -- shardsvr -- Port 27018 -- dbpath/srv/MongoDB/rs0-1 -- replset rs0
$ Mongod -- shardsvr -- Port 27019 -- dbpath/srv/MongoDB/rs0-2 -- replset rs0

$ Mongo -- Port 27017

> Rsconf = {_ ID: "rs0", members: [{_ ID: 0, host: "

> Rs. Initiate (rsconf)

> Rs. Add ("

2. Create a replica set similar

$ Mongod -- shardsvr -- Port 37017 -- dbpath/srv/MongoDB/rs1-0 -- replset RS1 $ mongod -- shardsvr -- Port 37018 -- dbpath/srv/MongoDB/rs1-1 -- replset RS1 $ mongod -- shardsvr -- Port 37019 -- dbpath/srv/MongoDB/rs1-2 -- replset RS1

$ Mongo -- Port 27017

> Rsconf = {_ ID: "RS1", members: [{_ ID: 0, host: "

> Rs. Initiate (rsconf)

> Rs. Add ("

3. Start configsvr for three Configuration Service nodes

Run the following command from the command line to configure three configsvr

Mongod.exe -- configsvr -- dbpath D:/data/configsvr/R0 -- Port 40000 -- shardsvr

Mongod.exe -- configsvr -- dbpath D:/data/configsvr/R0 -- Port 40001 -- shardsvr

Mongod.exe -- configsvr -- dbpath D:/data/configsvr/R0 -- Port 40002 -- shardsvr

4. Start two route nodes mongos

Ipvs.exe -- configdb 127.0.0.1: 40000,127.0 .0.1: 40001,127.0 .0.1: 40002 -- Port 50000

Ipvs.exe -- configdb 127.0.0.1: 40000,127.0 .0.1: 40001,127.0 .0.1: 40002 -- Port 60000

5. Configure parts

Call cmd.exe FIG: 50000
MongoDB shell version: 1.8.0
Connect to: 127.0.0.1: 50000

> Use Admin
Switched to DB Admin
> DB. runcommand ({addshard: "rs0/127.0.0.1: 27017,127.0 .0.1: 27018,127.0 .0.1: 27019", name: "shardseta "})
{"Shardadded": "shardseta", "OK": 1}
> DB. runcommand ({addshard: "RS1/127.0.0.1: 37017,127.0 .0.1: 37018,127.0 .0.1: 37019", name: "shardsetb "})
{"Shardadded": "shardsetb", "OK": 1}

The cluster has been configured.

You can use this cluster through the client. Here, only the Java client code is listed:

Publish client publish client = new publish client (arrays. aslist (
New serveraddress ("localhost", 50000 ),
New serveraddress ("localhost", 60000 )));

Here you will find that two mongos route server ports are used

Before using this cluster, enable the database you plan to sharding.

Connect to mongos using a client

Mongo -- Port 50000

> Sh. enablesharding ("Database Name ")

Or

> DB. runcommand ({enablesharding: <database> })

In this way.

If you want to sharding a large collection, You can execute the following command:

Sh. shardcollection ("People. addresses", {"state": 1, "_ id": 1 })

Everything is okay, but I copied it in a rush. If there is a copy leak in some places, don't scold me. You can ask me online or join me in QQ to discuss it.

B. FAQ:

C. Working Mechanism:

Working mechanism, client code, connect mongos through MongoDB Driver (a connection (generally from a connection pool provided by the driver) and select a mongos Service) mongos is only a Routing Server, which is equivalent to an access proxy

When an operation arrives at mongos, mongos forwards the operation to the corresponding mongod server based on the information on the config server. To improve the efficiency, after mongos is configured, the information on Mongo configservers has been cached in mongs.

Therefore, disabling the Mongo config server does not affect the cluster temporarily, unless the cluster performs the balance operation or new trunck changes.

 

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.