第五周

 

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

环境准备:两台centos8
10.0.0.8
10.0.0.18
#主节点下载mariadb 设为开机自启
[root@master ~]#[root@master ~]# yum -y install mariadb-server;systemctl enable --now mariadb
[root@master ~]#vim /etc/my.cnf
[mysqld]
log-bin
[root@master ~]# systemctl restart mysqld
#[root@master ~]# mysql
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql             |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
#模拟数据库已经运行了一段时间,上面进行了一些操作
MariaDB [(none)]> create database db1;
MariaDB [(none)]> use db1;
Database changed
MariaDB [db1]> create table test(id int auto_increment primary key ,name char(5));
Query OK, 0 rows affected (0.008 sec)
MariaDB [db1]> insert test (id,name) values(1,'yang');
Query OK, 1 row affected (0.002 sec)
MariaDB [db1]> insert test(id,name)values(2,'wang');

MariaDB [db1]> select *from test;
+----+------+
| id | name |
+----+------+
|  1 | yang |
|  2 | wang |
+----+------+
2 rows in set (0.000 sec)
#此时的二进制位置是
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |      550|

1 row in set (0.000 sec)
#创建授权账号
MariaDB [db1]> grant replication slave on *.* to repluser@'10.0.0.%' identified by 'magedu';

