使用Haproxy对MariaDB做负载均衡

服务器环境:

安装

vim /etc/apt/sources.list.d/mariadb.list     # 添加以下两句
deb http://mirror.yongbok.net/mariadb/repo/5.5/ubuntu precise main
deb-src http://mirror.yongbok.net/mariadb/repo/5.5/ubuntu precise main
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
apt-get update
apt-get install mariadb-server-5.5

/etc/mysql/my.cnf

多实例配置文件:3306,3307,3308

[mysqld_multi]
mysqld      = /usr/bin/mysqld_safe
mysqladmin  = /usr/bin/mysqladmin
user        = root           # 指定用于启动、停止mysql实例的用户
log     = /var/log/mysql/mysqld_multi.log
[client]
port        = 3306
socket      = /var/run/mysql/mysql.sock
default-character-set   = utf8
[mysqld3306]
port        = 3306
socket      = /var/run/mysql/mysql.sock
pid-file    = /var/run/mysql/mysql.pid
datadir     = /var/lib/mysql/3306
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
default-storage-engine  = MyISAM
character-set-server    = utf8
skip-name-resolve
collation-server        = utf8_general_ci
log-error               = /var/log/mysql/mysqld-error.log
log-slow-admin-statements
long-query-time         = 3
slow-query-log
slow-query-log-file     = /var/log/mysql/mysqld-slow.log
innodb_buffer_pool_size=1G
innodb_additional_mem_pool_size=20M
max_connections     = 1000
tmpdir      = /tmp/
server-id   = 2
log-slave-updates
log-bin         = mysql-bin
binlog_format   = mixed
relay_log       = mysql-relay-bin
replicate-do-db        = wordpress
replicate-ignore-db        = mysql
replicate-ignore-db        = information_schema
replicate-ignore-db        = performance_schema
replicate-ignore-db        = test
[mysqldump]
quick
max_allowed_packet = 16M
[mysqld3307]
port        = 3307
socket      = /var/run/mysql/mysql-3307.sock
pid-file    = /var/run/mysql/mysql-3307.pid
datadir     = /var/lib/mysql/3307
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
default-storage-engine  = MyISAM
character-set-server    = utf8
skip-name-resolve
collation-server        = utf8_general_ci
log-error               = /var/log/mysql/mysqld-error-3307.log
log-slow-admin-statements
long-query-time         = 3
slow-query-log
slow-query-log-file     = /var/log/mysql/mysqld-slow-3307.log
innodb_buffer_pool_size=1G
innodb_additional_mem_pool_size=20M
max_connections     = 1000
tmpdir      = /tmp/
server-id   = 2
log-slave-updates
log-bin         = mysql-bin
binlog_format   = mixed
relay_log       = mysql-relay-bin
replicate-do-db        = futurestar
replicate-do-db        = verywx
replicate-ignore-db        = mysql
replicate-ignore-db        = information_schema
replicate-ignore-db        = performance_schema
replicate-ignore-db        = test
[mysqldump]
quick
max_allowed_packet = 16M
[mysqld3308]
port        = 3308
socket      = /var/run/mysql/mysql-3308.sock
pid-file    = /var/run/mysql/mysql-3308.pid
datadir     = /var/lib/mysql/3308
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
default-storage-engine  = MyISAM
character-set-server    = utf8
skip-name-resolve
collation-server        = utf8_general_ci
log-error               = /var/log/mysql/mysqld-error-3308.log
log-slow-admin-statements
long-query-time         = 3
slow-query-log
slow-query-log-file     = /var/log/mysql/mysqld-slow-3308.log
innodb_buffer_pool_size=1G
innodb_additional_mem_pool_size=20M
max_connections     = 1000
tmpdir      = /tmp/
server-id   = 2
log-slave-updates
log-bin         = mysql-bin
binlog_format   = mixed
relay_log       = mysql-relay-bin
replicate-do-db        = futurestar
replicate-do-db        = verywx
replicate-ignore-db        = mysql
replicate-ignore-db        = information_schema
replicate-ignore-db        = performance_schema
replicate-ignore-db        = test
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
socket  = /var/run/mysql/mysql.sock
auto-rehash
default-character-set   = utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

初始化数据库文件:

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

启动数据库:

mysqld_multi start 3306
mysqld_multi start 3307
mysqld_multi start 3308

查看启动情况:

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

安装Haporxy

apt-get install haproxy

然后vim etc/default/haproxy

修改 ENABLED=0 -> ENABLED=1 保存。

修改haproxy配置文件:

vim /etc/haproxy/haproxy.cfg

# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
    log 127.0.0.1   local0
    log 127.0.0.1   local1 notice
    #log loghost    local0 info
    maxconn 4096
    #chroot /usr/share/haproxy
    user haproxy
    group haproxy
    daemon
    #debug
    #quiet
