• Home
  • About Us
  • Contact
  • Privacy Policy

LinuxAdmin.io

  • Home
  • Tutorials
    • System Administration
    • Linux Tutorials
    • Web Hosting
    • WordPress
    • Virtualization
    • Troubleshooting
    • OpenStack

MySQL Replication Master Slave Setup

Mysql Replication

MySQL Replication allows you to synchronize slave copies of a MySQL server. You can then use the slave to perform backups and a recovery option if the master should go offline for any reason. MySQL needs to be installed on both servers.

Install MySQL on both servers:

yum install -y mysql-server mysql-client mysql-devel

Edit /etc/my.cnf on both servers and set a unique numerical server id(any number is fine as long as they are not the same):

server-id = 1

Configure MySQL Replication On The Master

On the master ensure a bin log is set in /etc/my.cnf

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

Restart mysql

service mysqld restart

Connect o mysql on the master

mysql -u root -p

Grant privileges to the slave

GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'password';

Load the new privileges

FLUSH PRIVILEGES;

Lock the MySQL master  so no new updates can be written while you are creating the slave

FLUSH TABLE WITH READ LOCK;

Get the current master status

SHOW MASTER STATUS;

This will return a similar result to this:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 107 |  | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

This is the position the slave will be on, save this information for later. You will need to keep the mysql client open on the master. If you close it the read lock will stop and will cause replication issues when trying to sync it.

Open a new ssh client and dump the databases

mysqldump -u root -p --all-databases > all.sql

If it is particularly large mysql server, you can rsync all of /var/lib/mysql

Once the copy has completed go ahead and type the following on the MySQL master:

UNLOCK TABLES;

Go ahead and quit on the master

Configure MySQL Replication On The Slave

Import the databases on the slave

mysql < all.sql

You should also enabled the server-id in /etc/my.cnf and restart it

Once it has been restarted and the databases have been imported.  You can setup the replication with the following with the following command in the mysql client:

CHANGE MASTER TO MASTER_HOST='IP ADDRESS OF MASTER',MASTER_USER='slave', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  107;

Change MASTER_LOG_FILE and MASTER_LOG_POS to the values you got earlier from the master. Once you have entered the above command go ahead and start the slave:

START SLAVE;

To check current slave status to

SHOW SLAVE STATUS;

This is a basic Master-Slave Mysql replication configuration.

Apr 29, 2017LinuxAdmin.io
0 0 votes
Article Rating
PHP-FPM With PHP7 From SourceHot Clone A CentOS Server With Rsync
You Might Also Like
 
Install The ngx_cache_purge Module In Nginx
 
How To Setup SSH Key Authentication
Subscribe
Notify of
guest

guest

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Hitesh
Hitesh
7 years ago

Hey very interesting blog!

0
Reply
Sam
Sam
7 years ago

Hello I am so glad I found your website, I really found you by accident, while I was researching on Aol for something else, Anyhow I am here now and would just like to say many thanks for a fantastic post and a all round interesting blog (I also love the theme/design), I don’t have time to go through it all at the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the superb job.

0
Reply
LinuxAdmin.io
Author
LinuxAdmin.io
7 years ago
Reply to  Sam

Thank you!

0
Reply
8 years ago 3 Comments Linux Tutorials, MySQL centos583
Recent Posts
  • Configuring Firewalld to Work Seamlessly with Docker on CentOS
  • Mastering NGINX Tuning: Optimizing Web Server Performance
  • Exploring eBPF: Revolutionizing Network and System Monitoring
  • Kubernetes: Installation and Overview on CentOS 8
  • Install Docker on Centos 8
Most Commented
Hot Clone Linux Server with Rsync
Hot Clone A CentOS Server With Rsync
14 Comments
ngx_cache_purge module
Install The ngx_cache_purge Module In Nginx
8 Comments
piwik-nginx
Piwik Analytics on Nginx
8 Comments
Tags
linuxcentosLinux Performance Tuningsysadminkvmnetworkingmemcachedanalyticssystemd
About

We love Linux and are dedicated to creating Linux administration tutorials for System Administrators since 2016.

Most Viewed
Default Gateway
How To Configure A Default Gateway on CentOS
76,144 views
Zend Opcache
Setup and Optimize Zend OpCache
47,105 views
Install ffmpeg
FFMpeg Install On CentOS 7
21,675 views
Archives
Email subscription

Sign up for our newsletter to receive the latest news and event postings.

2018 © LinuxAdmin.io
wpDiscuz