Ubuntu 16 MySQL主从备份配置

本文详细介绍了如何在Ubuntu 16环境下配置MySQL 5.7版本的主从复制环境,包括环境搭建、配置文件解析、主从服务器设置及数据同步验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境准备

  1. 机器准备
  • master 192.168.1.101
  • slave 192.168.1.102
  1. 操作系统 Ubuntu 16
  2. MySQL版本5.7

配置文件说明

  1. Ubuntu中mysql配置文件在/etc/mysql可以找到其结构如下
/etc/mysql
├── conf.d
│   ├── mysql.cnf
│   └── mysqldump.cnf
├── debian.cnf #自动生成的系统脚本,不要动它
├── debian-start #5.7前该脚本用于检查奔溃的表和无密码的账户,5.7以后只是个空脚本
├── my.cnf -> /etc/alternatives/my.cnf #全局配置文件
├── my.cnf.fallback #用于回退全局配置文件
├── mysql.cnf 
└── mysql.conf.d
    ├── mysqld.cnf #配置的模板,可以拷贝至/etc/mysql/my.cnf和~/.my.cnf
    └── mysqld_safe_syslog.cnf #系统日志配置如果需要定制则注释该文件配置再my.cnf中配置
  1. 配置文件加载顺序
#使用下面命令查看
/usr/sbin/mysqld --verbose --help | grep -A -1 2 'Default options'
  1. 可以查看到如下内容,指明配置文件加载顺序如下/etc/my.cnf/etc/mysql/my.cnf~/.my.cnf
Default options are read from the following files in the given order:/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
  1. my.cnf文件中存在如下两句话,表示配置包含!includedir文件夹中的配置
...
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

Master 配置

  1. 配置参数文件/etc/mysql/my.cnf
[mysqld]
server-id=1                    #服务ID需要确保唯一性
log-bin=mysql-bin              #指定日志目录 ubuntu 默认 /var/lib/mysql/mysql-bin目录
#使事务的复制中尽最大可能的保证持久性和一致性
innodb_flush_log_at_trx_commit=1
#事务日志直接写入文件,性能降低,但高可靠
sync_binlog=1
  1. 重启master
/etc/init.d/mysql restart
  1. Master中创建用于同步的账号
CREATE USER 'repl'@'192.168.1.102' IDENTIFIED BY 'slavepass';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.102';
  1. Master中获取二进制日志坐标
--master加锁
FLUSH TABLES WITH READ LOCK;
--查看master状态信息
SHOW MASTER STATUS;

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | |--|--|--|--|--| |mysql-bin.000001|619|-|-|-|

Slave 配置

  1. 配置参数文件/etc/mysql/my.cnf
[mysqld]
server-id=2
  1. 重启Slave
/etc/init.d/mysql restart
  1. Master中释放锁
UNLOCK TABLES;
  1. Slave 配置主站信息
CHANGE MASTER TO MASTER_HOST='192.168.1.101',MASTER_USER='repl',MASTER_PASSWORD='slavepass',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=619;
  1. Slave中开启同步
mysql> START SLAVE;

测试

  1. 查看slave同步状态
show slave status\G

其中Slave_IO_RunningSlave_SQL_Running均为YES表示slave已经启动同步

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.101
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 619
               Relay_Log_File: iZuf60xdp6y5avwotnjkm9Z-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             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: 154
              Relay_Log_Space: 545
              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: 
             Master_Info_File: /var/lib/mysql/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. master配置要同步的数据库 创建测试库和表
create database test;
use test;
create table a(a int);

插入测试数据

insert into a(a)alues(1);
insert into a(a)alues(2);
insert into a(a)alues(3);
  1. slave上查看是否进行了同步
show databases;
use test;
select * from a;

得到结果

|a| |--| |1| |2| |3|

总结

本文是最简单的主从配置,一些高级特性需要参考MySQL手册进行定制,请读者自行查阅。

转载于:https://my.oschina.net/u/2264300/blog/899036

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值