欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mysql的MHA高可用及故障切換問題小結(jié)

 更新時(shí)間:2024年12月27日 14:25:58   作者:2401_85041083  
MHA是基于MySQL主從復(fù)制的高可用解決方案,通過自動切換到從節(jié)點(diǎn)并提升其為新主,實(shí)現(xiàn)數(shù)據(jù)庫的高可用和故障恢復(fù),配置包括主從復(fù)制、MHA組件、VIP管理等,通過配置無密碼認(rèn)證和測試連接,可以確保MHA正常運(yùn)行,感興趣的朋友一起看看吧

Mysql的MHA高可用及故障切換 

MHA

master high availability
建立在主從復(fù)制基礎(chǔ)之上的故障切換的軟件系統(tǒng)。

主從復(fù)制的單點(diǎn)問題

當(dāng)主從復(fù)制當(dāng)中,主服務(wù)器發(fā)生故障,會自動切換到一臺從服務(wù)器,然后把從服務(wù)器升格為主,繼續(xù)主從的架構(gòu)

  • master開啟二進(jìn)制日志,并允許從節(jié)點(diǎn)復(fù)制主節(jié)點(diǎn)的二進(jìn)制日志的內(nèi)容
  • 通過vip地址,當(dāng)主容機(jī)之后,vip會自動的飄移到從節(jié)點(diǎn)。
  • 從節(jié)點(diǎn)升級為主服務(wù)器,然后從宕機(jī)的master保存二進(jìn)制日志,將更新的內(nèi)容同步到新主,然后再同步到從節(jié)點(diǎn)。

配置

mysql1 192.168.246.6 主
mysql2 192.168.246.7 從1----主備
mysql3 192.168.246.10 從2
manager節(jié)點(diǎn):u3 192.168.246.9 MHA的組件
vip 192.168.246.100

1. 主從復(fù)制

ntpdate ntp.aliyun.com
#時(shí)間同步
vim /etc/my.cnf
#主
log-bin=master-bin
binlog_format=MIXED
log-slave-updates=true
relay_log_recovery=1
#啟用從庫崩潰或者重啟時(shí),會自動嘗試從日志當(dāng)中恢復(fù)。
#從1
server-id = 2
log-bin=master-bin
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
relay_log_recovery = 1
#從2
server-id = 3
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
relay_log_recovery = 1
#主從
mysql -u root -p123456
#從數(shù)據(jù)庫同步使用
CREATE USER 'myslave'@'192.168.246.%' IDENTIFIED WITH mysql_native_password BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.246.%';			
#manager 使用
CREATE USER 'mha'@'192.168.246.%' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'192.168.246.%' WITH GRANT OPTION;
#防止從庫通過主機(jī)名連接不上主庫
CREATE USER 'mha'@'master' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'master';
CREATE USER 'mha'@'slave1' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'slave1';
CREATE USER 'mha'@'slave2' IDENTIFIED WITH mysql_native_password BY 'manager';
GRANT ALL PRIVILEGES ON *.* TO 'mha'@'slave2';
flush privileges;
#主
show master status;

#從1 從2
mysql -u root -p123456
#同步建立
CHANGE master to master_host='192.168.246.6',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=157;
start slave;
show slave status\G;

#從庫必須全部設(shè)置為只讀模式
set global read_only=1;

2. MHA高可用

安裝MHA的組件

  • NODE :監(jiān)控每臺機(jī)器上mysql的狀態(tài),傳回給manager
  • MANAGER:管理節(jié)點(diǎn),控制mha的狀態(tài)。
#每個(gè)服務(wù)器都需要安裝MHA依賴的環(huán)境
apt install -y libdbd-mysql-perl \
libconfig-tiny-perl \
liblog-dispatch-perl \
libparallel-forkmanager-perl \
libextutils-cbuilder-perl \
libmodule-install-perl \
make
#在MHA的manager節(jié)點(diǎn)上安裝manager組件
cd /opt
tar -xf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
cd /opt
tar -xf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
#組件安裝后在/usr/local/bin 下面會生成幾個(gè)工具
masterha_check_ssh : 檢查mha節(jié)點(diǎn)之間ssh的配置和通信
masterha_manager: manager的啟動腳本
masterha_check_status: 檢查mha的運(yùn)行狀態(tài)
masterha_stop:關(guān)閉manager
masterha_master_switch: 控制故障轉(zhuǎn)移的方式
ave_binary_logs: 檢查,保存,復(fù)制master節(jié)點(diǎn)的二進(jìn)制日志
apply_diff_relay_logs: 識別二進(jìn)制日志當(dāng)中的差異部分,把差異的部分同步到slave

