Haproxy MySQL Master Master

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up an haproxy mysql master master cluster by routing traffic to active database nodes. HAProxy checks node health and sends writes to one primary server at a time. Teams build this layout to keep apps running when a database server fails. Still, engineers often share broad database logins across the whole team for fast fixes. Broad permissions let anyone run destructive queries or read sensitive rows on live nodes. How an haproxy mysql master master setup works In a dual-master layout

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up an haproxy mysql master master cluster by routing traffic to active database nodes. HAProxy checks node health and sends writes to one primary server at a time. Teams build this layout to keep apps running when a database server fails. Still, engineers often share broad database logins across the whole team for fast fixes. Broad permissions let anyone run destructive queries or read sensitive rows on live nodes.

How an haproxy mysql master master setup works

In a dual-master layout, two MySQL servers accept writes and copy changes to each other. Writing to both nodes at the exact same time can cause data conflicts. Two nodes might pick the same row key or overwrite the same record. To stop this, teams usually mark one node as active and one as backup.

HAProxy sits in front of the two MySQL nodes and directs client traffic. It runs regular health checks against each database node to see which one is healthy. When the active node is healthy, HAProxy sends all write traffic to that single server. If the active node goes down, HAProxy shifts traffic to the backup node right away. Teams often add a second port in HAProxy to split read queries across both nodes.

You also need a health check user inside MySQL so HAProxy can test each node. That user needs minimal rights since it only checks if the database is awake. Database admins also set offset rules for row keys so the two nodes never clash. This step keeps keys unique even if a sudden failover happens during heavy writes.

How health checks and failover work

A basic TCP check only sees if the MySQL port is open on the server. A port can keeps the session active even when the database is locked or out of sync. Teams use deeper checks that ask the node about its replication status before sending traffic. A lightweight script on the database host can check replication lag and return a status code. HAProxy reads that status code and pulls lagging nodes out of the active pool.

When the failed master comes back online, it catches up on missed changes first. Admins often hold the recovered node in backup mode until they verify data consistency. This prevents traffic from flipping back and forth between unstable database servers during network glitches. Once the node is stable, the team puts it back into the normal rotation.

MySQL Access Governance + HAProxy Security: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What the native setup solves and what it misses

This architecture solves high availability and keeps apps online when a database machine crashes. HAProxy handles network routing and server failover well at the transport level. It makes sure client apps always reach a working MySQL node without manual edits. Native MySQL roles also control which users can log in to the cluster.

Still, this setup only decides where connections go and who can log in. HAProxy moves raw database packets without inspecting what each SQL statement does in flight. Once an engineer or app logs in, the cluster trusts every query on that session. A developer with write rights can still drop a table or update the wrong rows. Anyone with read rights can pull raw emails, phone numbers, or card numbers in results. Shared database users also make it hard to see which person ran a bad query.

Add wire guardrails and masking beside MySQL

Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev runs beside the database as a sidecar for your MySQL. It keeps your login setup and drivers as they are while checking queries on the wire.

With hoop.dev MySQL support, guardrails refuse bad queries before they run and return a readable MySQL error. Data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.

Frequently asked questions

Should both MySQL masters accept writes at the same time?

Writing to both nodes at once often leads to replication conflicts and broken data. Most teams send all writes to one active master and keep the second as standby.

How does HAProxy know when a MySQL master fails?

HAProxy runs scheduled health checks against each database node every few seconds. If a node fails several checks in a row, HAProxy stops sending traffic to it.

Does HAProxy control which SQL queries a user can run?

No, HAProxy routes network connections and checks server health at the connection level. It does not read SQL statements, block dangerous commands, or mask sensitive fields in results.

The query runs. The emails stay hidden.