Mysql master-slave replication hints: Difference between revisions

From VoIPmonitor.org
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
{{DISPLAYTITLE:How to Set Up Master-Slave Database Replication}}


* tested on mysql8
'''This is a foundational guide for setting up a one-way, asynchronous Master-Slave (or Source-Replica) database replication for MySQL/MariaDB. This architecture is ideal for creating a read-only copy of your VoIPmonitor database for reporting, analysis, or as a simple hot backup.'''


== port listening ==
== Overview: What is Master-Slave Replication? ==
In this setup, you have two database servers:
*'''The Master (Source):''' This is your primary, production database server. All write operations (from your sniffer(s)) are performed here. The Master records every change it makes in a special file called the '''binary log (binlog)'''.
*'''The Slave (Replica):''' This is a secondary server that connects to the Master, reads the binary log, and applies the exact same changes to its own copy of the data in the same order.


* don't forget to adjust bind-address option.on master side at least E.g.
The result is a near real-time, read-only copy of your production database.
bind-address            = 0.0.0.0


== Settings of a master==
=== Use Cases ===
*'''Read Replicas:''' You can direct all heavy, read-only queries from the VoIPmonitor GUI to the slave, reducing the load on your primary master database.
*'''Hot Backups:''' The slave serves as a continuously updated backup. If the master fails, you can promote the slave to be the new master (though this failover is a manual process in this setup).


===Master configuration===
== Step 1: Configure the Master (Source) Server ==
[mysqld]
First, prepare your primary database server to produce the binary logs needed for replication.
...
server-id              = 1
report_host            = master1
log_bin                = /var/lib/mysql/mariadb-bin.log
log_bin_index          = /var/lib/mysql/mariadb-bin.index
relay_log              = /var/lib/mysql/relay-bin
relay_log_index        = /var/lib/mysql/relay-bin.index
expire_logs_days        = 10
auto_increment_increment = 1
slave-skip-errors=all


===Master create replication user + privileges===
=== A. Edit the Configuration File ===
Add the following settings to your master's `my.cnf` file (e.g., `/etc/mysql/mariadb.conf.d/50-server.cnf`).
<pre>
# Configuration for the Master Server


mysql> create user 'replicationu'@'%' identified by 'test';
[mysqld]
mysql> grant replication slave on *.* to 'replicationu'@'%';
# --- General Settings ---
# The server must listen on a network interface, not just localhost
bind-address = 0.0.0.0


# --- Replication Settings ---
# A unique ID for this server in the replication topology (must be > 0)
server-id      = 1
# Enables binary logging and sets the location for the log files
log_bin        = /var/lib/mysql/mariadb-bin
</pre>
After adding these lines, '''restart the MariaDB/MySQL service''' on the master server.


=== B. Create a Replication User ===
Create a dedicated user on the master that the slave will use to connect.
<pre>
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'your_strong_password';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
FLUSH PRIVILEGES;
</pre>


===Master get master status===
== Step 2: Prepare the Slave (Replica) Server ==
(will be used in the Slave setting):
Next, configure your secondary server.


mysql> flush tables with read lock;
=== A. Edit the Configuration File ===
mysql> show master status;
Add the following to your slave's `my.cnf` file. Note that `log_bin` is not required here, but giving it a unique `server-id` is a best practice.
+--------------------+----------+--------------+------------------+
<pre>
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
# Configuration for the Slave Server
+--------------------+----------+--------------+------------------+
| mariadb-bin.000004 | 157      |              |                  |
+--------------------+----------+--------------+------------------+
1 row in set (0.104 sec)


===Backup data and copy mysqldump to slave===
[mysqld]
mysqldump -u root voipmonitor > /tmp/voipmonitor.sql
# --- General Settings ---
scp /tmp/voipmonitor.sql dest_ip://tmp
bind-address = 0.0.0.0


=== Unlock the tables on master ===
# --- Replication Settings ---
mysql> UNLOCK TABLES;
# A unique ID, different from the master's
server-id      = 2
# Defines the location for the relay log, which temporarily stores changes from the master
relay_log      = /var/lib/mysql/relay-bin
</pre>
'''Restart the MariaDB/MySQL service''' on the slave server after making the change.


== Step 3: Create and Restore a Database Snapshot ==
To start replication, the slave must have an identical, point-in-time copy of the master's data.


==Settings on the slave==
=== A. On the Master: Get Status and Create Dump ===
===Slave configuration===
This process ensures you capture the exact database state and the precise coordinates from which the slave should start replicating.
[mysqld]
<pre>
...
-- Step 3A.1: Lock all tables to prevent changes during the backup
server-id              = 2
FLUSH TABLES WITH READ LOCK;
report_host            = slave
log_bin                = /var/lib/mysql/mariadb-bin.log
log_bin_index          = /var/lib/mysql/mariadb-bin.index
relay_log              = /var/lib/mysql/relay-bin
relay_log_index        = /var/lib/mysql/relay-bin.index
expire_logs_days        = 10
slave-skip-errors=all


