The construction of centos7-mongodb3.4.6 cluster

Source: Internet
Author: User
Tags mkdir mongodb

0. Need environment

Installation package: Mongodb-linux-x86_64-3.4.6.tgz

Installation path:/usr/mongodb

Server: 192.168.177.131/132/133

MONGOs

20000

Config server

21000

Shard1

20001

Shard2

20002

Shard3

20003

1. Unzip to/usr/local

Renamed to MongoDB after decompression, convenient for later operation

2. Create a Directory

Mkdir-p Mongodb/mongos/log

Mkdir-p Mongodb/config-server/data

Mkdir-p Mongodb/config-server/log

Mkdir-p Mongodb/shard1/data

Mkdir-p Mongodb/shard1/log

Mkdir-p Mongodb/shard2/data

Mkdir-p Mongodb/shard2/log

Mkdir-p Mongodb/shard3/data

Mkdir-p Mongodb/shard3/log

Where the data directory is stored, the log directory is stored

MONGOs service does not store data, so no data directory required, only log directory

3. configuring server Config Servers

Vim/usr/local/mongodb/config-server.conf

Systemlog:

Destination:file

Path: "/usr/local/mongodb/config-server/log/config-server.log"

Logappend:true

Storage

DbPath: "/usr/local/mongodb/config-server/data"

Journal

Enabled:true

Directoryperdb:true

Net

port:21000

Processmanagement:

Fork:true

Pidfilepath: "/usr/local/mongodb/config-server/config-server.pid"

Sharding:

Clusterrole:configsvr

Replication

Replsetname:configserver

A space is required between the colon (:) and the configuration

Start three servers

CD MongoDB

Bin/mongod-f config-server.conf

Log in to any of the configuration servers, initialize

#连接

Mongodb/bin/mongo--port 21000

Config = {

_id: "Configserver",

Members: [

{_id:0, Host: "192.168.177.131:21000"},

{_id:1, Host: "192.168.177.132:21000"},

{_id:2, Host: "192.168.177.133:21000"}

]

}

#初始化副本集

Rs.initiate (config);

#查看分区状态

Rs.status ();

4. Shard Replica Set Shard

Vim/usr/local/mongodb/shard1.conf

Systemlog:

Destination:file

Path: "/usr/local/mongodb/shard1/log/shard1.log"

Logappend:true

Storage

DbPath: "/usr/local/mongodb/shard1/data"

Journal

Enabled:true

Directoryperdb:true

Net

port:20001

Processmanagement:

Fork:true

Pidfilepath: "/usr/local/mongodb/shard1/shard1.pid"

Sharding:

Clusterrole:shardsvr

Replication

Replsetname:shard1

Start three servers:

CD MongoDB

Bin/mongod-f shard1.conf

Log on to any server and initialize the replica set

MONGO--port 20001

#使用admin数据库

Use admin

Config = {

_id: "Shard1",

Members: [

{_id:0, Host: "192.168.177.131:20001"},

{_id:1, Host: "192.168.177.132:20001"},

{_id:2, Host: "192.168.177.133:20001"}

]

}

#初始化副本集

Rs.initiate (config);

#查看分区状态

Rs.status ();

Configure the second, three Shard replica set, in the same way as the sixth step. Note Port changed to 20002 20003

Systemlog:

Destination:file

Path: "/usr/local/mongodb/shard2/log/shard2.log"

Logappend:true

Storage

DbPath: "/usr/local/mongodb/shard2/data"

Journal

Enabled:true

Directoryperdb:true

Net

port:20002

Processmanagement:

Fork:true

Pidfilepath: "/usr/local/mongodb/shard2/shard2.pid"

Sharding:

Clusterrole:shardsvr

Replication

Replsetname:shard2

Systemlog:

Destination:file

Path: "/usr/local/mongodb/shard3/log/shard3.log"

Logappend:true

Storage

DbPath: "/usr/local/mongodb/shard3/data"

Journal

Enabled:true

Directoryperdb:true

Net

port:20003

Processmanagement:

Fork:true

Pidfilepath: "/usr/local/mongodb/shard3/shard3.pid"

Sharding:

Clusterrole:shardsvr

Replication

Replsetname:shard3

Start three servers:

CD MongoDB

Bin/mongod-f shard2.conf

Bin/mongod-f shard3.conf

Log on to any server and initialize the replica set

MONGO--port 20002

MONGO--port 20003

#使用admin数据库

Use admin

Config = {

_id: "Shard2",

Members: [

{_id:0, Host: "192.168.177.131:20002"},

{_id:1, Host: "192.168.177.132:20002"},

{_id:2, Host: "192.168.177.133:20002"}

]

}

