mysql主主架构搭建,删库恢复

news/2024/5/12 8:02:31

mysql主主架构搭建,删库恢复

  • 搭建mysql主主架构
    • 环境信息
    • 安装msql服务
      • mysql1
      • mysql2
      • 设置mysql2同步mysql1
      • 设置mysql1同步mysql2
      • 授权测试用账户
    • 安装配置keepalived
      • mysql1
      • 检查脚本
      • mysql2
      • 检查脚本
    • 备份策略
      • mysqldump全量备份
      • mysqldump增量备份
      • 数据库目录全量备份
  • 删除mysql1数据库目录,恢复数据
      • 删除mysql1的数据库目录
      • 停止mysql1的数据库
      • 全备份mysql2的数据
      • 备份数据上传mysql1
      • mysql1启动数据库服务
      • 导入备份数据
      • 恢复授权信息
      • 设置mysql1同步mysql2
      • 设置mysql2同步mysql1的数据
      • 启动mysql1上的keepalived
  • 测试用库表

搭建mysql主主架构

环境信息

主机名IP地址服务角色
mysql1192.168.44.188mysql,keepalivedmysql-master,keepalived-master
mysql2192.168.44.190mysql,keepalivedmysql-master,keepalived-backup
192.168.44.100(vip)

安装msql服务

mysql1

[root@mysql1 ~]# tar xf mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar
[root@mysql1 ~]# yum -y install *.rpm[root@mysql1 ~]# vim /etc/my.cnf
[mysqld]
server_id=0001
log_bin=mysql-0001[root@mysql1 ~]# systemctl enable mysqld --now
#查看初始密码
[root@mysql1 ~]# grep password /var/log/mysqld.log | tail -1
#使用初始密码登录
[root@mysql1 ~]# mysql -uroot -p'AFKMqF?Kd2ul'mysql> alter user root@localhost identified by 'zzz-123-ZZZ';
Query OK, 0 rows affected (0.00 sec)mysql> grant replication slave on *.* to repluser@"%" identified by "zzz-123-ZZZ";
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysql-0001.000002 |      685 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql2

[root@mysql2 ~]# tar xf mysql-5.7.39-1.el7.x86_64.rpm-bundle.tar
[root@mysql2 ~]# yum -y install *.rpm[root@mysql2 ~]# vim /etc/my.cnf
[mysqld]
server_id=0002
log_bin=mysql-0002[root@mysql2 ~]# systemctl enable mysqld --now
#查看初始密码
[root@mysql2 ~]# grep password /var/log/mysqld.log | tail -1
#使用初始密码登录
[root@mysql2 ~]# mysql -uroot -p'(se1aYk;r3:g'mysql> alter user root@localhost identified by 'zzz-123-ZZZ';
Query OK, 0 rows affected (0.00 sec)mysql> alter user root@localhost identified by 'zzz-123-ZZZ';
Query OK, 0 rows affected (0.00 sec)mysql> grant replication slave on *.* to repluser@"%" identified by "zzz-123-ZZZ";
Query OK, 0 rows affected, 1 warning (0.00 sec)

设置mysql2同步mysql1

#配置mysql2为mysql1的从服务器,填写mysql1查询master status,中的file和 Position
mysql> change master to master_host="192.168.44.188",master_user="repluser",master_password="zzz-123-ZZZ",master_log_file="mysql-0001.000002",master_log_pos=685;
Query OK, 0 rows affected, 2 warnings (0.18 sec)#配置后查看master信息
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysql-0002.000002 |      929 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)#查看同步信息
mysql> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.44.188Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-0001.000002Read_Master_Log_Pos: 685Relay_Log_File: mysql2-relay-bin.000002Relay_Log_Pos: 321Relay_Master_Log_File: mysql-0001.000002Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 685Relay_Log_Space: 529Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 1Master_UUID: 196aed76-23c3-11ee-970c-000c29919b39Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 0Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:
1 row in set (0.00 sec)mysql> exit