#在主服务器完全备份
[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 472
-rw-r--r-- 1 root root 480092 Oct 16 19:47 fullbackup_2020-10-16_19:47:34.sql
#把完全备份的传给从节点18
[root@master ~]#scp /backup/fullbackup_2020-10-16_19:47:34.sql 10.0.0.18:/root
#配置从节点
#下载数据库修改配置文件
[root@slave ~]#dnf -y install mariadb-server
root@slave~]#systemctl enable --now mariadb
[root@slave~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=18
read-only
[root@slave ~]#systemctl restart mariadb
#将完全备份还原到新的从节点
#配置从节点,从完全备份的位置之后开始复制
[root@slave ~]#grep '^CHANGE MASTER' /data/fullbackup_2020-10-16_19:47:34.sql
CHANGE MASTER TO MASTER_LOG_FILE='mariadb-bin.000003', MASTER_LOG_POS=389;

#注意语法,在备份文件中添加主节点的服务器 账号和密码端口号
[root@slave ~]#vim /data/fullbackup_2020-10-16_19:47:34.sql
CHANGE MASTER TO
MASTER_HOST='10.0.0.8',
MASTER_USER='repluser',
MASTER_PASSWORD='magedu',
MASTER_PORT=3306,       
MASTER_LOG_FILE='mariadb-bin.000003', MASTER_LOG_POS=389;

#把备份导入到数据库中
[root@slave ~]#mysql < /data/fullbackup_2020-10-16_19:47:34.sql
[root@slave ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.11-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#开启线程
MariaDB [(none)]> start slave;
#开启之后线程连接成功
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.8
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000003
          Read_Master_Log_Pos: 389
              Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 557
        Relay_Master_Log_File: mariadb-bin.000003
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

#主节点的数据已经完全同步了过来
MariaDB [db1]> select *from db1.test;
+----+------+
| id | name |
+----+------+
|  1 | yang |
|  2 | wang |
+----+------+
2 rows in set (0.000 sec)
#主节点上出现binlog dump 连接
MariaDB [(none)]> show processlist;

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

在第一个道题实验主从架构的基础上再加一个从节点达到1主两从

#增加一个从节点10.0.0.28
#把完全备份的传给从节点18
[root@master ~]#scp /backup/fullbackup_2020-10-16_19:47:34.sql 10.0.0.18:/root
[root@slave2 ~]#dnf -y install mariadb-server;systemctl enable --now mariadb
[root@slave2 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=28
read-only

#
[root@slave2 ~]#systemctl restart mariadb

#修改文件同步位置
[root@slave2 ~]#vim fullbackup_2020-10-16_19:47:34.sql
MCHANGE MASTER TO
MASTER_HOST='10.0.0.8',
MASTER_USER='repluser',
MASTER_PASSWORD='magedu',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000003', MASTER_LOG_POS=389;

#导入主节点的数据,同步成功
[root@slave2 ~]#mysql <fullbackup_2020-10-16_19:47:34.sql
[root@slave2 ~]#mysql
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.8
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000003
          Read_Master_Log_Pos: 389
              Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 557
        Relay_Master_Log_File: mariadb-bin.000003
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

#模拟主节点宕机,先导入testlog.log 执行十万条记录,看哪个节点同步的数据多 选谁等新主节点
[root@master ~]# mysql
MariaDB [(none)]> use db1
#执行存储过程的时候 停掉服务
MariaDB [db1]> call sp_testlog;
ERROR 1053 (08S01): Server shutdown in progress
[root@master ~]# systemctl stop mariadb
#比较两个从节点的同步情况,选择slave1作为新主
[root@slave1~]#cat /var/lib/mysql/relay-log.info
5
./mariadb-relay-bin.000002
4172783
mariadb-bin.000003
4172615
0
[root@slave2 ~]#cat /var/lib/mysql/relay-log.info
5
./mariadb-relay-bin.000002
4160557
mariadb-bin.000003
4103890
0

#在18机器上修改作为新主
#新master修改配置文件,关闭read-only配置
[root@slave1 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=18
log-bin
[root@slave1 ~]#mysql
MariaDB [hellodb]>stop slave;
MariaDB [hellodb]>reset slave all;
[root@centos8 ~]#systemctl restart mariadb
[root@slave1 ~]#mysql
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       330 |
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1               |
| db2               |
| information_schema |
| mysql             |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)
#18上完全备份并拷贝给28
[root@slave1~]#mysqldump -A -F --single-transaction --master-data=1 > backup.sql
[root@slave1~]#scp backup.sql 10.0.0.28:/root

#在28上修改备份文件指向18
[root@slave2 ~]#vim backup.sql
CHANGE MASTER TO
MASTER_HOST='10.0.0.18',
MASTER_USER='repluser',
MASTER_PASSWORD='magedu',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=375;
#导入slave1的数据,在从节点上重新指向18,作为18的从节点
[root@slave2 ~]#mysql <backup.sql
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row
Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.18
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000002
          Read_Master_Log_Pos: 502
              Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 684
        Relay_Master_Log_File: mariadb-bin.000002
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
#数据同步 主从关系重新建立
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db1               |             |
| information_schema |
| mysql             |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)

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

准备环境:

环境:四台主机 centos8上面的数据库使用脚本跑的二进制安装mysql5.7.30
10.0.0.7 CentOS7 MHA管理端
10.0.0.8 CentOS8 Master
10.0.0.18 CentOS8 Slave1
10.0.0.28 CentOS8 Slave2

1、在管理节点上安装两个包

#在10.0.0.7上装上这两个准备好0.58的mha包和脚本
mha4mysql-manager
mha4mysql-node
[root@mha-manager ~]# ls
anaconda-ks.cfg                 mha4mysql-manager-0.58-0.el7.centos.noarch.rpm mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
install_mysql5.7_for_centos.sh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
#先装node包再装manager包
[root@mha-manager ~]#yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@mha-manager ~]#yum -y install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

#其他机器上装的node包

修改主机名看起来方便

[root@centos7mini ~]#hostnamectl set-hostname mha-manager
[root@centos7mini ~]#exit
[root@mha-manager ~]#

[root@centos8mini ~]#hostnamectl set-hostname Master
[root@centos8mini ~]#exit

[root@centos8 ~]#hostnamectl set-hostname slave1
[root@centos8 ~]#exit

[root@centos8 ~]#hostnamectl set-hostname slave2
[root@centos8 ~]#exit

2、在所有MySQL服务器节点上安装node包

#在三个节点上分别用脚本跑mysql5.7安装
[root@master ~]#bash install_mysql5.7_for_centos.sh mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
#安装成功后退出生效

#在三个节点上安装node包,可以在xshell全部会话一下安装(把包分别拷贝到每个机器上的情况下)
[root@master ~]#yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@slave1 ~]#yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm
[root@slave2 ~]#yum -y install mha4mysql-node-0.58-0.el7.centos.noarch.rpm

3、在所有节点实现相互之间ssh key 验证(涉及到安全生产中慎用)

#三次回车生成密钥
[root@mha-manager ~]#ssh-keygen
#拷贝到自己身上
[root@mha-manager ~]# ssh-copy-id 127.0.0.1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:8ISIZGc2lZ0Pp4HdoiYVyNUNyfI09vpHfuMc01Bxhqo.
ECDSA key fingerprint is MD5:1f:d9:62:2d:94:3d:b5:99:c8:e2:45:af:cd:b2:bd:5c.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

root@127.0.0.1's password: #输入密码
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '127.0.0.1'"
and check to make sure that only the key(s) you wanted were added..

#为了保留属性 使用rsycn复制整个目录本身给其他几个主机
[root@mha-manager ~]# yum -y install rsync (在全部会话都要下载)
[root@mha-manager ~]#rsync -av .ssh 10.0.0.8:/root/
[root@mha-manager ~]#rsync -av .ssh 10.0.0.18:/root/
[root@mha-manager ~]#rsync -av .ssh 10.0.0.28:/root/

4、在管理节点7上建立配置文件

#建一个单独文件 叫什么存在那不重要,存放mha内容
[root@mha-manager ~]#mkdir /etc/mastermha/

#mha可以监控多组 之所以叫app1是第一组主从
[root@mha-manager ~]#vim /etc/mastermha/app1.cnf 
[server default]
user=mhauser
password=magedu
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=magedu
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.8
candidate_master=1    
[server2]
hostname=10.0.0.18
[server3]
hostname=10.0.0.28
candidate_master=1  

在manager7上准备相关脚本

#发邮件的脚本 还有准备.mailrc文件
#想要邮件报警 需要下载邮件服务和.mailrc 服务
[root@mha-manager ~]# yum -y install mailx postfix
#mailrc文件
[root@mha-manager ~]#vim .mailrc
set from=1443964024@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=1443964024@qq.com
set smtp-auth-password=mzhhwiiuddqahbba                                                              
set smtp-auth=login
set ssl-verify=ignore
#测试一下使用命令看能不能收到,邮件服务能不能用
[root@mha-manager~]#vim /usr/local/bin/sendmail.sh
echo "MySQL is down" | mail -s "MHA Warning" 1443964024@qq.com  

[root@mha-manager ~]#chmod +x /usr/local/bin/sendmail.sh
[root@mha-manager ~]# /usr/local/bin/sendmail.sh 可以收到邮件警报了

#需要注意的是 网关ip和虚拟ip即vip 还有所在网卡要手动指定,想要当预备主节点的从网卡名一定要是文件里面指定的eth0
以及使用下面文件时 把注释汉语信息删掉
[root@mha-manager ~]#vim /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';                        
my $gateway = '10.0.0.254';
my $interface = 'eth0';
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";
}
#给执行权限
[root@mha-manager ~]#chmod +x /usr/local/bin/master_ip_failover
[root@mha-manager bin]# ll /usr/local/bin/master_ip_failover
-rwxr-xr-x 1 root root 2405 Oct 15 12:37 /usr/local/bin/master_ip_failover

5、配置Master

[root@master ~]#mkdir /data/mysql/
[root@master ~]#chown mysql.mysql /data/mysql/
[root@master ~]#vim /etc/my.cnf
[mysqld]
server_id=8
log-bin
skip_name_resolve=1
general_log     #非必须项

[root@Master ~]# systemctl restart mysqld
[root@Master ~]# mysql -uroot -pcentos 
#查看二进制日志位置 并创建授权账号
mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| Master-bin.000001 |       154 |
#建立授权账号
mysql>grant replication slave on *.* to repluser@'10.0.0.%' identified by 'magedu';
#和配置文件里mhauser密码一定要匹配
mysql>grant all on *.* to mhauser@'10.0.0.%' identified by 'magedu'; 

mysql> select user,host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| mhauser       | 10.0.0.%  |
| repluser      | 10.0.0.%  |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+

#配置VIP
#安所有节点装nte-tools包
[root@master ~]#yum -y install net-tools
[root@slave1 ~]#yum -y install net-tools
[root@slave2 ~]#yum -y install net-tools
[root@master ~]#ifconfig eth0:1 10.0.0.100/24
[root@Master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:c3:1f:d4 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.8/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.100/24 brd 10.0.0.255 scope global secondary eth0:1
       valid_lft forever preferred_lft forever

6、分别配置另外的slave节点

#配置slave1
[root@slave1 ~]vim /etc/my.cnf
[mysqld]
server_id=18   #不同节点此值各不相同
log-bin
read_only
general_log     #非必须项
relay_log_purge=0 #不让中继日志清理
skip_name_resolve=1    #禁止反向解析
[root@slave1 ~]#systemctl restart mysqld

#建立主从关系
[root@slave1 ~]#mysql -uroot -pcentos
mysql> CHANGE MASTER TO
MASTER_HOST='10.0.0.8',
MASTER_USER='repluser',
MASTER_PASSWORD='magedu',
MASTER_PORT=3306,
MASTER_LOG_FILE=' Master-bin.000001', MASTER_LOG_POS=154;
#开启进程
mysql>START SLAVE;
mysql> show slave status\G
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                Master_Host: 10.0.0.8
                Master_User: repluser
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: Master-bin.000001
        Read_Master_Log_Pos: 1198
              Relay_Log_File: slave1-relay-bin.000002
              Relay_Log_Pos: 1365
      Relay_Master_Log_File: Master-bin.000001
            Slave_IO_Running: Yes
          Slave_SQL_Running: Yes

#配置slave2
[root@slave2 ~]#vim /etc/my.cnf
[mysqld]
server-id=28
log-bin
read-only
general_log     #非必须项
relay_log_purge=0
skip_name_resolve=1
#建立主从关系
[root@slave1 ~]#mysql -uroot -pcentos
mysql> CHANGE MASTER TO
MASTER_HOST='10.0.0.8',
MASTER_USER='repluser',
MASTER_PASSWORD='magedu',
MASTER_PORT=3306,
MASTER_LOG_FILE=' Master-bin.000001', MASTER_LOG_POS=154;
#开机进程
mysql>START SLAVE;
mysql> show slave status\G
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                Master_Host: 10.0.0.8
                Master_User: repluser
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: Master-bin.000001
        Read_Master_Log_Pos: 894
              Relay_Log_File: slave2-relay-bin.000002
              Relay_Log_Pos: 1061
      Relay_Master_Log_File: Master-bin.000001
            Slave_IO_Running: Yes
          Slave_SQL_Running: Yes

#测试:主从是否同步
[root@Master ~]# mysql -uroot -pcentos
mysql> create database db1;
Query OK, 1 row affected (0.01 sec)
[root@slave1 ~]#mysql -uroot -pcentos
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1               |
| mysql             |
| performance_schema |
| sys               |
+--------------------+
5 rows in set (0.01 sec)
[root@slave2 ~]#mysql -uroot -pcentos
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1               |
| mysql             |
| performance_schema |
| sys               |
+--------------------+
5 rows in set (0.01 sec)

7、检查Mha的环境

#检查环境 确认当前环境是否满足mha的要求
#检查ssh环境是否符合
[root@mha-manager ~]#masterha_check_ssh --conf=/etc/mastermha/app1.cnf
Thu Oct 15 15:09:47 2020 - [info] All SSH connection tests passed successfully.
#检查复制的若干要求
[root@mha-manager ~]#masterha_check_repl --conf=/etc/mastermha/app1.cnf
Thu Oct 15 15:10:08 2020 - [info]  OK.
Thu Oct 15 15:10:08 2020 - [warning] shutdown_script is not defined.
Thu Oct 15 15:10:08 2020 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

8、启动MHA

#开启MHA,默认是前台运行,加上nohup执行完后台运行
nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null
#前台运行的状态如下 启动成功
[root@mha-manager ~]# masterha_manager --conf=/etc/mastermha/app1.cnf
Thu Oct 15 15:21:09 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Oct 15 15:21:09 2020 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Thu Oct 15 15:21:09 2020 - [info] Reading server configuration from /etc/mastermha/app1.cnf..

#查看状态,运行中
[root@mha-manager ~]# masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:3608) is running(0:PING_OK), master:10.0.0.8

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

1.环境准备
四台主机:四台centos7系统
pxc1:10.0.0.7
pxc2:10.0.0.17
pxc3:10.0.0.27
pxc4:10.0.0.37

2.安装Percona XtraDB Cluster 5.7

#建个仓库 此处使用清华大学yum源,官方源太慢了
[root@pxc1 ~]#vim /etc/yum.repos.d/pxc.repo
[percona]
name=percona_repo
baseurl =
https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch
enabled = 1
gpgcheck = 0
[root@pxc1 ~]# yum repolist

#拷贝到另外两个节点
[root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 10.0.0.17:/etc/yum.repos.d
[root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 10.0.0.27:/etc/yum.repos.d
在三个节点都安装好PXC 5.7 
[root@pxc1 ~]#yum install Percona-XtraDB-Cluster-57 -y
[root@pxc2 ~]#yum install Percona-XtraDB-Cluster-57 -y
[root@pxc3 ~]#yum install Percona-XtraDB-Cluster-57 -y

3.在各个节点上分别配置mysql及集群配置文件

#PXC的配置文件必须修改,先修改第一个节点的
[root@pxc1 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://10.0.0.7,10.0.0.17,10.0.0.27
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=10.0.0.7
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-1
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"

#修改另外两个节点配置文件
[root@pxc2 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://10.0.0.7,10.0.0.17,10.0.0.27
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2        
wsrep_node_address=10.0.0.17
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-2
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"   #取消本行注释

[root@pxc3 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so #核心库
wsrep_cluster_address=gcomm://10.0.0.7,10.0.0.17,10.0.0.27
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2  
wsrep_node_address=10.0.0.27
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-3
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"   #取消本行注释

4、启动PXC集群中第一个节点 和后续节点开启方法不一样

#启动第一个节点 和后续节点开启方法不一样
[root@pxc1 ~]#systemctl start mysql@bootstrap.service
#4567和3306端口打开
[root@pxc1 ~]#ss -ntul
Netid State     Recv-Q Send-Q     Local Address:Port                   Peer
Address:Port              
udp   UNCONN     0      0              127.0.0.1:323                            
  *:*  
udp   UNCONN     0      0                   ::1:323                            
  :::*  
tcp   LISTEN     0      128                   *:22                            
    *:*  
tcp   LISTEN     0      128                   *:4567                          
    *:*  
tcp   LISTEN     0      100            127.0.0.1:25                            
    *:*  
tcp   LISTEN     0      80                   :::3306                          
  :::*  
tcp   LISTEN     0      128                   :::22                            
  :::*  
tcp   LISTEN     0      100                 ::1:25                            
  :::*    
 
#查看root密码
[root@pxc1 ~]# grep "temporary password" /var/log/mysqld.log
2020-10-17T01:29:27.829098Z 1 [Note] A temporary password is generated for root@localhost: qc(/#nCr?1zk

[root@pxc1 ~]#mysql -uroot -p'=tWFP0oRJl8t'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.27-30-57-log
Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#修改root密码(已经不允许使用mysqladmin password修改密码了 不安全)
mysql> alter user 'root'@'localhost' identified by 'magedu';
Query OK, 0 rows affected (0.01 sec)
#创建相关用户并授权
mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 's3cretPass';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
Query OK, 0 rows affected (0.01 sec)
#查看有几个节点                                               mysql> show status like 'wsrep_cluster_size';
| Variable_name     | Value |
+--------------------+-------+
| wsrep_cluster_size | 1   |
+--------------------+-------+
                                                                               

5、启动PXC集群中其他所有节点

#后续节点直接启动即可
[root@pxc2 ~]#systemctl start mysql
[root@pxc3 ~]#systemctl start mysql
#4567和 3306端口打开
[root@pxc2 ~]#ss -ntulp

6、查看集群状态,验证集群是否成功

#在任意节点,查看集群状态 密码都是magedu
[root@pxc1 ~]#mysql -uroot -pmagedu
mysql> SHOW VARIABLES LIKE 'wsrep_node_name';
+-----------------+--------------------+
| Variable_name   | Value             |
+-----------------+--------------------+
| wsrep_node_name | pxc-cluster-node-1 |
+-----------------+--------------------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'wsrep_node_address';
+--------------------+----------+
| Variable_name     | Value   |
+--------------------+----------+
| wsrep_node_address | 10.0.0.7 |
+--------------------+----------+
1 row in set (0.01 sec)
mysql> SHOW VARIABLES LIKE 'wsrep_on';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_on     | ON   |
+---------------+-------+
1 row in set (0.00 sec)
mysql> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name     | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.01 sec)
#在任意节点查看数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql             |
| performance_schema |
| sys               |
+--------------------+
4 rows in set (0.00 sec)
#在任意节点创建数据库
mysql> create database testdb1;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql             |
| performance_schema |
| sys               |
| testdb1           |
+--------------------+
5 rows in set (0.00 sec)
mysql>
#在任意其它节点验证数据是否同步
[root@pxc2 ~]#mysql -uroot -pmagedu
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql             |
| performance_schema |
| sys               |
| testdb1           |
+--------------------+
5 rows in set (0.01 sec)

7 在PXC集群中加入节点

#在PXC集群中再加一台新的主机PXC4:10.0.0.37
[root@pxc4 ~]#yum install Percona-XtraDB-Cluster-57 -y
[root@pxc4 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[root@pxc4 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://10.0.0.7,10.0.0.17,10.0.0.27,10.0.0.37
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=10.0.0.37
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-4
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"
[root@pxc4 ~]#systemctl start mysql

[root@pxc4 ~]#mysql -uroot -pmagedu
Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30,
Revision
#加入之后变成4
mysql> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name     | Value |
+--------------------+-------+
| wsrep_cluster_size | 4     |
+--------------------+-------+ 1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql             |
| performance_schema |
| sys               |
| testdb1           |
| testdb2           |
| testdb3           |
+--------------------+ 8 rows in set (0.00 sec)
#将其它节点的配置文件加以修改
[root@@pxc1 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
wsrep_cluster_address=gcomm://10.0.0.7,10.0.0.17,10.0.0.27,10.0.0.37
#同上修改
[root@pxc2 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[root@pxc3 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf

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

[root@DESKTOP-NHT2EP5 ~]#ll mysql-8.0.19-linux-glibc2.12-x86_64.tar
---------- 1 root root 2651712000 Jul 30 16:48 mysql-8.0.19-linux-glibc2.12-x86_64.tar

[root@DESKTOP-NHT2EP5 ~]#cat my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql_error.log
pid-file=/data/mysql/mysql.pid
innodb_file_per_table = 1
server_id=10
log-bin
read_only=ON
relay-log=/data/mysql/relay-log
relay-log-index=/data/mysql/relay-log.index
[client]
port=3306
[root@DESKTOP-NHT2EP5 ~]#cat mysql.sh
#!/bin/bash
cp -f /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld && chkconfig --add mysqld
/usr/local/mysql/bin/mysqladmin -u root password "123.com"
[root@DESKTOP-NHT2EP5 ~]#cat mysql.yaml
---
- hosts: 100.0.0.10
remote_user: root
vars:
  pakgname: mysql-8.0.19-linux-glibc2.12-x86_64.tar
  linkname: mysql-8.0.19-linux-glibc2.12-x86_64
  datadir: /data/mysql/
tasks:
  - name: create mysql group
     group: name=mysql gid=306
  - name: create mysql user
     user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home={{ datadir }}
  - name: unzip mysqlzip
    unarchive: src=/root/{{ pakgname }} dest=/usr/local/ owner=mysql group=mysql
  - name: unzip directory soft link
    file: src=/usr/local/{{ linkname }} dest=/usr/local/mysql state=link owner=mysql group=mysql
  - name: create data directory
    file: path=/data/mysql state=directory owner=mysql group=mysql
  - name: copy my.cnf file
    copy: src=/root/my.cnf dest=/etc/my.cnf mode=644
  - name: create database bin soft link
      shell: echo PATH='/usr/local/mysql/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/root/bin' >> /etc/profile
    tags: bins
  - name: exec /etc/profile
    shell: source /etc/profile
    tags: exec
  - name: init data directory
    shell: /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --initialize-insecure --datadir={{ datadir }}
    tags: init
  - name: copy scripts
    copy: src=/root/mysql.sh dest=/root/ mode=755
  - name: start mysql
    service: name=mysqld state=started enabled=yes
    tags: sh_script
  - name: secure firm,append systemd
    script: chdir=/root mysql.sh
    tags: sh_mysql



posted @ 2020-10-18 20:43  肖豪  阅读(181)  评论(0编辑  收藏  举报