{"id":12401,"date":"2024-04-18T17:40:10","date_gmt":"2024-04-18T12:10:10","guid":{"rendered":"https:\/\/www.skynats.com\/?p=12401"},"modified":"2025-01-08T20:36:51","modified_gmt":"2025-01-08T15:06:51","slug":"setting-up-mysql-master-slave-replication-in-linux-server","status":"publish","type":"post","link":"https:\/\/www.skynats.com\/blog\/setting-up-mysql-master-slave-replication-in-linux-server\/","title":{"rendered":"Setting up MySQL master-slave replication in Linux server\u00a0"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>What is MySQL replication<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL replication is the process of copying data from one MySQL database server (master) to one or more MySQL database servers (slaves). This allows you to split the load of read queries across different servers, improve reliability by keeping multiple copies of your data, and simplify backup and reporting its activities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Setting up MySQL master to slave replication involves several steps. Here&#8217;s a detailed guide on how to do it:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, I have 2 mysql installed servers, one is master and the other is slave.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Master ip : 100.101.101.90\nSlave ip  : 100.201.202.80<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Lets look, how to setup a MySQL replication from the master server to slave:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, Configure the Master Server by making the necessary changes in the mysql configuration file inorder to enable binary logging:&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the master server to enable binary logging.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># vim \/etc\/my.cnf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add or modify the following lines under the [mysqld] section:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]\nserver-id = 1\nlog_bin = \/var\/log\/mysql\/mysql-bin.log<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Save and exit, then restart mysql make the changes take effect<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># systemctl restart mysqld<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After that, Log in to MySQL on the master server and create a user with replication privileges. Replace master_user and password with the desired username and password:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; CREATE USER 'master_user'@'%' IDENTIFIED BY 'password';\n&gt; GRANT REPLICATION SLAVE ON *.* TO 'master_user'@'%';\n&gt; FLUSH PRIVILEGES;\n&gt; SHOW MASTER STATUS;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The SHOW MASTER STATUS statement provides us status information about the binary log files of the source server. note the current binary log file and position.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to copy and move data that is already in the databases, you can copy all the databases from the master server to the slave server. To do that<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Take the DB dump using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># mysqldump --all-databases --user=root --password &gt; masterdatabase.sql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Transfer it to the slave server using the transfer tools. Here we are using <strong>scp:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># scp masterdatabase.sql root@100.201.202.80:\/root<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, go to the slave server and find the location where the dump is kept. In this case, it&#8217;s in \/root <strong>(#cd \/root)<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, upload the dump file into the slave server&#8217;s MySQL<a href=\"https:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noopener\">:<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># mysql -u root -p &lt; \/root\/masterdatabase.sql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After this, configure the slave server:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to make the slave server act as a replica of the master server, modify the following line under the [mysqld] section of the mysql configuration file \/etc\/my.cnf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># vim \/etc\/my.cnf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]\nserver-id = 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After that, restart MySQL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># systemctl restart mysqld<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then, login to mysql:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># mysql -u root -p<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And run these MySQL commands to make the slave node ready to replicate databases from the master node:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; STOP SLAVE;\n&gt; CHANGE MASTER TO MASTER_HOST='100.101.101.90', \nMASTER_USER='master_user',\nMASTER_PASSWORD='password', MASTER_LOG_FILE='master_log_file', MASTER_LOG_POS=\u2019master_log_pos\u2019;\n&gt; SLAVE START;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the values for MASTER_LOG_FILE and MASTER_LOG_POS, with the values we got when running SHOW MASTER STATUS command in master node.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After the setup is completed, check the slave status to verify the replication is working correctly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; SHOW SLAVE STATUS\\G;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it! Your master to slave replication should now be set up and working using MySQL. Make sure to monitor replication regularly to ensure it continues functioning as expected.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Our&nbsp;<a href=\"https:\/\/www.skynats.com\/server-management\/\">Technical Team<\/a>&nbsp;will be available to assist you with &#8220;Setting up MySQL master-slave replication in linux server&#8221;  that can make your job a lot easier. Get in Touch with&nbsp;<a href=\"https:\/\/www.skynats.com\/server-management\/\">Skynats<\/a>&nbsp;if you have any queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is MySQL replication MySQL replication is the process of copying data from one MySQL database server (master) to one or more MySQL database servers (slaves). This allows you to split the load of read queries across different servers, improve reliability by keeping multiple copies of your data, and simplify backup and reporting its activities. [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[257,778,5,216,217,239],"tags":[263,848],"class_list":["post-12401","post","type-post","status-publish","format-standard","hentry","category-mysql","category-almalinux-server","category-blog","category-cloudlinux-7","category-cloudlinux-8","category-linux","tag-linux-server","tag-mysql-master-slave-replication"],"_links":{"self":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/12401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/comments?post=12401"}],"version-history":[{"count":0,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/12401\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/media?parent=12401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/categories?post=12401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/tags?post=12401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}