My Blog
http://www.example.com

第十一周博客作业

1、如果主节点已经运行了一段时间,且有大量数据时,如何配置并启动slave节点(写出操作步骤)

图片

 #主服务器
[root@Master ~]#mkdir /backup
[root@Master ~]#mysqldump -A -F --single-transaction --master-data=1 > /backup/fullbackup_`date +%F_%T`.sql
[root@Master ~]#ll /backup/
total 480
-rw-r--r-- 1 root root 488075 Oct 13 19:18 fullbackup_2020-10-13_19:18:08.sql
[root@Master ~]#scp /backup/fullbackup_2020-10-13_19\:18\:08.sql 10.0.0.82:/data
[root@Master ~]#mysql
MariaDB [(none)]> set global innodb_flush_log_at_trx_commit=2; #性能优化
MariaDB [(none)]> set global sync_binlog=0;
MariaDB [(none)]> show variables like 'sync_binlog';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sync_binlog   | 0     |
+---------------+-------+
#Slave1服务器
MariaDB [(none)]> set global innodb_flush_log_at_trx_commit=2
#Slave2服务器
[root@Slave2 ~]#vim /etc/my.cnf.d/mariadb-server.cnf 
[mysqld]
server-id=82
read-only
[root@Slave2 ~]#systemctl start mariadb
[root@Slave2 ~]#vim /data/fullbackup_2020-10-13_19\:18\:08.sql 
CHANGE MASTER TO
  MASTER_HOST='10.0.0.80',
  MASTER_USER='repluser',
  MASTER_PASSWORD='diligent',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='master-bin.000003',
  MASTER_LOG_POS=387;
[root@Slave2 ~]#mysql < /data/fullbackup_2020-10-13_19\:18\:08.sql 
[root@Slave2 ~]#mysql
MariaDB [(none)]> start slave;

2、当master服务器宕机,提升一个slave成为新的master(写出操作步骤)

#找到数据库最新的节点,寻找新master
[root@Slave1 ~]#cat /var/lib/mysql/relay-log.info
#更改新master配置文件
[root@Slave1 data]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
log-bin=/data/mysql/logbin/mysql-bin
read_only=OFF
#清除旧master复制信息
MariaDB [(none)]> set global read_only=off;
MariaDB [(none)]> stop slave;
MariaDB [(none)]> reset slave all;
#在新master上完全备份
[root@Slave1 data]#mysqldump -A -F --single-transaction --master-data=1 > backup.sql
[root@Slave1 data]#scp backup.sql 10.0.0.82:
#让其他从节点指向新master
[root@Slave2 ~]#vim backup.sql
CHANGE MASTER TO
MASTER_HOST='10.0.0.81',
MASTER_USER='repluser',
MASTER_PASSWORD='diligent',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000004',
MASTER_LOG_POS=389;
MariaDB [(none)]> stop slave;
MariaDB [(none)]> reset slave all;
MariaDB [(none)]> set sql_log_bin=off;
MariaDB [(none)]> source backup.sql
MariaDB [mysql]> set sql_log_bin=on;
MariaDB [mysql]> start slave;

3、通过 MHA 0.58 搭建一个数据库集群结构

图片

(1)环境

MHA:安装mha manager和node 0.58、Mysql5.7

其他节点:安装node 0.58和Mysql5.7

(2)在所有节点上实现相互之间ssh key验证

[root@mha ~]#ssh-keygen
[root@mha ~]#ssh-copy-id 10.0.0.70
[root@mha ~]#rsync -av .ssh 10.0.0.80:/root/
[root@mha ~]#rsync -av .ssh 10.0.0.81:/root/
[root@mha ~]#rsync -av .ssh 10.0.0.82:/root/

(3)在管理节点上配置文件

[root@mha ~]#cat /etc/mastermha/app1.cnf 
[server default]
user=mhauser
password=diligent
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=lillian
repl_password=diligent
ping_interval=1
master_ip_failover_script=/usr/local/bin/master_ip_failover
report_script=/usr/local/bin/sendmail.sh
check_repl_delay=0
master_binlog_dir=/data/mysql/
[server1]
hostname=10.0.0.80
candidate_master=1
[server2]
hostname=10.0.0.81
candidate_master=1
[sercer3]
hostname=10.0.0.82

(4)准备相关脚本文件

[root@mha ~]#cat /usr/local/bin/sendmail.sh 
#!/bin/bash
echo "MySQL is down" | mail -s "MHA WARNING" 1223893670@qq.com
[root@mha ~]#cat /usr/local/bin/master_ip_failover 
#!/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 = '10.0.0.100/24';#设置Virtual IP
my $gateway = '10.0.0.2';#网关Gateway IP
my $interface = 'eth0'; #指定VIP所在网卡
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I
$interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$key down";
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" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
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" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
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";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
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";
}

(5)实现主从复制

#主节点
vim /etc/my.cnf
[mysqld]
server-id=80
log-bin
relay_bin_purge=0
general-log
(root@localhost) [(none)]> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       177 |
| mysql-bin.000002 |       154 |
+------------------+-----------+
(root@localhost) [(none)]> grant replication slave on *.* to lillian@'10.0.0.%' identified by 'diligent';
(root@localhost) [(none)]> grant all on *.* to mhauser@'10.0.0.%' identified by 'diligent';
[root@Master ~]#ifconfig eth0:1 10.0.0.100/24

#从节点
vim /etc/my.cnf
[mysqld]
server-id=82
log-bin
relay_log_purge=0
skip_name_reslove=1
(root@localhost) [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='10.0.0.80',
    ->   MASTER_USER='lillian',
    ->   MASTER_PASSWORD='diligent',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mysql-bin.000002',
    ->   MASTER_LOG_POS=154;
(root@localhost) [(none)]> start slave;
(root@localhost) [(none)]> show slave status;

(6)检查MHA的环境

[root@mha ~]#masterha_check_ssh --conf=/etc/mastermha/app1.cnf
[root@mha ~]#masterha_check_repl --conf=/etc/mastermha/app1.cnf

(7)启动MHA

#后台启动
[root@mha ~]#masterha_manager --conf=/etc/mastermha/app1.cnf &
#前台启动
[root@mha ~]#nohup masterha_manage --conf=/etc/mastermha/app1.cnf &> /dev/null
[root@mha ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:13627) is running(0:PING_OK), master:10.0.0.80

(8)排错日志

[root@mha ~]#tail /data/mastermha/app1/manager.log 
[root@mha ~]#cat /data/mastermha/app1/manager.log 

(9)故障

[root@Master ~]#service mysqld stop
Shutting down MySQL............ SUCCESS! 
[root@mha ~]#masterha_manager --conf=/etc/mastermha/app1.cnf
[root@mha ~]#cat /data/mastermha/app1/manager.log [root@mha ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 is stopped(2:NOT_RUNNING).

(10)再次运行MHA,需要删除下面文件

[root@mha ~]#rm -rf /data/mastermha/app1/app1.failover.complete

4、实战案例:Percona XtraDB Cluster(PXC 5.7)

5、通过 ansible 部署二进制 mysql 8

posted @ 2020-10-19 12:05  1平方m  阅读(53)  评论(0编辑  收藏  举报