Config = {

_id: "Shard3",

Members: [

{_id:0, Host: "192.168.177.131:20003"},

{_id:1, Host: "192.168.177.132:20003"},

{_id:2, Host: "192.168.177.133:20003"}

]

}

#初始化副本集

Rs.initiate (config);

#查看分区状态

Rs.status ();

5. Routing Server MONGOs

Vim/usr/local/mongodb/mongos.conf

Systemlog:

Destination:file

Path: "/usr/local/mongodb/mongos/log/mongos.log"

Logappend:true

Net

port:20000

bindip:0.0.0.0

Processmanagement:

Fork:true

Pidfilepath: "/usr/local/mongodb/mongos/mongos.pid"

Sharding:

configdb:configreplset/192.168.177.131:21000,192.168.177.132:21000,192.168.177.133:21000

Start three servers:

CD MongoDB

Bin/mongos-f mongos.conf

Note that starting here is using the MONGOs

6. Enable Sharding

At present, the MongoDB configuration server, routing server, each shard server, but the application to connect to the MONGOs routing server does not use the Shard mechanism, also need to set the Shard configuration in the program, let the Shard take effect.

#登陆任意一台mongos

MONGO--port 20000

#使用admin数据库

Use admin

#串联路由服务器与分配副本集

Sh.addshard ("shard1/192.168.177.131:20001,192.168.177.132:20001,192.168.177.133:20001");

Sh.addshard ("shard2/192.168.177.131:20002,192.168.177.132:20002,192.168.177.133:20002");

Sh.addshard ("shard3/192.168.177.131:20003,192.168.177.132:20003,192.168.177.133:20003");

#查看集群状态

Sh.status ()

The cluster is now complete.

7. Testing

Connection MONGOs

Bin/mongo--port 20000

Start the sharding (Shard) feature of a database first

Mongos> sh.enablesharding ("test")

The Shard,shard key for collection logs in the TestDB database is the SN field, and the Shard policy is hashed. Using the hashed policy is designed to allow MongoDB to distribute the data evenly, if you use {"SN", 1} for range Shard, the data will be distributed according to the size order, there will be a lot of shard data, other rare phenomenon.

Mongos> sh.shardcollection ("Testdb.logs", {"SN": "Hashed"})

View shard Information

Mongos> Sh.status ()

Add test data

Mongos> Use TestDB

Mongos> for (var i=1; i<=100000; i++) {Db.log.insert ({sn:i, msg: ' Message ' + i});}

Wait for some time

View Log's Shard information

Mongos> Db.logs.stats ()

You can see that three shards have almost average data saved.

33143 33755 33102

{

"sharded": true,

"Capped": false,

"NS": "Testdb.logs",

"Count": 100000,

"Size": 5688895,

"Storagesize": 1773568,

"Totalindexsize": 3944448,

"Indexsizes": {

"_id_": 983040,

"Sn_hashed": 2961408

},

"Avgobjsize": 56,

"Nindexes": 2,

"Nchunks": 6,

"Shards": {

"Shard1": {

"NS": "Testdb.logs",

"Size": 1920351,

"Count": 33755,

"Avgobjsize": 56,

"Storagesize": 634880,

"Capped": false,

...

"OK": 1

},

"Shard2": {

"NS": "Testdb.logs",

"Size": 1885397,

"Count": 33143,

"Avgobjsize": 56,

"Storagesize": 544768,

"Capped": false,

...

"OK": 1

},

"Shard3": {

"NS": "Testdb.logs",

"Size": 1883147,

"Count": 33102,

"Avgobjsize": 56,

"Storagesize": 593920,

"Capped": false,

...

"OK": 1

}

},

"OK": 1

}

8. Later operation and maintenance

The start-up sequence for MongoDB is to start the configuration server first, start the Shard, and finally start MONGOs.

/usr/local/mongodb/bin/mongod-f/usr/local/mongodb/config-server.conf

/usr/local/mongodb/bin/mongod-f/usr/local/mongodb/shard1.conf

/usr/local/mongodb/bin/mongod-f/usr/local/mongodb/shard2.conf

/usr/local/mongodb/bin/mongod-f/usr/local/mongodb/shard3.conf

/usr/local/mongodb/bin/mongos-f/usr/local/mongodb/mongos.conf

When off, direct killall kills all processes

Killall Mongod

Killall MONGOs

Installation without killall command using <yum install psmisc>

The construction of centos7-mongodb3.4.6 cluster

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.