Centos7 builds standard universal MySQL8.0 master-slave replication (one master and one slave)

table of Contents

1. Master-slave replication

2. Role division

3. Configure master and slave

4. Skip copy errors


1. Master-slave replication

The principle of master-slave replication is simply that the master database writes the addition, deletion, modification, and check operations into a binary file, reads the file from the database, and copies data to keep the master-slave database consistent.

The advantages of master-slave replication are:

       1. High availability: When the main library is abnormal, quickly switch from the library to restore the application

       2. Load balancing: read and write separation. Write from the main library and read from the library. But need other plug-in implementation, such as MaxScale

       3. Backup data: The data from the slave database is consistent with the master database, which is equivalent to a backup method

2. Role division

IP address Roles
192.168.44.66 master
182.168.44.88 slave

3. Configure master and slave

For MySQL8.0 database installation , please refer to https://blog.csdn.net/ct_666/article/details/111248343 , and start the configuration directly here.

Prerequisites:

# 关闭主从数据库服务器的防火墙
systemctl stop firewalld && systemctl disable firewalld

# 两台机器网络互通

Main library configuration:

# 编辑配置文件
vi /etc/my.cnf

# 在文件结尾添加以下内容:

# 主服务器唯一ID
server-id=1
# 启用二进制日志
log-bin=mysql-bin
# 设置不要复制的数据库(可设置多个),不设置则默认同步所有库
binlog-ignore-db=sys
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
# 设置需要复制的数据库(可设置多个),不设置则默认同步所有库
binlog-do-db=test
# 设置logbin格式
binlog_format=STATEMENT

Configuration from the library:

# 编辑配置文件
vi /etc/my.cnf

# 在文件结尾添加以下内容:

# 从服务器唯一ID
server-id=2
# 启用中继日志
relay-log=mysql-relay

Restart the master and slave databases to make the configuration effective

systemctl restart mysqld

Log in to the main library to create the user slave used for replication, and record the value of file and position

# 登录主库
mysql -uroot -p
或者
mysql -uroot -p -h127.0.0.1

# 在主库创建密码为Admin#2020的用户slave
mysql> create user 'slave'@'%' identified with mysql_native_password by 'Admin#2020';
Query OK, 0 rows affected (0.41 sec)

# 授权
mysql> grant replication slave on *.* to 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)

# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

# 查看master状态,并记录file和position的值
mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000001 |      156 | test         | sys,mysql,information_schema,performance_schema |                   |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)

# 退出主库以防误操作改变file和position的值
quit;

Log in to configure from the library:

# 登录从库
mysql -uroot -p
或者
mysql -uroot -p -h127.0.0.1

# 设置从库向主库同步数据,参数说明:
# master_host的值是你的主库IP
# master_port的值是你的主库端口
# master_user就是创建的slave用户
# master_password就是创建的slave用户的密码
# master_log_file的值就是前面记录的file的值
# master_log_pos的值就是前面记录的position的值
mysql> change master to master_host='192.168.44.66',master_port=3306,master_user='slave',master_password='Admin#2020',master_log_file='mysql-bin.000001',master_log_pos=156;
Query OK, 0 rows affected, 2 warnings (0.19 sec)

# 开始同步
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

# 查看slave同步状态
# Slave_IO_Running: Yes,Slave_SQL_Running: Yes,两个yes则同步成功,否则查看相应的报错
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.44.66
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 156
               Relay_Log_File: mysql-relay.000001
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

Test master-slave synchronization:

# 主数据库创建数据库test
create database test;

# 从数据库查看
show databases;

# 主数据库创建表
use test;
create table user(id int, name varchar(20));

# 插入数据
insert into user values(1, '张三');

# 从数据库查看
use test;
select * from user;

If there is a synchronization error, stop the synchronization and restart the synchronization after resetting:

stop slave;
reset slave;
start slave;

4. Skip copy errors

mysql master-slave replication often encounters errors and interrupts replication on the slave side. At this time, manual intervention is generally required to skip the error to continue synchronization. There are three ways to skip the error:

①Skip the specified number of transactions:

mysql>stop slave;
mysql>SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1 #跳过一个事务
mysql>start slave;

② Modify the configuration file of the slave database mysql, and skip the specified type of error or skip all errors through the slave_skip_errors parameter:

# 编辑配置文件
vi /etc/my.cnf

# 文件末尾添加,三选一
# 跳过1062,1053,1146类型的错误
slave-skip-errors=1062,1053,1146
# 跳过所有错误      
slave-skip-errors=all
# MySQL8.0可以添加参数ddl_exist_errors跳过一系列的error code,包括(1007,1008,1050,1051,1054,1060,1061,1068,1094,1146)
slave-skip-errors=ddl_exist_errors  

# 重启MySQL
systemctl restart mysqld

 

Guess you like

Origin blog.csdn.net/ct_666/article/details/111842000