优势:
- 无需停止数据库进行InnoDB热备
- 增量备份MySQL
- 流压缩到传输到其它服务器
- 能比较容易地创建主从同步
- 备份MySQL时不会增大服务器负载
同步的过程:master将改变记入binary log,通过dump线程传输给slave,slave通过I/O线程将binlog记录进中继日志(relay log),而slave之后便通过SQL线程执行relay log中的步骤,从而达达到主从一致。
复制类型:
半同步复制,异步复制
半同步复制:主库写完binlog,通过dump线程传输,从库通过I/O线程接收后,写入relaylog,而不等待SQL线程进行操作就返回确认给客户端
异步复制:主库写完binlog后即返回确认给客户端
安装过程:
1,下载二进制包:
[[email protected] ~]# cd /usr/local/src/ [[email protected] src]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.7/binary/tarball/percona-xtrabackup-2.4.7-Linux-x86_64.tar.gz (过程省略) [[email protected] src]# tar xvf /usr/local/src/percona-xtrabackup-2.4.7-Linux-x86_64.tar.gz /usr/local/src [[email protected] src]# tar xvf percona-xtrabackup-2.4.7-Linux-x86_64.tar.gz [[email protected] src]# mv percona-xtrabackup-2.4.7-Linux-x86_64 /usr/local/xtrabackupex [[email protected] src]# ln -s /usr/local/xtrabackupex/bin/innobackupex /usr/local/innobackupex
(给innobackupex做个软连接到/usr/bin下,这样就可以直接使用此命令了)
2,修改my.cnf,增加server-id=128(可根据ip来定)和log_bin=alex(logbin的前缀)
[[email protected] mysql]# /etc/init.d/mysqld restart##重启mysqld Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS!
3,创建用作同步数据的用户
[[email protected] src]# mysql -uroot -p123456 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.36-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant replication slave on *.* to 'repl'@192.168.65.129 identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +-------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------+----------+--------------+------------------+-------------------+ | alex.000001 | 410 | | | | +-------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
记住File 和 Position,是 alex.000001 和 410
4,全量备份:
[[email protected] src]# innobackupex --defaults-file=/etc/my.cnf --socket=/tmp/mysql.sock --user=root --password=123456 /tmp/ [[email protected] src]# ls /tmp/ 1212.txt mysql_all.sql systemd-private-596758be2a0e48f7922dd9f1aa755aa1-chronyd.service-6aV5Qx 2018-04-05_19-39-02 mysql.sock systemd-private-596758be2a0e48f7922dd9f1aa755aa1-vgauthd.service-B6YXxw aming.sock mysql.sql systemd-private-596758be2a0e48f7922dd9f1aa755aa1-vmtoolsd.service-EVFEfv hsperfdata_root percona-version-check user.sql mysql2.sql php-fcgi.sock
5,传输备份数据到从上:
[[email protected] src]# scp -r /tmp/2018-04-05_19-39-02/ 192.168.65.129:/tmp/
6,准备prepare:
[[email protected] tmp]# innobackupex --defaults-file=/etc/my.cnf --socket=/tmp/mysql.sock --user=root --password=123456 --apply-log /tmp/2018-04-05_19-39-02/
(--apply-log的作用是通过回滚未提交的事务及同步已经提交的事务至数据文件使数据文件处于一致性状态)
7,恢复数据(确认datadir为空,如果不为空先备份再删除):
[[email protected] tmp]# innobackupex --defaults-file=/etc/my.cnf --socket=/tmp/mysql.sock --user=root --password=123456 --copy-back /tmp/2018-04-05_19-39-02/ [[email protected] tmp]# ls /data/mysql ibdata1 ib_logfile0 ib_logfile1 ibtmp1 mysql mysql2 performance_schema test xtrabackup_info [[email protected] tmp]# cp /data/mysql.bak/localhost.localdomain.err /data/mysql [[email protected] tmp]# cp /data/mysql.bak/localhost.localdomain.pid /data/mysql
(如果没有localhost.localdomain.pid和.err会报错)
8,更改datadir的属主属组为mysql后重启
[[email protected] tmp]# chown -R mysql:mysql /data/mysql [[email protected] tmp]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! [[email protected] tmp]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [[email protected] tmp]# ps aux|grep mysqld root 4374 0.0 0.1 113264 1616 pts/0 S 19:48 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.localdomain.pid mysql 4470 2.6 45.1 1300812 451340 pts/0 Sl 19:48 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid root 4494 0.0 0.0 112676 984 pts/0 R+ 19:48 0:00 grep --color=auto mysqld
9,更改my.cnf,主要是server_id的值,和主的id区分开
[[email protected] tmp]# vim /etc/my.cnf
[[email protected] tmp]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [[email protected] tmp]# mysql -uroot -p123456 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec)
10,change master (master_log_file=, master_log_pos=值为之前主上show master status的值)
mysql> change master to master_host='192.168.65.128', master_user='repl', master_password='123456', master_log_file='alex.000001', master_log_pos=410; Query OK, 0 rows affected, 2 warnings (0.02 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
11,在主上添加一个新的数据库,看从上的变化
从————>
主————>
mysql> create database alex;
Query OK, 1 row affected (0.00 sec)
从————>
主从同步成功
常见问题
1,Slave_IO_Running: No
查看错误问题:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
查看主从服务器两台机器/data/mysql/auto.cnf的UUID是否一样,如果一样,删除其中一个,并重启该服务器,再次进行从服务器mysql的change master操作就能成功了。
2,(网上看来的)
一般是事务回滚造成的
stop slave;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
原文地址:http://blog.51cto.com/11530642/2095117