负载均衡mysql的使用_MySQL负载均衡

本文介绍了如何监控MySQL的状态并通过xinetd守护进程运行检查。配置了一个名为mysqlchk_status.sh的脚本来检查MySQL服务,并在HAProxy中设置httpchk选项进行健康检查。此外,还展示了HAProxy的日志配置、配置文件以及启动脚本,确保了MySQL的高可用性和负载均衡。
摘要由CSDN通过智能技术生成

1. 添加监控MySQL状态的端口

# vi /etc/services

mysqlcheck 6033

/

tcp # MySQL status check

2. 使用xinetd守护进程运行MySQL状态检测

# cat /etc/xinetd.d/mysqlchk

service mysqlcheck

{

disable = no

flags = REUSE

socket_type = stream

port = 6033

wait

= no

user = root

server = /

usr/

local/

haproxy/

sbin/

mysqlchk_status.sh

log_on_failure += USERID

}

3. 状态检测脚本

# vi /usr/local/haproxy/sbin/mysqlchk_status.sh

#/bin/bash

MYSQL_HOST

="localhost"

MYSQL_PORT

="3306"

MYSQL_USERNAME

="root"

MYSQL_PASSWORD

="123456"

ERROR_MSG

=/

usr/

bin/

mysql --host

=$MYSQL_HOST

--port

=$MYSQL_PORT

--user

=$MYSQL_USERNAME

--password

=$MYSQL_PASSWORD

-e

"show databases;"

if

[

"$ERROR_MSG

"

!

= ""

]

then

# mysql is fine, return http 200

/

bin/

echo

-e

"HTTP/1.1 200 OK\r

\n

"

/

bin/

echo

-e

"Content-Type: Content-Type: text/plain\r

\n

"

/

bin/

echo

-e

"\r

\n

"

/

bin/

echo

-e

"MySQL is running.\r

\n

"

/

bin/

echo

-e

"\r

\n

"

else

# mysql is fine, return http 503

/

bin/

echo

-e

"HTTP/1.1 503 Service Unavailable\r

\n

"

/

bin/

echo

-e

"Content-Type: Content-Type: text/plain\r

\n

"

/

bin/

echo

-e

"\r

\n

"

/

bin/

echo

-e

"MySQL is *down*.\r

\n

"

/

bin/

echo

-e

"\r

\n

"

fi

# chown haproxy.haproxy /usr/local/haproxy/sbin/mysqlchk_status.sh

4. HAproxy日志

# touch /var/log/haproxy.log

# chown haproxy.haproxy /var/log/haproxy.log

编辑/etc/syslog.conf文件,添加如下语句

local0.*

/

var/

log/

haproxy.log

5. HAProxy配置文件

[root@localhost htdocs]# cat /usr/local/haproxy/conf/haproxy.conf

global

maxconn 4096

daemon

pidfile /

usr/

local/

haproxy/

run/

haproxy.pid

#debug

#quiet

user haproxy

group haproxy

defaults

log global

mode http

option httplog

option dontlognull

log 127.0.0.1 local0

retries 3

option redispatch

maxconn 2000

contimeout 5000

clitimeout 50000

srvtimeout 50000

listen admin_stats 192.168.0.1:80

mode http

stats uri /

dbs

stats realm Global\ statistics

stats auth test

:123456

listen proxy-mysql 0.0.0.0:23306

mode tcp

balance roundrobin

option httpchk OPTIONS *

HTTP/

1.1

\r\nHost:\ www

server db01 192.168.0.1:3306

weight 1

check port 6033

inter 1s rise 2

fall 2

server db02 192.168.0.2:3306

weight 1

check port 6033

inter 1s rise 2

fall 2

option tcpka

6. HAProxy启动脚本

# cat /etc/init.d/haproxy

#! /bin/sh

set

-e

PATH

=/

sbin:/

bin:/

usr/

sbin:/

usr/

bin:/

usr/

local/

haproxy/

sbin

PROGDIR

=/

usr/

local/

haproxy

PROGNAME

=haproxy

DAEMON

=$PROGDIR

/

sbin/

$PROGNAME

CONFIG

=$PROGDIR

/

conf/

$PROGNAME

.conf

PIDFILE

=$PROGDIR

/

run/

$PROGNAME

.pid

DESC

="HAProxy daemon"

SCRIPTNAME

=/

etc/

init.d/

$PROGNAME

# Gracefully exit if the package has been removed.

test

-x

$DAEMON

||

exit

0

start(

)

{

echo

-n

"Starting $DESC

: $PROGNAME

"

$DAEMON

-f

$CONFIG

echo

"."

}

stop(

)

{

echo

-n

"Stopping $DESC

: $PROGNAME

"

haproxy_pid

=cat

$PIDFILE

kill

$haproxy_pid

echo

"."

}

restart(

)

{

echo

-n

"Restarting $DESC

: $PROGNAME

"

$DAEMON

-f

$CONFIG

-p

$PIDFILE

-sf

$(

cat

$PIDFILE

)

echo

"."

}

case

"$1"

in

start)

start

;;

stop)

stop

;;

restart)

restart

;;

*

)

echo

"Usage: $SCRIPTNAME

{start|stop|restart}"

>&

2

exit

1

;;

esac

exit

0

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2010-08-08 23:39

浏览 4435

分类:互联网

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值