<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://serverrental.store/index.php?action=history&amp;feed=atom&amp;title=Setting_Up_Database_Replication</id>
	<title>Setting Up Database Replication - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://serverrental.store/index.php?action=history&amp;feed=atom&amp;title=Setting_Up_Database_Replication"/>
	<link rel="alternate" type="text/html" href="https://serverrental.store/index.php?title=Setting_Up_Database_Replication&amp;action=history"/>
	<updated>2026-04-14T18:39:13Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.36.1</generator>
	<entry>
		<id>https://serverrental.store/index.php?title=Setting_Up_Database_Replication&amp;diff=5744&amp;oldid=prev</id>
		<title>Admin: New server guide</title>
		<link rel="alternate" type="text/html" href="https://serverrental.store/index.php?title=Setting_Up_Database_Replication&amp;diff=5744&amp;oldid=prev"/>
		<updated>2026-04-12T15:58:07Z</updated>

		<summary type="html">&lt;p&gt;New server guide&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Setting Up Database Replication =&lt;br /&gt;
&lt;br /&gt;
This guide explains how to set up master-slave replication for MySQL and PostgreSQL databases, a crucial technique for improving database availability, read scalability, and disaster recovery. We will cover the fundamental concepts and provide practical, step-by-step instructions for both database systems.&lt;br /&gt;
&lt;br /&gt;
== Prerequisites ==&lt;br /&gt;
&lt;br /&gt;
Before you begin, ensure you have the following:&lt;br /&gt;
&lt;br /&gt;
*   '''Two or more Linux servers''': One will act as the master, and the others as slaves. These servers should have network connectivity between them. For reliable performance, consider using a VPS provider like [[PowerVPS|PowerVPS]] (https://powervps.net/?from=32) or [[Immers Cloud GPU|Immers Cloud GPU]] (https://en.immers.cloud/signup/r/20241007-8310688-334/) for your database servers.&lt;br /&gt;
*   '''Root or sudo access''': You'll need administrative privileges on all servers.&lt;br /&gt;
*   '''Installed Database Software''': MySQL (or MariaDB) and/or PostgreSQL must be installed on all participating servers.&lt;br /&gt;
*   '''Basic Networking Knowledge''': Understanding of IP addresses, ports, and firewalls.&lt;br /&gt;
*   '''Firewall Configuration''': Ensure that the database ports (default 3306 for MySQL, 5432 for PostgreSQL) are open between your master and slave servers.&lt;br /&gt;
&lt;br /&gt;
== Understanding Replication ==&lt;br /&gt;
&lt;br /&gt;
Database replication is the process of copying data from one database server (the master) to one or more other database servers (the slaves).&lt;br /&gt;
&lt;br /&gt;
*   '''Master Server''': Handles all write operations (INSERT, UPDATE, DELETE).&lt;br /&gt;
*   '''Slave Server''': Receives changes from the master and applies them. Slaves can handle read operations, distributing the read load from the master.&lt;br /&gt;
&lt;br /&gt;
There are different replication methods, but we will focus on asynchronous replication, which is the most common.&lt;br /&gt;
&lt;br /&gt;
== MySQL/MariaDB Master-Slave Replication ==&lt;br /&gt;
&lt;br /&gt;
MySQL replication works by logging all changes made to the master database in a binary log (binlog). Slave servers connect to the master, read these binlog events, and apply them to their own data.&lt;br /&gt;
&lt;br /&gt;
=== Step 1: Configure the Master Server ===&lt;br /&gt;
&lt;br /&gt;
1.  '''Edit MySQL Configuration File''':&lt;br /&gt;
    Open your MySQL configuration file (typically `/etc/mysql/my.cnf` or `/etc/my.cnf`) with a text editor.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo nano /etc/mysql/my.cnf&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.  '''Add/Modify Replication Settings''':&lt;br /&gt;
    Under the `[mysqld]` section, add or uncomment the following lines. Replace `your_server_id` with a unique integer for this server (e.g., `1`).&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    [mysqld]&lt;br /&gt;
    server-id = 1&lt;br /&gt;
    log_bin = /var/log/mysql/mysql-bin.log&lt;br /&gt;
    binlog_format = ROW&lt;br /&gt;
    # Optional: If you want to replicate specific databases&lt;br /&gt;
    # binlog_do_db = your_database_name&lt;br /&gt;
    # Optional: If you want to exclude specific databases&lt;br /&gt;
    # binlog_ignore_db = mysql&lt;br /&gt;
    # binlog_ignore_db = information_schema&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    *   `server-id`: Must be unique for each server in the replication setup.&lt;br /&gt;
    *   `log_bin`: Specifies the base name for the binary log files.&lt;br /&gt;
    *   `binlog_format`: `ROW` is generally recommended for consistency.&lt;br /&gt;
&lt;br /&gt;
3.  '''Restart MySQL Server''':&lt;br /&gt;
    Apply the changes by restarting the MySQL service.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo systemctl restart mysql&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4.  '''Create a Replication User''':&lt;br /&gt;
    Connect to your MySQL server as root and create a dedicated user for replication. Replace `slave_user`, `your_password`, and `slave_ip_address` with your desired credentials and the IP address of your slave server.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    mysql -u root -p&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    ```sql&lt;br /&gt;
    CREATE USER 'slave_user'@'slave_ip_address' IDENTIFIED BY 'your_password';&lt;br /&gt;
    GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'slave_ip_address';&lt;br /&gt;
    FLUSH PRIVILEGES;&lt;br /&gt;
    EXIT;&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
5.  '''Get Master Status''':&lt;br /&gt;
    You'll need the current binary log file name and position.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    mysql -u root -p&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    ```sql&lt;br /&gt;
    SHOW MASTER STATUS;&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
    Note down the `File` (e.g., `mysql-bin.000001`) and `Position` (e.g., `12345`).&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Configure the Slave Server ===&lt;br /&gt;
&lt;br /&gt;
1.  '''Edit MySQL Configuration File''':&lt;br /&gt;
    Open your MySQL configuration file on the slave server.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo nano /etc/mysql/my.cnf&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.  '''Add/Modify Replication Settings''':&lt;br /&gt;
    Under the `[mysqld]` section, add or uncomment the following lines. Use a different `server-id` (e.g., `2`) than the master.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    [mysqld]&lt;br /&gt;
    server-id = 2&lt;br /&gt;
    relay_log = /var/log/mysql/mysql-relay-bin.log&lt;br /&gt;
    read_only = 1 # Recommended for slaves to prevent accidental writes&lt;br /&gt;
    # Optional: If you want to replicate specific databases&lt;br /&gt;
    # replicate_do_db = your_database_name&lt;br /&gt;
    # Optional: If you want to exclude specific databases&lt;br /&gt;
    # replicate_ignore_db = mysql&lt;br /&gt;
    # replicate_ignore_db = information_schema&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    *   `server-id`: Must be unique and different from the master.&lt;br /&gt;
    *   `relay_log`: Specifies the base name for relay log files.&lt;br /&gt;
    *   `read_only`: Prevents direct writes to the slave.&lt;br /&gt;
&lt;br /&gt;
3.  '''Restart MySQL Server''':&lt;br /&gt;
    Apply the changes.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo systemctl restart mysql&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4.  '''Configure Slave Connection''':&lt;br /&gt;
    Connect to your MySQL server as root and configure the slave to connect to the master using the details obtained in Step 1. Replace `master_ip_address`, `slave_user`, `your_password`, and the `MASTER_LOG_FILE` and `MASTER_LOG_POS` with your actual values.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    mysql -u root -p&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    ```sql&lt;br /&gt;
    CHANGE MASTER TO&lt;br /&gt;
        MASTER_HOST='master_ip_address',&lt;br /&gt;
        MASTER_USER='slave_user',&lt;br /&gt;
        MASTER_PASSWORD='your_password',&lt;br /&gt;
        MASTER_LOG_FILE='mysql-bin.000001', -- Use the File from SHOW MASTER STATUS&lt;br /&gt;
        MASTER_LOG_POS=12345;             -- Use the Position from SHOW MASTER STATUS&lt;br /&gt;
&lt;br /&gt;
    START SLAVE;&lt;br /&gt;
    SHOW SLAVE STATUS\G&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
    Check the output of `SHOW SLAVE STATUS\G`. Look for `Slave_IO_Running: Yes` and `Slave_SQL_Running: Yes`. If not, check your logs and firewall.&lt;br /&gt;
&lt;br /&gt;
== PostgreSQL Master-Slave Replication (Streaming Replication) ==&lt;br /&gt;
&lt;br /&gt;
PostgreSQL uses a WAL (Write-Ahead Logging) system for replication. Changes are written to WAL files, and these can be streamed to replica servers.&lt;br /&gt;
&lt;br /&gt;
=== Step 1: Configure the Master Server ===&lt;br /&gt;
&lt;br /&gt;
1.  '''Edit PostgreSQL Configuration Files''':&lt;br /&gt;
    Locate your `postgresql.conf` and `pg_hba.conf` files. The location varies by PostgreSQL version and installation method (e.g., `/etc/postgresql/&amp;lt;version&amp;gt;/main/`).&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo nano /etc/postgresql/&amp;lt;version&amp;gt;/main/postgresql.conf&lt;br /&gt;
    sudo nano /etc/postgresql/&amp;lt;version&amp;gt;/main/pg_hba.conf&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.  '''Modify `postgresql.conf`''':&lt;br /&gt;
    Add or modify the following parameters:&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    wal_level = replica&lt;br /&gt;
    max_wal_senders = 5       # Number of concurrent WAL sender processes&lt;br /&gt;
    wal_keep_segments = 64    # Number of WAL files to keep on disk (adjust based on network speed and load)&lt;br /&gt;
    # Or for newer versions:&lt;br /&gt;
    # wal_keep_size = 1GB     # Equivalent to wal_keep_segments but in size&lt;br /&gt;
    archive_mode = on&lt;br /&gt;
    archive_command = 'cp %p /path/to/wal/archive/%f' # Example: copy WAL files to a directory&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    *   `wal_level = replica`: Enables WAL streaming.&lt;br /&gt;
    *   `max_wal_senders`: Allows multiple replicas to connect.&lt;br /&gt;
    *   `wal_keep_segments`/`wal_keep_size`: Prevents WAL files from being removed before the replica receives them.&lt;br /&gt;
    *   `archive_mode` and `archive_command`: Essential for recovery and ensuring WAL segments are not lost. Ensure the archive directory exists and is writable by the PostgreSQL user.&lt;br /&gt;
&lt;br /&gt;
3.  '''Modify `pg_hba.conf`''':&lt;br /&gt;
    Allow replication connections from your replica server(s). Add a line like this, replacing `replica_ip_address` and `replication_user` with your details.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    # TYPE  DATABASE        USER            ADDRESS                 METHOD&lt;br /&gt;
    host    replication     replication_user  replica_ip_address/32   md5&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    *   `METHOD=md5` requires a password for the `replication_user`.&lt;br /&gt;
&lt;br /&gt;
4.  '''Restart PostgreSQL Server''':&lt;br /&gt;
    Apply the configuration changes.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo systemctl restart postgresql&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5.  '''Create a Replication User''':&lt;br /&gt;
    Connect to your PostgreSQL master and create a user with replication privileges.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo -u postgres psql&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    ```sql&lt;br /&gt;
    CREATE USER replication_user REPLICATION LOGIN PASSWORD 'your_password';&lt;br /&gt;
    \q&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Prepare the Slave Server ===&lt;br /&gt;
&lt;br /&gt;
1.  '''Stop PostgreSQL on Slave''':&lt;br /&gt;
    Ensure PostgreSQL is stopped on the replica server.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo systemctl stop postgresql&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2.  '''Clean Slave Data Directory''':&lt;br /&gt;
    Remove the existing data directory to ensure a clean copy from the master. '''WARNING: This will delete all data on the slave server.'''&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo rm -rf /var/lib/postgresql/&amp;lt;version&amp;gt;/main/*&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3.  '''Perform Base Backup''':&lt;br /&gt;
    Use `pg_basebackup` to copy the master's data directory to the slave. Replace `master_ip_address`, `replication_user`, and `your_password` accordingly.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo -u postgres pg_basebackup -h master_ip_address -U replication_user -D /var/lib/postgresql/&amp;lt;version&amp;gt;/main -P -v -X stream&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    *   `-h`: Master host.&lt;br /&gt;
    *   `-U`: Replication user.&lt;br /&gt;
    *   `-D`: Destination data directory on the slave.&lt;br /&gt;
    *   `-P`: Show progress.&lt;br /&gt;
    *   `-v`: Verbose output.&lt;br /&gt;
    *   `-X stream`: Stream WAL files during the backup.&lt;br /&gt;
&lt;br /&gt;
4.  '''Create `standby.signal` File''':&lt;br /&gt;
    Create an empty file named `standby.signal` in the slave's data directory. This tells PostgreSQL to start in standby mode.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo touch /var/lib/postgresql/&amp;lt;version&amp;gt;/main/standby.signal&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
5.  '''Configure `postgresql.conf` on Slave''':&lt;br /&gt;
    Edit `postgresql.conf` on the slave.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo nano /etc/postgresql/&amp;lt;version&amp;gt;/main/postgresql.conf&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Ensure the following (or similar):&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    hot_standby = on        # Allows read queries on the standby&lt;br /&gt;
    primary_conninfo = 'host=master_ip_address port=5432 user=replication_user password=your_password'&lt;br /&gt;
    # For older versions, you might need:&lt;br /&gt;
    # restore_command = 'cp /path/to/wal/archive/%f %p'&lt;br /&gt;
    # Or for streaming:&lt;br /&gt;
    # primary_slot_name = 'your_slot_name' # If using replication slots&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    *   `hot_standby = on`: Allows read queries on the slave.&lt;br /&gt;
    *   `primary_conninfo`: Connection string to the master.&lt;br /&gt;
&lt;br /&gt;
6.  '''Start PostgreSQL Server''':&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;pre&amp;gt;&lt;br /&gt;
    sudo systemctl start postgresql&lt;br /&gt;
    &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    Check the PostgreSQL logs (`/var/log/postgresql/postgresql-&amp;lt;version&amp;gt;-main.log`) for connection status.&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
*   '''Firewall Issues''': Ensure the database ports (3306 for MySQL, 5432 for PostgreSQL) are open between master and slave.&lt;br /&gt;
*   '''Incorrect Credentials''': Double-check replication user, password, and IP addresses.&lt;br /&gt;
*   '''MySQL: `SHOW SLAVE STATUS\G` errors''': Look for `Last_IO_Error` and `Last_SQL_Error` for clues. Common issues include incorrect `MASTER_LOG_FILE` or `MASTER_LOG_POS`.&lt;br /&gt;
*   '''PostgreSQL: Logs not showing connection''': Verify `pg_hba.conf` rules, `primary_conninfo` settings, and firewall rules. Ensure the `replication_user` has the `REPLICATION` privilege.&lt;br /&gt;
*   '''Master Binary Logs/WAL Files Missing''': If a slave falls too far behind, it might miss WAL segments. Ensure `binlog_expire_logs_seconds` (MySQL) or `wal_keep_segments`/`wal_keep_size` (PostgreSQL) are set appropriately, or use archiving.&lt;br /&gt;
&lt;br /&gt;
== Monitoring Replication ==&lt;br /&gt;
&lt;br /&gt;
Regularly monitor the replication status to ensure it's functioning correctly.&lt;br /&gt;
&lt;br /&gt;
*   '''MySQL''': `SHOW SLAVE STATUS\G`&lt;br /&gt;
*   '''PostgreSQL''': `SELECT pg_is_in_recovery();` (should return `t` on slave), and check logs.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
Setting up database replication is a vital step for creating robust and scalable database systems. By following these steps, you can establish reliable master-slave replication for both MySQL and PostgreSQL, enhancing your server's availability and performance.&lt;br /&gt;
&lt;br /&gt;
[[Category:Database Setup]]&lt;br /&gt;
[[Category:MySQL]]&lt;br /&gt;
[[Category:PostgreSQL]]&lt;br /&gt;
[[Category:Server Administration]]&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>