MySQL Cluster with HA-Proxy over Master-Master Replication for Loadbalancing

Posted on 2014-11-12
Last Modified: 2014-11-13
dear experts,

i have a question: i must setup a mysql database loadbalancer.
now i have found much tutorials with master-master replication an ha-proxy as balancer.
but then i have found another tutorials with master-master galera cluster soltuion.

now my question:
what i should use?

on our servers there are now 30.000 peoples on the day. the system was is running is drupal.

my mainsystem is debian wheezy.

now i have 4 servers (2 active)

1. intel xeon 3,4 ghz, 32 gb ram, 4 x 1 tb enterprise hdd with raid 10
2. intel xeon 3,4 ghz, 32 gb ram, 4 x 1 tb enterprise hdd with raid 10
3. intel xeon 3,4 ghz, 32 gb ram, 4 x 400 ssd with raid 10
4. intel xeon 3,4 ghz, 32 gb ram, 4 x 400 ssd with raid 10

Currently only server 1 and 2 are active (the servers 3 and 4 was ordered).
the 1 is a webserver, and the 2 is a db server.

the load from the webserver is currently about 30%, the dbserver is heavy loaded...

the website access is very slow (10-15 seconds).

now that is my solution:

server 1 + 2 = webserver loadbalancer

main question: how i can replicate website data throw the both server,
how i can do a fix for session sickness?

second question, db: what is now better as cluster (for speed) a real cluster solution,
or master-master replication with ha proxy?

my msql database is currently 1 gigabyte (each month the database grows up about 100 mb).
how i can optimize my my.cnf configuration file for mysql? how i can find out what my db does?
i mean write, read... and where i must do changes in the configuration?

do you have some infos (any possible tutorials) for me how i can do that to get the website working fine?

thank you

many greets

Question by:M K
  • 3
  • 3
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 40439409

You are facing two problems. A need for load balancing due to many users and heavy workload on the db due to forenamed reason and query-perfomance issues.

For tuning mysql I recommend ( as you are on Linux/unix system) to use the mysqltuner utility tool.
It will give you some idea where to tune.
Also you should enable slow-query log to capture queries that are running slow. That is mostly due to missing indexes or
poorly written queries.
In newer versions of MySQL and MariaDB you have the Performance Schema that aids you in your tuning

MariaDB Galera Cluster with HAProxy looks to me as a very good solution although I haven't tested it thoroughly yet. It is a master-master replication and recommended setup is a 3 node cluster as minimum.
Note that InnoDB engine is the engine used in the cluster replication.

     Tomas Helgi

Author Comment

by:M K
ID: 40439435
Hello Tomas Helgi,

thanks for you reply!

okay, i will activate the slow query log.

so i have done a run with mysqltuner:
mysqltuner says:
-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.5.35-0+wheezy1
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: +Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 106M (Tables: 269)
[--] Data in InnoDB tables: 7G (Tables: 3443)
[--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
[!!] Total fragmented tables: 3468

-------- Security Recommendations  -------------------------------------------
[OK] All database users have passwords assigned

-------- Performance Metrics -------------------------------------------------
[--] Up for: 4d 18h 39m 48s (1B q [2K qps], 1M conn, TX: 302B, RX: 196B)
[--] Reads / Writes: 37% / 63%
[--] Total buffers: 12.4G global + 2.7M per thread (500 max threads)
[!!] Maximum possible memory usage: 13.7G (87% of installed RAM)
[OK] Slow queries: 0% (187K/1B)
[OK] Highest usage of available connections: 73% (365/500)
[OK] Key buffer size / total MyISAM indexes: 32.0M/152.6M
[OK] Key buffer hit rate: 99.9% (3B cached / 1M reads)
[OK] Query cache efficiency: 85.8% (442M cached / 515M selects)
[!!] Query cache prunes per day: 4241452
[OK] Sorts requiring temporary tables: 0% (76K temp sorts / 33M sorts)
[!!] Joins performed without indexes: 10255
[OK] Temporary tables created on disk: 0% (78K on disk / 8M total)
[OK] Thread cache hit rate: 98% (13K created / 1M connections)
[!!] Table cache hit rate: 0% (400 open / 379K opened)
[OK] Open file limit used: 3% (97/2K)
[OK] Table locks acquired immediately: 99% (205M immediate / 205M locks)
[OK] InnoDB data size / buffer pool: 7.3G/12.0G

-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    Reduce your overall MySQL memory footprint for system stability
    Enable the slow query log to troubleshoot bad queries
    Adjust your join queries to always utilize indexes
    Increase table_cache gradually to avoid file descriptor limits
Variables to adjust:
    query_cache_size (> 100M)
    join_buffer_size (> 128.0K, or always use indexes with joins)
    table_cache (> 400)

currently the Server load is:
top - 10:29:29 up 174 days, 23:05,  1 user,  load average: 31,24, 17,56, 17,01
Tasks: 220 total,   1 running, 219 sleeping,   0 stopped,   0 zombie
%Cpu(s): 97,4 us,  1,5 sy,  0,0 ni,  0,6 id,  0,2 wa,  0,0 hi,  0,4 si,  0,0 st
KiB Mem:  16471916 total, 14058328 used,  2413588 free,   146844 buffers
KiB Swap: 21229564 total,   115880 used, 21113684 free,  2973840 cached

17546 mysql     20   0 14,1g 9,5g 5496 S  2377 60,6  81515:19 mysqld

17546 mysql     20   0 14,1g 9,5g 5496 S   254 60,6  81447:26 mysqld

what do you mean?

thank you!
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 40439446

You should run OPTIMIZE TABLE on all those 3468 tables that needs to be optimized (or the whole database ).
Adjust these variables that mysqltuner mentions
    query_cache_size (> 100M)
    join_buffer_size (> 128.0K, or always use indexes with joins)
    table_cache (> 400)
to some values slightly above the recommended values.
[!!] Joins performed without indexes: 10255
Investigate the slow queries and see if you can add missing indexes to the tables involved to speed up the queries.

    Tomas Helgi
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.


Author Comment

by:M K
ID: 40439465
Thank You!

And what i should set variable now?

currently set:
query_cache_size        = 100M
join_buffer_size is not set
table_cache            = 400

LVL 25

Accepted Solution

Tomas Helgi Johannsson earned 500 total points
ID: 40439572

You can start with these values

query_cache_size        = 200M
join_buffer_size  = 1M
table_cache            = 500

And I recommend you run OPTIMIZE table at least once a week.
Create a script where you run the command
mysqloptimize --all-databases -u username -p passw

where username is a user with required privilege to do the optimize.

Also note that mysqloptimize is a clone (or linked name) of mysqlcheck

"mysqlcheck has a special feature compared to other client programs. The default behavior of checking tables (--check) can be changed by renaming the binary. If you want to have a tool that repairs tables by default, you should just make a copy of mysqlcheck named mysqlrepair, or make a symbolic link to mysqlcheck named mysqlrepair. If you invoke mysqlrepair, it repairs tables."

      Tomas Helgi

Author Comment

by:M K
ID: 40439619
Thanks Tomas!!!

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been using r1soft Continuous Data Protection ( for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question