使用percona-xtrabackup实现对线上zabbix监控系统数据库的主从同步
业务背景:
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系列(一)centos7搭建zabbix3.0.4服务端及配置详解
http://blog.csdn.net/reblue520/article/details/52136850
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)
至此,主从同步已配置完成,别忘记了添加主从同步的监控