mysql8主从数据配置binlog+position模式

Posted gsls200808

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql8主从数据配置binlog+position模式相关的知识,希望对你有一定的参考价值。

mysql8除了传统的binlog+position模式可以主从复制外,还可以用GTID复制,这里介绍传统的放hi

本文系统版本Rocky linux 8.5,MySQL版本 8.0.26

主ip:10.24.220.102

从ip:10.24.220.103

一、系统安装

详见:RockyLinux8安装体验_gsls200808的专栏-CSDN博客

二、myql8安装

安装mysql仓库

# 下载repo仓库
wget https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm
# 安装仓库
rpm -ivh mysql80-community-release-el8-1.noarch.rpm

安装myql server

yum install mysql-server mysql-devel

注意这里不能用yum install mysql-community-server 会报找不到这个包

查看版本:

mysqladmin --version

启动

#启动mysql
systemctl start mysqld
#状态
systemctl status mysqld

开启root远程登录,并设置root密码

mysql -u root -p
直接回车(密码为空),并执行以下语句:


use mysql;
update user set host='%' where user ='root' ;
update user set authentication_string = "" where user = 'root' ;
flush privileges;
#by 语句后写自己的密码
alter user 'root'@'%' identified with mysql_native_password by 'apppass';
flush privileges;

修改时区

查看时区

#查看时区
show variables like '%time_zone%';
#结果
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.01 sec)

修改时区

vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]项最后添加
default-time-zone = '+8:00'

#重启mysqld
systemctl restart mysqld

修改后重新查看时区

#重新查看时区结果
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | +08:00 |
+------------------+--------+
2 rows in set (0.01 sec)

开启3306端口

firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload 

三、主从复制配置

1.主数据库配置

编辑mysql-server.cnf,早期版本为my.cnf

vi /etc/my.cnf.d/mysql-server.cnf
早期版本路径/etc/my.cnf 


[mysqld]
# 设置同步的binary log二进制日志文件名前缀,默认为binlog
log-bin=mysql-bin
# 服务器唯一id,主数据库和从数据库的server-id不能重复,默认为1
server-id=1          

###可选配置
# 主从复制的格式(mixed,statement,row,默认格式是statement。建议是设置为row,主从复制时数据更加能够统一)
binlog_format=row

重启mysqld

#重启mysqld
systemctl restart mysqld

创建用户和授权

#创建同步用户
CREATE USER 'repl'@'%' IDENTIFIED BY '123456';
#授权
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
#刷新权限
FLUSH PRIVILEGES;

查看master的binlog名和位置

SHOW MASTER STATUS;
#结果
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      818 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

2.从数据库配置

编辑mysql-server.cnf,早期版本为my.cnf

vi /etc/my.cnf.d/mysql-server.cnf
早期版本路径/etc/my.cnf 

[mysqld]
# 设置同步的binary log二进制日志文件名前缀,默认为binlog
log-bin=mysql-bin
# 服务器唯一id,主数据库和从数据库的server-id不能重复,默认为1
server-id=2

###可选配置
# 主从复制的格式(mixed,statement,row,默认格式是statement。建议是设置为row,主从复制时数据更加能够统一)
binlog_format=row

重启mysqld
 

重启mysqld
systemctl restart mysqld

连接主库binlog和位置

CHANGE REPLICATION SOURCE TO SOURCE_HOST='10.24.220.102',SOURCE_PORT=3306,SOURCE_USER='repl',SOURCE_PASSWORD='123456',SOURCE_LOG_FILE='mysql-bin.000001',SOURCE_LOG_POS=818;

启动从服务器复制
 

#启动从服务器复制
START REPLICA;

查看从服务器复制状态
 

#查看从服务器复制状态
SHOW REPLICA STATUS\\G;
#结果
mysql> SHOW REPLICA STATUS\\G;
*************************** 1. row ***************************
             Replica_IO_State: Waiting for source to send event
                  Source_Host: 10.24.220.102
                  Source_User: repl
                  Source_Port: 3306
                Connect_Retry: 60
              Source_Log_File: mysql-bin.000001
          Read_Source_Log_Pos: 1236
               Relay_Log_File: localhost-relay-bin.000003
                Relay_Log_Pos: 742
        Relay_Source_Log_File: mysql-bin.000001
           Replica_IO_Running: Yes
          Replica_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_Source_Log_Pos: 1236
              Relay_Log_Space: 955
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Source_SSL_Allowed: No
           Source_SSL_CA_File:
           Source_SSL_CA_Path:
              Source_SSL_Cert:
            Source_SSL_Cipher:
               Source_SSL_Key:
        Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Source_Server_Id: 1
                  Source_UUID: 19f28c9b-8cd7-11ec-81c3-000c29086f0a
             Source_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
    Replica_SQL_Running_State: Replica has read all relay log; waiting for more                                                                                    updates
           Source_Retry_Count: 86400
                  Source_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Source_SSL_Crl:
           Source_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Source_TLS_Version:
       Source_public_key_path:
        Get_Source_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

ERROR:
No query specified

假如显示 Slave_IO_Running/Replica_IO_Running和 Slave_SQL_Running/Replica_SQL_Running 为 Yes ,以及Slave_IO_State/Replica_IO_State 为 Waiting for master to send event/Waiting for source to send event,则证明主从复制成功!

以上是关于mysql8主从数据配置binlog+position模式的主要内容,如果未能解决你的问题,请参考以下文章

MySQL8.0.25使用插件实现主从半同步复制

MySQL主从配置

mysql主从配置

mysql主从配置

Mysql主从配置

mysql8.0主从配置