CentOS7 mysql8.0 主从复制

MySQL主从复制的几种复制方式
MySQL主从复制的几种复制方式 - itbsl - 博客园

CentOS8 安装 MySQL5.7 MySQL8.0 多版本,多实例 glibc tar.zg tar.xz
CentOS8 安装 MySQL5.7 MySQL8.0 多版本,多实例 glibc tar.zg tar.xz_躁动的青年的博客-CSDN博客

基于binlog文件的主从复制

IP角色
194master
193salve

一、主数据库配置

修改主数据库的配置文件my.cnf,加入

简单配置

# 服务器的唯一标识(可以用IP结尾)
server-id = 1948

# 开启mysql binlog功能,名字可自定义,一般是mysql-bin,(可以配置路径)
log-bin = mysql-bin

复杂配置

# 服务器的唯一标识(可以用IP结尾)
server-id = 194

# 开启mysql binlog功能,名字可自定义,一般是binlog,(可以配置路径)
log-bin = mysql-bin

# 需要复制的数据库名,可配置多行(不指定复制所有)
binlog-do-db=dbname1
#binlog-do-db=dbname2

# 忽略复制得数据库,可配置多行
#binlog-ignore-db=information_schema
#binlog-ignore-db=mysql
#binlog-ignore-db=performance_schema
#binlog-ignore-db=sys

# 自动清理 7 天前的log文件
expire_logs_days=7

其他配置

# binlog记录内容的方式,记录被操作的每一行
# Default:ROW,Valid:MIXED,STATEMENT,ROW
binlog_format = ROW
 
# 减少记录日志的内容,只记录受影响的列
# Default:full,Valid:full,minimal,noblob
binlog_row_image = minimal

重启主服务

systemctl stop mysqld80

systemctl start mysqld80

查看server_id,log_bin变量

[root@bogon bin]# ./mysql -uroot -p

mysql -uroot -h 127.0.0.1 -P 3308 -p

mysql> SHOW GLOBAL VARIABLES like 'server\_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1948     |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show variables like '%log_bin%';
+---------------------------------+----------------------------------------+
| Variable_name                   | Value                                  |
+---------------------------------+----------------------------------------+
| log_bin                         | ON                                     |
| log_bin_basename                | /opt/mysql-5.7.30/data/mysql-bin       |
| log_bin_index                   | /opt/mysql-5.7.30/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF                                    |
| log_bin_use_v1_row_events       | OFF                                    |
| sql_log_bin                     | ON                                     |
+---------------------------------+----------------------------------------+
6 rows in set (0.00 sec)

在主数据库,创建用户复制得用户,并授权

