mysql版本5.7

外网访问需要”申请外网连接”

从库配置

查看数据库版本

mysql --version

停止从库的mysql服务

systemctl stop mysqld

修改从库MySQL的配置文件

vim /etc/my.cnf

mysqld下添加如下配置

server-id=2222 #服务 ID,主从实例 server-id 需不同。
log_bin=mysql-bin.log #这里要确保路径存在
expire_logs_days=10
max_binlog_size=100M
replicate-do-db=exampledb #需要同步的数据库
replicate-ignore-db=mysql  #不需要同步的数据库
replicate-ignore-db=information_schema #不需要同步的数据库
replicate-ignore-db=performance_schema  #不需要同步的数据库
 
 
#配置GTID同步模式,binlog格式为ROW,同样放置在[mysqld]下:
 
#GTID
gtid_mode=on  # MySQL实例开启GTID模式
enforce_gtid_consistency=on # 使用GTID模式复制时,需要开启此参数,用来保证GTID的一致性。
binlog_format=row  # 强烈建议 binlog_format 使用ROW格式,其他格式可能造成数据不一致。
log-slave-updates=1  #当SLAVE数据库启动的时候,SLAVE不会自动开启复制

进入MySQL,执行语句,查看配置是否生效(值为ROW即生效):

mysql> show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+

将主库数据全量导出(可以用mysqlfront之类的工具)并导入到从库中

从库导入数据
CREATE DATABASE 数据库名;
USE 数据库名;
SOURCE 导出的文件名.sql

启动slave,因为之前是在配置文件中配置的主从,报如下错误:

mysql> start slave;
ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO

可以通过,在从库中执行命令,配置主库:

change master to master_host='rm-2ze9nr61bnvokza924o.mysql.rds.aliyuncs.com',
master_port=3306,
master_user='***********',
master_password='***********', 
master_log_file='mysql-bin.000003',#Master服务器产生的日志    主库 show master status 获取
master_log_pos=129558; #binlog起始位置   主库 show master status 获取

执行上述命令报错:

Access denied; you need (at least one of) the SUPER privilege(s) for this operation
从库需要超级管理员权限才可以执行以上命令

启动slave:

start slave;

查看slave状态,需要注意 Slave_IO_Running 和 Slave_SQL_Running: show slave status\G:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: rm-2ze9nr61bnvokza924o.mysql.rds.aliyuncs.com
                  Master_User: fengguowuhen
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 139146
               Relay_Log_File: VM-24-3-centos-relay-bin.000002
                Relay_Log_Pos: 9908
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: testdb
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           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: 139146
              Relay_Log_Space: 10124
              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: 20742498
                  Master_UUID: b960ea63-9050-11ec-b8f4-00163e34b379
             Master_Info_File: /www/server/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: b960ea63-9050-11ec-b8f4-00163e34b379:460-493
            Executed_Gtid_Set: 2c142236-5a62-11ec-aa71-5254007d026f:1-4,
b960ea63-9050-11ec-b8f4-00163e34b379:460-493
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

停止主从:
stop slave;

重新配置主从:
stop slave;
reset master;

从库在同步的时候报如下错误:
2022-03-13T14:10:59.186136Z 672 [Warning] Slave: Unknown or incorrect time zone: ‘Asia/Shanghai’ Error_code: 1298
2022-03-13T14:10:59.186140Z 672 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with “SLAVE START”. We stopped at log ‘mysql-bin.000097’ position 306148.
解决方案:到https://dev.mysql.com/downloads/timezones.html下载时区文件,解压的sql文件在从库中运行sql导入时区数据后,重新start slave即可。
timezone_2021e_posix_sql.zip – POSIX standard

发表评论

邮箱地址不会被公开。 必填项已用*标注