设置mysql1同步mysql2

#配置mysql1为mysql2的从服务器,填写mysql1查询master status,中的file和 Position
mysql> change master to master_host="192.168.44.190",master_user="repluser",master_password="zzz-123-ZZZ",master_log_file="mysql-0001.000002",master_log_pos=929;
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 eventMaster_Host: 192.168.44.190Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-0002.000002Read_Master_Log_Pos: 929Relay_Log_File: mysql1-relay-bin.000002Relay_Log_Pos: 321Relay_Master_Log_File: mysql-0002.000002Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 929Relay_Log_Space: 529Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 2Master_UUID: 2f686140-23c3-11ee-98f2-000c29fe7242Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 0Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:
1 row in set (0.00 sec)

授权测试用账户

#对测试mysql运行状态的用户授权
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test_user'@'%' IDENTIFIED BY 'zzz-123-ZZZ' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.03 sec)mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

安装配置keepalived

mysql1

[root@mysql1 ~]# yum -y install keepalived.x86_64
[root@mysql1 ~]# vim /etc/keepalived/keepalived.conf

/etc/keepalived/keepalived.conf

! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id mysql1		#集群唯一标识vrrp_iptables		#防火墙放行vrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}
vrrp_script check_mysql {script "/usr/local/bin/check_mysql.sh"    # 检测MySQL服务的脚本路径interval 3                   # 检测频率(单位:秒)
#    weight -4                    # 检测失败时扣除的权重 通过脚本停止了keepalived服务,此处不再配置
#    fall 2                       # 连续检测失败次数
#    rise 2                       # 连续检测成功次数
}vrrp_instance VI_1 {state MASTER			#节点为masterinterface ens33			#网卡名virtual_router_id 51priority 100			#节点权重,越大越重advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.44.100/24	#虚拟ip}track_script {check_mysql                # 监测MySQL服务脚本的名称}
}

检查脚本

[root@mysql1 ~]# vim /usr/local/bin/check_mysql.sh

/usr/local/bin/check_mysql.sh

#!/bin/bash# 定义MySQL相关配置
MYSQL_USER="test_user"
MYSQL_PASS="zzz-123-ZZZ"
MYSQL_HOST="192.168.44.188"
MYSQL_PORT="3306"
MYSQL_VIP="192.168.44.100"# 检测MySQL状态
check_mysql_status() {# 尝试连接MySQL并执行查询if ! mysql -h "${MYSQL_HOST}" -P "${MYSQL_PORT}" -u "${MYSQL_USER}" -p"${MYSQL_PASS}" -e "SELECT 1" >/dev/null; thenecho "无法连接到MySQL!"return 1fi# MySQL状态正常return 0
}if check_mysql_status; thenecho "MySQL服务正常"exit 0
elseecho "MySQL服务异常"# 停止Keepalived服务systemctl stop keepalived.service# 释放VIP(虚拟IP)ip address del "${MYSQL_VIP}"/24 dev ens33exit 1
fi
[root@mysql1 ~]# chmod a+x /usr/local/bin/check_mysql.sh
[root@mysql1 ~]# systemctl enable keepalived.service --now
[root@mysql1 ~]# systemctl status keepalived.service

mysql2

[root@mysql2 ~]# yum -y install keepalived.x86_64
[root@mysql2 ~]# vim /etc/keepalived/keepalived.conf

/etc/keepalived/keepalived.conf

! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id mysql2	#集群唯一标识vrrp_iptables		#防火墙放行vrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}
vrrp_script check_mysql {script "/usr/local/bin/check_mysql.sh"    # 检测MySQL服务的脚本路径interval 3                   # 检测频率(单位:秒)
#    weight -4                    # 检测失败时扣除的权重 通过脚本停止了keepalived服务,此处不再配置
#    fall 2                       # 连续检测失败次数
#    rise 2                       # 连续检测成功次数
}vrrp_instance VI_1 {state BACKUP			#节点为BACKUPinterface ens33			#网卡名virtual_router_id 51priority 98			#节点权重,比master要小advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.44.100/24	#虚拟ip}track_script {check_mysql                # 监测MySQL服务脚本的名称}
}

