Mongo Installation & Replica set step by Step
Mongo
Installation
Step 1: Create a
mongo repo file in /etc/yum.repos.d
[root@web01
yum.repos.d]# more mongodb.repo
[mongodb-org-3.6]
name=MongoDB
Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
Step 2: create
mongod user & data directory
Useradd mongod
Passwd mongod
Mkdir -p
/data/DATA
chown -R
mongod:mongod /data/DATA
[Su to mongod]
Step 3: Create a Key
File
openssl rand -base64 756 > mongo-keyfile
sudo mkdir /opt/mongo
sudo mv ~/mongo-keyfile /opt/mongo
sudo chmod 400 /opt/mongo/mongo-keyfile
sudo chmod 400 /opt/mongo/mongo-keyfile
sudo chown mongod:mongod /opt/mongo/mongo-keyfile
Step 4: Allow the
port (iptables -Nl)
firewall-cmd
--add-port=27017/tcp --permanent
firewall-cmd
--reload
Step 3 : Intall
Mongo
Yum install mongo
yum install -y
mongodb-org
Step 4: Modify
/etc/mongod.conf file
dbPath:
/data/DATA
bindIp:
10.19.24.16
Step 4 : create
sudoer file
vi
/etc/sudoers.d/mongo
mongod ALL = (ALL) NOPASSWD: ALL
mongod ALL = (ALL) NOPASSWD: ALL
mongod ALL=(ALL)
NOPASSWD: /usr/bin/systemctl start mongod
mongod ALL=(ALL)
NOPASSWD: /usr/bin/systemctl stop mongod
mongod ALL=(ALL)
NOPASSWD: /usr/bin/systemctl restart mongod
mongod ALL=(ALL)
NOPASSWD: /usr/bin/systemctl status mongod
Step 5: Start mongo
from mongod user
sudo systemctl start
mongod
Step 6:
mongo --port 27017 --host 10.19.24.16
use admin
db.createUser({user:
"mongo-admin", pwd: "password125", roles:[{role:
"root", db: "admin"}]})
Successfully added
user: {
"user" :
"mongo-admin",
"roles" : [
{
"role" :
"root",
"db" :
"admin"
}
]
}
Successfully added
user: {
"user" :
"mongo-admin",
"roles" : [
{
"role" :
"root",
"db" :
"admin"
}
]
}
Step 8:
Repeat everything
same on node 2 from Step 1 to Step 5
Step 9: On node 1
Vi /etc/mongod.conf
security:
keyFile: /opt/mongo/mongo-keyfile
replication:
replSetName: rs0
sudo systemctl
restart mongod
Step 10:
mongo --port 27017
-u mongo-admin -p password125 --authenticationDatabase admin --host 10.19.24.16
rs.initiate()
Rs.status()
Rs.add("10.19.24.17:27017")
"errmsg" : "Quorum check failed
because not enough voting nodes responded; required 2 but only the following 1
voting nodes responded10.19.24.17:27017; the following nodes did not
respond affirmatively: 10.19.24.16:27017 failed with Authentication
failed.",
rs.add( { host: "10.19.24.17:27017", priority: 0, votes: 0 } )
{
"ok" : 1,
"operationTime" :
Timestamp(1540556102, 1),
"$clusterTime" : {
"clusterTime" :
Timestamp(1540556102, 1),
"signature" : {
"hash" :
BinData(0,"drp3iE9HNq5ciPWaqHWedjHR9Vg="),
"keyId" :
NumberLong("6616633136530849793")
}
}
}
In rs.status you can see some err " not reachable/healthy"
You need to add
below to /etc/mongod.conf
security:
keyFile: /opt/mongo/mongo-keyfile
replication:
replSetName: rs0
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Testing if
replication is working fine
rs0:PRIMARY> use
ashishtest
switched to db
ashishtest
rs0:PRIMARY> db
ashishtest
rs0:PRIMARY>
db.movie.insert({"name":"Ashish Francis"})
WriteResult({
"nInserted" : 1 })
rs0:PRIMARY>
db.movie.find()
{ "_id" :
ObjectId("5bd3073ee3751e75430decfe"), "name" : "Ashish
Francis" }
rs0:PRIMARY> show
dbs;
admin 0.000GB
ashishtest 0.000GB
config 0.000GB
local 0.000GB
rs0:PRIMARY>
Now login to node to
see if data replicated
mongo --port 27017 -u mongo-admin -p
password125 --authenticationDatabase admin --host 10.19.24.17
rs.slaveOk() -- so
you can query slave node
rs0:SECONDARY>
use ashishtest
switched to db
ashishtest
rs0:SECONDARY>
db.movie.find()
{ "_id" :
ObjectId("5bd3073ee3751e75430decfe"), "name" : "Ashish
Francis" }
rs0:SECONDARY>
We see the same
data. Hence, replication is setup correctly.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
For application I
created a database and a user to access that db
rs0:PRIMARY> use
FIRSTDB
switched to db FIRSTDB
rs0:PRIMARY>
db.createUser(
... {
... user: "firstuser",
... pwd: "firstuser123",
... roles: [{role: "userAdmin", db:
"FIRSTDB"}]
... }
... );
Successfully added
user: {
"user" : "firstuser",
"roles" : [
{
"role" :
"userAdmin",
"db" :
"FIRSTDB"
}
]
}
==========================================================
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home