mysql负责均衡读写分离_MySQL读写分离之负载均衡

mysql官方文档中有这么一句话:

MySQL Proxy is currently an Alpha release and should not be used within production environments.

So。。。

使用haproxy来做这些事,以下仅供参考:

环境配置

master        192.168.1.106             master1

slave1         192.168.1.107             master2master1(与master1主-主复制)

slave2         192.168.1.110             slave2---->master1(master1的从库)

slave3         192.168.1.111             slave3---->master1(master1的从库)

slave4         192.168.1.112             slave4----> master2(master2的从库)

monitor     192.168.1.200

192.168.1.105   eth1 写ip

192.168.1.113   eth2 读ip

说明:

当 master 停止复制, slave1 成为主库,haproxy停止发送请求到master和slave2,slave3, slave1与slave2,slave3依然可以从master接收日志。

当slave1停止复制,master成为主库,haproxy停止发送请求到slave1和slave4,master与slave4依然可以从slave1接收日志。

当 master和slave1同时停止复制,这时2台主库变成readonly模式,数据库不能写入 ,haproxy停止发送请求到slave2,slave3,slave4(脑裂)。

当slave1 offline时,master进入backup mode,haproxy停止发送请求到slave1,slave4。

当master offline时,slave1进入backup mode,haproxy停止发送请求到master,slave2,slave3。

当master和slave1同时offline,整个DB停止工作。

1、主从配置(略)

2、安装  xinetd ,配置mysqlchk服务

vi /etc/xinetd.d/mysqlchk

--两个master配置

service mysqlchk-write

{

flags           = REUSE

socket_type     = stream

port            = 9201

wait            = no

user= root

server          = /opt/script/mysqlchk_status.sh

log_on_failure  += USERID

disable         = no

only_from       = 192.168.1.0/24  #recommended toput the IPs that need

# toconnectexclusively (security purposes)

per_source      = UNLIMITED

}

service mysqlchk-read

{

flags           = REUSE

socket_type     = stream

port            = 9202

wait            = no

user= root

server          = /opt/script/mysqlchk_replication.sh

log_on_failure  += USERID

disable         = no

only_from       = 192.168.1.0/24  #recommended toput the IPs that need

# toconnectexclusively (security purposes)

per_source      = UNLIMITED

}

--所有slaves只需配置复制状态检查脚本

service mysqlchk-read

{

flags           = REUSE

socket_type     = stream

port            = 9202

wait            = no

user= root

server          = /opt/script/mysqlchk_replication.sh

log_on_failure  += USERID

disable         = no

only_from       = 192.168.1.0/24  #recommended toput the IPs that need

# toconnectexclusively (security purposes)

per_source      = UNLIMITED

}

vi /etc/services

--两个master添加:

mysqlchk-write    9201/tcp         # MySQL status check

mysqlchk-read9202/tcp         # MySQL replicationcheck

--所有slaves添加:

mysqlchk-read9202/tcp         # MySQL replicationcheck

重启xinetd

# /etc/init.d/xinetd stop

# /etc/init.d/xinetd start

查看端口号确认

[root@master xinetd.d]#  netstat -antup|grep xinetd

tcp        0      0 0.0.0.0:9201                0.0.0.0:*                   LISTEN      3077/xinetd

tcp        0      0 0.0.0.0:9202                0.0.0.0:*                   LISTEN      3077/xinetd

3、monitor主机安装haproxy

tar zxvf haproxy-1.4.23.tar.gz

cd haproxy-1.4.23

make TARGET=linux26 ARCH=x86_64

make install

4、配置haproxy配置文件

vi /usr/local/haproxy-1.4.23/conf/haproxy-db.cfg

# HAProxy configuration - haproxy-db.cfg

global

maxconn 4096

daemon

pidfile /usr/local/haproxy-1.4.23/haproxy.pid

#debug

#quiet

#chroot /usr/share/haproxy

defaults

log     global

mode    http

#optionhttplog

optiondontlognull

log 127.0.0.1 local0

retries 3

optionredispatch

maxconn 4096

timeout connect1000ms

timeout client 50000ms

timeout server 50000ms

listen  stats :8011

balance

mode http

stats enable

stats auth root:monitor

##

## FRONTEND ##

##

# Load-balanced IPsforDB writesandreads

#

frontend db_write

mode tcp

bind 192.168.1.105:3306

default_backend cluster_db_write

frontend db_read

mode tcp

bind 192.168.1.113:3306

default_backend cluster_db_read

# Monitor DB server availability

#

frontend monitor_master

