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

使用Percona XtraBackup实现对线上Zabbix监控系统数据库的主从同步

327次阅读
没有评论

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

业务背景:
Zabbix3.0.4 是业务的主要监控,部署在一台单机中,为避免数据丢失先对其做数据主从同步,因主数据库已经运行了一段时间,数据量大概有 10G 左右为快速实现主从,使用 percona-xtrabackup 工具进行备份和恢复。

master db:
操作系统: CentOS7.2 x86_64
mysql: mariadb 5.5.47
通过 yum 安装的 mariadb(yum -y install mariadb mariadb-server php php-mysql httpd)
具体的过程参考:Zabbix 系列教程:CentOS 7 搭建 Zabbix3.0.4 服务端及配置详解  http://www.linuxidc.com/Linux/2016-11/137638.htm

slave db:
操作系统:centos6.5

1. 在 slave 中安装 mariadb

因之前安装过 mysql5.7 需要卸载
rpm -e –nodeps mysql*

删除残留文件,否则会无法启动 mariadb
rm -rf /var/lib/mysql
rm -f /etc/my.cnf
rm -rf /etc/my.cnf.d

先创建关于 mariadb 的 yum 源
vi /etc/yum.repos.d/MariaDB.repo

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5.47/centos6-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

yum 安装
yum install MariaDB-server MariaDB-client

编辑 my.cnf 文件
vim /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d

2. 主从服务器均开启 binlog 日志,从服务器开启 relay-log:

log-bin=/data/binlog/mysql-bin
binlog-do-db=zabbix
binlog-ignore-db = mysql
expire_logs_day=15
max_binlog_size = 200M
binlog_format=mixed
server-id = 1

验证:
MariaDB [(none)]> show binary logs;
+——————+———–+
| Log_name        | File_size |
+——————+———–+
| mysql-bin.000001 |    396365 |
+——————+———–+
1 row in set (0.00 sec)

从服务器配置,开启中继日志:
vim /etc/my.cnf.d/server.cnf

[mariadb]
log-bin=/data/binlog/mysql-bin
relay-log = /data/relaylog/relay-bin
binlog_format=mixed
server-id=10
replicate_wild_do_table=zabbix.% 
replicate_wild_ignore_table=mysql.%
改变权限
chown -R mysql.mysql /data/

3. 备份主服务器的数据库 zabbix
在 master 和 slave 两台服务器中都安装 percona-xtrabackup
方法:
上传文件 percona-toolkit-2.2.4-1.noarch.rpm、percona-xtrabackup-2.1.8-733.rhel6.x86_64.rpm 到服务器中
yum localinstall percona-*.rpm

备份 zabbix
/usr/bin/innobackupex –user=root –password=root#$!#140341@ –databases=”zabbix” /data/

报错如下:

161114 13:53:03  innobackupex: Connecting to MySQL server with DSN ‘dbi:mysql:;mysql_read_default_file=/etc/my.cnf;mysql_read_default_group=xtrabackup;host=localhost’ as ‘root’  (using password: YES).
innobackupex: Error: Failed to connect to MySQL server: DBI connect(‘;mysql_read_default_file=/etc/my.cnf;mysql_read_default_group=xtrabackup;host=localhost’,’root’,…) failed: Access denied for user ‘root’@’localhost’ (using password: YES) at /usr/bin/innobackupex line 2945.
原因分析:
1. 在相同的 centos7.2 的 mariadb5.5.47 其他环境是可以的,排除操作系统和数据库与 xtrabackup 的兼容性问题
2. 使用 zabbix 账户可以进入命令行,但是后面权限不足报错,排除软件本身安装的问题
3. 以前碰到过 mysql 密码复杂以后无法修改数据库的问题,于是把 root 密码修改成简单的 root
并 flush privileges 后,发现问题解决

可能是命令行工具的 bug,当密码配置复杂以后,特别是有特殊符号后命令行工具不能正确识别

继续报错:

xtrabackup_55: Error writing file ‘/data/2016-11-14_15-00-33/ibdata1’ (Errcode: 28)
[01] xtrabackup: Error: xtrabackup_copy_datafile() failed.
[01] xtrabackup: Error: failed to copy datafile.
innobackupex: Error: The xtrabackup child process has died at /usr/bin/innobackupex line 2622. 解决办法:
将文件夹 /data/2016-11-14_15-00-33 删除,重新执行备份即可