检查脚本

[root@mysql2 ~]# vim /usr/local/bin/check_mysql.sh

/usr/local/bin/check_mysql.sh

#!/bin/bash# 定义MySQL相关配置
MYSQL_USER="test_user"
MYSQL_PASS="zzz-123-ZZZ"
MYSQL_HOST="192.168.44.190"
MYSQL_PORT="3306"
MYSQL_VIP="192.168.44.100"# 检测MySQL状态
check_mysql_status() {# 尝试连接MySQL并执行查询if ! mysql -h "${MYSQL_HOST}" -P "${MYSQL_PORT}" -u "${MYSQL_USER}" -p"${MYSQL_PASS}" -e "SELECT 1" >/dev/null; thenecho "无法连接到MySQL!"return 1fi# MySQL状态正常return 0
}if check_mysql_status; thenecho "MySQL服务正常"exit 0
elseecho "MySQL服务异常"# 停止Keepalived服务systemctl stop keepalived.service# 释放VIP(虚拟IP)ip address del "${MYSQL_VIP}"/24 dev ens33exit 1
fi
[root@mysql1 ~]# chmod a+x /usr/local/bin/check_mysql.sh
[root@mysql1 ~]# systemctl enable keepalived.service --now
[root@mysql1 ~]# systemctl status keepalived.service

备份策略

mysqldump全量备份

#!/bin/bash#全量备份TIME=$(date +%Y-%m-%d)
BACKUP_DIR="/mysqldump_back/"mysqldump -u 用户名 -p --master-data=2 --all-databases --result-file=${BACKUP_DIR}back-${TIME}.sql

mysqldump增量备份

#此脚本尚未亲测
#!/bin/bash#先手动全量备份,后执行脚本
mysqldump -u 用户名 -p --master-data=2 --all-databases --result-file=${BACKUP_DIR}last_backup.sqlTIME=$(date +%Y-%m-%d)
BACKUP_DIR="/路径/"
LAST_BACKUP="${BACKUP_DIR}last_backup.sql"mysqldump -u 用户名 -p --master-data=2 --databases --result-file=${BACKUP_DIR}back-${TIME}.sql --incremental=snar
rsync ${BACKUP_DIR}back-${TIME}.sql ${LAST_BACKUP}

数据库目录全量备份

#/bin/bash
rsync -av /var/lib/mysql /mysqlback/var-lib-mysql

删除mysql1数据库目录,恢复数据

删除mysql1的数据库目录

[root@mysql1 ~]# rm -rf /var/lib/mysql

查看keepalived停止,VIP漂移到mysql2


停止mysql1的数据库

[root@mysql1 ~]# systemctl stop mysqld

全备份mysql2的数据

[root@mysql2 ~]# mysqldump -uroot -pzzz-123-ZZZ --all-databases --master-data=2 > /root/20230716allback.sql
[root@mysql2 zzz]# grep mysql-0002 /root/20230716allback.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-0002.000002', MASTER_LOG_POS=1493;

备份数据上传mysql1

[root@mysql2 ~]# scp /root/20230716allback.sql 192.168.44.188:/root

mysql1启动数据库服务