#

# setmaster_backupto'up'or'down'

#

bind 127.0.0.1:9301

mode http

#optionnolinger

acl no_repl_master nbsrv(master_replication) eq 0

acl no_repl_slave1 nbsrv(slave1_replication) eq 0

acl no_master nbsrv(master_status) eq 0

acl no_slave1 nbsrv(slave1_status) eq 0

monitor-uri /monitor

monitor fail unless no_repl_master no_repl_slave1 no_slave1

monitor fail if no_master no_slave1

frontend monitor_slave1

#

# setslave1_backupto'up'or'down'

#

bind 127.0.0.1:9302

mode http

#optionnolinger

acl no_repl_master nbsrv(master_replication) eq 0

acl no_repl_slave1 nbsrv(slave1_replication) eq 0

acl no_master nbsrv(master_status) eq 0

acl no_slave1 nbsrv(slave1_status) eq 0

monitor-uri /monitor

monitor fail unless no_repl_master no_repl_slave1 no_master

monitor fail if no_master no_slave1

frontend monitor_slave2

#

# setslave2read-onlyslaveto'down'

#

bind 127.0.0.1:9303

mode http

#optionnolinger

acl no_repl_slave2 nbsrv(slave2_replication) eq 0

acl no_repl_master nbsrv(master_replication) eq 0

acl slave1 nbsrv(slave1_status) eq 1

monitor-uri /monitor

monitor fail if no_repl_slave2

monitor fail if no_repl_master slave1

frontend monitor_slave3

#

# setslave3read-onlyslaveto'down'

#

bind 127.0.0.1:9304

mode http

#optionnolinger

acl no_repl_slave3 nbsrv(slave3_replication) eq 0

acl no_repl_master nbsrv(master_replication) eq 0

acl slave1 nbsrv(slave1_status) eq 1

monitor-uri /monitor

monitor fail if no_repl_slave3

monitor fail if no_repl_master slave1

frontend monitor_slave4

#

# setslave4read-onlyslaveto'down'

#

bind 127.0.0.1:9305

mode http

#optionnolinger

acl no_repl_slave4 nbsrv(slave4_replication) eq 0

acl no_repl_slave1 nbsrv(slave1_replication) eq 0

acl master nbsrv(master_status) eq 1

monitor-uri /monitor

monitor fail if no_repl_slave4

monitor fail if no_repl_slave1 master

# Monitor forsplit-brain syndrome

#

frontend monitor_splitbrain

#

# setmaster_splitbrainandslave1_splitbrainto'up'

#

bind 127.0.0.1:9300

mode http

#optionnolinger

acl no_repl01 nbsrv(master_replication) eq 0

acl no_repl02 nbsrv(slave1_replication) eq 0

acl master nbsrv(master_status) eq 1

acl slave1 nbsrv(slave1_status) eq 1

monitor-uri /monitor

monitor fail unless no_repl01 no_repl02 master slave1

##

## BACKEND ##

##

# Checkevery DB server replication status

# - perform an http checkonport 9201 (replication status)

# - setto'down'if responseis'503 Service Unavailable'

# - setto'up'if responseis'200 OK'

#

backend master_replication

mode tcp

balance roundrobin

optiontcpka

optionhttpchk

server master 192.168.1.106:3306 checkport 9202 inter 5s rise 1 fall 1

backend slave1_replication

mode tcp

balance roundrobin

optiontcpka

optionhttpchk

server slave1 192.168.1.107:3306 checkport 9202 inter 5s rise 1 fall 1

backend slave2_replication

mode tcp

balance roundrobin

optiontcpka

optionhttpchk

server slave2 192.168.1.110:3306 checkport 9202 inter 5s rise 1 fall 1

backend slave3_replication

mode tcp

balance roundrobin

optiontcpka

optionhttpchk

server slave3 192.168.1.111:3306 checkport 9202 inter 5s rise 1 fall 1

backend slave4_replication

mode tcp

balance roundrobin

optiontcpka

optionhttpchk

server slave4 192.168.1.112:3306 checkport 9202 inter 5s rise 1 fall 1

# CheckMaster DB server mysql status

# - perform an http checkonport 9201 (mysql status)

# - setto'down'if responseis'503 Service Unavailable'

# - setto'up'if responseis'200 OK'

#

backend master_status

mode tcp

balance roundrobin

optiontcpka

optionhttpchk

server master 192.168.1.106:3306 checkport 9201 inter 5s rise 2 fall 2

backend slave1_status

mode tcp

balance roundrobin

