阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS7.1下MySQL数据库主从同步

106次阅读
没有评论

共计 3267 个字符,预计需要花费 9 分钟才能阅读完成。

环境:

Mater:   CentOS7.1  5.5.52-MariaDB  192.168.108.133

Slave:   CentOS7.1  5.5.52-MariaDB  192.168.108.140

1. 导出主服务数据,将主备初始数据同步

master:

//从 master 上导出需要同步的数据库信息
mysqldump -u*** -p*** --database test > test.sql
//将 master 上的备份信息传输到 slave 上
scp /root/test.sql root@192.168.108.140:/opt/

slave:

//进入 slave 的数据库
mysql -u*** -p***
//清空 test 数据库
drop database test
//导入 master 的 test 数据库信息
source /opt/test.sql

2. 配置 master 和 slave 上的 mysql 数据库

master:

//修改 master 的 my.cnf 文件
vim /etc/my.cnf
//master 配置如下,在 [mysqld] 下添加如下配置
#log-bin
server-id          =   1
log_bin            =   master-bin
expire_logs_days   =   10
max_binlog_size    =   100M
binlog-do_db       =   test
binlog_ignore_db   =   mysql
//重启 mysql 数据库
service mysqld restart
//如果安装的是 mariadb 可以重启 mariadb
systemctl restart mariadb.service

slave:

//修改 slave 的 my.cnf 文件
vim /etc/my.cnf
//slave 配置如下,在 [mysqld] 下添加如下配置
server-id         =   2
//重启 mysql 数据库
service mysqld restart
//如果安装的是 mariadb 可以重启 mariadb
systemctl restart mariadb.service

简单说明一下参数配置,保证主备 server-id 唯一。在 master 上需要开启 mysql 的 binlog,log_bin=master_bin,指定 binlog 文件的名称。

3. 创建一个复制用户,具有 replication slave 权限,能保证 slave 能把 master 的数据同步过去

master:

grant replication slave on *.* to 'replication'@'192.168.108.140' identified by 'replication';

4. 获取 master 的 binlog 位置

master:

//进入 mysql 数据库
mysql -u*** -p***
//设置读锁
flush tables with read lock;
//获取 mysql 的 binlog 文件信息和偏移量
show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000010 |     3713 | test         | mysql            |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
//解锁
unlock tables;

5. 设置备端数据库

//进入 mysql 数据库
mysql -u*** -p***
//停止 slave
stop slave;
//设置对应 master 的 binlog 信息
MariaDB [(none)]> change master to
    -> master_host='192.168.108.133',
    -> master_user='replication',
    -> master_password='replication',
    -> master_log_file='master-bin.000010',
    -> master_log_pos=3713;
//启动 slave
start slave;

6. 查看备端状态

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.108.133
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000010
          Read_Master_Log_Pos: 3881
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 698
        Relay_Master_Log_File: master-bin.000010
             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: 3881
              Relay_Log_Space: 994
              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
1 row in set (0.00 sec)

ERROR: No query specified
 
如果:Slave_IO_Running: Yes,Slave_SQL_Running: Yes 则为配置成功,配置错误重复上面操作即可。如果解决不了可通过查看 mysql 日志分析处理。
vim /var/log/mariadb/mariadb.log

7. 测试。其实测试没啥好写的,配置成功之后直接连到主从数据库,在 master 上改变表、字段、数据,slave 会同步变化。

写在最后:当时想的试一试能不能用 mysql 自带的功能做数据库灾备,后来发现 mysql 数据库主从同步会有一些问题。第一个不好脚本化的东西是在同步之前需保证两边的数据库初始信息一样,因为备端配置的 mysql-binlog 位置只是当前主数据库信息的位置,在该位置之前的数据只能通过人工导入。第二个就是 mysql 主从同步时,只能进行数据库的增量同步,不能进行全量同步;还有就是如果备端出现脏数据,多了一条数据,当主那边新增一条主键相同的数据,则同步失败。之后我会尝试的能不能把这些操作脚本化,发现 mysql 自带的同步功能限制性很大,并且手工干预的东西太多了。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/139007tm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3267字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中