Migrate Mongo Replicatset DB

Giang Trung
3 min readSep 21, 2020
  1. Setup MongoDB
  • Chuẩn bị

mongodb-org-server-4.0.9–1.el7.x86_64.rpm
mongodb-org-mongos-4.0.9–1.el7.x86_64.rpm
mongodb-org-4.0.9–1.el7.x86_64.rpm
mongodb-org-shell-4.0.9–1.el7.x86_64.rpm
mongodb-org-tools-4.0.9–1.el7.x86_64.rpm

  • Install

yum install -y — cacheonly — disablerepo=* *.rpm

  • Note:

If install 3 mongo in 1 PC, we need to make

> 3 /etc/mongod.conf file

> 3 data/mongodb (log, db)

> 3 /var/run/mongodb (pid)

> usr/local (mongod run file) → change in service file

cd /data/
sudo cp -R mongodb/ mongodb1
chown -R mongod:mongod mongodb2
sudo cp -R mongodb/ mongodb2
chown -R mongod:mongod mongodb2
cd /var/run/
sudo cp -R mongodb/ mongodb1
chown -R mongod:mongod mongodb1
sudo cp -R mongodb/ mongodb2
chown -R mongod:mongod mongodb2
cd /etc
sudo cp mongod.conf mongod1.conf
sudo cp mongod.conf mongod2.conf
vim mongod1.conf
sudo vim mongod1.conf
sudo vim mongod2.conf

cd /usr/local
sudo cp -R bin/ mongod1
chown -R mongod:mongod mongodb1
sudo cp -R bin/ mongod2
chown -R mongod:mongod mongodb2

View log at /data/mongodb1/log/mongodb.log
/usr/local/bin/mongod -f /etc/mongod1.conf

Err : 2017–08–24T03:57:21.311–0400 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
→ sudo rm -rf /tmp/mongodb-27017.soc (solution)

  • Create user

db.createUser({user: “admin”, pwd: “Admin@1”, roles:[{role: “root”, db: “admin”}]})

use vn-db
db.createUser({user: ‘vn123’,pwd: ‘Vkkllkji@1’,roles: [ ‘readWrite’, ‘dbAdmin’ ],mechanisms:[‘SCRAM-SHA-1’]})

db.getUsers()

  • Config file:

systemLog:
destination: file
logAppend: true
path: /var/log/mongodb2/mongod.log
storage:
dbPath: /var/lib/mongo2
journal:
enabled: true

net:
port: 27028
bindIp: 127.0.0.1, data01.vn.vsm, 10.100.119.51

security:
authorization: enabled
keyFile: /opt/mongo/mongo-key.pem

replication:
replSetName: “vos_rep”

  • insert to hosts file:

10.100.119.51 data01.vn.vsm
10.100.119.51 data02.vn.vsm
10.100.119.51 data03.vn.vsm

2. Setup MongoCluster

security:
authorization: enabled
keyFile: /data/mongodb/key/mongo-key.pem

#operationProfiling:

replication:
replSetName: “vn_rep”

  • Init replica set

rs.initiate( {_id : ‘vt_rep’, members: [{ _id: 0, host: ‘data01.vn.bee:27017’ },{ _id: 1, host: ‘data02.vn.bee:27017’ },{ _id: 2, host: ‘data03.vn.bee:27017’ }]})

#############################

# Common command
use admin
db.createUser({user: ‘dba’,pwd: ‘dyhknkhne@1’,roles: [{role: ‘root’, db: ‘admin’}]})
db.auth(‘dba’, ‘dyhknkhne@1’)
db.createUser({user: ‘rs-dba’,pwd: ‘dyhknkhne@1’,roles: [{role: ‘clusterAdmin’, db: ‘admin’}]})
use vsm-cloud
db.createUser({user: ‘dyhknkhne’,pwd: ‘dyhknkhne@1’,roles: [ ‘readWrite’, ‘dbAdmin’ ],mechanisms:[‘SCRAM-SHA-1’]})

# References:
https://docs.mongodb.com/manual/replication/
https://docs.mongodb.com/manual/tutorial/expand-replica-set/
https://docs.mongodb.com/manual/tutorial/remove-replica-set-member/

# Simulation parameter:
- Old service:
localhost:27018
localhost:27019
localhost:27020
- New service:
localhost:27021
localhost:27022
localhost:27023

# Simulate old service

// Run 3 mongod instance
systemctl start mongod18.service
systemctl start mongod19.service
systemctl start mongod20.service

// Initiate replica set
mongo — host localhost — port 27018
rs.initiate( {_id : ‘rs-replica’, members: [{ _id: 0, host: ‘localhost:27018’ },{ _id: 1, host: ‘localhost:27019’ },{ _id: 2, host: ‘localhost:27020’ }]})
rs.status()

// run service using rs mongodb
spring.data.mongodb.uri=mongodb://localhost:27018,localhost:27019,localhost:27020/db-name?replicaSet=rs-replica

# Synchronize data

// Run 3 mongod instance
systemctl start mongod21.service
systemctl start mongod22.service
systemctl start mongod23.service

// Add new members to replica set
mongo — host localhost — port 27018
rs.add( { host: “localhost:27021”, priority: 0, votes: 0 } )
rs.add( { host: “localhost:27022”, priority: 0, votes: 0 } )
rs.add( { host: “localhost:27023”, priority: 0, votes: 0 } )

// Wait until state of 3 instances is SECONDARY
// Checking new db
mongo — host localhost — port 27021
rs.slaveOk()
show dbs

# Simulate new service

// run service using rs mongodb
// Database read and write operation can be done
spring.data.mongodb.uri=mongodb://localhost:27021,localhost:27022,localhost:27023/db-name?replicaSet=rs-replica

# Migration

// re-config rs
mongo — host localhost — port 27021
var cfg = rs.conf();

cfg.members[3].priority = 1
cfg.members[3].votes = 1
cfg.members[4].priority = 1
cfg.members[4].votes = 1
cfg.members[5].priority = 1
cfg.members[5].votes = 1

// remove old members At position 0, remove 3 members:
cfg.members.splice(0,3)

// print cfg
cfg

// just do it, it can take a while (need use “force” param because mongod27021 is not PRIMARY)
rs.reconfig(cfg, {“force”:true})

# Point request to new DC

Because before and after Migration step is completed, old and new services can work with their own mongodb uri.
=> Can change configuration on vsm-discovery before or after Migration step

--

--