optiontcpka

optionhttpchk

server slave1 192.168.1.107:3306 checkport 9201 inter 5s rise 2 fall 2

# DB write cluster

# Failure scenarios:

# - replication 'up'onmaster & slave1 = writestomaster

# - replication 'down'onslave1 = writestomaster

# - replication 'down'onmaster = writestoslave1

# - replication 'down'onmaster & slave1 = go nowhere, split-brain, cluster FAIL!

# - mysql 'down'onslave1 = writestomaster_backup

# - mysql 'down'onmaster = writestoslave1_backup

# - mysql 'down'onmaster & slave1 = go nowhere, cluster FAIL!

#

backend cluster_db_write

#

# - max1 db server availableatalltimes

# - master ispreferred (topoflist)

# - db_backups settheir'up'or'down'basedonresultsfrommonitor_monitor

#

mode tcp

optiontcpka

balance roundrobin

optionhttpchk GET /monitor

server master 192.168.1.106:3306 weight 1 checkport 9202 inter 5s rise 2 fall 1

server slave1 192.168.1.107:3306 weight 1 checkport 9202 inter 5s rise 2 fall 1 backup

server master_backup 192.168.1.106:3306 weight 1 checkport 9301 inter 5s rise 2 fall 2 addr 127.0.0.1 backup

server slave1_backup 192.168.1.107:3306 weight 1 checkport 9302 inter 5s rise 2 fall 2 addr 127.0.0.1 backup

# DB readcluster

# Failure scenarios

# - replication 'up'onmaster & slave1 = readsonmaster, slave1,alldb_slaves

# - replication 'down'onslave1 = readsonmaster, slavesofmaster

# - replication 'down'onmaster = readsonslave1, slavesofslave1

# - replication 'down'onmaster & slave1 = readsonmaster_splitbrainandmaster_splitbrainonly

# - mysql 'down'onslave1 = readsonmaster_backup, slavesofmaster

# - mysql 'down'onmaster = readsonslave1_backup, slavesofslave1

# - mysql 'down'onmaster & slave1 = go nowhere, cluster FAIL!

#

backend cluster_db_read

#

# - max2 master db servers availableatalltimes

# - maxN slave db servers availableatalltimesexceptduring split-brain

# - monitor track 'up'and'down'ofmonitorinthe cluster_db_write

# - db_backups track 'up'and'down'ofdb_backupsinthe cluster_db_write

# - db_splitbrains settheir'up'or'down'basedonresultsfrommonitor_splitbrain

#

mode tcp

optiontcpka

balance roundrobin

optionhttpchk GET /monitor

server master 192.168.1.106:3306 weight 1 track cluster_db_write/master

server slave1 192.168.1.107:3306 weight 1 track cluster_db_write/slave1

server master_backup 192.168.1.106:3306 weight 1 track cluster_db_write/master_backup

server slave1_backup 192.168.1.107:3306 weight 1 track cluster_db_write/slave1_backup

server master_splitbrain 192.168.1.106:3306 weight 1 checkport 9300 inter 5s rise 1 fall 2 addr 127.0.0.1

server slave1_splitbrain 192.168.1.107:3306 weight 1 checkport 9300 inter 5s rise 1 fall 2 addr 127.0.0.1

#

# Scaling & redundancy options

# - db_slaves settheir'up'or'down'basedonresultsfrommonitor_monitor

# - db_slaves should take longer torise

#

server slave2_slave 192.168.1.110:3306 weight 1 checkport 9303 inter 5s rise 5 fall 1 addr 127.0.0.1

server slave3_slave 192.168.1.111:3306 weight 1 checkport 9304 inter 5s rise 5 fall 1 addr 127.0.0.1

server slave4_slave 192.168.1.112:3306 weight 1 checkport 9305 inter 5s rise 5 fall 1 addr 127.0.0.1

5、启动haproxy

haproxy -f /usr/local/haproxy-1.4.23/conf/haproxy-db.cfg

监控地址:http://192.168.1.200:8011/haproxy?stats

user:root    password:monitor

一些参数说明 :

maxconn

Sets the maximum per-process number of concurrent connections to . It

is equivalent to the command-line argument "-n". Proxies will stop accepting

connections when this limit is reached.

daemon

Makes the process fork into background. This is the recommended mode of

operation. It is equivalent to the command line "-D" argument. It can be

disabled by the command line "-db" argument.

pidfile

Writes pids of all daemons into file . This option is equivalent to

the "-p" command line argument. The file must be accessible to the user

starting the process.

retries