成功提示:

>> log scanned up to (130775113381)
xtrabackup: Creating suspend file ‘/data/2016-11-14_15-08-34/xtrabackup_suspended_2’ with pid ‘5883’

161114 15:10:04  innobackupex: Continuing after ibbackup has suspended
161114 15:10:04  innobackupex: Starting to lock all tables…
161114 15:10:04  innobackupex: All tables locked and flushed to disk

161114 15:10:04  innobackupex: Starting to backup non-InnoDB tables and files
innobackupex: in subdirectories of ‘/var/lib/mysql’
innobackupex: Backing up files ‘/var/lib/mysql/zabbix/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}’ (114 files)
>> log scanned up to (130775124431)
161114 15:10:04  innobackupex: Finished backing up non-InnoDB tables and files

161114 15:10:04  innobackupex: Waiting for log copying to finish

xtrabackup: The latest check point (for incremental): ‘130773362503’
xtrabackup: Stopping log copying thread.
.>> log scanned up to (130775124431)

xtrabackup: Creating suspend file ‘/data/2016-11-14_15-08-34/xtrabackup_log_copied’ with pid ‘5883’
xtrabackup: Transaction log of lsn (130772830819) to (130775124431) was copied.
161114 15:10:05  innobackupex: All tables unlocked

innobackupex: Backup created in directory ‘/data/2016-11-14_15-08-34’
innobackupex: MySQL binlog position: filename ‘mysql-bin.000003’, position 2848177
161114 15:10:05  innobackupex: Connection to database server closed
161114 15:10:05  innobackupex: completed OK!
4. 执行数据库事物一致性

innobackupex –apply-log /data/2016-11-14_15-08-34/

[notice (again)]
  If you use binary log and don’t use any hack of group commit,
  the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 2848177, file name /data/binlog/mysql-bin.000003

xtrabackup: starting shutdown with innodb_fast_shutdown = 1
161114 15:12:20  InnoDB: Starting shutdown…
161114 15:12:24  InnoDB: Shutdown completed; log sequence number 130775124492
161114 15:12:24  innobackupex: completed OK!
将数据拷贝到要恢复的服务器上:
scp -r /data/2016-11-11_16-03-44 x.x.x.x:/data

5. 从服务器上的操作:
停用数据库服务
service mysql stop
删除原来的文件,并用备份的替换
# rm -f /var/lib/mysql/ibdata1
# rm -f /var/lib/mysql/ib_logfile0
# rm -f /var/lib/mysql/ib_logfile1

mv /data/2016-11-11_16-03-44/ibdata1 ib_logfile0 ib_logfile1 zabbix /var/lib/mysql/
chown -R mysql.mysql /var/lib/mysql/

5. 从数据库能正常访问后,配置主从

在主服务器上建立同步帐号
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘repluser’@’10.19.110.36’ IDENTIFIED BY ‘replpass’;
mysql> FLUSH PRIVILEGES;

MariaDB [(none)]> show master status;
+——————+———-+————–+——————+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000003 | 10829376 | zabbix      | mysql            |
+——————+———-+————–+——————+
1 row in set (0.00 sec)

从服务器
mysql> CHANGE MASTER TO MASTER_HOST=’10.19.50.236′,MASTER_USER=’repluser’,MASTER_PASSWORD=’replpass’,MASTER_LOG_FILE=’mysql-bin.000003′,MASTER_LOG_POS=10829376;

MariaDB [zabbix]> start slave;

出现如下信息,说明主从同步配置成功

MariaDB [zabbix]> show slave status\G
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.19.50.236
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 11515844
              Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 316264
        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: zabbix.%
  Replicate_Wild_Ignore_Table: mysql.%
                  Last_Errno: 0
                  Last_Error:
                Skip_Counter: 0
          Exec_Master_Log_Pos: 11515844
              Relay_Log_Space: 316552
              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)

至此���主从同步已配置完成,别忘记了添加主从同步的监控。

更多 XtraBackup 相关教程见以下内容

MySQL 管理之使用 XtraBackup 进行热备 http://www.linuxidc.com/Linux/2014-04/99671.htm

MySQL 开源备份工具 Xtrabackup 备份部署 http://www.linuxidc.com/Linux/2013-06/85627.htm

MySQL Xtrabackup 备份和恢复 http://www.linuxidc.com/Linux/2011-12/50275.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-11/137643.htm

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