配置無密碼認(rèn)證

#配置所有節(jié)點(diǎn)之間的MHA的ssh的通信
#manager節(jié)點(diǎn)
ssh-keygen -t rsa
回車
ssh-copy-id 192.168.246.6
ssh-copy-id 192.168.246.7
ssh-copy-id 192.168.246.10
#主
ssh-keygen -t rsa
回車
ssh-copy-id 192.168.246.7
ssh-copy-id 192.168.246.10
#從
ssh-keygen -t rsa
回車
ssh-copy-id 192.168.246.6
ssh-copy-id 192.168.246.10

manager節(jié)點(diǎn)配置

root@u3:/usr/local/bin# cd /opt/mha4mysql-manager-0.57/samples/
root@u3:/opt/mha4mysql-manager-0.57/samples# cp -rp /opt/mha4mysql-manager-0.57/samples/scripts  /usr/local/bin
root@u3:/opt/mha4mysql-manager-0.57/samples# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/
root@u3:/opt/mha4mysql-manager-0.57/samples# vim /usr/local/bin/master_ip_failover
#刪除全部,復(fù)制以下代碼
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
my $vip = '192.168.233.100';
my $brdc = '192.168.233.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
### A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
root@u3:/opt/mha4mysql-manager-0.57/samples# mkdir /etc/masterha
root@u3:/opt/mha4mysql-manager-0.57/samples# ls
conf  scripts
root@u3:/opt/mha4mysql-manager-0.57/samples# cd conf/
root@u3:/opt/mha4mysql-manager-0.57/samples/conf# ls
app1.cnf  masterha_default.cnf
root@u3:/opt/mha4mysql-manager-0.57/samples/conf# cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
root@u3:/opt/mha4mysql-manager-0.57/samples/conf# vim /etc/masterha/app1.cnf
#復(fù)制以下代碼,需要清除注釋
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
#指向到myql的默認(rèn)位置
master_ip_failover_script=/usr/local/bin/master_ip_failover
#切換vip的腳本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=manager
ping_interval=1
#每一秒檢測一次主的狀態(tài)
remote_workdir=/tmp
repl_password=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.246.7 -s 192.168.246.10 
#從對主監(jiān)聽
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.246.6 
#主服務(wù)器
port=3306
[server2]
candidate_master=1   
#聲明server2備服務(wù)器
check_repl_delay=0
#立刻切換
hostname=192.168.246.7
#備用主服務(wù)器
port=3306
[server3]
hostname=192.168.246.10  
#從服務(wù)器2
port=3306
root@u3:/opt/mha4mysql-manager-0.57/samples/conf# vim /usr/local/share/perl/5.34.0/MHA/NodeUtil.pm
#192行修改添加
  $str =~ /(\d+)\.(\d+)/;
  my $strmajor = "$1.$2";
  my $result = sprintf( '%03d%03d', $1, $2 ) if $str =~ m/(\d+)\.(\d+)/;

#主需要手動開啟虛擬IP
ifconfig ens33:1 192.168.246.100/24
#主從設(shè)置軟連接
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

manager節(jié)點(diǎn)上測試

#測試ssh無密碼認(rèn)證,如果正常最后會輸出為successfully
masterha_check_ssh -conf=/etc/masterha/app1.cnf

#測試mysql主從連接情況,最后出現(xiàn)is OK字樣則正常。
masterha_check_repl -conf=/etc/masterha/app1.cnf

啟動連接

#manager節(jié)點(diǎn)上
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
#nohup:執(zhí)行記錄,把執(zhí)行記錄保存到指定的文件。
#檢查master節(jié)點(diǎn)
root@u3:~# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:4171) is running(0:PING_OK), master:192.168.246.6
#客戶端
apt -y install mariadb-server
#用vip連接
mysql -h 192.168.246.100 -u root -p123456
MySQL [(none)]> create database test1;
Query OK, 1 row affected (0.139 sec)
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.004 sec)

故障切換模擬

#manager節(jié)點(diǎn)打開日志
tail -f /var/log/masterha/app1/manager.log
#停止mha
master_stop --conf=/etc/masterha/appl.cnf
#主
systemctl stop mysqld
#從1
ip addr

