Large Database Scaling in MySQL
By: Jagesh Maharjan
Tags: MySQL, Programming, Coding, Database, Engineering

Database scaling and elasticity is vitally important to the successful operation of any software application. A failure to scale adequately can result in business impairment because applications are running slowly or are not available. Databases must be kept online and operational to protect businesses. Below we talk about database scaling in MySQL.

Scaling a production database becomes important when the number of customers performing read-write operations at any particular instance of time (seconds, minutes) suddenly increases beyond the capacity of your database. If you have only a single database, a sudden rush of create, read, update and delete (CRUD) operations may cause a bottleneck and bring your system to halt.

In order for your system to handle a growing amount of work, or its potential to perform more total work in the same elapsed time you need to increase the processing power to accommodate growth that growth through scaling. There are various ways to perform database scaling such as creating databases with independent tables to do CRUD operations. An alternate method is the Master-Slave replica copy. For the purpose of this article, we shall dig into details of the master-slave concept and show a few examples of this in action.

For the sake of simplicity, we shall just create one master database and one slave database. The write operations will be handled on the master database and read operations from the slave database.

First, we should create two instances using any cloud infrastructure provider such as Azure, AWS, GCP, etc., and set up a master instance and a slave replica.

Figure 1: Master instance

Figure 2: Slave replica

Next, we install the MySQL client and server, on both instances. Yes, we need to install the client and server on both instances. Since we are using Ubuntu 18.04, all bash/command will be Ubuntu specific (Apt instead of YUM). If you need help, please refer to the installation guide here.

sudo apt update

sudo apt install mysql-client

sudo apt install mysql-server

sudo mysql_secure_installation

Once the installation is done, we generate the SSH key and setup the key authentication for access right on both servers (in some cases, only for the Slave Node).

ssh-keygen

After the key is generated, we copy and paste it in the file of either of teh servers.

~/.ssh/authorized_keys

In this example use case, we shall not be using the public IP but rather the cluster IP. We can find the cluster IP of each server by entering

hostname -I

in the terminal.

We will have two IP addresses, as for the master node and the other for the slave node.

master 172.31.17.41

slave 172.31.0.127

Now we shall configure in for the master node. Let us first edit the MySQL config file. (Note: if you cannot find the config file, it is probable that you did not install the MySQL-server correctly).

ubuntu@ip:~$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Go to the following lines, make the changes indicated below and save the file:

bind-address = 172.31.17.41 # use your own cluster_ip

server-id = 1 # can gave any id

log_bin = /var/log/mysql/mysql-bin.log

Next, log in to the MySQL server with

`mysql -u root -p`

and input the root password at the MySQL prompt.

mysql> GRANT REPLICATION SLAVE ON *.* TO 'root'@'%' IDENTIFIED BY 'root_user_password';

mysql> FLUSH PRIVILEGES;

Finally, exit MySQL, restart the MySQL service, and check the status to ensure it restarted successfully.

sudo systemctl restart mysql

sudo systemctl status mysql

We should be careful to make the database read lock to disallow any changes.

FLUSH TABLES WITH READ LOCK;

We shall go to the slave node, and do some configuration similar to what we did in the master node.

Here, we show how to check the status of the Master Node.

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.025471 | 154 | | | |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

Figure 3: Master status

Please make a note of the File and Position in the master status fields. We shall be using those values to tell the Slave Node to make a replica.

Since we need to export the database as a DB dump, we need to first export the database using the following command:

$ mysqldump -u root -p --all-databases --lock-all-tables --events > mysqldb_bump.sql

Once the dump is exported, we go to the MySQL to unlock the tables and check the status of the Master Node.

mysql -u root -p

mysql> unlock tables;

show master status\G

If all is good, you should see something similar to this:

Figure 4: Master status\G

Please remember to note the Position.

We can now copy the dump from the Master Node to a Slave Node in temp directory. In this case, because we are using cloud instances, we shall use a public IP.

~$ scp -i slavekey.pem mysqldb_bump.sql ubuntu@ec2-13-212-1xx-2xx.ap-southeast-1.compute.amazonaws.com:/tmp/

Next, we shall go to the Slave Node and do some configurations similar to what we did with the Master Node. Here is what that looks like:

ubuntu@ip:~$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf bind-address = 172.31.0.127 # use your own cluster_ip

server-id = 1 # can gave any id

log_bin = /var/log/mysql/mysql-bin.log [Add these lines at buttom]

read_only = 1

report_host = 172.31.17.41

Once done save the file and restart the MySQL service. As we did with the master, check the MySQL service status to ensure everything is working fine.

Since we just copied all the data from the Master Node, we should have the SQL dump file in /tmp directory of the Slave Node, which we can import to MySQL as follows:

~$ mysql -u root -p < /tmp/mysqldb_bump.sql

Then, we go to the MySQL and make some changes on the slave configuration as follows:

mysql> CHANGE MASTER TO MASTER_HOST='172.31.17.41',MASTER_USER='root', MASTER_PASSWORD='root_user_password', MASTER_LOG_FILE='mysql-bin.025469', MASTER_LOG_POS= 154;

Once the above config is set, we can start the Slave Node , and of course check the status of the Slave Node by logging in to MySQL.

mysql> start slave

mysql> show slave status\G

The status should look similar to the screenshot below.

Figure 5: Slave node status

Next, create a database in the Master Node by simply logging in to MySQL and creating the database command as follows:

mysql> create database delvifydb

Then exit from the MySQL and logoff from the system.

Now we will confirm that the database created in Master Node can also be seen in Slave Node. We do this by going to the Slave Node and type the show databases command.

Figure 6: Show databases

Bingo! We now have a database created in the Master Node replicated in the Slave Node.

Just to make sure, let us test to confirm if it is working the way we set it up.

First, log in to MySQL and select the database to use. Create a table and write a record from the master. Then we can check if we can see if it is replicated in the Slave Node.

Figure 7: Write operation from the Master Node

Figure 8: Read operation from the Slave Node

This test was successful, and you can confirm that the write operation from the Master Node was replicated in the Slave Node. As this is an asynchronous process, there might be a very small delay when working with a massively large database. However, the delay is almost always negligible at 1/10th of a second.

Today, we discussed a simple method to create scalability in MySQL. This can be expanded to handle many problems you might encounter. A database needs to be scalable so that it is available at all times. There is also the issue of elasticity in your workloads and this will be covered in a later post.

Ready To Know More?

Get in touch with our team of experts to find out how we can help you build predictive AI solutions for your business. Welcome to any feedback and comments as well!

www.delvify.ai