Set the number of retries to perform on a server after a connection failure

May be used in sections:    defaults | frontend | listen | backend

yes   |    no    |   yes  |   yes

Arguments :

   is the number of times a connection attempt should be retried on

a server when a connection either is refused or times out. The

default value is 3.

It is important to understand that this value applies to the number of

connection attempts, not full requests. When a connection has effectively

been established to a server, there will be no more retry.

In order to avoid immediate reconnections to a server which is restarting,

a turn-around timer of 1 second is applied before a retry occurs.

When "option redispatch" is set, the last retry may be performed on another

server even if a cookie references a different server.

See also : "option redispatch"

option redispatch

no option redispatch

Enable or disable session redistribution in case of connection failure

May be used in sections:    defaults | frontend | listen | backend

yes   |    no    |   yes  |   yes

Arguments : none

In HTTP mode, if a server designated by a cookie is down, clients may

definitely stick to it because they cannot flush the cookie, so they will not

be able to access the service anymore.

Specifying "option redispatch" will allow the proxy to break their

persistence and redistribute them to a working server.

It also allows to retry last connection to another server in case of multiple

connection failures. Of course, it requires having "retries" set to a nonzero

value.

This form is the preferred form, which replaces both the "redispatch" and

"redisp" keywords.

If this option has been enabled in a "defaults" section, it can be disabled

in a specific instance by prepending the "no" keyword before it.

option dontlognull

no option dontlognull

Enable or disable logging of null connections

May be used in sections :   defaults | frontend | listen | backend

yes   |    yes   |   yes  |   no

Arguments : none

In certain environments, there are components which will regularly connect to

various systems to ensure that they are still alive. It can be the case from

another load balancer as well as from monitoring systems. By default, even a

simple port probe or scan will produce a log. If those connections pollute

the logs too much, it is possible to enable option "dontlognull" to indicate

that a connection on which no data has been transferred will not be logged,

which typically corresponds to those probes.

It is generally recommended not to use this option in uncontrolled

environments (eg: internet), otherwise scans and other malicious activities

would not be logged.

If this option has been enabled in a "defaults" section, it can be disabled

in a specific instance by prepending the "no" keyword before it.

另外,使用keepalived实现代理层的HA。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540075.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql使用技巧_MySQL使用不得不看的几个小技巧

程序中写入的一行行的SQL语句,如果使用了一些优化小技巧,定能达到事半功倍的效果。1. 优化你的MySQL查询缓存在MySQL服务器上进行查询,可以启用高速查询缓存。让数据库引擎在后台悄悄的处理是提高性能的最有效方法之一。当同一个查询被执行多…

mysql oracle 数据类型转换_Mysql与Oracle之间的数据类型转换

[转]MYSQL 与 Oracle 之间的数据类型转换

rad linux下安装mysql_Linux(CentOS或RadHat)下MySQL源码安装

MySQL 5.6开始,需要使用g进行编译。cmake :MySQL 5.5开始,使用cmake进行工程管理,cmake需要2.8以上版本。bison :MySQL语法解析器需要使用bison进行编译。ncurses-devel :用于终端操作的开发包。zlib …

mysql5.1数据库乱码_MySql5.1以上版本中文乱码的解决方法

在my.cnf内添加以下代码输出err日志信息:[safe_mysqld]err-log /var/log/mysqld.logpid-file /var/lib/mysql/localhost.localdomain.pid在shell中输入/bin/sh /usr/bin/mysqld_safe &启动mysql,shell输出如下:110328 11:39:55 mysqld_…

mysql 命令行批量sql_命令行中执行批量SQL的方法

