码迷,mamicode.com
首页 > 其他好文 > 详细

,如果主节点已经运行了一段时间,且有大量数据时,如何配置并启动slave节点

时间:2020-10-19 22:23:32      阅读:22      评论:0      收藏:0      [点我收藏+]

标签:usr   new   recover   mtu   ash   perm   back   ups   ansi   

1,如果主节点已经运行了一段时间,且有大量数据时,如何配置并启动slave节点
技术图片

#备份主节点数据库
[root@master ~] mysqldump -A -F --single-transaction --master-data=1 > /data/mariadb_backup_`date +%F_%T`.sql

[root@master ~] ll /data/
total 1496 -rw-r--r-- 1 root  root   478777 Oct 16 22:04 mariadb_backup_sql

#将备份拷贝到slave服务器
[root@master ~] scp /data/mariadb_backup_2020-10-18_16\:08\:08.sql 10.0.0.81:/data/
The authenticity of host ‘10.0.0.81 (10.0.0.81)‘ can‘t be established.
ECDSA key fingerprint is SHA256:pWYlWWDNZAyPanC0foRMoATFOGGBma8XydeNm0U1+CI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added ‘10.0.0.81‘ (ECDSA) to the list of known hosts.
root@10.0.0.81‘s password:
mariadb_backup_2020-10-18_16\:09\:08.sql                                                                                     100%  468KB  45.1MB/s   00:00
#优化主从节点服务器性能
MariaDB [(none)]> SET GLOBAL innodb_flush_log_at_trx_commit=2;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> SET GLOBAL sync_binlog= 0;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> SHOW VARIABLES LIKE ‘sync_binlog‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sync_binlog   | 0     |
+---------------+-------+
1 row in set (0.001 sec)
#将备份还原到从节点:
#安装数据库:
[root@slave1 ~] dnf -y install mariadb-server
#修改配置文件
[root@slave1 ~] vim /etc/my.cnf.d/mariadb-server.cnf
[mysql]
server-id=81
log-bin
read-only

[root@slave1 ~] systemctl restart mariadb.service
配置从节点从备份之后开始复制:
[root@slave1 ~] vim /data/mariadb_backup_2020-10-18_08\:12\:52.sql
CHANGE MASTER TO
  MASTER_HOST=‘10.0.0.81‘,
  MASTER_USER=‘abc‘,
  MASTER_PASSWORD=‘123456‘,
  MASTER_PORT=3306,
  MASTER_LOG_FILE=‘mariadb-bin.000002‘,
  MASTER_LOG_POS=375;

[root@slave1 ~] mysql < /data/mariadb_backup_2020-10-18_12:06\:11.sql

[root@slave1 ~] mysql
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: abc
                   Master_Port: 3306
                  Connect_Retry: 60
                 Master_Log_File: mariadb-bin.000004
              Read_Master_Log_Pos: 389
                 Relay_Log_File: mariadb-relay-bin.000002
                  Relay_Log_Pos: 557
             Relay_Master_Log_File: mariadb-bin.000002
                Slave_IO_Running: Yes 
               Slave_SQL_Running: Yes 
                 Replicate_Do_DB:
              Replicate_Ignore_DB:
               Replicate_Do_Table:
            Replicate_Ignore_Table:
         Replicate_Wild_Ignore_Table:
                    Last_Errno: 0
                    Last_Error:
                  Skip_Counter: 0
              Exec_Master_Log_Pos: 389
               Relay_Log_Space: 868
               Until_Condition: None
                Until_Log_File:
                 Until_Log_Pos: 0
            Master_SSL_Allowed: No
            Master_SSL_CA_File:
            Master_SSL_CA_Path:
               Master_SSL_Cert:
             Master_SSL_Cipher:
                Master_SSL_Key:
         S   econds_Behind_Master: 0
       Master_SSL_Verify_Server_Cert: No
                 Last_IO_Errno: 0
                 Last_IO_Error:
                Last_SQL_Errno: 0
                Last_SQL_Error:
       Replicate_Ignore_Server_Ids:
              Master_Server_Id: 8
                Master_SSL_Crl:
             Master_SSL_Crlpath:
                    Using_Gtid: No
                   Gtid_IO_Pos:
          Replicate_Do_Domain_Ids:
        Replicate_Ignore_Domain_Ids:
                 Parallel_Mode: conservative
                    SQL_Delay: 0
             SQL_Remaining_Delay: NULL
           Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
               Slave_DDL_Groups: 0
      Slave_Non_Transactional_Groups: 0
         Slave_Transactional_Groups: 0
1 row in set (0.000 sec)

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

技术图片

查询数据库节点最新的为新master
MariaDB [(none)]> show slave status\G

#修改配置文件并关闭read-only配置
[root@slave1 ~] vi /etc/my.cnf.d/mariadb-server.cnf
[mysql]
server-id=81
log-bin
read-only=off

[root@slave1 ~] systemctl restart mariadb.service

#清除旧节点信息
MariaDB [(none)]> set global read_only=off;
MariaDB [(none)]> stop slave;
MariaDB [(none)]> reset slave all;
#备份新master的信息
[root@slave1 ~] mysqldump -A -F --single-transaction --master-data=1> /data/mariadb_backup_`date +%F_%T`.sql
[root@slave1 ~] scp /data/mariadb_backup_2020-10-18_15\:32\:22.sql 10.0.0.82:/data
#其他所有slave还原数据库,指向新master:
[root@slave2 ~] vi /data/mariadb_backup_2020-10-18_11\:33\:25.sql