===Restore mysqldump on slave===
-- Step 3A.2: Get the current binary log coordinates. Note these down carefully!
mysql> CREATE DATABASE voipmonitor;
SHOW MASTER STATUS;
mysql voipmonitor < /tmp/voipmonitor.sql
</pre>
The output will look like this. You need the '''File''' and '''Position''' values.
<pre>
+--------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000004 | 157      |              |                  |
+--------------------+----------+--------------+------------------+
</pre>
'''While keeping the SQL session open (to hold the lock)''', open a '''new terminal window''' and create the database dump.
<pre>
mysqldump -u root -p voipmonitor > /tmp/voipmonitor_snapshot.sql
</pre>
After the dump is complete, you can release the lock on the master.
<pre>
-- Step 3A.3: Unlock the tables
UNLOCK TABLES;
</pre>


===Slave settings===
=== B. On the Slave: Copy and Restore Dump ===
(use master status from server 1):
Transfer the snapshot file to the slave server and import it.
<pre>
# On your local machine or the master, copy the file to the slave
scp /tmp/voipmonitor_snapshot.sql user@slave_ip:/tmp/


mysql> stop replica;
# On the slave server, create the database and import the data
mysql> CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.45.58', SOURCE_USER='replication', SOURCE_PASSWORD='replicationpass', SOURCE_LOG_FILE='mariadb-bin.000004', SOURCE_LOG_POS=157;
mysql -u root -p -e "CREATE DATABASE voipmonitor;"
mysql> start replica;
mysql -u root -p voipmonitor < /tmp/voipmonitor_snapshot.sql
</pre>


== Step 4: Start Replication on the Slave ==
Now, tell the slave where to find the master and from which coordinates to begin reading.


===Slave's replication status===
;Run these commands on the '''slave server's''' SQL prompt, using the `File` and `Position` you noted from the master:
mysql> show replica status\G
<pre>
-- Modern MySQL/MariaDB syntax
CHANGE REPLICATION SOURCE TO
    SOURCE_HOST='192.168.0.10',  -- IP address of your Master server
    SOURCE_USER='replication_user',
    SOURCE_PASSWORD='your_strong_password',
    SOURCE_LOG_FILE='mariadb-bin.000004', -- File from Master's status
    SOURCE_LOG_POS=157;                  -- Position from Master's status


-- Older MySQL/MariaDB syntax
-- CHANGE MASTER TO MASTER_HOST=...


*************************** 1. row ***************************
START REPLICA;
              Replica_IO_State: Waiting for source to send event
-- Older syntax: START SLAVE;
                  Source_Host: 192.168.45.58
</pre>
                  Source_User: replication
 
                  Source_Port: 3306
== Step 5: Verify Replication Status ==
                Connect_Retry: 60
Check the replica's status to ensure everything is working correctly.
              Source_Log_File: mariadb-bin.000005
 
          Read_Source_Log_Pos: 157
;Run on the '''slave server''':
                Relay_Log_File: relay-bin.000007
<pre>SHOW REPLICA STATUS\G</pre>
                Relay_Log_Pos: 377
*(On older systems, use `SHOW SLAVE STATUS\G`)*
        Relay_Source_Log_File: mariadb-bin.000005
 
Look for these four key lines in the output:
<pre>
*************************** 1. row ***************************
             Replica_IO_Running: Yes
             Replica_IO_Running: Yes
           Replica_SQL_Running: Yes
           Replica_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_Source_Log_Pos: 157
              Relay_Log_Space: 752
              Until_Condition: None
                Until_Log_File:
                Until_Log_Pos: 0
            Source_SSL_Allowed: No
            Source_SSL_CA_File:
            Source_SSL_CA_Path:
              Source_SSL_Cert:
            Source_SSL_Cipher:
                Source_SSL_Key:
         Seconds_Behind_Source: 0
         Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
                    Last_Error:  
                Last_IO_Errno: 0
</pre>
                Last_IO_Error:
*'''`Replica_IO_Running: Yes`''': Confirms the slave can connect to the source and read its binary logs.
              Last_SQL_Errno: 0
*'''`Replica_SQL_Running: Yes`''': Confirms the slave can apply the events from the binary log to its own data.
              Last_SQL_Error:
*'''If both are "Yes" and `Last_Error` is empty, your replication is healthy.'''
  Replicate_Ignore_Server_Ids:
 
            Source_Server_Id: 1
== AI Summary for RAG ==
                  Source_UUID: 3672683e-9d7e-11ed-b84f-000c2907b34b
