VPS Database Replication: A Practical Step-by-Step Setup Guide
Ready to make your VPS-hosted apps more resilient? This practical, step-by-step VPS database replication guide shows how to configure MySQL/MariaDB and PostgreSQL, avoid common pitfalls, and gain reliable high availability and read scalability.
Database replication is a foundational technique for improving availability, scalability, and data durability in modern web architectures. For site owners, developers, and enterprises using virtual private servers (VPS), setting up reliable replication between database instances can turn a single point of failure into a resilient, maintainable system. This article provides a practical, step-by-step guide focused on VPS environments, with concrete configuration details, common pitfalls, and operational best practices for MySQL/MariaDB and PostgreSQL replication.
Why replicate databases on a VPS?
Replication offers multiple tangible benefits for VPS-hosted applications:
- High availability: a secondary node can take over or serve reads if the primary fails.
- Read scalability: distribute read-heavy workloads to one or more replicas.
- Disaster recovery: maintain a geographically separated copy to reduce RTO/RPO.
- Backup offloading: run backups and analytic queries on replicas to avoid impacting primary performance.
Replication principles (overview)
At a high level, replication involves a primary (master) node that accepts writes and one or more replicas that apply a stream of changes to reach the same logical state. Two common models are:
- Statement-based vs. row-based replication (MySQL): whether SQL statements or row-change events are shipped.
- Synchronous vs. asynchronous replication: synchronous waits for confirmation from replicas (strong consistency, higher latency); asynchronous returns immediately (lower latency, potential data loss on failover).
- Logical vs. physical replication (PostgreSQL): logical sends a decoupled change stream (allows selective replication); physical streams WAL segments (byte-for-byte replica).
Environment prerequisites
Before you begin, ensure the following on all VPS nodes:
- OS with SSH access and root or sudo privileges.
- Database engine installed (MySQL/MariaDB or PostgreSQL) and service running.
- Static or predictable IP addresses, or reliable DNS names between nodes.
- Firewall rules allowing the DB replication port (MySQL default 3306, PostgreSQL default 5432) only between trusted hosts.
- Time synchronization (e.g., chrony or ntpd) to avoid clock drift that can complicate logs and troubleshooting.
Step-by-step: MySQL / MariaDB replication (asynchronous)
The following steps describe classic asynchronous replication using binary logs. Adjust paths and package names for your distribution.
1. Configure the primary (master)
- Edit my.cnf (commonly /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf) and set:
- server-id = 1
- log_bin = mysql-bin
- binlog_format = ROW (recommended for accuracy)
- expire_logs_days = 7
- Restart MySQL: run `sudo systemctl restart mysql`.
- Create a replication user with replication privileges: `CREATE USER ‘repl’@’replica_ip’ IDENTIFIED BY ‘strongpassword’; GRANT REPLICATION SLAVE ON . TO ‘repl’@’replica_ip’; FLUSH PRIVILEGES;`
- Lock the tables for a consistent snapshot (or take a filesystem snapshot): `FLUSH TABLES WITH READ LOCK;` Obtain the master status: `SHOW MASTER STATUS;` Note File and Position. You will need these for the replica.
- Dump the database: `mysqldump –all-databases –master-data=2 –single-transaction –quick –routines –events > dump.sql` (single-transaction works for InnoDB).
- Unlock tables: `UNLOCK TABLES;`
2. Prepare the replica
- Transfer dump.sql to the replica host and import: `mysql < dump.sql`.
- Edit my.cnf on the replica:
- server-id = 2 (unique per server)
- relay_log = mysqld-relay-bin
- Restart MySQL on the replica.
- Configure replication: `CHANGE MASTER TO MASTER_HOST=’master_ip’, MASTER_USER=’repl’, MASTER_PASSWORD=’strongpassword’, MASTER_LOG_FILE=’mysql-bin.00000X’, MASTER_LOG_POS=YYYY;` using values from `SHOW MASTER STATUS`.
- Start replication: `START SLAVE;`
- Verify: `SHOW SLAVE STATUSG` — ensure `Slave_IO_Running` and `Slave_SQL_Running` are both Yes and there are no errors.
3. Firewall and security considerations
- Open the MySQL port only between the master and replica IPs (e.g., `ufw allow from replica_ip to any port 3306`), and block public access.
- Use strong, unique passwords for the replication user and consider network-level encryption (TLS) for replication traffic by enabling SSL certificates in MySQL config (`ssl-ca`, `ssl-cert`, `ssl-key`).
4. Testing and failover
- Test replication by creating a test database on master and verifying it appears on the replica.
- Simulate failure: stop MySQL on master and test read-only continuity from replica. If promoting replica to primary, run `STOP SLAVE; RESET SLAVE ALL;` and update application configuration to point to the new primary.
- For automated failover, integrate tools like MHA, Orchestrator, or ProxySQL with a health-checking mechanism; these require additional configuration and usually a separate management node.
Step-by-step: PostgreSQL streaming replication
PostgreSQL offers robust physical streaming replication with WAL shipping. This example uses built-in streaming replication (asynchronous) and base backups.
1. Configure the primary
- Edit postgresql.conf (commonly /etc/postgresql/VERSION/main/postgresql.conf):
- wal_level = replica
- max_wal_senders = 3 (or more as needed)
- wal_keep_segments = 64 (adjust based on workload)
- archive_mode = on (optional for WAL archiving)
- Edit pg_hba.conf to allow replication connections from replica: `host replication replicator replica_ip/32 md5`.
- Reload or restart PostgreSQL: `sudo systemctl restart postgresql`.
- Create a replication role: `CREATE ROLE replicator WITH REPLICATION PASSWORD ‘strongpassword’ LOGIN;`
2. Create a base backup and configure the replica
- On the replica node, stop PostgreSQL and remove or move the data directory.
- Use pg_basebackup to copy data: `pg_basebackup -h master_ip -D /var/lib/postgresql/12/main -U replicator -Fp -Xs -P` (options: plain format, include WAL, show progress).
- Create a recovery configuration (PostgreSQL 12+ uses standby.signal and primary_conninfo in postgresql.conf or a separate file):
- Add to postgresql.conf: `primary_conninfo = ‘host=master_ip port=5432 user=replicator password=strongpassword application_name=replica1’`
- Create an empty file named `standby.signal` in the data directory to mark the server as a standby.
- Start PostgreSQL on the replica. Check logs to ensure WAL streaming connects and replication begins.
3. Monitoring and failover
- Monitor replication lag using `pg_stat_replication` on master (`SELECT client_addr, state, sync_priority, write_lag, flush_lag, replay_lag FROM pg_stat_replication;`).
- For controlled failover, use tools like repmgr, Patroni, or pg_auto_failover. Patroni uses distributed consensus (etcd/consul) to coordinate leader election and can provide near-automatic failover on VPS clusters.
Comparing replication options and trade-offs
When choosing replication technology and topology on VPS, consider:
- Consistency vs. latency: synchronous replication gives stronger guarantees but increases write latency. Asynchronous is faster but risks data loss on sudden primary failure.
- Complexity: logical replication allows column/table-level replication and version upgrades with less downtime, but is more complex than physical streaming.
- Operational cost: more replicas mean better read scaling and redundancy but higher resource and management costs on VPS instances.
- Disaster recovery: off-site replicas reduce correlated failure risk; ensure network latency and bandwidth are sufficient for your WAL/binlog throughput.
Practical operational best practices
To run replication reliably on VPS infrastructure, follow these best practices:
- Automate configuration: use IaC tools (Ansible, Terraform) to provision and maintain consistent DB configs across nodes.
- Secure communication: allow replication ports only between trusted hosts, enable TLS where supported, and use dedicated replication users with minimal privileges.
- Monitor lag and capacity: set alerting for write/flush/replay lag thresholds and disk saturation; WAL/binlog growth can quickly consume disk.
- Backups and point-in-time recovery: combine replication with regular backups and retention of WAL/binlog archives for PITR.
- Test failover regularly: run scheduled failover drills to exercise procedures and ensure application compatibility with new primaries.
- Document topology and runbooks: keep clear instructions for promotion, reconfiguration, and reintegration of recovered nodes.
Choosing the right VPS and sizing guidance
Replication performance depends heavily on VPS resources and network characteristics. Key considerations:
- CPU and memory: replicas still need sufficient CPU/RAM to apply changes and serve reads. For write-heavy workloads, prioritize CPU and I/O for the primary.
- Disk I/O: use SSD-backed volumes and consider IOPS guarantees; write amplification from WAL/binlog and backups can be I/O intensive.
- Network: choose VPS plans with sufficient bandwidth and low latency between replica peers, especially for geographically distributed setups.
- Storage snapshot support: snapshot-friendly block storage simplifies consistent backups and rapid replica provisioning.
When in doubt, start with modest VPS sizes for secondary nodes and scale based on observed replication lag and read performance. Reserve larger, higher-IO VPS instances for the primary.
Summary
Implementing database replication on VPS is a powerful way to boost availability, scale reads, and enable robust disaster recovery. Whether you choose MySQL/MariaDB binary-log replication or PostgreSQL streaming replication, pay attention to configuration details: unique server IDs, secure replication users, proper firewalling, and WAL/binlog retention. Automate provisioning, monitor replication health actively, and test failovers periodically to reduce surprises.
For VPS-hosted deployments, selecting the right VPS plan matters: prioritize network reliability, low-latency connections between nodes, and SSD-backed storage for sustained WAL/binlog workloads. If you host on VPS.DO, consider the USA VPS offerings for low-latency connectivity and strong I/O performance when architecting cross-region replication setups: https://vps.do/usa/. Learn more about the platform at https://VPS.DO/.