CHANGE MASTER TO
  MASTER_HOST=‘10.0.0.80‘,
  MASTER_USER=‘abc‘,
  MASTER_PASSWORD=‘123456‘,
  MASTER_PORT=3306,
  MASTER_LOG_FILE=‘mariadb-bin.000002‘,
  MASTER_LOG_POS=389;

MariaDB [(none)]> stop slave;
MariaDB [(none)]> reset slave all;
ariaDB [(none)]> set sql_log_bin=off;
MariaDB [(none)]> source /data/mariadb_backup_2020-10-17_11\:33\:25.sql
MariaDB [(none)]> set sql_log_bin=on;
MariaDB [(none)]> start slave;

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

#给管理端安装mha-manager和mha-node包
[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
#给mysql服务器节点安装mha-node包
[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

#在所有节点之间实现基于key验证
[root@mha-manager ~]#ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory ‘/root/.ssh‘.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:fTNTrKIR3DWJFNW7fUJsCGz/JuKE7QIgfiSjhWrB/Rc root@mha-manager
The key‘s randomart image is:
+---[RSA 2048]----+
|         oo++o   |
|       . .=..o.  |
|...     o..o oo. |
|.o=.o  E o  oo=  |
|.+.=..  Soo *+ o |
|o.. .....o+o.+= o|
|.  .  ...+ . o ..|
|        . o      |
|         .       |
+----[SHA256]-----+
[root@mha-manager ~]#rsync -av .ssh 10.0.0.8:
root@10.0.0.80‘s password: 
sending incremental file list
.ssh/
.ssh/authorized_keys
.ssh/id_rsa
.ssh/id_rsa.pub
.ssh/known_hosts

sent 4,007 bytes  received 96 bytes  1,641.20 bytes/sec
total size is 3,670  speedup is 0.89
[root@mha-manager ~]#rsync -av .ssh 10.0.0.18:
root@10.0.0.81‘s password: 
sending incremental file list
.ssh/
.ssh/authorized_keys
.ssh/id_rsa
.ssh/id_rsa.pub
.ssh/known_hosts

sent 4,007 bytes  received 96 bytes  1,641.20 bytes/sec
total size is 3,670  speedup is 0.89
[root@mha-manager ~]#rsync -av .ssh 10.0.0.28:
root@10.0.0.28‘s password: 
sending incremental file list
.ssh/
.ssh/authorized_keys
.ssh/id_rsa
.ssh/id_rsa.pub
.ssh/known_hosts

sent 4,007 bytes  received 96 bytes  1,641.20 bytes/sec
total size is 3,670  speedup is 0.89

#在管理节点建立配置文件
[root@mha-manager ~]#mkdir /etc/mastermha/
[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
master
[server3]
hostname=10.0.0.28
candidate_master=1

#相关脚本
[root@mha-manager ~]#vim /usr/local/bin/sendmail.sh
echo "MySQL is down" | mail -s "MHA Warning" 1098182702@qq.com
[root@mha-manager ~]#chmod +x /usr/local/bin/sendmail.sh
[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.2‘;
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

#配置master
[root@master ~]#mkdir /data/mysql
[root@master ~]#chown mysql.mysql /data/mysql/
[root@master ~]#vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=8
log-bin=/data/mysql/mysql-bin
skip_name_resolve=1
general_log

[root@master ~]#systemctl start mysqld.service 
[root@master ~]#mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21 Source distribution

Copyright (c) 2000, 2020, 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.

mysql> show master logs;
+------------------+-----------+-----------+
| Log_name         | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 |       179 | No        |
| mysql-bin.000002 |       156 | No        |
+------------------+-----------+-----------+
2 rows in set (0.00 sec)

mysql> create user repluser@‘10.0.0.%‘ identified by ‘magedu‘;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to repluser@‘10.0.0.%‘;
Query OK, 0 rows affected (0.00 sec)

mysql> create user mhauser@‘10.0.0.%‘ identified by ‘magedu‘;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on *.* to mhauser@‘10.0.0.%‘;
Query OK, 0 rows affected (0.00 sec)

[root@master ~]#ifconfig eth0:1 10.0.0.100/24

#配置slave1
[root@slave1 ~]#mkdir /data/mysql
[root@slave1 ~]#chown mysql.mysql /data/mysql/
[root@slave1 ~]#vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=18
log-bin=/data/mysql/mysql-bin
read-only
relay_log_purge=0
skip_name_resolve=1

[root@slave1 ~]#systemctl start mysqld.service 
[root@slave1 ~]#mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21 Source distribution

Copyright (c) 2000, 2020, 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.

mysql> CHANGE MASTER TO 
    -> MASTER_HOST=‘10.0.0.8‘, 
    -> MASTER_USER=‘repluser‘, 
    -> MASTER_PASSWORD=‘magedu‘, 
    -> MASTER_LOG_FILE=‘mysql-bin.000002‘, 
    -> MASTER_LOG_POS=156;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

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: mysql-bin.000002
          Read_Master_Log_Pos: 1201
               Relay_Log_File: slave1-relay-bin.000002
                Relay_Log_Pos: 1369
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1201
              Relay_Log_Space: 1579
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 8
                  Master_UUID: 00db05a9-1074-11eb-abdd-000c2980df3c
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

#配置slave2
[root@slave2 ~]#mkdir /data/mysql
[root@slave2 ~]#chown mysql.mysql /data/mysql
[root@slave2 ~]#vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=28
log-bin=/data/mysql/mysql-bin
read-only
relay_log_purge=0
skip_name_resolve=1

[root@slave2 ~]#systemctl start mysqld.service 
[root@slave2 ~]#mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21 Source distribution

Copyright (c) 2000, 2020, 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.

mysql> 
mysql> CHANGE MASTER TO 
    -> MASTER_HOST=‘10.0.0.8‘, 
    -> MASTER_USER=‘repluser‘, 
    -> MASTER_PASSWORD=‘magedu‘, 
    -> MASTER_LOG_FILE=‘mysql-bin.000002‘, 
    -> MASTER_LOG_POS=156;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

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: mysql-bin.000002
          Read_Master_Log_Pos: 1201
               Relay_Log_File: slave2-relay-bin.000002
                Relay_Log_Pos: 1369
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1201
              Relay_Log_Space: 1579
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 8
                  Master_UUID: 00db05a9-1074-11eb-abdd-000c2980df3c
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

#检查管理端环境
[root@mha-manager ~]#masterha_check_ssh --conf=/etc/mastermha/app1.cnf
Sat Oct 17 12:37:11 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Oct 17 12:37:11 2020 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Sat Oct 17 12:37:11 2020 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
Sat Oct 17 12:37:11 2020 - [info] Starting SSH connection tests..
Sat Oct 17 12:37:12 2020 - [debug] 
Sat Oct 17 12:37:11 2020 - [debug]  Connecting via SSH from root@10.0.0.8(10.0.0.8:22) to root@10.0.0.18(10.0.0.18:22)..
Sat Oct 17 12:37:11 2020 - [debug]   ok.
Sat Oct 17 12:37:11 2020 - [debug]  Connecting via SSH from root@10.0.0.8(10.0.0.8:22) to root@10.0.0.28(10.0.0.28:22)..
Sat Oct 17 12:37:11 2020 - [debug]   ok.
Sat Oct 17 12:37:12 2020 - [debug] 
Sat Oct 17 12:37:11 2020 - [debug]  Connecting via SSH from root@10.0.0.18(10.0.0.18:22) to root@10.0.0.8(10.0.0.8:22)..
Sat Oct 17 12:37:11 2020 - [debug]   ok.
Sat Oct 17 12:37:11 2020 - [debug]  Connecting via SSH from root@10.0.0.18(10.0.0.18:22) to root@10.0.0.28(10.0.0.28:22)..
Sat Oct 17 12:37:12 2020 - [debug]   ok.
Sat Oct 17 12:37:13 2020 - [debug] 
Sat Oct 17 12:37:12 2020 - [debug]  Connecting via SSH from root@10.0.0.28(10.0.0.28:22) to root@10.0.0.8(10.0.0.8:22)..
Sat Oct 17 12:37:12 2020 - [debug]   ok.
Sat Oct 17 12:37:12 2020 - [debug]  Connecting via SSH from root@10.0.0.28(10.0.0.28:22) to root@10.0.0.18(10.0.0.18:22)..
Sat Oct 17 12:37:12 2020 - [debug]   ok.
Sat Oct 17 12:37:13 2020 - [info] All SSH connection tests passed successfully.

[root@mha-manager ~]#masterha_check_repl --conf=/etc/mastermha/app1.cnf
Sat Oct 17 12:37:43 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Oct 17 12:37:43 2020 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Sat Oct 17 12:37:43 2020 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
Sat Oct 17 12:37:43 2020 - [info] MHA::MasterMonitor version 0.58.
Sat Oct 17 12:37:44 2020 - [info] GTID failover mode = 0
Sat Oct 17 12:37:44 2020 - [info] Dead Servers:
Sat Oct 17 12:37:44 2020 - [info] Alive Servers:
Sat Oct 17 12:37:44 2020 - [info]   10.0.0.8(10.0.0.8:3306)
Sat Oct 17 12:37:44 2020 - [info]   10.0.0.18(10.0.0.18:3306)
Sat Oct 17 12:37:44 2020 - [info]   10.0.0.28(10.0.0.28:3306)
Sat Oct 17 12:37:44 2020 - [info] Alive Slaves:
Sat Oct 17 12:37:44 2020 - [info]   10.0.0.18(10.0.0.18:3306)  Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Sat Oct 17 12:37:44 2020 - [info]     Replicating from 10.0.0.8(10.0.0.8:3306)
Sat Oct 17 12:37:44 2020 - [info]   10.0.0.28(10.0.0.28:3306)  Version=8.0.21 (oldest major version between slaves) log-bin:enabled
Sat Oct 17 12:37:44 2020 - [info]     Replicating from 10.0.0.8(10.0.0.8:3306)
Sat Oct 17 12:37:44 2020 - [info]     Primary candidate for the new Master (candidate_master is set)
Sat Oct 17 12:37:44 2020 - [info] Current Alive Master: 10.0.0.8(10.0.0.8:3306)
Sat Oct 17 12:37:44 2020 - [info] Checking slave configurations..
Sat Oct 17 12:37:44 2020 - [info] Checking replication filtering settings..
Sat Oct 17 12:37:44 2020 - [info]  binlog_do_db= , binlog_ignore_db= 
Sat Oct 17 12:37:44 2020 - [info]  Replication filtering check ok.
Sat Oct 17 12:37:44 2020 - [info] GTID (with auto-pos) is not supported
Sat Oct 17 12:37:44 2020 - [info] Starting SSH connection tests..
Sat Oct 17 12:37:47 2020 - [info] All SSH connection tests passed successfully.
Sat Oct 17 12:37:47 2020 - [info] Checking MHA Node version..
Sat Oct 17 12:37:47 2020 - [info]  Version check ok.
Sat Oct 17 12:37:47 2020 - [info] Checking SSH publickey authentication settings on the current master..
Sat Oct 17 12:37:47 2020 - [info] HealthCheck: SSH to 10.0.0.8 is reachable.
Sat Oct 17 12:37:47 2020 - [info] Master MHA Node version is 0.58.
Sat Oct 17 12:37:47 2020 - [info] Checking recovery script configurations on 10.0.0.8(10.0.0.8:3306)..
Sat Oct 17 12:37:47 2020 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/mysql/ --output_file=/data/mastermha/app1//save_binary_logs_test --manager_version=0.58 --start_file=mysql-bin.000002 
Sat Oct 17 12:37:47 2020 - [info]   Connecting to root@10.0.0.8(10.0.0.8:22).. 
  Creating /data/mastermha/app1 if not exists.. Creating directory /data/mastermha/app1.. done.
   ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /data/mysql/, up to mysql-bin.000002
Sat Oct 17 12:37:48 2020 - [info] Binlog setting check done.
Sat Oct 17 12:37:48 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Sat Oct 17 12:37:48 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user=‘mhauser‘ --slave_host=10.0.0.18 --slave_ip=10.0.0.18 --slave_port=3306 --workdir=/data/mastermha/app1/ --target_version=8.0.21 --manager_version=0.58 --relay_dir=/var/lib/mysql --current_relay_log=slave1-relay-bin.000002  --slave_pass=xxx
Sat Oct 17 12:37:48 2020 - [info]   Connecting to root@10.0.0.18(10.0.0.18:22).. 
Creating directory /data/mastermha/app1/.. done.
  Checking slave recovery environment settings..
    Relay log found at /var/lib/mysql, up to slave1-relay-bin.000002
    Temporary relay log file is /var/lib/mysql/slave1-relay-bin.000002
    Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
    Testing mysql connection and privileges..
mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Sat Oct 17 12:37:48 2020 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user=‘mhauser‘ --slave_host=10.0.0.28 --slave_ip=10.0.0.28 --slave_port=3306 --workdir=/data/mastermha/app1/ --target_version=8.0.21 --manager_version=0.58 --relay_dir=/var/lib/mysql --current_relay_log=slave2-relay-bin.000002  --slave_pass=xxx
Sat Oct 17 12:37:48 2020 - [info]   Connecting to root@10.0.0.28(10.0.0.28:22).. 
Creating directory /data/mastermha/app1/.. done.
  Checking slave recovery environment settings..
    Relay log found at /var/lib/mysql, up to slave2-relay-bin.000002
    Temporary relay log file is /var/lib/mysql/slave2-relay-bin.000002
    Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
    Testing mysql connection and privileges..
mysql: [Warning] Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Sat Oct 17 12:37:48 2020 - [info] Slaves settings check done.
Sat Oct 17 12:37:48 2020 - [info] 
10.0.0.8(10.0.0.8:3306) (current master)
 +--10.0.0.18(10.0.0.18:3306)
 +--10.0.0.28(10.0.0.28:3306)

Sat Oct 17 12:37:48 2020 - [info] Checking replication health on 10.0.0.18..
Sat Oct 17 12:37:48 2020 - [info]  ok.
Sat Oct 17 12:37:48 2020 - [info] Checking replication health on 10.0.0.28..
Sat Oct 17 12:37:48 2020 - [info]  ok.
Sat Oct 17 12:37:48 2020 - [info] Checking master_ip_failover_script status:
Sat Oct 17 12:37:48 2020 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=10.0.0.8 --orig_master_ip=10.0.0.8 --orig_master_port=3306 

IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1 10.0.0.100/24;/sbin/arping -I
eth0 -c 3 -s 10.0.0.100/24 10.0.0.2 >/dev/null 2>&1===

Checking the Status of the script.. OK 
/sbin/arping: option requires an argument -- ‘I‘
Usage: arping [-fqbDUAV] [-c count] [-w timeout] [-I device] [-s source] destination
  -f : quit on first reply
  -q : be quiet
  -b : keep broadcasting, don‘t go unicast
  -D : duplicate address detection mode
  -U : Unsolicited ARP mode, update your neighbours
  -A : ARP answer mode, update your neighbours
  -V : print version and exit
  -c count : how many packets to send
  -w timeout : how long to wait for a reply
  -I device : which ethernet device to use
  -s source : source ip address
  destination : ask for what ip address
Sat Oct 17 12:37:48 2020 - [info]  OK.
Sat Oct 17 12:37:48 2020 - [warning] shutdown_script is not defined.
Sat Oct 17 12:37:48 2020 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

[root@mha-manager ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 is stopped(2:NOT_RUNNING).

#启动mha
[root@mha-manager ~]#masterha_manager --conf=/etc/mastermha/app1.cnf
Sat Oct 17 12:39:09 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Oct 17 12:39:09 2020 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Sat Oct 17 12:39:09 2020 - [info] Reading server configuration from /etc/mastermha/app1.cnf..

[root@master ~]#tail -f /var/lib/mysql/master.log 
2020-10-17T12:41:33.799439Z    15 Query SELECT 1 As Value
2020-10-17T12:41:34.800094Z    15 Query SELECT 1 As Value
2020-10-17T12:41:35.800754Z    15 Query SELECT 1 As Value
2020-10-17T12:41:36.802357Z    15 Query SELECT 1 As Value
2020-10-17T12:41:37.802221Z    15 Query SELECT 1 As Value
2020-10-17T12:41:38.802487Z    15 Query SELECT 1 As Value
2020-10-17T12:41:39.803444Z    15 Query SELECT 1 As Value
2020-10-17T12:41:40.804324Z    15 Query SELECT 1 As Value

[root@mha-manager ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:12292) is running(0:PING_OK), master:10.0.0.8

#模拟故障
[root@mha-manager ~]#masterha_manager --conf=/etc/mastermha/app1.cnf
Sat Oct 17 12:39:09 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Oct 17 12:39:09 2020 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Sat Oct 17 12:39:09 2020 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
Sat Oct 17 12:44:19 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Oct 17 12:44:19 2020 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Sat Oct 17 12:44:19 2020 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
[root@mha-manager ~]#

[root@mha-manager ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 is stopped(2:NOT_RUNNING).

[root@slave2 ~]# 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:5c:a0:c5 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.28/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
    inet6 fe80::20c:29ff:fe5c:a0c5/64 scope link 
       valid_lft forever preferred_lft forever

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

#配置pxc清华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 ~]#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/
[root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 10.0.0.37:/etc/yum.repos.d/

[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
[root@pxc4 ~]#yum install Percona-XtraDB-Cluster-57 -y

#配置各个节点mysql和集群配置文件
[root@pxc1 ~]#vim  /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
[mysqld]
server-id=7
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7

[root@pxc2 ~]#vim  /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
[mysqld]
server-id=17
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7

[root@pxc3 ~]#vim  /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
[mysqld]
server-id=27
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7

[root@pxc4 ~]#vim  /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
[mysqld]
server-id=37
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin
log_slave_updates
expire_logs_days=7

[root@pxc1 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[root@pxc1 ~]#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
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
[root@pxc2 ~]#
[root@pxc2 ~]#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
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
[root@pxc3 ~]#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
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"

#启动PXC集群中第一个节点
[root@pxc1 ~]#systemctl start mysql@bootstrap.service
[root@pxc1 ~]#ss -ntl
State      Recv-Q Send-Q Local Address:Port                Peer Address:Port              
LISTEN     0      128                *:22                             *:*                  
LISTEN     0      128                *:4567                           *:*                  
LISTEN     0      100        127.0.0.1:25                             *:*                  
LISTEN     0      128             [::]:22                          [::]:*                  
LISTEN     0      100            [::1]:25                          [::]:*                  
LISTEN     0      80              [::]:3306                        [::]:* 

[root@pxc1 ~]#grep "temporary password" /var/log/mysqld.log
2020-10-17T06:59:53.765387Z 1 [Note] A temporary password is generated for root@localhost: Vkyf+nVci9wW
[root@pxc1 ~]#mysql -uroot -pVkyf+nVci9wW
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 11
Server version: 5.7.31-34-57-log

Copyright (c) 2009-2020 Percona LLC and/or its affiliates
Copyright (c) 2000, 2020, 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.

mysql> alter user root@‘localhost‘ identified by ‘magedu‘;
Query OK, 0 rows affected (0.00 sec)

mysql> grant reload,lock tables,process,replication client on *.* to sstuser@‘localhost‘ identified by ‘s3cretPass‘;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show variables like ‘wsrep%‘\G
*************************** 1. row ***************************
Variable_name: wsrep_OSU_method
        Value: TOI
*************************** 2. row ***************************
Variable_name: wsrep_RSU_commit_timeout
        Value: 5000
*************************** 3. row ***************************
Variable_name: wsrep_auto_increment_control
        Value: ON
*************************** 4. row ***************************
Variable_name: wsrep_causal_reads
        Value: OFF
*************************** 5. row ***************************
Variable_name: wsrep_certification_rules
        Value: strict
*************************** 6. row ***************************
Variable_name: wsrep_certify_nonPK
        Value: ON
*************************** 7. row ***************************
Variable_name: wsrep_cluster_address
        Value: gcomm://10.0.0.7,10.0.0.17,10.0.0.27
*************************** 8. row ***************************
Variable_name: wsrep_cluster_name
        Value: pxc-cluster
*************************** 9. row ***************************
Variable_name: wsrep_convert_LOCK_to_trx
        Value: OFF
*************************** 10. row ***************************
Variable_name: wsrep_data_home_dir
        Value: /var/lib/mysql/
*************************** 11. row ***************************
Variable_name: wsrep_dbug_option
        Value: 
*************************** 12. row ***************************
Variable_name: wsrep_debug
        Value: OFF
*************************** 13. row ***************************
Variable_name: wsrep_desync
        Value: OFF
*************************** 14. row ***************************
Variable_name: wsrep_dirty_reads
        Value: OFF
*************************** 15. row ***************************
Variable_name: wsrep_drupal_282555_workaround
        Value: OFF
*************************** 16. row ***************************
Variable_name: wsrep_forced_binlog_format
        Value: NONE
*************************** 17. row ***************************
Variable_name: wsrep_load_data_splitting
        Value: ON
*************************** 18. row ***************************
Variable_name: wsrep_log_conflicts
        Value: ON
*************************** 19. row ***************************
Variable_name: wsrep_max_ws_rows
        Value: 0
*************************** 20. row ***************************
Variable_name: wsrep_max_ws_size
        Value: 2147483647
*************************** 21. row ***************************
Variable_name: wsrep_node_address
        Value: 10.0.0.7
*************************** 22. row ***************************
Variable_name: wsrep_node_incoming_address
        Value: AUTO
*************************** 23. row ***************************
Variable_name: wsrep_node_name
        Value: pxc-cluster-node-1
*************************** 24. row ***************************
Variable_name: wsrep_notify_cmd
        Value: 
*************************** 25. row ***************************
Variable_name: wsrep_on
        Value: ON
*************************** 26. row ***************************
Variable_name: wsrep_preordered
        Value: OFF
*************************** 27. row ***************************
Variable_name: wsrep_provider
        Value: /usr/lib64/galera3/libgalera_smm.so
*************************** 28. row ***************************
Variable_name: wsrep_provider_options
        Value: base_dir = /var/lib/mysql/; base_host = 10.0.0.7; base_port = 4567; cert.log_conflicts = no; cert.optimistic_pa = yes; debug = no; evs.auto_evict = 0; evs.causal_keepalive_period = PT1S; evs.debug_log_mask = 0x1; evs.delay_margin = PT1S; evs.delayed_keep_period = PT30S; evs.inactive_check_period = PT0.5S; evs.inactive_timeout = PT15S; evs.info_log_mask = 0; evs.install_timeout = PT7.5S; evs.join_retrans_period = PT1S; evs.keepalive_period = PT1S; evs.max_install_timeouts = 3; evs.send_window = 10; evs.stats_report_period = PT1M; evs.suspect_timeout = PT5S; evs.use_aggregate = true; evs.user_send_window = 4; evs.version = 0; evs.view_forget_timeout = P1D; gcache.dir = /var/lib/mysql/; gcache.freeze_purge_at_seqno = -1; gcache.keep_pages_count = 0; gcache.keep_pages_size = 0; gcache.mem_size = 0; gcache.name = /var/lib/mysql//galera.cache; gcache.page_size = 128M; gcache.recover = no; gcache.size = 128M; gcomm.thread_prio = ; gcs.fc_debug = 0; gcs.fc_factor = 1; gcs.fc_limit = 100; gcs.fc_master_slave = no; gcs
*************************** 29. row ***************************
Variable_name: wsrep_recover
        Value: OFF
*************************** 30. row ***************************
Variable_name: wsrep_reject_queries
        Value: NONE
*************************** 31. row ***************************
Variable_name: wsrep_replicate_myisam
        Value: OFF
*************************** 32. row ***************************
Variable_name: wsrep_restart_slave
        Value: OFF
*************************** 33. row ***************************
Variable_name: wsrep_retry_autocommit
        Value: 1
*************************** 34. row ***************************
Variable_name: wsrep_slave_FK_checks
        Value: ON
*************************** 35. row ***************************
Variable_name: wsrep_slave_UK_checks
        Value: OFF
*************************** 36. row ***************************
Variable_name: wsrep_slave_threads
        Value: 8
*************************** 37. row ***************************
Variable_name: wsrep_sst_auth
        Value: ********
*************************** 38. row ***************************
Variable_name: wsrep_sst_donor
        Value: 
*************************** 39. row ***************************
Variable_name: wsrep_sst_donor_rejects_queries
        Value: OFF
*************************** 40. row ***************************
Variable_name: wsrep_sst_method
        Value: xtrabackup-v2
*************************** 41. row ***************************
Variable_name: wsrep_sst_receive_address
        Value: AUTO
*************************** 42. row ***************************
Variable_name: wsrep_start_position
        Value: 00000000-0000-0000-0000-000000000000:-1
*************************** 43. row ***************************
Variable_name: wsrep_sync_wait
        Value: 0
43 rows in set (0.00 sec)

mysql> show status like ‘wsrep%‘;
+----------------------------------+--------------------------------------+
| Variable_name                    | Value                                |
+----------------------------------+--------------------------------------+
| wsrep_local_state_uuid           | 6085b686-1046-11eb-ac84-631f2cc73305 |
| wsrep_protocol_version           | 9                                    |
| wsrep_last_applied               | 3                                    |
| wsrep_last_committed             | 3                                    |
| wsrep_replicated                 | 3                                    |
| wsrep_replicated_bytes           | 816                                  |
| wsrep_repl_keys                  | 3                                    |
| wsrep_repl_keys_bytes            | 96                                   |
| wsrep_repl_data_bytes            | 521                                  |
| wsrep_repl_other_bytes           | 0                                    |
| wsrep_received                   | 2                                    |
| wsrep_received_bytes             | 150                                  |
| wsrep_local_commits              | 0                                    |
| wsrep_local_cert_failures        | 0                                    |
| wsrep_local_replays              | 0                                    |
| wsrep_local_send_queue           | 0                                    |
| wsrep_local_send_queue_max       | 1                                    |
| wsrep_local_send_queue_min       | 0                                    |
| wsrep_local_send_queue_avg       | 0.000000                             |
| wsrep_local_recv_queue           | 0                                    |
| wsrep_local_recv_queue_max       | 2                                    |
| wsrep_local_recv_queue_min       | 0                                    |
| wsrep_local_recv_queue_avg       | 0.500000                             |
| wsrep_local_cached_downto        | 1                                    |
| wsrep_flow_control_paused_ns     | 0                                    |
| wsrep_flow_control_paused        | 0.000000                             |
| wsrep_flow_control_sent          | 0                                    |
| wsrep_flow_control_recv          | 0                                    |
| wsrep_flow_control_interval      | [ 100, 100 ]                         |
| wsrep_flow_control_interval_low  | 100                                  |
| wsrep_flow_control_interval_high | 100                                  |
| wsrep_flow_control_status        | OFF                                  |
| wsrep_cert_deps_distance         | 1.000000                             |
| wsrep_apply_oooe                 | 0.000000                             |
| wsrep_apply_oool                 | 0.000000                             |
| wsrep_apply_window               | 1.000000                             |
| wsrep_commit_oooe                | 0.000000                             |
| wsrep_commit_oool                | 0.000000                             |
| wsrep_commit_window              | 1.000000                             |
| wsrep_local_state                | 4                                    |
| wsrep_local_state_comment        | Synced                               |
| wsrep_cert_index_size            | 1                                    |
| wsrep_cert_bucket_count          | 22                                   |
| wsrep_gcache_pool_size           | 2256                                 |
| wsrep_causal_reads               | 0                                    |
| wsrep_cert_interval              | 0.000000                             |
| wsrep_open_transactions          | 0                                    |
| wsrep_open_connections           | 0                                    |
| wsrep_ist_receive_status         |                                      |
| wsrep_ist_receive_seqno_start    | 0                                    |
| wsrep_ist_receive_seqno_current  | 0                                    |
| wsrep_ist_receive_seqno_end      | 0                                    |
| wsrep_incoming_addresses         | 10.0.0.7:3306                        |
| wsrep_cluster_weight             | 1                                    |
| wsrep_desync_count               | 0                                    |
| wsrep_evs_delayed                |                                      |
| wsrep_evs_evict_list             |                                      |
| wsrep_evs_repl_latency           | 0/0/0/0/0                            |
| wsrep_evs_state                  | OPERATIONAL                          |
| wsrep_gcomm_uuid                 | 60853201-1046-11eb-b428-d757c0543d63 |
| wsrep_cluster_conf_id            | 1                                    |
| wsrep_cluster_size               | 1                                    |
| wsrep_cluster_state_uuid         | 6085b686-1046-11eb-ac84-631f2cc73305 |
| wsrep_cluster_status             | Primary                              |
| wsrep_connected                  | ON                                   |
| wsrep_local_bf_aborts            | 0                                    |
| wsrep_local_index                | 0                                    |
| wsrep_provider_name              | Galera                               |
| wsrep_provider_vendor            | Codership Oy <info@codership.com>    |
| wsrep_provider_version           | 3.45(ra60e019)                       |
| wsrep_ready                      | ON                                   |
+----------------------------------+--------------------------------------+
71 rows in set (0.00 sec)

#启动PXC集群中其它所有节点
[root@pxc2 ~]#systemctl start mysql
[root@pxc2 ~]#ss -ntl
State       Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
LISTEN      0      128                     *:22                                  *:*                  
LISTEN      0      128                     *:4567                                *:*                  
LISTEN      0      100             127.0.0.1:25                                  *:*                  
LISTEN      0      80                   [::]:3306                             [::]:*                  
LISTEN      0      128                  [::]:22                               [::]:*                  
LISTEN      0      100                 [::1]:25                               [::]:*

[root@pxc3 ~]#systemctl start mysql
[root@pxc3 ~]#ss -ntl
State       Recv-Q Send-Q      Local Address:Port                     Peer Address:Port              
LISTEN      0      128                     *:22                                  *:*                  
LISTEN      0      128                     *:4567                                *:*                  
LISTEN      0      100             127.0.0.1:25                                  *:*                  
LISTEN      0      80                   [::]:3306                             [::]:*                  
LISTEN      0      128                  [::]:22                               [::]:*                  
LISTEN      0      100                 [::1]:25                               [::]:*

#查看集群状态,验证集群是否成功
[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.00 sec)

mysql> show variables like ‘wsrep_on‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_on      | ON    |
+---------------+-------+
1 row in set (0.01 sec)

mysql> show status like ‘wsrep_cluster_size‘;
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.00 sec)

[root@pxc2 ~]#mysql -uroot -pmagedu
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 14
Server version: 5.7.31-34-57-log Percona XtraDB Cluster (GPL), Release rel34, Revision 7359e4f, WSREP version 31.45, wsrep_31.45

Copyright (c) 2009-2020 Percona LLC and/or its affiliates
Copyright (c) 2000, 2020, 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.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

[root@pxc3 ~]#mysql -uroot -pmagedu
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 11
Server version: 5.7.31-34-57-log Percona XtraDB Cluster (GPL), Release rel34, Revision 7359e4f, WSREP version 31.45, wsrep_31.45

Copyright (c) 2009-2020 Percona LLC and/or its affiliates
Copyright (c) 2000, 2020, 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.

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

[root@pxc1 ~]#mysql -uroot -pmagedu
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 16
Server version: 5.7.31-34-57-log Percona XtraDB Cluster (GPL), Release rel34, Revision 7359e4f, WSREP version 31.45, wsrep_31.45

Copyright (c) 2009-2020 Percona LLC and/or its affiliates
Copyright (c) 2000, 2020, 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.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

##在PXC集群中再加一台新的主机PXC4:10.0.0.37
[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
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 11
Server version: 5.7.31-34-57-log Percona XtraDB Cluster (GPL), Release rel34, Revision 7359e4f, WSREP version 31.45, wsrep_31.45

Copyright (c) 2009-2020 Percona LLC and/or its affiliates
Copyright (c) 2000, 2020, 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.

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 |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

#修改其他节点的配置文件
[root@pxc1 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[root@pxc1 ~]#grep -E "^wsrep_cluster_address=gcomm:" /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 ~]#grep -E "^wsrep_cluster_address=gcomm:" /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@pxc3 ~]#grep -E "^wsrep_cluster_address=gcomm:" /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

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

#安装ansible并配置
[root@ansible ~]#yum -y install ansible
[root@ansible ~]#vim /etc/ansible/hosts
[dbservers]
10.0.0.18

[root@ansible ~]#ansible dbservers --list-hosts
  hosts (1):
    10.0.0.18

[root@ansible ~]#ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LqM4c9HgY483iuh1Qrvit92VT9c17QuRh96phKjLKmk root@ansible
The key‘s randomart image is:
+---[RSA 3072]----+
|                 |
|                 |
|                 |
|    .         o .|
|   o o  S    + oo|
|  . * ..  o o =.+|
|   =.*o .+ o = +.|
| o+EB.*oo o o o .|
|+o**==.*.  . . . |
+----[SHA256]-----+
[root@ansible ~]#
[root@ansible ~]#ssh-copy-id 10.0.0.18:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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@10.0.0.18‘s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh ‘10.0.0.18‘"
and check to make sure that only the key(s) you wanted were added.

#创建二进制安装所需的文件
[root@ansible ~]#mkdir -p /data/ansible/files
[root@ansible ~]#ll /data/ansible/files/
total 473708
-rw-r--r-- 1 root root 485074552 Jul 30 16:48 mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz

[root@ansible ~]#vim /data/ansible/files/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

[client]
port=3306
socket=/tmp/mysql.sock

[mysqld_safe]

vim /data/ansible/files/secure_mysql.sh
#!/bin/bash
#!/bin/bash
passwd=`grep "temporary password" /data/mysql/mysql.log|sed -nr ‘s/^.*\: (.*)$/\1/p‘`
mysqladmin -uroot -p`echo $passwd` password magedu
expect <<EOF
spawn /usr/local/mysql/bin/mysql_secure_installation
expect {
    "Enter password for user root:" {send magedu\n;exp_continue}
    "Press y|Y for Yes, any other key for No:" {send n\n;exp_continue}
    "Press y|Y for Yes, any other key for No" {send y\n;exp_continue}
    "Press y|Y for Yes, any other key for No" {send y\n;exp_continue}
    "Press y|Y for Yes, any other key for No" {send y\n;exp_continue}
    "Press y|Y for Yes, any other key for No" {send y\n;exp_continue}
}
expect eof
EOF

[root@ansible ~]#tree /data/ansible/files/
/data/ansible/files/
├── my.cnf
├── mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
└── secure_mysql.sh

0 directories, 3 files

[root@ansible ~]#vim /data/ansible/install_mysql.yml
---
#insatll mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
- hosts: dbservers
  remote_user: root
  gather_facts: no

  tasks:
    - name: istall packages
      yum: name=mysql,libaio,perl-Data-Dumper,perl-Getopt-Long,expect,ncurses-compat-libs
    - 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=/data/mysql
    - name: copy tar to remote host and file mode
      unarchive: src=/data/ansible/files/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz dest=/usr/local/ owner=root group=root
    - name: create linkfile /usr/local/mysql
      file: src=/usr/local/mysql-8.0.19-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
    - name: PATH variable
      shell: echo ‘PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh;source /etc/profile.d/mysql.sh
    - name: config my.cnf
      copy: src=/data/ansible/files/my.cnf  dest=/etc/my.cnf
    - name: data dir
      shell: mysqld --initialize --user=mysql --datadir=/data/mysql
      tags: data
    - name: service script
      shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    - name: enable service
      shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on
      tags: service
    - name: secure script
      script: /data/ansible/files/secure_mysql.sh
      tags: script

#执行mysql.yml
[root@ansible ~]#ansible-playbook /data/ansible/install_mysql.yml

PLAY [dbservers] ***************************************************************************

TASK [istall packages] *********************************************************************
changed: [10.0.0.18]

TASK [create mysql group] ******************************************************************
changed: [10.0.0.18]

TASK [create mysql user] *******************************************************************
changed: [10.0.0.18]

TASK [copy tar to remote host and file mode] ***********************************************
changed: [10.0.0.18]

TASK [create linkfile /usr/local/mysql] ****************************************************
changed: [10.0.0.18]

TASK [PATH variable] ***********************************************************************
changed: [10.0.0.18]

TASK [config my.cnf] ***********************************************************************
changed: [10.0.0.18]

TASK [data dir] ****************************************************************************
changed: [10.0.0.18]

TASK [service script] **********************************************************************
changed: [10.0.0.18]

TASK [enable service] **********************************************************************
changed: [10.0.0.18]

TASK [secure script] ***********************************************************************
changed: [10.0.0.18]

PLAY RECAP *********************************************************************************
10.0.0.18                  : ok=11   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

#验证被管理端结果
[root@dbserver ~]#mysql -uroot -pmagedu
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 11
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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.

mysql>

,如果主节点已经运行了一段时间,且有大量数据时,如何配置并启动slave节点

标签:usr   new   recover   mtu   ash   perm   back   ups   ansi   

原文地址:https://blog.51cto.com/13887323/2542400

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!