How To Set Up Master-Slave Replication in MySQL

Hakan Bayraktar
2 min readMar 11, 2019

MySQL replication is a process that enables data from one MySQL database server (the master) to be copied automatically to one or more MySQL database servers (the slaves).

First, let’s set up 2 MySQL servers. (MySQL install)
Master Database: 10.0.0.11
Slave Database : 10.0.0.12

Change settings on Master Host

Change in /etc/my.cnf file settings and create a user for replication on MySQL

vi /etc/my.cnf

[mysqld]
log-bin=mysql-bin
server-id=11

Restart MySQL Service

#systemctl restart mysqld

create a user

#mysql -u root -p
Enter password:
mysql>grant replication slave on *.* to replica@’%’ identified by ‘password’;
Query OK, 0 rows affected (0.00 sec)
mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit

Change settings on Slave Host

vi /etc/my.cnf

[mysqld]
log-bin=mysql-bin
server-id=12
read_only=1

Restart MySQL Service

#systemctl restart mysqld

Change settings on Master Host

We should lock all tables and learn show master status (remember File, Position value)

#mysql -u root -p
Enter password:
mysql>flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 212 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Get Dump-Data on Master Host

#mysqldump -u root -p — all-databases — lock-all-tables — events > database_dump.sql

Copy the dump file to Slave Host

#scp database_dump.sql root@10.0.0.12:/tmp/

We should unlock all the tables

#mysql -u root -p
Enter password:
mysql>unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql>exit

Change settings on Slave Host

import dump MySQL database file

#mysql -u root -p < /tmp/database_dump.sql
Enter password:

replica settings and start replication

#mysql -u root -p
Enter password:
mysql>change master to
->master_host='10.0.0.11',master_user='replica',
->master_password='password',
->master_log_file='mysql-bin.000005',
->master_log_pos=212;

Query OK, 0 rows affected (0.54 sec)
mysql>start slave;
Query OK, 0 rows affected (0.00 sec)
mysql>show slave status\G*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.11
Master_User: replica
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 732
Relay_Log_File: node01-relay-bin.000006
Relay_Log_Pos: 460
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 632
Relay_Log_Space: 568
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 101
Master_UUID: 2222c042-dd31-12d6-7f68-5754505d05ae
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

--

--