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.pswp.cn/news/540075.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540075.shtml
英文地址,請注明出處:http://en.pswp.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…