Mongodb3. X replica set (cluster) Add user name authentication

Source: Internet
Author: User

Toss for a few days this, due to work problems, time is also more scattered, today finally took a moment to record it down, and local test through the good, nonsense not to say, the following began,

First you need to build a MongoDB replica set, and again, my environment is as follows:

CentOS 6.5

MongoDB 3.2.11

Mongdb:https://www.mongodb.com/download-center?jmp=nav#community

Here we need to configure three Mong single nodes, and then combine them to form a replica set,

Installation path/USR/LOCAL/MONGD

Three mongdb of ports 27017 27018 27019

Create four data directories and a configuration directory and a log directory below/USR/LOCAL/MONGD

MKDIR/USR/LOCAL/MONGD/DATA{1..4}

Mkdir/usr/local/mongd/log

Mkdir/usr/local/mongd/etc

The three MongoDB configuration files are as follows: The path to the configuration file:/usr/local/mongd/etc/

First MongoDB configuration file: mongod.conf

port=27017

dbpath=/usr/local/mongod/data1/

Logpath=/usr/local/mongod/log/mongodb.log

Pidfilepath=/usr/local/mongod//mongod.pid

Fork=true

Logappend=true

Shardsvr=true

Directoryperdb=true

#auth =true

#replSet =ZPF

#oplogSize =100

#keyFile =/usr/local/mongod/etc/mongo.pass # # #这个文件后边需要手动生成, the path and file name are custom

The second MongoDB configuration file: mongod2.conf content is as follows:

port=27018

dbpath=/usr/local/mongod/data2/

Logpath=/usr/local/mongod/log/mongodb.log

Pidfilepath=/usr/local/mongod//mongod.pid

Fork=true

Logappend=true

Shardsvr=true

Directoryperdb=true

#auth =true

Replset=zpf

oplogsize=100

keyfile=/usr/local/mongod/etc/mongo.pass # # #三个配置文件中这个文件需要一致

The third MongoDB configuration file: mongod3.conf, which reads as follows:

port=27019

dbpath=/usr/local/mongod/data3/

Logpath=/usr/local/mongod/log/mongodb3.log

Pidfilepath=/usr/local/mongod//mongod3.pid

Fork=true

Logappend=true

Shardsvr=true

Directoryperdb=true

#auth =true

Replset=zpf

oplogsize=100

keyfile=/usr/local/mongod/etc/mongo.pass # # #三个配置文件中这个文件需要一致

Start a node first, that is, start the first mongodb,27017 port, because again I want to use it to do the primary node

Mongod--config/usr/local/mongod/etc/mongod.conf

After launching, connect the node, add the user inside, here I added a root role of the user (the most privileged user),

Here's how to add it:

Use admin

Db.createuser ({User: "admin", pwd: "123456", Roles:[{role: "Root", DB: "admin"}])

Because my is mongodb3.2 version, so add the user need to use the above action

After the user has finished, close MongoDB, and then modify the MongoDB configuration file, just modify the first configuration file on the line, because again, we started the first MongoDB database,

The modified configuration is as follows:

port=27017

dbpath=/usr/local/mongod/data1/

Logpath=/usr/local/mongod/log/mongodb.log

Pidfilepath=/usr/local/mongod//mongod.pid

Fork=true

Logappend=true

Shardsvr=true

Directoryperdb=true

Auth=true # # #去掉注释

REPLSET=ZPF # # #去掉注释

OPLOGSIZE=100 # # #去掉注释

Keyfile=/usr/local/mongod/etc/mongo.pass # # #去掉注释

It then generates a keyfile, which is validated by this file between the replica sets.

OpenSSL rand-base64 521 >/usr/local/mongod/etc/mongo.pass # # #这个名字随意定义, but this file also needs to be developed in the configuration file, where my is the same machine, so the path is the same, If it's a replica set between different machines, you'll need to copy this file to a different machine.

After the creation is complete, you also need to modify the permissions of this file to 600, only the owner has permissions, the remaining groups and other users do not have any permissions

chmod 600/usr/local/mongod/etc/mongo.pass

After the modification is complete, start the MONGDB database

This time, we need three data to start,

Then log in to the database to do the work, but remember that you need to verify the account and password before you can

[Email protected] etc]# MONGO

MongoDB Shell version:3.2.11

Connecting To:test

zpf:primary> Use admin

Switched to DB admin

zpf:primary> Db.auth ("admin", "123456")

1

Below is the detailed configuration of the replica set, you can refer to another document, the configuration of the replica set, the link to any of the three will need to verify the account and password before operation, copy set configuration operation, see another document: Mongod cluster shard


Mongodb3. X replica set (cluster) Add user name authentication

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.