Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Get a better understanding of the MongoDB master slave configuration

30 September 2020

First the basics: what is the master/slave?

SQLS*Plus - 1649775775031350BE3E1 A5F5 4CB0 974D 3A72805C8C4C optimize

One database server (“master”) responds and can do anything. A lot of other database servers store copies of all the data that have been written to the master and can optionally be requested (they are “slaves”). Slaves cannot be written directly, they are just copies of the main database. The configuration of the master and slave allows you to scale your reads well because you can simply keep adding the slave to increase the read capacity. Slaves also make great backup machines. If your master explodes, you get a copy of the data safe and sound.

Convenient comparison table between main database servers and subordinate database servers:

Master
Slave
Number of servers
1
permissions
read write
 read
is used to
query, insert, update, delete
requests

So, how do you set up Mongo in master/slave configuration? Assuming that you have downloaded MongoDB from mongodb.org, you can run the master and slave by cutting and inserting the following lines into your shell:

$ mkdir -p ~/dbs/master ~/dbs/slave
$ ./mongod --master --dbpath ~/dbs/master >> ~/dbs/master.log &
$ ./mongod --slave --port 27018 --dbpath ~/dbs/slave --source localhost:27017 >> ~/dbs/slave.log &

What do these lines do?

  1. First, we create directories to store the database (~/ dbs / master and ~/ dbs / slave).
  2. Now we start the wizard by specifying that it should place its files in the directory ~ / dbs / master and the log in the file ~ / dbs / master.log.
  3. So, now we have a wizard working on localhost: 27017.
  4. Then we start with the slave. It has to listen to a port different from the main one because they are on the same machine, so we will choose 27018. He will store his files in ~ / db / slave and his logs in ~ / dbs / slave.log.
  5. The most important part is to let him know who is the master of the house: the option -source localhost: 27017 lets him know that the master from which he should read is at localhost: 27017.

There are many possible master/slave configurations. A few examples:

  • You can have a dozen slave boxes where you want to distribute the readings evenly overall.
  • You may have one small weak subordinate machine on which you do not want to read operations to be performed, you just use it for backups.
  • You may have the most powerful server in the world as your main machine, and you want it to handle both read and write operations… unless you receive more than 1000 requests per second, in which case you want some of them to be redirected to your slaves.

In short, Mongo cannot automatically configure your application to take advantage of your “master-slave” configuration. I am sorry. You will have to do it yourself.

However, this is not too difficult, especially for what MG wants to do. MG uses 3 servers: the main one and two subordinates, so we need three connections: one to the main one and one to each slave. Assuming that it has a wizard on master.example.com and subordinates on slave1.example.com and slave2.example.com, it can create connections to:

$master = new Mongo("master.example.com:27017");
$slave1 = new Mongo("slave1.example.com:27017");
$slave2 = new Mongo("slave2.example.com:27017");

This next fragment is a bit unpleasant, and it would be great if someone created a basis for it (hint). We want to abstract the master-slave logic into a separate layer, so the application communicates with the master-slave logic which communicates with the driver.

However, I’m lazy so I’ll just extend the MongoCollection class and add the master-slave logic. Then, if a person creates a MongoMSCollection from his $ master connection, he can add his subordinates and use the collection as if it were a normal MongoCollection. Meanwhile, the MongoMSCollection will distribute the reads evenly among the slaves.

class MongoMSCollection extends MongoCollection {
public $currentSlave = -1;

// call this once to initialize the slaves
public function addSlaves($slaves) {
// extract the namespace for this collection: db name and collection name
$db = $this->db->__toString();
$c = $this->getName();

// create an array of MongoCollections from the slave connections
$this->slaves = array();
foreach ($slaves as $slave) {
$this->slaves[] = $slave->$db->$c;
}

$this->numSlaves = count($this->slaves);
}

public function find($query, $fields) {
// get the next slave in the array
$this->currentSlave = ($this->currentSlave+1) % $this->numSlaves;

// use a slave connection to do the query
return $this->slaves[$this->currentSlave]->find();
}
}

To use this class, we create an instance in the master database and then add an array of slaves to it:

$master = new Mongo("master.example.com:27017");
$slaves = array(new Mongo("slave1.example.com:27017"), new Mongo("slave2.example.com:27017"));

$c = new MongoMSCollection($master->foo, "bar");
$c->addSlaves($slaves);

Now we can use $ c as a regular MongoCollection. MongoMSCollection: Find will alternate between two slave devices and all other operations (inserting, updating, and deleting) will be performed on the master device. If MG also wants the master descriptor to read, it can simply add it to the $ slaves array (which can now be better called the $ reader array):

$slaves = array($master, new Mongo(“slave1.example.com:27017”), new Mongo(“slave2.example.com:27017”));
Alternatively, it can change the logic in the MongoMSCollection: find method.

Edit: since version 1.4.0, slaveOkay is not required to read from slaves. slaveOkay should be used if you use replica sets, not -master and -slave. Thus, the following section no longer applies to a normal master/slave device.

The only trick in the Mongo master/slave implementation is that by default slave is not even readable, it is just a way to back up the master database. If you really want to read from the slave, you have to set a flag in your query called “slaveOkay”. Instead of talking:

$cursor = $slave->foo->bar->find();

we have one:

$cursor = $slave->foo->bar->find()->slaveOkay();
Or, since it’s an ass in the job to set for each query (and almost impossible to do for findOnes if you don’t know the inside), you can set a static variable in MongoCursor that will contain all your queries:

MongoCursor::$slaveOkay = true;

And now you will be allowed to normally query your slave without calling slaveOkay () for each cursor.

  MongoDB 6 How to Deploy a MongoDB ReplicaSet

Enteros

About Enteros

Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of RDBMS, NoSQL, and machine learning database platforms.

 
Tags: ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...

Preamble​​When administering PostgreSQL database servers, one of the most common tasks you will probably perform is enumerating databases and their tables....