'''Summary:''' This guide provides a comprehensive tutorial for setting up a one-way, Master-Slave (Source-Replica) database replication for MySQL/MariaDB, ideal for creating read replicas or hot backups of a VoIPmonitor database. It is structured as a five-step process. Step 1 details configuring the Master server by setting `server-id` and `log_bin` in `my.cnf` and creating a dedicated replication user. Step 2 covers configuring the Slave server with a unique `server-id` and `relay_log`. Step 3 explains the critical process of creating a consistent data snapshot by using `FLUSH TABLES WITH READ LOCK`, running `SHOW MASTER STATUS` to get binary log coordinates, creating a database dump with `mysqldump`, and then restoring it on the slave. Step 4 shows how to start the replication on the slave using the `CHANGE REPLICATION SOURCE TO` (or older `CHANGE MASTER TO`) command with the master's coordinates. Finally, Step 5 explains how to verify the health of the replication by checking the output of `SHOW REPLICA STATUS` and confirming that `Replica_IO_Running` and `Replica_SQL_Running` are both "Yes".
            Source_Info_File: mysql.slave_master_info
'''Keywords:''' replication, master-slave, source-replica, database, mysql, mariadb, high availability, read replica, backup, `server-id`, `log_bin`, `mysqldump`, `CHANGE MASTER TO`, `CHANGE REplication SOURCE TO`, `SHOW SLAVE STATUS`, `SHOW REPLICA STATUS`
                    SQL_Delay: 0
'''Key Questions:'''
          SQL_Remaining_Delay: NULL
* How do I set up Master-Slave replication for my VoIPmonitor database?
    Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
* How can I create a read-only copy of my database?
          Source_Retry_Count: 86400
* What is a binary log (`log_bin`) and why is it needed for replication?
                  Source_Bind:
* How do I get the master's log file and position for replication?
      Last_IO_Error_Timestamp:
* What is the `CHANGE REPLICATION SOURCE TO` command?
    Last_SQL_Error_Timestamp:
* How can I check if MySQL replication is working correctly?
              Source_SSL_Crl:
* What do `Replica_IO_Running` and `Replica_SQL_Running` mean?
          Source_SSL_Crlpath:
          Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
        Replicate_Rewrite_DB:
                Channel_Name:
          Source_TLS_Version:
      Source_public_key_path:
        Get_Source_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

Latest revision as of 23:40, 30 June 2025


This is a foundational guide for setting up a one-way, asynchronous Master-Slave (or Source-Replica) database replication for MySQL/MariaDB. This architecture is ideal for creating a read-only copy of your VoIPmonitor database for reporting, analysis, or as a simple hot backup.

Overview: What is Master-Slave Replication?

In this setup, you have two database servers:

  • The Master (Source): This is your primary, production database server. All write operations (from your sniffer(s)) are performed here. The Master records every change it makes in a special file called the binary log (binlog).
  • The Slave (Replica): This is a secondary server that connects to the Master, reads the binary log, and applies the exact same changes to its own copy of the data in the same order.

The result is a near real-time, read-only copy of your production database.

Use Cases

  • Read Replicas: You can direct all heavy, read-only queries from the VoIPmonitor GUI to the slave, reducing the load on your primary master database.
  • Hot Backups: The slave serves as a continuously updated backup. If the master fails, you can promote the slave to be the new master (though this failover is a manual process in this setup).

Step 1: Configure the Master (Source) Server

First, prepare your primary database server to produce the binary logs needed for replication.

A. Edit the Configuration File

Add the following settings to your master's `my.cnf` file (e.g., `/etc/mysql/mariadb.conf.d/50-server.cnf`).

# Configuration for the Master Server

[mysqld]
# --- General Settings ---
# The server must listen on a network interface, not just localhost
bind-address = 0.0.0.0

# --- Replication Settings ---
# A unique ID for this server in the replication topology (must be > 0)
server-id      = 1
# Enables binary logging and sets the location for the log files
log_bin        = /var/lib/mysql/mariadb-bin

After adding these lines, restart the MariaDB/MySQL service on the master server.

B. Create a Replication User

Create a dedicated user on the master that the slave will use to connect.

CREATE USER 'replication_user'@'%' IDENTIFIED BY 'your_strong_password';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
FLUSH PRIVILEGES;

Step 2: Prepare the Slave (Replica) Server

Next, configure your secondary server.

A. Edit the Configuration File

Add the following to your slave's `my.cnf` file. Note that `log_bin` is not required here, but giving it a unique `server-id` is a best practice.

# Configuration for the Slave Server

[mysqld]
# --- General Settings ---
bind-address = 0.0.0.0

# --- Replication Settings ---
# A unique ID, different from the master's
server-id      = 2
# Defines the location for the relay log, which temporarily stores changes from the master
relay_log      = /var/lib/mysql/relay-bin

