Use Haproxy for load balancing on MariaDB _ MySQL

Source: Internet
Author: User
Use Haproxy for MariaDB load balancing MariaDB

Server environment:

Install

Vim/etc/apt/sources. list. d/mariadb. list # add the following two statements deb http://mirror.yongbok.net/mariadb/repo/5.5/ubuntu precise maindeb-src http://mirror.yongbok.net/mariadb/repo/5.5/ubuntu precise mainapt-key adv -- recv-keys -- keyserver keyserver.ubuntu.com 0xcb082a1bb943dbapt-get updateapt-get install mariadb-server-5.5

Multi-instance configuration files: 3306, 3307, 3308

[Mysqld_multi] mysqld =/usr/bin/mysqld_safemysqladmin =/usr/bin/mysqladminuser = root # specify the user log used to start and stop mysql instances =/var/log/mysql/mysqld_multi.log [client] port = 3306 socket =/var/run/mysql. sockdefault-character-set = utf8 [mysqld3306] port = 3306 socket =/var/run/mysql. sockpid-file =/var/run/mysql. piddatadir =/var/lib/mysql/packages = bytes = 1Mtable_open_cache = 64sort_buffer_size = bytes = 8Kread_buffer_size = bytes = MyISAMcharacter-set-server = utf8skip-name-resolvecollation-server =/var/ log/mysql/mysqld-error.loglog-slow-admin-statementslong-query-time = connector =/var/log/mysql/mysqld-slow.loginnodb_buffer_pool_size = connector = 20Mmax_connections = 1000 tmpdir =/tmp/server-id = 2log-slave-updateslog-bin = mysql-binbinlog_format = mixedrelay_log = mysql-relay -binreplicate-do-db = wordpressreplicate-ignore-db = mysqlreplicate-ignore-db = information_schemareplicate-ignore-db = performance_schemareplicate-ignore-db = test [mysqldump] quickmax_allowed_packet = 16 M [mysqld3307] port = 3307 socket =/ var/run/mysql/mysql-3307.sockpid-file =/var/run/mysql/mysql-3307.piddatadir =/var/lib/mysql/connector = 16Mmax_allowed_packet = 1Mtable_open_cache = 64sort_buffer_size = bytes = 8Kread_buffer_size = bytes 8Mdefault-storage-engine = MyISAMcharacter-set-server = utf8skip-name-resolvecollation-server = utf8_general_cilog-error =/var/log/mysql/mysqld-error-3307.loglog-slow-admin-statementslong-query-time = connector =/var/log/mysql/mysqld-slow-3307.loginnodb_buffer_pool_size = connector = 20Mmax_connections = 1000 tmpdir =/ tmp/server-id = mysql-binbinlog_format = mixedrelay_log = mysql-relay-binreplicate-do-db = futurestarreplicate-do-db = verywxreplicate-ignore-db = mysqlreplicate-ignore-db = information_schemareplicate-ignore-db = performance_schemareplicate-ignore-db = test [mysqldump] quickmax_allowed_packet = 16 M [mysqld3308] port = 3308 socket =/var/run/mysql/mysql-3308.sockpid-file =/var/run/mysql/mysql-3308.piddatadir =/var /lib/mysql/connector = protocol = 1Mtable_open_cache = 64sort_buffer_size = protocol = 8Kread_buffer_size = protocol = MyISAMcharacter-set-server = utf8skip-name-resolvecollation-server = utf8_general_cilog-error =/var/log/mysql/ mysqld-error-3308.loglog-slow-admin-statementslong-query-time = connector =/var/log/mysql/mysqld-slow-3308.loginnodb_buffer_pool_size = connector = 20Mmax_connections = 1000 tmpdir =/tmp/server-id = 2log-slave-updateslog-bin = mysql-binbinlog_format = mixedrelay_log = mysql-relay-binreplicate-do -db = futurestarreplicate-do-db = verywxreplicate-ignore-db = mysqlreplicate-ignore-db = information_schemareplicate-ignore-db = test [mysqldump] quickmax_allowed_packet = 16 M [mysql] socket =/var /run/mysql. sockauto-rehashdefault-character-set = utf8 [myisamchk] key_buffer_size = 20Msort_buffer_size = 20Mread_buffer = 2Mwrite_buffer = 2 M [mysqlhotcopy] interactive-timeout

Initialize the database file:

mysql_install_db --datadir=/var/lib/mysql/3306 --user=mysqlmysql_install_db --datadir=/var/lib/mysql/3307 --user=mysqlmysql_install_db --datadir=/var/lib/mysql/3308 --user=mysql

Start the database:

mysqld_multi start 3306mysqld_multi start 3307mysqld_multi start 3308

View the startup status:

netstat -tunlp | grep -E '(3306|3307|3308)'

Install Haporxy

apt-get install haproxy

Then vim etc/default/haproxy

Modify ENABLED = 0-> ENABLED = 1 to save.

Modify the haproxy Configuration File:

Vim/etc/haproxy. cfg

# this config needs haproxy-1.1.28 or haproxy-1.2.1globallog 127.0.0.1 local0log 127.0.0.1 local1 notice#log loghostlocal0 infomaxconn 4096#chroot /usr/share/haproxyuser haproxygroup haproxydaemon#debug#quietdefaultslog globalmodehttp#option httplogoptiondontlognullretries 3option redispatchmaxconn 2000contimeout5000clitimeout50000srvtimeout50000listenmysql 0.0.0.0:6666mode tcpoption mysql-check user rootbalance roundrobinservermysql_1 127.0.0.1:3306 weight 1 check inter 1s rise 2 fall 5servermysql_2 127.0.0.1:3307 weight 1 check inter 1s rise 2 fall 5servermysql_3 127.0.0.1:3308 weight 1 check inter 1s rise 2 fall 5 listen statsmode httpbind 0.0.0.0:8888stats enablestats uri /dbsstats realm Global/ statisticsstats auth admin:admin errorfile 400 /etc/haproxy/errors/400.httperrorfile 403 /etc/haproxy/errors/403.httperrorfile 408 /etc/haproxy/errors/408.httperrorfile 500 /etc/haproxy/errors/500.httperrorfile 502 /etc/haproxy/errors/502.httperrorfile 503 /etc/haproxy/errors/503.httperrorfile 504 /etc/haproxy/errors/504.http
/etc/init.d/haproxy start

View the haproxy monitoring page:

Configuration successful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.