defaults
    log global
    mode    http
    #option httplog
    option  dontlognull
    retries 3
    option redispatch
    maxconn 2000
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000
listen  mysql 0.0.0.0:6666
    mode tcp
    option mysql-check user root
    balance roundrobin
    server  mysql_1 127.0.0.1:3306 weight 1 check inter 1s rise 2 fall 5
    server  mysql_2 127.0.0.1:3307 weight 1 check inter 1s rise 2 fall 5
    server  mysql_3 127.0.0.1:3308 weight 1 check inter 1s rise 2 fall 5

listen stats
    mode http
    bind 0.0.0.0:8888
    stats enable
    stats uri /dbs
    stats realm Global\ statistics
    stats auth admin:admin

    errorfile   400 /etc/haproxy/errors/400.http
    errorfile   403 /etc/haproxy/errors/403.http
    errorfile   408 /etc/haproxy/errors/408.http
    errorfile   500 /etc/haproxy/errors/500.http
    errorfile   502 /etc/haproxy/errors/502.http
    errorfile   503 /etc/haproxy/errors/503.http
    errorfile   504 /etc/haproxy/errors/504.http

执行命令:

/etc/init.d/haproxy start

测试结果:

查看haproxy监控页面:

配置成功。

使用Haproxy对MariaDB做负载均衡

时间: 2024-10-08 02:45:35

使用Haproxy对MariaDB做负载均衡的相关文章

haproxy给mysql做负载均衡

一.首先安装haproxy wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz tar zcvf haproxy-1.4.24.tar.gz cd haproxy-1.4.24 make TARGET=linux26 PREFIX=/usr/local/haproxy #将haproxy安装到/usr/local/haproxy make install PREFIX=/usr/local/haproxy #PREF

Haproxy 为 mysql 做负载均衡

tar zxvf haproxy-1.4.21.tar.gz cd haproxy-1.4.21 uname -r vim /etc/haproxy.cfg global #日志 log 127.0.0.1 local0 maxconn 4096 chroot /tmp uid 0 gid 0 daemon nbproc 2 pidfile logs/haproxy.pid #debug #quiet defaults log 127.0.0.1 local3 mode http #option

使用haproxy做负载均衡时保持客户端真实的IP

haproxy里添加设置项 option forwardfor option httpclose apache的日志格式修改 LogFormat "MY IP=%{X-Forwarded-For}i AAA %h %u %t" cookie_log 这样%{X-Forwarded-For} 就可以显示用户的真实IP,而%h是显示做负载均衡的haproxy的IP. 如果是公司内部系统都是为这个正式IP另外定义了一个名字ORIG_CLIENT_IP,则可以修改转发的名字. option f

基于HAProxy+Keepalived高可用负载均衡web服务的搭建

一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在时下的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合进当前的架构中, 同时可以保护web服务器不被暴露到网络上.2.KeepalivedKeepalived 是一个基于VRRP协议来实现的LVS服务高

Haproxy+keepalied实现web负载均衡

一.实验拓扑图: keepalived: keepalived的主要目的是为ipvs提供高可用性,因此keepalived除了能够实现资源转移之外,自身能够调用ipvsadm命令来生成规则,而且还能够检测后端Rserver服务的健康状态检测.keepalived自身就是一个服务 ,启动之后工作在两个节点上(可以是多个节点),一个为当前活动节点,一个为备用节点简称为一主(master)一备(backup).master会不时的向backup传递通告信息(这种通告信息机制是基于vrrp协议),备用节

Keepalived+HAproxy实现高可用负载均衡

总概:       Keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器       HAProxy提供高可用性

案例一(haproxy+keepalived高可用负载均衡系统)【转】

1.搭建环境描述: 操作系统: [[email protected] ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 地址规划: 主机名 IP地址 集群角色 虚拟IP haproxy-server 10.0.0.35 主HAProxyServer 10.0.0.40 haproxy-backup 10.0.0.36 备用HAProxyServer webapp1 10.0.0.150 Backend Server 无 webapp2

Nginx做负载均衡时session共享问题详解

用nginx做负载均衡时,同一个IP访问同一个页面会被分配到不同的服务器上,如果session不同步的话,就会出现很多问题,比如说最常见的登录状态. 再者Nginx连接Memcached集群时,Nignx的请求从memcached服务器中根据key获得了value则直接返回value,如果没有获得到value则去MySQL中查询再返回. location / { set $memcached_key "$request_uri"; #设置请求memcached服务器的key memca

linux 下Haproxy实现简单四层负载均衡

HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. Haproxy的四层负载均衡搭建 主机规划: server 192.168.1.148  提供haproxy服务 后台  web1  192.168.1.150 web2  192.168.1.136 使用源码包安装 解压: tar -zxvf haproxy-1.4.24.tar.gz 进入到解压目录: cd  haproxy-1.4.24 编译: make TARGE