{"id":1033,"date":"2019-09-02T14:01:49","date_gmt":"2019-09-02T14:01:49","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=1033"},"modified":"2019-09-02T14:01:49","modified_gmt":"2019-09-02T14:01:49","slug":"setup-mysql-master-slave-replication","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=1033","title":{"rendered":"Setup MySQL Replication Cluster: Architecture, Use Cases and Tutorial"},"content":{"rendered":"<p>In the era of cloud computing, there are many managed MySQL solutions available. However, due to data compliance, and other audit requirements, we will have to choose self-managed solutions. In such cases, a MySQL master-slave replication offers data replication on multiple nodes for scalability and data availability.<\/p>\n<h2 id=\"mysql-master-slave-replication-architecture\">MySQL Master-Slave Replication Architecture<\/h2>\n<p>In this section, we will look at the MySQL replication architecture and how it works.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/unnamed-1.png\" class=\"kg-image\" alt=\"MySQL Master Slave Replication topology\" loading=\"lazy\"><\/figure>\n<p>Here is how MySQL replication works.<\/p>\n<ol>\n<li>All database operations are copied to the master&#8217;s binary log.<\/li>\n<li>Salves connect to the master and asks for the data.<\/li>\n<li>The slave servers get the masters binary log.<\/li>\n<li>Slaves then apply the binary log to its realy log.<\/li>\n<li>The relay log is read by the SQL thread process and it applies all the operations\/data to the slave&#8217;s database and its binary log.<\/li>\n<\/ol>\n<h3 id=\"tl-dr\">TL;DR<\/h3>\n<ol>\n<li>Replication can be Asynchronous or Semi-Synchronous<\/li>\n<li>In asynchronous replication write latency is lower as the writes are acknowledged by master locally.<\/li>\n<li>You can add more read replicas to improve the read throughput as your business grows.<\/li>\n<li>Replication is not an HA solution. If the master goes down, there needs to be some work done to get another slave as a master. So this strategy can be used for business systems which can afford a small interruption.<\/li>\n<\/ol>\n<h3 id=\"use-cases\">Use Cases<\/h3>\n<p><!--kg-card-begin: html--><\/p>\n<ol>\n<li>This architecture is <strong>suitable for scaling MySQL<\/strong> with many read slaves.<\/li>\n<li>For <strong>backup strategy<\/strong>:\n<ul>\n<li>Backup solutions like mysqldump can cause locking problems when the backup is being done.<\/li>\n<li>Replication allows you to reduce the load from the master as the replication slave will be involved in serving the application requests.<\/li>\n<\/ul>\n<\/li>\n<li>For <strong>disaster recovery,<\/strong> replication is a great fit as you can set up a replication slave in a different region which asynchronously gets the master data. In an <strong>event of regional failures<\/strong>, we can bring up the database in a different region with the help of replication slave.<\/li>\n<\/ol>\n<p><!--kg-card-end: html--><\/p>\n<p>Here is a reference architecture of a replication cluster with ETL, reporting and backup use cases.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/nn09rfy9-1.png\" class=\"kg-image\" alt=\"MySQL Master Slave Replication reference architecture\" loading=\"lazy\"><\/figure>\n<h2 id=\"mysql-master-slave-replication-setup\">MySQL Master Slave Replication Setup<\/h2>\n<p>In this tutorial, we will explain the step by step guide for setting up a MySQL master-slave replication. For the tutorial purpose, we will use three Nodes for the MySQL replication cluster. One master node and two slave nodes.<\/p>\n<p>Server Details:<\/p>\n<pre><code>Master Node: 10.128.0.11\nSlave 01:    10.128.0.12\nSlave 02:    10.128.0.13<\/code><\/pre>\n<h3 id=\"prerequisites-\">Prerequisites:<\/h3>\n<ol>\n<li>Minimum 2 \u00a0Nodes &#8211; one master &amp; one slave. [<strong>Centos\/Redhat 7 or greater<\/strong>]<\/li>\n<li>Connectivity to install packages using yum or through corp proxy if any.<\/li>\n<li>Root access to the servers.<\/li>\n<li>Each servers firewall should allow traffic on ports 22 and 3306.<\/li>\n<\/ol>\n<h3 id=\"install-mysql-server-on-all-three-nodes\">Install MySQL Server on all three nodes<\/h3>\n<p>Let&#8217;s install MySQL server on all the three nodes with specific configurations for master-slave replication.<\/p>\n<p>Execute the following steps on all the three nodes.<\/p>\n<p><strong>Step 1:<\/strong> Install wget<\/p>\n<pre><code>sudo yum install wget -y<\/code><\/pre>\n<p><strong>Step 2: <\/strong>To install MySQL, we need to add the <a href=\"https:\/\/dev.mysql.com\/downloads\/repo\/yum\/?ref=devopscube.com\" rel=\"noreferrer noopener\">MySQL community repo<\/a> which contains the actual MySQL community edition<\/p>\n<p>Download the repo<\/p>\n<pre><code>wget https:\/\/dev.mysql.com\/get\/mysql57-community-release-el7-9.noarch.rpm<\/code><\/pre>\n<p>Install the repo<\/p>\n<pre><code>sudo rpm -ivh mysql57-community-release-el7-9.noarch.rpm<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Install MySQL server.<\/p>\n<pre><code>sudo yum install mysql-server -y<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Start and enable MySQL server.<\/p>\n<pre><code>sudo systemctl start mysqld\nsudo systemctl enable mysqld<\/code><\/pre>\n<p><strong>Step 5:<\/strong> Get the default generated admin password from the mysqld.log file. This password has to be used in step 5 to change the default root password.<\/p>\n<pre><code>sudo grep 'temporary password' \/var\/log\/mysqld.log<\/code><\/pre>\n<p><strong>Step 6: <\/strong>Setup MySQL password and other key configurations using the following command. All the steps are self-explanatory.<\/p>\n<pre><code>mysql_secure_installation<\/code><\/pre>\n<h3 id=\"configure-mysql-master-node\">Configure MySQL Master Node<\/h3>\n<p>First, we will configure the master node with the required configuration.<\/p>\n<p>The key configurations are<\/p>\n<ol>\n<li>Binding static IP address to MySQL server.<\/li>\n<li>Unique ID config for master. Each server in the cluster should be identified uniquely.<\/li>\n<li>Enable Binary log &#8211; It contains information about the data changes made to MySQL.<\/li>\n<li>Create a replication user that will be used by the slaves to replicate the data from the master.<\/li>\n<\/ol>\n<blockquote><p><strong>Note:<\/strong><em> In this installation, MySQL data dir will default to \/var\/lib\/mysql in the root file system. For production use cases, this path should be an external disk attached to the VM.<\/em><\/p><\/blockquote>\n<p><strong>Step 1:<\/strong> Open \/etc\/my.cnf file.<\/p>\n<pre><code>sudo vi \/etc\/my.cnf<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Add the following three parameters under the [mysqld] section and save it. Replace 10.128.0.11 your server IP.<\/p>\n<pre><code>bind-address           = 10.128.0.11\nserver-id              = 1\nlog_bin                = mysql-bin<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Restart the MySQL server for the configurations changes to take place.<\/p>\n<pre><code>sudo systemctl restart mysqld<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Check the MySQL status to make sure all the configurations are applied as expected without any errors.<\/p>\n<pre><code>sudo systemctl status mysqld<\/code><\/pre>\n<p><strong>Step 5:<\/strong> Login to MySQL server as the root user.<\/p>\n<pre><code>mysql -uroot -p<\/code><\/pre>\n<p><strong>Step 6:<\/strong> Create a user named <code>replicauser<\/code> with a strong password. This user will be used by the slaves to replicate the data from the master. Replace 10.128.0.11 with your master IP<\/p>\n<pre><code>CREATE USER 'replicauser'@'10.128.0.11' IDENTIFIED BY 'my-secret-password';<\/code><\/pre>\n<p><strong>Step 7:<\/strong> Grant privileges to the slave user for slave replication.<\/p>\n<pre><code>GRANT REPLICATION SLAVE ON *.* TO 'replicauser'@'%' IDENTIFIED BY 'my-secret-password';<\/code><\/pre>\n<p><strong>Step 8:<\/strong> From the MySQL prompt, Check the master status. Note down the file [<strong>mysql-bin.000001<\/strong>] and Position[<strong>706<\/strong>] parameters from the output. It is required for the slave replication configuration.<\/p>\n<pre><code>SHOW MASTER STATUS\\G<\/code><\/pre>\n<p>The output would look like the following.<\/p>\n<pre><code>mysql&gt; SHOW MASTER STATUS\\G\n*************************** 1. row ***************************\n             File: mysql-bin.000001\n         Position: 706\n     Binlog_Do_DB:\n Binlog_Ignore_DB:\nExecuted_Gtid_Set:\n1 row in set (0.00 sec)<\/code><\/pre>\n<h3 id=\"configure-mysql-replication-slave-node\">Configure MySQL Replication Slave Node<\/h3>\n<p>Execute the following steps in all the slaves.<\/p>\n<p><strong>Step 1:<\/strong> Add the same configurations as the master to the \u00a0\/etc\/my.cnf file with the Slave Ip address and unique server ID.<\/p>\n<pre><code>bind-address           = 10.128.0.12\nserver-id              = 2\nlog_bin                = mysql-bin<\/code><\/pre>\n<blockquote><p><strong>Note:<\/strong> If you more than one slave, make sure you replace the respective slave IP and add a unique server-id per slave.<\/p><\/blockquote>\n<p><strong>Step 2:<\/strong> Restart the MySQL service.<\/p>\n<pre><code>sudo systemctl restart mysqld<\/code><\/pre>\n<p>Step 3: Login to MySQL with root credentials.<\/p>\n<pre><code>mysql -uroot -p<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Stop the <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/stop-slave.html?ref=devopscube.com\" rel=\"noreferrer noopener\">slave threads<\/a> using the following command.<\/p>\n<pre><code>STOP SLAVE;<\/code><\/pre>\n<p><strong>Step 5:<\/strong> Execute the following statement from MySQL prompt replacing the master IP [10.128.0.15], replicauser password [replicauser-secret-password].<\/p>\n<p>Replace <code>MASTER_LOG_FILE<\/code> &amp; <code>MASTER_LOG_POS<\/code> with the values, you got from step 8 in master configuration.<\/p>\n<pre><code>CHANGE MASTER TO MASTER_HOST='10.128.0.15',MASTER_USER='replicauser', MASTER_PASSWORD='replicauser-secret-password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  706;<\/code><\/pre>\n<p><strong>Step 6:<\/strong> Now, start the slave threads.<\/p>\n<pre><code>START SLAVE;<\/code><\/pre>\n<p><strong>Step 7:<\/strong> Check the MySQL replication slave status.<\/p>\n<pre><code>SHOW SLAVE STATUS\\G<\/code><\/pre>\n<p><code>Slave_SQL_Running_State<\/code> parameter will show the current slave status.<\/p>\n<h2 id=\"test-mysql-master-slave-replication\">Test MySQL Master-Slave Replication<\/h2>\n<p>In this section, we will test master-slave replication.<\/p>\n<h3 id=\"on-master-server\">On Master Server<\/h3>\n<p>Login to master mySQL CLI.<\/p>\n<pre><code>mysql -uroot -p<\/code><\/pre>\n<p>Create a database named \u00a0testdb<\/p>\n<pre><code>CREATE DATABASE testdb;<\/code><\/pre>\n<h3 id=\"on-slave-server\">On Slave Server<\/h3>\n<p>Login to slave mySQL CLI.<\/p>\n<pre><code>mysql -uroot -p<\/code><\/pre>\n<p>List the Databases. You should see the testdb database created from the master server.<\/p>\n<pre><code>show databases;<\/code><\/pre>\n<p>Example, output.<\/p>\n<pre><code>mysql&gt; show databases;\n+--------------------+\n| Database           |\n+--------------------+\n| information_schema |\n| mysql              |\n| performance_schema |\n| sys                |\n| testdb             |\n+--------------------+<\/code><\/pre>\n<h3 id=\"conclusion\">Conclusion<\/h3>\n<p>If you are going to use MySQL master-slave replication in production, there are many other performance and MySQL parameters to be considered from a DBA perspective. Even if you have master-slave replication, a solid backup strategy should be in place for data restoration and disaster recovery.<\/p>\n<p>So, are you planning to use MySQL master-slave replication in production? What is your existing setup?<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-mysql-master-slave-replication\/\" target=\"_blank\" rel=\"noopener noreferrer\">Setup MySQL Replication Cluster: Architecture, Use Cases and Tutorial \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-mysql-master-slave-replication\/<\/p>\n","protected":false},"author":1,"featured_media":1034,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1033","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1033"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1033\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/1034"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}