恢復(fù)

#manager節(jié)點(diǎn)
vim /etc/masterha/app1.cnf

原來的主自動刪除了
修改并添加如下圖的內(nèi)容

#原來的主
vim /etc/my.cnf
log-bin=master-bin
binlog_format=MIXED
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
relay_log_recovery=1
systemctl restart mysqld
#從1
server-id = 2
log-bin=master-bin
log-slave-updates=true
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
relay_log_recovery = 1
mysql -u root -p123456
show master status;

原主與從2
CHANGE master to master_host='192.168.246.7',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=352;
start slave;
show slave status\G;
#客戶端
root@u4:~# mysql -h 192.168.246.100 -u root -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.30 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.004 sec)
4 rows in set (0.013 sec)
MySQL [(none)]> create database test2;
Query OK, 1 row affected (0.139 sec)
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.004 sec)

到此這篇關(guān)于Mysql的MHA高可用及故障切換的文章就介紹到這了,更多相關(guān)mysql mha高可用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 獲取MySQL數(shù)據(jù)表列信息的三種方法實(shí)現(xiàn)

    獲取MySQL數(shù)據(jù)表列信息的三種方法實(shí)現(xiàn)

    本文介紹了獲取MySQL數(shù)據(jù)表列信息的三種方法實(shí)現(xiàn),包含SHOWCOLUMNS命令、DESCRIBE命令以及查詢INFORMATION_SCHEMA.COLUMNS表,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-12-12
  • MYSQL字符集設(shè)置的方法詳解(終端的字符集)

    MYSQL字符集設(shè)置的方法詳解(終端的字符集)

    這篇文章主要給大家介紹了關(guān)于MYSQL字符集設(shè)置(終端的字符集)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • MySQL授予用戶權(quán)限命令詳解

    MySQL授予用戶權(quán)限命令詳解

    這篇文章主要給大家介紹了關(guān)于MySQL授予用戶權(quán)限命令的相關(guān)資料,授權(quán)就是為某個(gè)用戶賦予某些權(quán)限,例如可以為新建的用戶賦予查詢所有數(shù)據(jù)庫和表的權(quán)限,需要的朋友可以參考下
    2023-11-11
  • MySQL select、insert、update批量操作語句代碼實(shí)例

    MySQL select、insert、update批量操作語句代碼實(shí)例

    這篇文章主要介紹了MySQL select、insert、update批量操作語句代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Mysql5.7并發(fā)插入死鎖問題解決

    Mysql5.7并發(fā)插入死鎖問題解決

    死鎖是數(shù)據(jù)庫并發(fā)控制中的一種現(xiàn)象,它涉及多個(gè)事務(wù)在執(zhí)行過程中相互等待對方占有的資源,導(dǎo)致無法繼續(xù)執(zhí)行,本文就來介紹一下Mysql5.7并發(fā)插入死鎖問題解決,感興趣的可以了解一下
    2024-09-09
  • 淺談InnoDB隔離模式的使用對MySQL性能造成的影響

    淺談InnoDB隔離模式的使用對MySQL性能造成的影響

    這篇文章主要介紹了InnoDB隔離模式的使用對MySQL性能造成的影響,作為基于MySQL的最出名的數(shù)據(jù)庫,InnoDB相關(guān)的性能問題一直是DBA關(guān)注的熱點(diǎn),需要的朋友可以參考下
    2015-06-06
  • MYSQL大表改字段慢問題的解決

    MYSQL大表改字段慢問題的解決

    本文主要介紹了MYSQL大表改字段慢問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • MySQL存儲數(shù)據(jù)亂碼的問題解析

    MySQL存儲數(shù)據(jù)亂碼的問題解析

    這篇文章主要介紹了MySQL存儲數(shù)據(jù)亂碼的問題解析,作者從實(shí)際使用中的多個(gè)方面定位其原因然后解決,需要的朋友可以參考下
    2015-05-05
  • MySQL綠色版設(shè)置編碼以及1067錯(cuò)誤詳解

    MySQL綠色版設(shè)置編碼以及1067錯(cuò)誤詳解

    這篇文章主要介紹了MySQL綠色版設(shè)置編碼,以及1067錯(cuò)誤的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 詳解MySql如何不插入重復(fù)數(shù)據(jù)

    詳解MySql如何不插入重復(fù)數(shù)據(jù)

    本文主要介紹了詳解MySql如何不插入重復(fù)數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01

最新評論