基础信息介绍测试库:test;测试表:user;user表定义:CREATE TABLE user (id int(11) NOT NULL AUTO_INCREMENT,name char(30) NOT NULL,age int(11) NOT NULL,gender tinyint(1) DEFAULT 1 COMMENT 性别:1男;…

mysql双主数据一致性_MySQL双主一致性架构优化 | 架构师之路-阿里云开发者社区...

一、双主保证高可用MySQL数据库集群常使用一主多从,主从同步,读写分离的方式来扩充数据库的读性能,保证读库的高可用,但此时写库仍然是单点。在一个MySQL数据库集群中可以设置两个主库,并设置双向同步,以冗…

spool导出姓名中文乱码_MySQL不同字符集转化标准—7步实现,杜绝乱码!

引言作为资深的DBA程序员,在工作中是否会遇到更这样的情况呢?原有数据库的字符集由于前期规划不足,随着业务的发展不能满足业务的需求。如原来业务系统用的是utf8字符集,后期有存储表情符号的需求,uft8字符集就不能满足…

appium和airtest_关于Airtest自动化测试工具

一开始知道Airtest大概是在年初的时候,当时,看了一下官方的文档,大概是类似Sikuli的一个工具,主要用来做游戏自动化的,通过截图的方式用来解决游戏自动化测试的难题。最近,移动端测试的同事尝试用它的poco库…

easyexcel 设置标题_使用easyexcel完成复杂表头及标题的导出功能(自定义样式)

如需客户端指定excel版本,只需要判断后缀名然后在controller中的.excelType(ExcelTypeEnum.XLS)做指定输出内容格式即可***(注意表格行高列宽统一设置是在实体类的类名注解上,如果需要对表格进行精细的宽高设置需要删除掉这两个注解,可以在拦截器使用row的方法进行设置)1. ## 引…

mysql distinct两列_正在检索两列,并对MySQL中的每列应用“distinct”

这是一张桌子books----------------------------| author_fname | author_lname |----------------------------| Dan | Harris || Freida | Harris || George | Saunders |----------------------------我知道如果DISTINCT用作SELECT DISTINCT author_fname, author_lname FRO…

mysql笛卡尔积 去重_MySQL入门(函数、条件、连接)

MySQL入门(四)distinct:去重mysql>:create table t1(id int,x int,y int);mysql>: insert into t1 values(1, 1, 1), (2, 1, 2), (3, 2, 2), (4, 2, 2);mysql>: select distinct * from t1; # 全部数据mysql>: select distinct x, y from t1; # 结果 1,…

nmon安装为什么重启mysql_Nmon的安装及使用

一、下载Nmon根据CPU的类型选择下载相应的版本:二、初始化工具[rootmululu ~]# cd /opt[rootmululu opt]# mkdir nmon[rootmululu opt]# cd nmon[rootmululu nmon]#wget http://sourceforge.net/projects/nmon/files/download/nmon_x86_12a.zip[rootmululu nmon]# u…

mysql join 循环_关于mysql联表的内嵌循环操作nested loop join中on和where执行顺序问题...

mysql的理论依据没找到,个人理解是先执行where的过滤条件,先关联再过滤明显做的是无用功。oracle中倒是能在执行计划中看到,先执行的是过滤条件(下面代码中最后一行)。explain plan for SELECT * FROM tmp_t2 t2 LEFT JOIN tmp_t1 t1 ON t2.i…

python非法语句是_python 如何优雅的处理大量异常语句?

bs4的链式调用很赞,所以我把soup包装了一下class MY_SOUP():包装类def __init__(self,soup):self.soup soupif soup:if soup.string:self.string soup.string.strip()else:self.string Noneelse:self.string Nonedef find(self, *args, **kw):ret self.soup.fi…

Iptables详解+实例

2019独角兽企业重金招聘Python工程师标准>>> Iptabels是与Linux内核集成的包过滤防火墙系统,几乎所有的linux发行版本都会包含Iptables的功能。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则Iptables有利于…

django ipython shell_通過django的shell_plus編寫ipython腳本

Im writing a shell script which runs a command through ipython with the -c option like this:我正在編寫一個shell腳本,它通過ipython運行一個命令,使用-c選項,如下所示:ipython -c "from blah import myfunct; myfunct()"but…

阿里云服务器安装onlyoffice_阿里云服务器安装 JDK 8

欢迎关注“科技毒瘤君”&#xff01;上一期给大家分享了如何申请阿里云的免费云服务器&#xff0c;还没有看过的小伙伴可以先前往了解 >>阿里云免费服务器<<这一次将会为大家分享如何在服务器上配置 Java环境&#xff0c;这里演示使用的系统为Ubuntu 18.04 64位&am…

js发送请求

1.Chrome控制台中 net::ERR_CONNECTION_REFUSED js频繁发送请求&#xff0c;有可能连接被拒绝&#xff0c;可用setTimeout&#xff0c;过几秒发送&#xff0c;给个缓冲时间 var overlayAnalystService L.supermap.spatialAnalystService(serviceUrl); setTimeout(function () …

据说有99%的人都会做错的面试题

这道题主要考察了面试者对浮点数存储格式的理解。另外&#xff0c;请不要讨论该题本身是否有意义之类的话题。本题只为了测试面试者相关的知识是否掌握&#xff0c;题目本身并没有实际的意义。 下面有6个浮点类型变量&#xff0c;其中前三个是float类型的&#xff0c;后三个是d…