如何为生产MySQL配置全新的备库

本文介绍了如何为生产环境配置MySQL备库,包括备库安装、配置、数据同步及主从切换等步骤。强调了版本兼容性、配置参数如server_id、binlog_format和read_only的重要性,并提供了主从配置的建议,如sync_binlog和skip_slave_start的设置。

MySQL版本8.0.12

前言:备库常见的使用场景
  • 不同角色使用不同数据库。
  • 备库当作备用的主库,必要时进行主备切换。
  • 备库放到远程数据中心,用作灾备。
  • 延迟备库,用于数据误删恢复。
  • 备库作为备份、培训、开发或测试用服务器。
1、备机上安装MySQL服务

版本必须等于或高于主库版本,因为MySQL是向前兼容的,高版本的MySQL可以解析低版本的binlog,反之则不一定。

2、进行配置

修改/etc/my.cnf配置文件

  • 配置server_id,主库和备库必须不同
  • 配置bin_log=binlog,建议配置
  • 配置binlog_format=row
  • 备库上配置read_only=1,relay_log=/var/lib/mysql/mysql-relay-bin,log_slave_updates=1(可以让备库成为其他备库的主库)
  • 创建备份用户,建议主备库上都创建
create user 'repl'@'172.17.1.%' identifed by 'passwd';
grant replication slave, replication client on *.* to 'repl'@'172.17.1.%';
3、备库上手动备份主库所有数据
mysqldump -uroot -ppassword --single-transaction --all-databases --master-data=1 --host=mysql-master | mysql --host=mysql-slave -uroot -ppassword
4、备库上切换master
change master to master_host='mysql-master',
master_user='repl',
master_password='password',
master_log_file='binlog.000010',//主库上最新的binlog
master_log_pos=156,//主库上最新的log位置
get_master_public_key=1;	//mysql8版本需要增加这个命令

可以在主库上执行

show master status\G
*************************** 1. row ***************************
             File: binlog.000010
         Position: 156
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
5、启动slave线程
start slave;
show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-master
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000011
          Read_Master_Log_Pos: 11851048
               Relay_Log_File: mysql-relay-bin.000006
                Relay_Log_Pos: 11851257
        Relay_Master_Log_File: binlog.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: 11851048
              Relay_Log_Space: 11851510
              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: 1
                  Master_UUID: e6d56f98-74f2-11eb-8399-5254ec7d2651
             Master_Info_File: mysql.slave_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:
       Master_public_key_path:
        Get_master_public_key: 1
            Network_Namespace:
1 row in set, 1 warning (0.00 sec)
最后:强烈推荐的主从配置
  • 主库上sync_binlog=1,每次提交事务前,把binlog刷到磁盘上,保证服务崩溃时不会丢失事件。
  • innodb_flush_logs_at_trx_commit
  • 指定binlog的名称
  • 备库上设置relay_log绝对路径,可以避免MySQL各版本中存在的bug
  • 备库上设置skip_slave_start,防止备库崩溃后自动启动复制。
  • 备库上设置read_only,防止大部分用户更改非临时表,导致的数据不一致现象。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值