[root@mysql1 ~]# systemctl start mysqld
#生成初始密码,初始密码登录,改密码#数据库没有生成初始密码
[root@mysql1 ~]# grep "password" /var/log/mysqld.log | tail -1
2023-07-16T07:28:38.032091Z 882 [Note] Access denied for user 'test_user'@'localhost' (using password: YES)
#停止mysql服务,删除数据库目录,再次启动数据库
[root@mysql1 ~]# systemctl stop mysqld.service
[root@mysql1 ~]# rm -rf /var/lib/mysql
[root@mysql1 ~]# systemctl start mysqld
[root@mysql1 ~]# grep "password" /var/log/mysqld.log | tail -1
2023-07-16T07:30:43.170590Z 15 [Note] Access denied for user 'test_user'@'localhost' (using password: YES)
#依旧没有生成初始密码#重置root密码
vim /etc/mysql
#增加免密配置
skip-grant-tables#重启数据库
systemctl restart mysqld#免密登录
mysql#修改root密码
mysql> update  mysql.user set authentication_string=password("123qqq...A") 
where user="root" and host="localhost"; 
#确保修改生效
mysql> flush privileges; 
mysql> exit; 断开连接#注释免密登录
vim /etc/mysql
#增加免密配置
#skip-grant-tables#重启数据库
systemctl restart mysqld#使用密码登录
[root@mysql1 ~]# mysql -uroot -p123qqq...A#重置密码
mysql> alter user root@localhost identified by 'zzz-123-ZZZ';
Query OK, 0 rows affected (0.00 sec)mysql> exit
#修改好密码后
#登录查看为空库
[root@mysql1 ~]# mysql -uroot -pzzz-123-ZZZ
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 337
Server version: 5.7.39-log MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.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.01 sec)
mysql> exit

导入备份数据

[root@mysql1 ~]# mysql -uroot -pzzz-123-ZZZ < /root/20230716allback.sql
#查看数据
[root@mysql1 ~]# mysql -uroot -pzzz-123-ZZZ
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 631
Server version: 5.7.39-log MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.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                |
| test1              |
+--------------------+
5 rows in set (0.00 sec)mysql> use test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| table1          |
| table2          |
+-----------------+
2 rows in set (0.00 sec)#查看授权信息
mysql> show grants for repluser;
ERROR 1141 (42000): There is no such grant defined for user 'repluser' on host '%'

查看mysql2授权信息

[root@mysql2 zzz]# mysql -uroot -pzzz-123-ZZZ -e "show grants for repluser"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------------------------------------+
| Grants for repluser@%                            |
+--------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' |
+--------------------------------------------------+

恢复授权信息

#即授权信息未被恢复#恢复授权信息
#上传mysql2数据库目录的mysql目录到mysql1
[root@mysql1 ~]# scp -r 192.168.44.190:/var/lib/mysql/mysql /var/lib/mysql/
#查看 上传来的mysql目录的属性信息
[root@mysql1 ~]# ll var/lib/mysql/mysql
[root@mysql1 ~]# chown -R mysql:mysql /var/lib/mysql#mysql1的数据库服务重新加载配置
[root@mysql1 ~]# ps -ef | grep mysql
mysql     93344      1  0 15:38 ?        00:00:01 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root     116253   1275  0 15:52 pts/0    00:00:00 grep --color=auto mysql
[root@mysql1 ~]#
[root@mysql1 ~]#
#kill -1 或 kill -SIGHUP
[root@mysql1 ~]# kill -1 93344#再次查看授权信息,此时MySQL1的root密码也和mysql2同步
[root@mysql1 ~]# mysql -uroot -pzzz-123-ZZZ -e "show grants for repluser"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------------------------------------+
| Grants for repluser@%                            |
+--------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%' |
+--------------------------------------------------+

设置mysql1同步mysql2

设置mysql1同步mysql2的数据,使用备份数据里的binlog数据

[root@mysql2 zzz]# grep mysql-0002 /root/20230716allback.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-0002.000002', MASTER_LOG_POS=1493;
[root@mysql1 ~]# mysql -uroot -pzzz-123-ZZZ
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 2388
Server version: 5.7.39-log MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.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="192.168.44.190",master_user="repluser",master_password="zzz-123-ZZZ",master_log_file="mysql-0002.000002",master_log_pos=1493;
Query OK, 0 rows affected, 2 warnings (0.00 sec)mysql>
mysql>
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 eventMaster_Host: 192.168.44.187Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-0002.000003Read_Master_Log_Pos: 154Relay_Log_File: mysql1-relay-bin.000003Relay_Log_Pos: 369Relay_Master_Log_File: mysql-0002.000003Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 744Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 2Master_UUID: 88729250-22fc-11ee-af60-000c29fe7242Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 0Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:
1 row in set (0.00 sec)

