• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » MySQL Configuration to Set Up Master-Slave Replication

By Abhishek Ghosh October 6, 2019 10:14 pm Updated on October 6, 2019

MySQL Configuration to Set Up Master-Slave Replication

Advertisement

Our Use-Case of Master-Slave Replication is For WordPress. Master-Slave Replication For WordPress Makes it Fail-Proof to WordPress Database Errors Making the Website Fully Unreadable (which we have discussed in our article on Difference Between Standalone Database and Distributed Database). The main reason for so much discussion is for one big reason – the master MySQL database will read and write like a normal MySQL database while the slave MySQL database only is readable to WordPress. It does mean – easy switch to one-server setup. So, this kind of setup needs work on WordPress settings file too. One can use the same methodology to setup MySQL replication for other purposes other than for using WordPress. A WordPress setup with MySQL master-slave replication has three parts :

  1. WordPress configuration part
  2. Server configuration part
  3. MySQL configuration part

 

Among the server configuration, there are optional matters like using Percona XtraBackup to initially copy the backup. As the initial step, you should have at least two servers running MySQL with identical optimization except the configaration part we are going to discuss. One of the servers probably your live WordPress website which will act as master. Please note that we are using InnoDB engine. Make sure that you have allowed TCP port 3306 on Iptables.

We will suggest using two fresh cloud server instances to learn than directly using a live website’s MySQL database as a master. You can keep the instances running for 1-2 days to get the grasp. We have guide to install Percona MySQL on Ubuntu. You must resolve debian-sys-maint user matters related to Debian, Ubuntu and MySQL.

Advertisement

---

MySQL Configuration to Set Up Master-Slave Replication

 

Required MySQL Configuration

 

A default my.cnf is provided with any flavour of MySQL but we will use the master.cnf and slave.cnf configuration files when starting up the MySQL servers at least for testing. We are mentioning only the minimum required parameters for [mysqld] sections.

master.cnf
Vim
1
2
3
4
5
6
7
8
[mysqld]
server-id=1
log-bin=master-slave.log
datadir=/path/to/mysql/master/data
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog-ignore-db = "mysql"
bind-address = 0.0.0.0

and this is of slave :

slave.cnf
Vim
1
2
3
4
5
6
7
8
9
10
[mysqld]
server-id=2
master-host =  [private-IP-of-master]
master-user = [replication-username]
master-password = [replication-password]
master-connect-retry = 60
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
datadir=/path/to/mysql/slave/data
bind-address = 0.0.0.0

Edit the /path/to/mysql/.. in both.

innodb_flush_log_at_trx_commit=1 and sync_binlog=1 options are used for the greater durability and consistency in a replication setup using InnoDB with transactions. Now, we can start the MySQL servers :

Vim
1
2
3
4
5
6
7
8
#
 
mysqld --defaults-file=/path/to/mysql/master/master.cnf &
 
# slave server
 
mysqld --defaults-file=/path/to/mysql/slave/slave.cnf &
#

We need to create replication user on the master server that the slave server can use to connect :

Vim
1
2
3
mysql -u root --prompt='master> '
master> CREATE USER repl_user@slave-ip-address;
master> GRANT REPLICATION SLAVE ON *.* TO repl_user@slave-ip-address IDENTIFIED BY 'your-password';

OR, depending on the flavour you may do in this way :

Vim
1
2
3
4
# mysql -u root -p
mysql> grant replication slave on *.* TO [replication_username]@'[IP of slave]' identified by '[some password]';
mysql> flush privileges;
mysql> quit

We can now start replication on the slave :

Vim
1
2
3
4
5
slave> CHANGE MASTER TO MASTER_HOST='master-ip-address',
-> MASTER_USER='repl_user',
-> MASTER_PASSWORD='your-password',
-> MASTER_LOG_FILE='',
-> MASTER_LOG_POS=4;

You can start replication on the slave by issuing this command :

Vim
1
mysql> start slave;

Restart both the MySQL servers. You can test the system by creating database on the master :

Vim
1
2
3
4
5
6
7
# mysql -u root -p
mysql> create database testing;
mysql> use testing
mysql> create table users(id int not null auto_increment, primary key(id), username varchar(30) not null);
mysql> insert into users (username) values ('foo');
mysql> insert into users (username) values ('bar');
mysql> exit

In case, the master is a MySQL database of a live website, use the following command to ensure that nothing can write to the master database during a database dump:

Vim
1
2
3
# mysql -u root -p
mysql> FLUSH TABLES WITH READ LOCK;
mysql> SHOW MASTER STATUS;

Also, you need to note the filename and position of the binary log to complete the replication configuration on the slave server. After creating a database dump, lift the read lock from the master:

Vim
1
2
# mysql -u root -p
mysql> UNLOCK TABLES;

Complete the slave replication steps:

Vim
1
2
3
4
5
6
7
8
9
# mysql -u root -p
mysql> SLAVE STOP;
mysql> CHANGE MASTER TO MASTER_HOST='[IP-of-master]',
MASTER_USER='[replication-username]',
MASTER_PASSWORD='[replication-password]',
MASTER_LOG_FILE='[file-listed-on-master]',
MASTER_LOG_POS=[log-position-listed-on-master];
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G

 

Conclusion

 

After the test setup works, move the perfect settings to production. Setup of MySQL master-slave replication is just a matter of getting used. Also, you may read different guides on the topic for extra tips, like that from Percona :

Vim
1
https://www.percona.com/doc/percona-xtrabackup/2.3/howtos/setting_up_replication.html

Tagged With setting up mysql in 5 7 21 slave on windows , master cnf , master slave configuration in database , master slave replication , mysql master -slave configuration , mysql master slave in same computer windows 7 , mysql set up a master slave on windows 7 , mysqld cnf replication-same-server-id , use case mysql master slave
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to MySQL Configuration to Set Up Master-Slave Replication

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • Steps to Install Percona MySQL Server on Ubuntu 18.04 LTS

    Here is Latest, Tested Steps to Install Percona MySQL Server on Ubuntu 18.04 LTS For Cloud Server, VPS and Dedicated Server.

  • Fix WordPress Error in Database Connection : MySQL Part

    MySQL Setup & Corrupted Database Has to Do With the Error Establishing a Database Connection. Fix WordPress Error in Database Connection.

  • Join/Merge Multiple Log Files For Big Data Analysis

    Here Are The Ways To Join/Merge Multiple Log Files For Big Data Analysis, Store Them To OpenStack Based Cloud Storage And Delete Old Files.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy