2020-10-17

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

主节点数据

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| db3                |
| db4                |
| db5                |
| db6                |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
11 rows in set (0.02 sec)

(1)在主节点对数据库进行完全备份,并拷贝到新的从节点。

[21:11:32 root@centos7 ~]#mysqldump -A -F --single-transaction --master-data=1 > /backup/fullbackup_`date +%F_%T`.sql
[21:11:44 root@centos7 ~]#ll /backup/
total 5636
-rw-r--r--. 1 root root 5768510 Oct 16 21:11 fullbackup_2020-10-16_21:11:44.sql
[21:11:47 root@centos7 ~]#scp /backup/fullbackup_2020-10-16_21\:11\:44.sql 10.0.0.51:/backup/

(2)在从节点配置并启动slave节点

[21:14:35 root@centos7 ~]#yum -y install mariadb
[21:16:55 root@centos7 ~]#vi /etc/my.cnf
[mysqld]
server-id=51
log-bin
read-only
[21:53:06 root@centos7 ~]#systemctl start mariadb
[21:57:03 root@centos7 ~]#vim  /backup/fullbackup_2020-10-16_21\:11\:44.sql
CHANGE MASTER TO
 MASTER_HOST='10.0.0.7',
 MASTER_USER='repluser',
MASTER_PASSWORD='123',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000005', MASTER_LOG_POS=245;
[21:53:22 root@centos7 ~]#mysql < /backup/fullbackup_2020-10-16_21\:11\:44.sql
MariaDB [(none)]> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.7
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000005
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000003
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000005
             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: 245
              Relay_Log_Space: 827
              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: 7
1 row in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| db3                |
| db4                |
| db5                |
| db6                |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
11 rows in set (0.00 sec)

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

(1)找到哪个从节点的数据库是最新,让它成为新master

[22:11:10 root@centos7 ~]#cat /var/lib/mysql/relay-log.info 
./mariadb-relay-bin.000003
531
mariadb-bin.000005
245

(2)新master修改配置文件,关闭read-only配置

[22:16:37 root@centos7 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=51
read-only=off

(3)清除旧的master复制信息

MariaDB [hellodb]>set global read_only=off;
MariaDB [hellodb]>stop slave;
MariaDB [hellodb]>reset slave all;

(4)在新master上完全备份,并拷贝到从节点

[22:16:37 root@centos7 ~]#mysqldump -A -F --single-transaction --master-data=1 > /backup/fullbackup_`date +%F_%T`.sql
[22:29:25 root@centos7 ~]#scp /backup/fullbackup_2020-10-16_22:28:37.sql 10.0.0.52:/backup/

(5)在从节点重新还原数据库,指向新的master

MariaDB [hellodb]>stop slave;
MariaDB [hellodb]>reset slave all;
[22:30:01 root@centos7 ~]#vi /backup/fullbackup_2020-10-16_22\:28\:37.sql 
CHANGE MASTER TO
MASTER_HOST='10.0.0.51',
MASTER_USER='repluser',
MASTER_PASSWORD='123',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=245;
[22:33:50 root@centos7 ~]#mysql < /backup/fullbackup_2020-10-16_22\:28\:37.sql
MariaDB [hellodb]>start slave;
MariaDB [hellodb]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.51
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000002
          Read_Master_Log_Pos: 407
               Relay_Log_File: mariadb-relay-bin.000003
                Relay_Log_Pos: 531
        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_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 407
              Relay_Log_Space: 1275
              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: 51
1 row in set (0.00 sec)

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

环境:四台主机

10.0.0.7  CentOS7 MHA管理端
10.0.0.51 CentOS8 Master
10.0.0.52 CentOS8 Slave1
10.0.0.53 CentOS8 Slave2

(1)在MHA管理端安装mha4mysql-manager和mha4mysql-node ,在所有MySQL服务器上安装mha4mysql-node包

[root@mha-manager ~]#yum -y install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
[root@mha-manager ~]#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
[root@mha-manager ~]#rsync -a .ssh 10.0.0.51:/root/
[root@mha-manager ~]#rsync -a .ssh 10.0.0.52:/root/
[root@mha-manager ~]#rsync -a .ssh 10.0.0.53:/root/

(4)在管理节点建立配置文件

[root@mha-manager ~]#vim /etc/mastermha/app1.cnf
[server default]
user=mhauser       		
password=123
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.51
candidate_master=1    
[server2]
hostname=10.0.0.52
candidate_master=1  
[server3]
hostname=10.0.0.53

(5)相关脚本

[15:59:14 root@centos7 ~]#cat /usr/local/bin/sendmail.sh 
echo "MySQL is down" | mail -s "MHA Warning" rc1255090639@gmail.com
[15:58:27 root@centos7 ~]#chmod +x /usr/local/bin/sendmail.sh
[16:17:38 root@centos7 ~]#cat /usr/local/bin/master_ip_failover 
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
my $vip = '10.0.0.100/24';
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";
}
[16:17:42 root@centos7 ~]#chmod +x /usr/local/bin/master_ip_failover

(6)实现Master

[root@master ~]#vim /etc/my.cnf
[mysqld]
server_id=51
log-bin
skip_name_resolve=1
general_log 
 
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |      8275 |
| mariadb-bin.000002 |       507 |
| mariadb-bin.000003 |       507 |
| mariadb-bin.000004 |       264 |
| mariadb-bin.000005 |       245 |
+--------------------+-----------+
5 rows in set (0.00 sec)
mysql>grant replication slave on *.* to repluser@'10.0.0.%' identified by '123';
mysql>grant all on *.* to mhauser@'10.0.0.%' identified by '123';

(7)实现slave