设置mysql2同步mysql1的数据

#查看mysql1的master信息
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysql-0001.000004 |   549254 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#配置mysql2同步mysql1的数据
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)mysql>
mysql> RESET SLAVE ALL;
Query OK, 0 rows affected (0.00 sec)mysql> change master to master_host="192.168.44.186",master_user="repluser",master_password="zzz-123-ZZZ",master_log_file="mysql-0001.000004",master_log_pos=549254;
Query OK, 0 rows affected, 2 warnings (0.01 sec)mysql> start slave;
Query OK, 0 rows affected (0.00 sec)mysql>
mysql>
mysql> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.44.186Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-0001.000004Read_Master_Log_Pos: 707915Relay_Log_File: mysql2-relay-bin.000002Relay_Log_Pos: 158982Relay_Master_Log_File: mysql-0001.000004Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 707915Relay_Log_Space: 159190Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 1Master_UUID: 0f1f81d7-23b1-11ee-b1e8-000c29919b39Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 0Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:
1 row in set (0.00 sec)

数据恢复完成,主主同步恢复

启动mysql1上的keepalived

[root@mysql1 ~]# systemctl start keepalived.service
#此时vip会回到mysql1 

测试用库表

期间创建test库,和表table1,表table2测试数据

CREATE DATABASE test CHARACTER SET utf8;CREATE TABLE table1 (id INT AUTO_INCREMENT PRIMARY KEY,home VARCHAR(255),love VARCHAR(255),age INT
);CREATE TABLE table2 (id INT ,home VARCHAR(255),love VARCHAR(255),age INT
);

创建两个脚本测试插入随机数据

#!/bin/bashvip_my="192.168.44.100"while true
do# 生成一个随机数作为 age 字段的值age=$(shuf -i 1-100 -n 1)# 生成一个随机字符串作为 home 和 love 字段的值,长度为 10home=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)love=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)# 检查是否已经存在相同的 home 和 love 值result=$(mysql -h ${vip_my} -u test_user -pzzz-123-ZZZ -e "SELECT COUNT(*) FROM test.table1 WHERE home='$home' AND love='$love';" -s)# 如果不存在相同的值,则插入新的记录if [ "$result" -eq 0 ]; thenmysql -h${vip_my} -u test_user -pzzz-123-ZZZ -e "INSERT INTO test.table1 (home, love, age) VALUES ('$home', '$love', $age);"fisleep 1  # 暂停 1 秒
done
#!/bin/bashvip_my="192.168.44.100"while true
do# 生成一个随机数作为 id 字段的值,因为table2的id未设置自增长id=$(shuf -i 1-100000 -n 1)# 生成一个随机字符串作为 home 和 love 字段的值,长度为 10home=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)love=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)# 生成一个随机数作为 age 字段的值age=$(shuf -i 1-100 -n 1)# 插入数据到 table2 表mysql -h${vip_my} -u test_user -pzzz-123-ZZZ -e "INSERT INTO test.table2 (id, home, love, age) VALUES ($id, '$home', '$love', $age);"sleep 1  # 暂停 1 秒
done

http://www.mrgr.cn/p/48805554

相关文章

卷积神经网络识别人脸项目—使用百度飞桨ai计算

卷积神经网络识别人脸项目的详细过程 整个项目需要的准备文件&#xff1a; 下载链接&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1WEndfi14EhVh-8Vvt62I_w 提取码&#xff1a;7777 链接&#xff1a;https://pan.baidu.com/s/10weqx3r_zbS5gNEq-xGrzg 提取码&#x…

Transformer 论文学习笔记

