Files
notes/tiddlywiki/MongoDB - replication setup.txt
2026-03-12 22:01:38 +01:00

101 lines
2.8 KiB
Plaintext
Executable File

~~ server names:
~~ ajara
~~ atrisia
~~ anaxes
~~ enable SCRAM authentification on ALL MongoDB instances
mongo
> use admin
> db.createUser({ user: "superhero", pwd: "secret", roles: ["root"]});
> db.shutdownServer();
~~ add in MongoDB configuration file ->
security:
authorization: 'enabled'
<-------------------------------------
~~ start MongoDB instance
/app/mongodb/product/current_version/bin/mongod --config=/app/mongodb/conf/mongod.conf --logpath=/app/mongodb/log/mongod.log --fork
~~ test connection
mongo --username=superhero --password=secret
~~ for internal communication between instances we will use a basic keyFile method
~~ generate keyfile
openssl rand -base64 756 > /app/mongodb/conf/keyfile.basic
chmod 600 /app/mongodb/conf/keyfile.basic
~~ add the keyfile in MongoDB configuration file ->
security:
authorization: 'enabled'
keyFile: /app/mongodb/conf/keyfile.basic
<-------------------------------------
~~ restart MongoDB instance and test connection again
/app/mongodb/product/current_version/bin/mongod --config=/app/mongodb/conf/mongod.conf --shutdown
/app/mongodb/product/current_version/bin/mongod --config=/app/mongodb/conf/mongod.conf --logpath=/app/mongodb/log/mongod.log --fork
mongo --username=superhero --password=secret
~~ repeat theses operations on other 2 MongoDB instances using the SAME keyfile generated for the first instance
~~ for all MongoDB instances, declare the replication in configuration file
------------------------------------------>
replication:
replSetName: rs0
<-----------------------------------------
mongo --username=superhero --password=secret
rsconf = {
_id: "rs0",
members: [
{
_id: 0,
host: "ajara:27017"
}
]
}
rs.initiate(rsconf);
rs.add('atrisia:27017');
rs.add('anaxes:27017');
rs.conf();
rs.status();
~~ ckeck if replication works
~~ on PRIMARY instance create a database and a collection
rs0:PRIMARY> use db01;
rs0:PRIMARY> db.movies.insertOne({"title" : "Stand by Me"});
~~ on SECONDARIES check if the collection has been replicated
~~ note that a slave, before running a query, we should activate the read-only acces using the following command
rs0:SECONDARY> rs.slaveOk();
rs0:SECONDARY> use db01;
rs0:SECONDARY> db.movies.find();
~~ finaly, drop the test database from the master node
rs0:PRIMARY> db.dropDatabase();
~~ to user on SECONDARY replica to display lag and oplog size
db.getReplicationInfo();
~~ to find the mester of a replica set, use the following command on any member of replica set
db.isMaster();
~~ get replica set congig
config = rs.conf();
~~ remove a member from a replica set
rs.remove('anaxes:27017');
rs.reconfig(config, {force: true});