MongoDB 3.4 Shard, Replica, authentication cluster deployment.

Source: Internet
Author: User
Tags auth mkdir mongodb server mongodb version

MongoDB version:

Environment: one virtual machine

Three copies of Configsvr: Port is 27020,27021,27022

Two shards:

Shard1:-> three replicas with Port 27010,27011,27012

Shard2:-> three replicas with Port 27013,27014,27015

One route: MONGOs-Port 27023


Create a directory of data store files

mkdir /usr/local/mongodb/datamkdir /usr/local/mongodb//usr/local/mongodb/ Data mkdir mkdir mkdir mkdir mkdir s101 &&  mkdirmkdirmkdirmkdir s202

Generate Authentication Required KeyFile

1024x768 >/usr/local/mongodb/keyfilechmod /usr/local/mongodb/keyfile

Replica mode start Configsvr

27020 --replset cs--configsvr--bind_ip=192.168. 1.9  27021 --replset cs--configsvr--bind_ip=192.168. 1.9  27022 --replset cs--configsvr--bind_ip=192.168. 1.9

Cluster configuration, login any one configsvr

 MONGO 192.168 . 1.9 : 27020  
var css={_id:"cs","configsvr":true, members:[{_id :0, Host:""},{_id:1, Host:""},{_id:2, Host:""  }]}rs.initiate (CSS)

Replica mode start Shard 1

27010 --replset shard1--shardsvr--bind_ip=192.168. 1.9  27011 --replset shard1--shardsvr--bind_ip=192.168. 1.9  27012 --replset shard1--shardsvr--bind_ip=192.168. 1.9

Log on to any one Shard 1

Replica mode start Shard 2

27013 --replset shard2--shardsvr--bind_ip=192.168. 1.9  27014 --replset shard2--shardsvr--bind_ip=192.168. 1.9  27015 --replset shard2--shardsvr--bind_ip=192.168. 1.9

Log in any one Shard 2, operation with Shard 1

Start a route

27023 --fork--configdb cs/192.168. 1.9:27020,192.168. 1.9:27021,192.168. 1.9:27022 --bind_ip=192.168. 1.9

Landing route

192.168. 1.9:27023

Adding shards

Set up the Shard database, set the slice key

sh. Shardcollection ("testdb.orderinfo", {"_id": " hashed""collectionsharded""testdb.orderinfo"  "OK"1 }

Add the user before you have the authentication.

Mongos>Db.createuser (... {... User:"testuser",...     pwd:"testuser",... roles: [{role:"ReadWrite"Db:"TestDB" } ]...   } ... ) Successfully added User: {"User":"testuser",    "Roles" : [        {            "role":"ReadWrite",            "DB":"TestDB"}]}mongos> Db.auth ("testuser","testuser")1MONGOs> Exit

Then, turn off MongoDB, and then add authentication and then start.

Because lazy, I choose to restart, My computer, casual, not too serious ...

Start Mongod in turn, this time with the authentication parameter--keyfile/usr/local/mongodb/keyfile

Mongod--dbpath/usr/local/mongodb/data/c0--logpath/usr/local/mongodb/log/c0.log--keyfile/usr/local/mongodb/ KeyFile--fork--smallfiles--port27020--replset CS--configsvr--bind_ip= Fork--smallfiles--port27021--replset CS--configsvr--bind_ip= Fork--smallfiles--port27022--replset CS--configsvr--bind_ip= KeyFile--fork--smallfiles--port27010--replset shard1--shardsvr--bind_ip= KeyFile--fork--smallfiles--port27011--replset shard1--shardsvr--bind_ip= KeyFile--fork--smallfiles--port27012--replset shard1--shardsvr--bind_ip= KeyFile--fork--smallfiles--port27013--replset Shard2--shardsvr--bind_ip= KeyFile--fork--smallfiles--port27014--replset Shard2--shardsvr--bind_ip= KeyFile--fork--smallfiles--port27015--replset Shard2--shardsvr--bind_ip= cs/,,


[[Email protected]192conf]# MONGO192.168.1.9:27023MongoDB Shell version v3.4.10Connecting to: Server version:3.4.TenMONGOs>Use testdbswitched to DB Testdbmongos> Db.auth ("testuser","testuser")1MONGOs> Db.auth ("testuser","testuser")1MONGOs>Exitbye


192.168. 1.9:27023/testdb-u testuser-p

Test does not use account password

MongoDB 3.4 Shard, Replica, authentication cluster deployment.

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: 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.