重新学习了一下&#xff0c;整理了一下笔记 论文&#xff1a;《Attention Is All You Need》 代码&#xff1a;http://nlp.seas.harvard.edu/annotated-transformer/ 地址&#xff1a;https://arxiv.org/abs/1706.03762v5 翻译&#xff1a;Transformer论文翻译 特点&#xff1…

网络面试合集

传输层的数据结构是什么&#xff1f; 就是在问他的协议格式&#xff1a;UDP&TCP 2.1.1三次握手 通信前&#xff0c;要先建立连接&#xff0c;确保双方都是在线&#xff0c;具有数据收发的能力。 2.1.2四次挥手 通信结束后&#xff0c;会有一个断开连接的过程&#xff0…

二十、像素流送,软件即游戏,SaaG时代到来

有了像素流送插件,就可以在远程计算机上运行虚幻引擎应用,并将其渲染效果和音频流送到任意平台的现代网页浏览器上。此外,还可以在浏览器上与流互动,遥控虚幻引擎应用。 1、启用像素流插件 虚幻编辑器的主菜单中选择 编辑(Edit) > 插件(Plugins),在图像(Graph…

selenium-web自动化测试

一、selenium环境部署 1.准备chrome浏览器&#xff08;其他浏览器也行&#xff09; 2.准备chrome驱动包 步骤一&#xff1a;查看自己的谷歌浏览器版本(浏览器版本和驱动版本一定要对应) 步骤二&#xff1a;下载对应的驱动包, 下载路径 : ChromeDriver - WebDriver for Chrom…

DragGAN:用崭新的方式进行图像处理

该项目的论文被SIGGRAPH 2023 收录&#xff0c;论文以 StyleGAN2 架构为基础&#xff0c;实现了 “Drag” 关键点就能轻松 P 图的效果。 https://github.com/XingangPan/DragGAN https://vcai.mpi-inf.mpg.de/projects/DragGAN/ 目录 原图1测试一测试二测试三 原图2测试一测试…

【数据结构与算法】基数排序

基数排序 基数排序&#xff08;Radix Sort&#xff09;属于“分配式排序”&#xff0c;又称“桶子法”或 bin sort&#xff0c;顾名思义&#xff0c;它是通过键值的各个位的值&#xff0c;将要排序的元素分配至某些“桶”中&#xff0c;达到排序的作用。基数排序法是属于稳定性…

【文献分享】比目前最先进的模型轻30%!高效多机器人SLAM蒸馏描述符!

论文题目&#xff1a;Descriptor Distillation for Efficient Multi-Robot SLAM 中文题目&#xff1a;高效多机器人SLAM蒸馏描述符 作者&#xff1a;Xiyue Guo, Junjie Hu, Hujun Bao and Guofeng Zhang 作者机构&#xff1a;浙江大学CAD&CG国家重点实验室 香港中文大学…

51单片机——串行口通信

目录 1、51单片机串口通信介绍 2、串行口相关寄存器 2.1 、串行口控制寄存器SCON和PCON 2.1.1 SCON&#xff1a;串行控制寄存器 (可位寻址) 2.1.2 PCON&#xff1a;电源控制寄存器&#xff08;不可位寻址&#xff09; 2.2、串行口数据缓冲寄存器SBUF 2.3、从机地址控制…

持续贡献开源力量,棱镜七彩加入openKylin

近日&#xff0c;棱镜七彩签署 openKylin 社区 CLA&#xff08;Contributor License Agreement 贡献者许可协议&#xff09;&#xff0c;正式加入openKylin 开源社区。 棱镜七彩成立于2016年&#xff0c;是一家专注于开源安全、软件供应链安全的创新型科技企业。自成立以来&…

如何有效地使用ChatGPT写小说讲故事?

​构思故事情节&#xff0c;虽有趣但耗时&#xff0c;容易陷入写作瓶颈。ChatGPT可提供灵感&#xff0c;帮你解决写作难题。要写出引人入胜的故事&#xff0c;关键在于抓住八个要素——主题、人物、视角、背景、情节、语气、冲突和解决办法。 直接给出故事模板&#xff0c;你可…

Selenium+Java环境搭建(测试系列6)

目录 前言&#xff1a; 1.浏览器 1.1下载Chrome浏览器 1.2查看Chrome浏览器版本 1.3下载Chrome浏览器的驱动 2.配置系统环境变量path 3.验证是否成功 4.出现的问题 结束语&#xff1a; 前言&#xff1a; 这节中小编给大家讲解一下有关于Selenium Java环境的搭建&…

C语言指针进阶-1

本篇文章带来 1. 字符指针 2. 数组指针 3. 指针数组的相关知识详细讲解&#xff01; 如果您觉得文章不错&#xff0c;期待你的一键三连哦&#xff0c;你的鼓励是我创作的动力之源&#xff0c;让我们一起加油&#xff0c;一起奔跑&#xff0c;让我们顶峰相见&#xff01;&#…

智慧农业:科技赋能农村发展

智慧农业发展前景灿烂多彩&#xff0c;正为农业带来新的转型升级。随着科技的不断发展&#xff0c;数字化、自动化和智能化技术逐渐渗透进农业领域&#xff0c;为农民提供了更多高效便捷的农业管理方式。智慧农业通过物联网、大数据、人工智能等先进技术&#xff0c;实现对农田…

前端小练-仿掘金导航栏

文章目录 前言项目结构导航实现创作中心移动小球消息提示 完整代码 前言 闲的&#xff0c;你信嘛&#xff0c;还得开发一个基本的门户社区网站&#xff0c;来给到Hlang,不然我怕说工作量不够。那么这个的话&#xff0c;其实也很好办&#xff0c;主要是这个门户网站的UI写起来麻…

【深度学习】以图搜索- 2021sota repVgg来抽取向量 + facebook的faiss的做特征检索, 从环境搭建到运行案例从0到1

文章目录 前言安装小试牛刀用repVgg抽取向量构建Faiss索引进行相似性搜索本项目延伸其它项目拓展总结 前言 Faiss的全称是Facebook AI Similarity Search。 这是一个开源库&#xff0c;针对高维空间中的海量数据&#xff0c;提供了高效且可靠的检索方法。 暴力检索耗时巨大&a…

【Lua学习笔记】Lua进阶——协程

文章目录 协程协程的定义和调度StatusRunning 协程 协程是一种并发操作&#xff0c;相比于线程&#xff0c;线程在执行时往往是并行的&#xff0c;并且线程在创建销毁执行时极其消耗资源&#xff0c;并且过长的执行时间会造成主进程阻塞。而协程可以以并发时轮值时间片来执行&…

《QDebug 2023年7月》

一、Qt Widgets 问题交流 1.QPainter旋转角度绘制线条的一点问题 QPainter 旋转角度&#xff0c;等距绘制若干线条&#xff0c;会出现绘制不均匀的情况&#xff1a; 但是在测试 QML Canvas 绘制时&#xff0c;发现效果是正常的&#xff0c;原来是因为 Canvas 默认的 capStyle…

uni-app点击按钮弹出提示框(以弹窗的形式显示),选择确定和取消

学习目标&#xff1a; 学习目标如下所示&#xff1a; uni-app点击提交按钮后弹出提示框&#xff0c;&#xff08;以弹窗的形式显示&#xff09;,提示用户是否确认提交&#xff08;即确定和取消&#xff09;&#xff0c;点击确定后调用真正的提交方法&#xff0c;将数据传给后端…

这三件事没理顺,你过不了软考

下午好&#xff0c;我的网工朋友 上周软考成绩出来了&#xff0c;大家都过了没&#xff1f; 我看好多人都说早上的题目稳过&#xff0c;下午的好多都挂了。 软考每年这个通过率&#xff0c;确实是一言难尽。 到底怎么样才能过&#xff0c;自学、培训&#xff0c;各种诀窍&am…