环境:
使用2台虚拟机,如图-1所示。其中192.168.4.51是主服务器,另一台192.168.4.52作为从服务器,通过调取主服务器上的binlog日志,在本地重做对应的库、表,实现与主服务器的数据同步。 主服务器、从服务器都已安装好mysql5.7.44

步骤一:配置主服务器192.168.4.51
启用binlog日志
[root@master ~]vim /etc/my.cnf
        binlog配置
下面为mysql5.7 中的binglog配置,在mysql8.0 中有区分
            
            
              plain
              
              
            
          
          server_id=51
log-bin=mysql-bin
max_binlog_size = 100M
binlog_format=row
log_slave_updates
expire_logs_days=7
        用户授权
用户名自定义、客户端地址使用% 或 只指定 从服务器的地址 都可以、只给复制数据的权限即可。
            
            
              plain
              
              
            
          
          [root@master ~]# mysql -uroot -p123456
mysql> mysql> grant replication slave on *.* to repluser@"%" identified by "srebro@test";
mysql> quit;
        步骤二:配置从服务器192.168.4.52
在从服务器上,指定server_id
Server_id值可以自定义,但不可以与主服务器相同
            
            
              plain
              
              
            
          
          vim /etc/my.cnf
#主从
server_id=52
relay-log=mysqld-relay-bin
        在主服务器上备份数据
步骤大致如下:
- mysqldump 备份主库数据,加上--master-data选项
 - 登录从库,指定主服务器信息,指定主从同步用户,密码
 - 登录从库 source XXX.sql; 恢复数据
 - start slave; 启动主从同步
 - show slave status\G; 查看主从同步状态
 - 查看主库 和 从库的 数据库 数量是否一致!!!
 
指定mysqldump用户名密码
            
            
              plain
              
              
            
          
          [root@localhost ~]# vim /home/.my.cnf 
[mysqldump]
host=localhost
user=root
password=123456
        找出需要备份的库名
            
            
              plain
              
              
            
          
          name=`/home/application/mysql/app/bin/mysql --defaults-extra-file=/home/.my.cnf -e "show databases;" | grep -Ev "^(Database|sys|information_schema|mysql|performance_schema)$" | tr "\n"  " "`
        备份数据,加上存储过程,加上备份时刻主服务器的二进制日志文件名(LOG_FILE)和位置(LOG_POS)。
            
            
              plain
              
              
            
          
          /home/application/mysql/app/bin/mysqldump --defaults-extra-file=/home/.my.cnf --master-data --routines --set-gtid-purged=OFF --single-transaction --databases $name > /home/application/mysql/bak/full_bak/full_`date +"%F"`.sql 2>&1
        将备份文件拷贝给从服务器
[root@master ~]# scp /home/application/mysql/bak/full_bak/full_2024-1230.sql root@192.168.4.52:/root/ 
        登录从服务器,指定主服务器信息,指定主从同步用户,密码
这里无需指定主服务器的二进制日志文件名(LOG_FILE)和位置(LOG_POS),我们在备份sql的时候会带上备份时刻主服务器的二进制日志文件名
            
            
              plain
              
              
            
          
          [root@slave ~]# mysql -uroot --pxxx
change master to \
master_host="192.168.10.5", \
master_user="repluser", \
master_password="srebro@test";
        tips: 设置从库延迟同步时间
主要是防止意外的数据删除或更新
            
            
              plain
              
              
            
          
          #设置从库延迟一小时同步主库
CHANGE MASTER TO master_delay = 3600
        在从服务器使用备份文件恢复数据
[root@slave ~]#mysql -uroot --pXXX < /root/full_2024-1230.sq
        在从服务器上,启动slave服务
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 event
                  Master_Host: 172.16.0.244
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 6257382
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000011
             Slave_IO_Running: Yes
            Slave_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_Master_Log_Pos: 6257382
              Relay_Log_Space: 531
              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: 244
                  Master_UUID: 7a4f4a6a-5693-11ec-9810-3cecef8451c5
             Master_Info_File: /home/application/mysql/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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.01 sec)
ERROR: 
No query specified
        可选配置:一般加上该配置
            
            
              plain
              
              
            
          
          vim /etc/my.cnf
#限定普通用户只读
read_only=1
 
#限定root只读
super_read_only=on