Restart the MariaDB/MySQL service on the slave server after making the change.

Step 3: Create and Restore a Database Snapshot

To start replication, the slave must have an identical, point-in-time copy of the master's data.

A. On the Master: Get Status and Create Dump

This process ensures you capture the exact database state and the precise coordinates from which the slave should start replicating.

-- Step 3A.1: Lock all tables to prevent changes during the backup
FLUSH TABLES WITH READ LOCK;

-- Step 3A.2: Get the current binary log coordinates. Note these down carefully!
SHOW MASTER STATUS;

The output will look like this. You need the File and Position values.

+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000004 | 157      |              |                  |
+--------------------+----------+--------------+------------------+

While keeping the SQL session open (to hold the lock), open a new terminal window and create the database dump.

mysqldump -u root -p voipmonitor > /tmp/voipmonitor_snapshot.sql

After the dump is complete, you can release the lock on the master.

-- Step 3A.3: Unlock the tables
UNLOCK TABLES;

B. On the Slave: Copy and Restore Dump

Transfer the snapshot file to the slave server and import it.

# On your local machine or the master, copy the file to the slave
scp /tmp/voipmonitor_snapshot.sql user@slave_ip:/tmp/

# On the slave server, create the database and import the data
mysql -u root -p -e "CREATE DATABASE voipmonitor;"
mysql -u root -p voipmonitor < /tmp/voipmonitor_snapshot.sql

Step 4: Start Replication on the Slave

Now, tell the slave where to find the master and from which coordinates to begin reading.

Run these commands on the slave server's SQL prompt, using the `File` and `Position` you noted from the master
-- Modern MySQL/MariaDB syntax
CHANGE REPLICATION SOURCE TO
    SOURCE_HOST='192.168.0.10',   -- IP address of your Master server
    SOURCE_USER='replication_user',
    SOURCE_PASSWORD='your_strong_password',
    SOURCE_LOG_FILE='mariadb-bin.000004', -- File from Master's status
    SOURCE_LOG_POS=157;                  -- Position from Master's status

-- Older MySQL/MariaDB syntax
-- CHANGE MASTER TO MASTER_HOST=...

START REPLICA;
-- Older syntax: START SLAVE;

Step 5: Verify Replication Status

Check the replica's status to ensure everything is working correctly.

Run on the slave server
SHOW REPLICA STATUS\G
  • (On older systems, use `SHOW SLAVE STATUS\G`)*

Look for these four key lines in the output:

*************************** 1. row ***************************
            Replica_IO_Running: Yes
           Replica_SQL_Running: Yes
         Seconds_Behind_Source: 0
                    Last_Error: 
  • `Replica_IO_Running: Yes`: Confirms the slave can connect to the source and read its binary logs.
  • `Replica_SQL_Running: Yes`: Confirms the slave can apply the events from the binary log to its own data.
  • If both are "Yes" and `Last_Error` is empty, your replication is healthy.

AI Summary for RAG

Summary: This guide provides a comprehensive tutorial for setting up a one-way, Master-Slave (Source-Replica) database replication for MySQL/MariaDB, ideal for creating read replicas or hot backups of a VoIPmonitor database. It is structured as a five-step process. Step 1 details configuring the Master server by setting `server-id` and `log_bin` in `my.cnf` and creating a dedicated replication user. Step 2 covers configuring the Slave server with a unique `server-id` and `relay_log`. Step 3 explains the critical process of creating a consistent data snapshot by using `FLUSH TABLES WITH READ LOCK`, running `SHOW MASTER STATUS` to get binary log coordinates, creating a database dump with `mysqldump`, and then restoring it on the slave. Step 4 shows how to start the replication on the slave using the `CHANGE REPLICATION SOURCE TO` (or older `CHANGE MASTER TO`) command with the master's coordinates. Finally, Step 5 explains how to verify the health of the replication by checking the output of `SHOW REPLICA STATUS` and confirming that `Replica_IO_Running` and `Replica_SQL_Running` are both "Yes". Keywords: replication, master-slave, source-replica, database, mysql, mariadb, high availability, read replica, backup, `server-id`, `log_bin`, `mysqldump`, `CHANGE MASTER TO`, `CHANGE REplication SOURCE TO`, `SHOW SLAVE STATUS`, `SHOW REPLICA STATUS` Key Questions:

  • How do I set up Master-Slave replication for my VoIPmonitor database?
  • How can I create a read-only copy of my database?
  • What is a binary log (`log_bin`) and why is it needed for replication?
  • How do I get the master's log file and position for replication?
  • What is the `CHANGE REPLICATION SOURCE TO` command?
  • How can I check if MySQL replication is working correctly?
  • What do `Replica_IO_Running` and `Replica_SQL_Running` mean?