[root@slave ~]#vim /etc/my.cnf
[mysqld]
server_id=52/53
log-bin
read_only
relay_log_purge=0
skip_name_resolve=1 

mysql>CHANGE MASTER TO 
MASTER_HOST='10.0.0.51', 
MASTER_USER='repluser',
MASTER_PASSWORD='123', 
MASTER_LOG_FILE='mariadb-bin.000005',
MASTER_LOG_POS=245;

mysql>START SLAVE;

(8)检查MHA的环境

[17:07:43 root@centos7 ~]#masterha_check_ssh --conf=/etc/mastermha/app1.cnf
Sun Oct 18 17:09:12 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Oct 18 17:09:12 2020 - [info] Reading application default configuration from /etc/mastermha/app1.cnf..
Sun Oct 18 17:09:12 2020 - [info] Reading server configuration from /etc/mastermha/app1.cnf..
Sun Oct 18 17:09:12 2020 - [info] Starting SSH connection tests..
Sun Oct 18 17:09:14 2020 - [debug] 
Sun Oct 18 17:09:12 2020 - [debug]  Connecting via SSH from root@10.0.0.52(10.0.0.52:22) to root@10.0.0.51(10.0.0.51:22)..
Sun Oct 18 17:09:13 2020 - [debug]   ok.
Sun Oct 18 17:09:13 2020 - [debug]  Connecting via SSH from root@10.0.0.52(10.0.0.52:22) to root@10.0.0.53(10.0.0.53:22)..
Warning: Permanently added '10.0.0.53' (ECDSA) to the list of known hosts.
Sun Oct 18 17:09:13 2020 - [debug]   ok.
Sun Oct 18 17:09:14 2020 - [debug] 
Sun Oct 18 17:09:12 2020 - [debug]  Connecting via SSH from root@10.0.0.51(10.0.0.51:22) to root@10.0.0.52(10.0.0.52:22)..
Warning: Permanently added '10.0.0.52' (ECDSA) to the list of known hosts.
Sun Oct 18 17:09:12 2020 - [debug]   ok.
Sun Oct 18 17:09:12 2020 - [debug]  Connecting via SSH from root@10.0.0.51(10.0.0.51:22) to root@10.0.0.53(10.0.0.53:22)..
Warning: Permanently added '10.0.0.53' (ECDSA) to the list of known hosts.
Sun Oct 18 17:09:14 2020 - [debug]   ok.
Sun Oct 18 17:09:15 2020 - [debug] 
Sun Oct 18 17:09:13 2020 - [debug]  Connecting via SSH from root@10.0.0.53(10.0.0.53:22) to root@10.0.0.51(10.0.0.51:22)..
Sun Oct 18 17:09:13 2020 - [debug]   ok.
Sun Oct 18 17:09:13 2020 - [debug]  Connecting via SSH from root@10.0.0.53(10.0.0.53:22) to root@10.0.0.52(10.0.0.52:22)..
Sun Oct 18 17:09:15 2020 - [debug]   ok.
Sun Oct 18 17:09:15 2020 - [info] All SSH connection tests passed successfully.

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

环境准备 四台主机:
pxc1:10.0.0.7
pxc2:10.0.0.51
pxc3:10.0.0.52
pxc4:10.0.0.53

(1)配置yum源,通过yum安装PXC(使用清华大学yum源,安装前需卸载mariadb)

[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.51:/etc/yum.repos.d
[root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 10.0.0.52:/etc/yum.repos.d
[root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 10.0.0.53:/etc/yum.repos.d

#在三个节点都安装好PXC 5.7
[root@pxc1 ~]#yum -y install Percona-XtraDB-Cluster-57
[root@pxc2 ~]#yum -y install Percona-XtraDB-Cluster-57
[root@pxc3 ~]#yum -y install Percona-XtraDB-Cluster-57

(2)修改配置文件

[root@pxc1 ~]#vi /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 
# Template my.cnf for PXC
# Edit to your requirements.
[client]
socket=/var/lib/mysql/mysql.sock

[mysqld]
server-id=7|51|52|53					
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 ~]#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.51,10.0.0.52,10.0.0.53 
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|51|52|53			     
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-1|2|3|4	
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:s3cretPass"		 

(3)启动PXC集群的第一个节点

[10:36:34 root@centos7 ~]#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      100                   127.0.0.1:25                                        *:*                  
tcp   LISTEN     0      128                           *:22                                        *:*                  
tcp   LISTEN     0      100                       [::1]:25                                     [::]:*                  
tcp   LISTEN     0      128                        [::]:22                                     [::]:*                  
[10:36:36 root@centos7 ~]#systemctl start mysql@bootstrap.service
[10:40:12 root@centos7 ~]#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                           *:4567                                      *:*                  
tcp   LISTEN     0      100                   127.0.0.1:25                                        *:*                  
tcp   LISTEN     0      128                           *:22                                        *:*                  
tcp   LISTEN     0      100                       [::1]:25                                     [::]:*                  
tcp   LISTEN     0      80                         [::]:3306                                   [::]:*                  
tcp   LISTEN     0      128                        [::]:22                                     [::]:*    

[14:54:51 root@centos7 ~]#cat /var/log/mysqld.log |grep password
2020-10-20T06:54:09.331210Z 1 [Note] A temporary password is generated for root@localhost: 84D.ha(j(-yK
 [14:58:01 root@centos7 ~]# mysql -p'84D.ha(j(-yK'   
mysql>  alter user 'root'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
     

(4)创建相关账户并授权

mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 's3cretPass';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
Query OK, 0 rows affected (0.01 sec)

(5)启动其他节点

[root@pxc2 ~]#systemctl start mysql
#在任意节点创建数据库
mysql> create database db1;
Query OK, 1 row affected (0.02 sec)

#在任意节点查看数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值