mysql> CREATE USER 'replicator'@'%' IDENTIFIED BY 'myPassword21';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replicator'@'%';
Query OK, 0 rows affected (0.02 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql 5.7 默认身份验证插件default_authentication_plugin是:mysql_native_password
mysql 8.0 默认身份验证插件default_authentication_plugin是:caching_sha2_password

CREATE USER 'replicator'@'10.10.10.193' IDENTIFIED WITH caching_sha2_password BY 'myPassword21';
CREATE USER 'replicator'@'10.10.10.193' IDENTIFIED WITH mysql_native_password BY 'myPassword21';

查询主状态,(待会查询最新的,会用到)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      771 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

二、从数据库配置

从数据库配置my.cnf,添加

简单配置

# 服务器的唯一标识(可以用IP结尾)
server-id = 1938

复杂配置,从数据库也可以开启bin-log,供其他复制

# 服务器的唯一标识(可以用IP结尾)
server-id = 1938

# 开启mysql binlog功能,名字可自定义,一般是mysql-bin,(可以配置路径)
log-bin = mysql-bin

其他配置

# binlog记录内容的方式,记录被操作的每一行
# Default:ROW,Valid:MIXED,STATEMENT,ROW
binlog_format = ROW
 
# 减少记录日志的内容,只记录受影响的列
# Default:full,Valid:full,minimal,noblob
binlog_row_image = minimal

重启从数据库

在从库上建立复制关系

mysql> change master to master_host='10.10.10.194', master_port=3308, master_user='replicator', master_password='myPassword21', master_log_file='mysql-bin.000005', master_log_pos= 771, master_connect_retry=60, GET_MASTER_PUBLIC_KEY=1;

说明:
master_host 主服务器地址
master_port 主服务器端口号
master_user 主服务器用于复制的用户
master_password 主服务器用于复制的用户的密码
master_log_file 就是主服务器show master status; 中的 File
master_log_pos  就是主服务器show master status; 中的 Postion
master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒

GET_MASTER_PUBLIC_KEY = {0|1}

此选项适用于使用GET_MASTER_PUBLIC_KEY身份验证插件进行身份验证的副本。对于使用此插件进行身份验证的帐户连接,除非请求,否则源不会发送公钥,因此必须在客户端请求或指定公钥。如果给出了MASTER_PUBLIC_KEY_PATH并指定了有效的公钥文件,则它优先于GET_MASTER_PUBLIC_KEY。如果您使用的复制用户帐户使用caching_sha2_password插件(MySQL 8.0中的默认插件)进行身份验证,并且您没有使用安全连接,则必须指定此选项或MASTER_PUBLIC_KEY_PATH选项,以向复制副本提供RSA公钥。

如果复制用户的身份验证插件是:caching_sha2_password,则需要指定GET_MASTER_PUBLIC_KEY=1;

否则报错:

[root@bogon data]# tail -f /opt/mysql-8.0.20/data/mysqld.log
2022-05-12T14:34:32.048865Z 14 [ERROR] [MY-010584] [Repl] Slave I/O for channel '': error connecting to master 'replicator@10.10.10.194:3308' - retry-time: 60 retries: 4 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061

如果复制用户的身份验证插件是:mysql_native_password,则不需要指定GET_MASTER_PUBLIC_KEY=1;

可选择修改用户的密码(使用的身份验证插件)

ALTER USER 'replicator'@'%' IDENTIFIED WITH mysql_native_password BY 'myPassword21';
ALTER USER 'replicator'@'%' IDENTIFIED WITH caching_sha2_password BY 'myPassword21';

6.4.1.2 Caching SHA-2 Pluggable Authentication,页面搜索GET_MASTER_PUBLIC_KEY

13.4.2.1 CHANGE MASTER TO Statement,页面搜索GET_MASTER_PUBLIC_KEY

如果报错

ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in the MySQL error log.

在主从数据库分别查看server-id的配置是否生效

mysql> SHOW GLOBAL VARIABLES like 'server\_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 0     |
+---------------+-------+
1 row in set (0.00 sec)
修改完配置要重启数据库,停止后看看进程里是不是还有mysql。

在从数据库查看状态

mysql> show slave status \G
             Slave_IO_Running: No
            Slave_SQL_Running: No

在从数据库开启复制

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

mysql> show slave status \G
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

如果这两个状态不是Yes,需要查看日志

[root@bogon data]# tail -f /opt/mysql-8.0.20/data/mysqld.log

在从数据库关闭复制

mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

三、测试

在主数据库,建库,建表,增删改记录。

在从数据库查询,以上数据已同步。

如果从数据库不设置只读,数据复制会有问题,相同ID的数据不会更新。

从数据库

mysql> show slave status \G

             Slave_IO_Running: Yes
            Slave_SQL_Running: No

Last_Error: Could not execute Write_rows event on table test.user; Duplicate entry '4' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000006, end_log_pos 4358

可尝试删除冲突的数据,关闭复制,开启复制,再查看状态。

或重新建立复制关系,指定日志文件,指定位置,(位置可以考虑从0开始)

四、主从复制文档

17.1.6 Replication and Binary Logging Options and Variables

17.1.6.1 Replication and Binary Logging Option and Variable Reference

17.1.6.2 Replication Source Options and Variables

17.1.6.3 Replica Server Options and Variables

17.1.6.4 Binary Logging Options and Variables

17.1.6.5 Global Transaction ID System Variables

五、文档

 MySQL 8.0 Reference Manual

2.2 Installing MySQL on Unix/Linux Using Generic Binaries

2.3.4.2 Creating an Option File

4.2.2.2 Using Option Files

5.1.2 Server Configuration Defaults

5.1.7 Server Command Options

5.1.8 Server System Variables (my.cnf中的属性的解释及默认值)

10.4 Connection Character Sets and Collations

17.1.6 Replication and Binary Logging Options and Variables (主从复制)

六、其他

日志警告

2022-05-12T15:52:37.772791Z 21 [Warning] [MY-010956] [Server] Invalid replication timestamps: original commit timestamp is more recent than the immediate commit timestamp. This may be an issue if delayed replication is active. Make sure that servers have their clocks set to the correct time. No further message will be emitted until after timestamps become valid again.

2021-03-15[Warning] [MY-010956] [Server] Invalid replication timestamps:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值