Beruflich Dokumente
Kultur Dokumente
In this article, we create and test a high‑availability database cluster using MySQL,
Galera Cluster, and the TCP load balancing capability introduced in NGINX Plus
Release 5 (R5). We demonstrate the appropriate MySQL load balancing configuration
with NGINX Plus, and techniques for handling conflicting writes and failures across the
database cluster.
The example in this article uses stock MySQL, following these installation instructions to
deploy three database servers (db1 db2, and db3) on Ubuntu 14.04 images running on
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
1 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
the end goal of creating a stable cluster of three database servers where each replicates
from its two peers. Galera Cluster also supports MariaDB and Percona XtrDB Cluster.
We execute this CREATE on one database instance and verify that the table is replicated
to the peer instances.
stream {
upstream db {
server db1:3306;
server db2:3306;
server db3:3306;
}
server {
listen 3306;
proxy_pass db;
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
2 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
Then we connect to the databases through NGINX Plus and use an SQL query to
determine which instance we’re connected to:
To confirm that NGINX Plus is load balancing our connections across the three servers
in round‑robin fashion, we can reconnect repeatedly.
The attached simple Perl script (query1.pl) inserts or increments 100 entries, and
prints the name of the instance to which each transaction was sent:
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
3 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
db3
db1
db2
db3
...
We observe that the UPDATEs were round‑robin load balanced across the databases,
and that when we run more UPDATEs in series, the count is correctly updated:
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
4 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
Inspecting the table shows that very few entries have been incremented all the way to
20:
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
5 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
This is a consequence of the Galera replication process; when UPDATEs to the same
record are executed in parallel across databases, a deadlock can occur and the
database rejects the transaction.
If this behavior is not acceptable, the simplest solution is to designate a single primary
database instance in the upstream server group, by marking the others as backup and
down:
upstream db {
server db1:3306;
server db2:3306 backup;
server db3:3306 down;
}
With this configuration, all transactions are routed to db1. If db1 fails, current
connections are dropped and NGINX Plus fails over to db2 for new connections. Note
that db3 acts as a silent partner in the cluster, and only receives updates from db1 and
db2.
If you test this configuration, you’ll observe that on each database instance, all entries
are present and have the correct count value of 20. However, if db1 fails during the test,
a small number of transactions will be lost.
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
6 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
In our test client, it’s sufficient to catch failures and wrap database transactions in an
exception handler that restarts the transaction after a short pause, as illustrated by
query2.pl:
} or do {
print "Failed: $@";
select( undef, undef, undef, $backoff );
$backoff *= 1.5;
goto TRY;
};
With this modification, we can return to the original load‑balancing approach (three
active databases) and test parallel updates again. The frequent deadlock errors are
detected and the corresponding transactions retried. Counts are correctly incremented
and the system is demonstrated to be reliable with multiple active masters:
7 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
shutting down one or more of the database servers while submitting multiple parallel
updates to the cluster. Note that we reduce the value of proxy_connect_timeout to
1 second (1s) in our NGINX server configuration so that NGINX can detect connect
failures quickly.
As expected, the client code receives a range of errors as the databases are shut down
and restarted mid‑transaction:
Despite these errors, throughout extensive testing not a single transaction was dropped
or executed twice, and the three databases remained consistent. The combination of
appropriate application logic, NGINX Plus load balancing, and Galera Clustering delivers
a solid, high‑performance, and above all, 100% reliable MySQL database cluster.
To try out NGINX Plus for MySQL load balancing in your own environment, start your
free 30-day trial today or contact us for a live demo.
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
8 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
DOWNLOAD NOW
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
9 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
8 Comments NGINX
1 Login
Sort by Best
Recommend 1 ⤤ Share
Name
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
10 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
ASK US A QUESTION
Products Solutions
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
11 of 12 5/2/18, 2:25 PM
MySQL Load Balancing & High Availability | NGINX https://www.nginx.com/blog/mysql-high-availabil...
Webinars IBM
Case studies Microsoft Azure
Blog Red Hat
FAQ Certified module program
Glossary
Company
Support About NGINX
Professional Services Careers
Training Leadership
Press
Events
Connect With Us
FTL R
Our site uses cookies to provide the best possible browsing experience. By continuing to use
the site you are agreeing to the use of our cookies. Find out more here
12 of 12 5/2/18, 2:25 PM