环境说明:
主库(mysql1),从库(mysql2)
现在搭建二级从库(mysql3)从一级从库(mysql2)上同步数据。
一、mysql1上做一个全量备份
1、创建目录
[root@mysql1 ~]# mkdir /db_backup
[root@mysql1~]# chown -R mysql:mysql /db_backup/
2、执行备份
[mysql@mysql1 ~]$ innobackupex --defaults-file=/etc/my.cnf --user=root --password=111111 --slave-info --safe-slave-backup /db_backup
……………………
xtrabackup: Transaction log of lsn (2494657) to (2495681) was copied.
171208 15:46:10 completed OK!
–slave-info会将master的binary log文件名和偏移量保存到xtrabackup_slave_info文件中
–safe-slave-backup会暂停slave的sql线程,待备份结束后再启动
3、拷贝全备文件到mysql3
[mysql@mysql1 ~]$ scp -r /db_backup/2017-12-08_16-44-22/ 192.168.3.203:/home/mysql/
二、丛库上恢复
[mysql@mysql3 ~]$ innobackupex --use-memory=500M --apply-log --redo-only /home/mysql/2017-12-08_16-44-22/
171208 16:47:03 innobackupex: Starting the apply-log operation
IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints “completed OK!”.
innobackupex version 2.4.3 based on MySQL server 5.7.11 Linux (x86_64) (revision id: 6a46905)
xtrabackup: cd to /home/mysql/2017-12-08_16-44-22
xtrabackup: This target seems to be not prepared yet.
InnoDB: Number of pools: 1
xtrabackup: xtrabackup_logfile detected: size=8388608, start_lsn=(2456803)
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = .
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 8388608
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = .
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 8388608
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 524288000 bytes for buffer pool (set by --use-memory parameter)
InnoDB: PUNCH HOLE support available
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Uses event mutexes
InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: Number of pools: 1
InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, total size = 512M, instances = 1, chunk size = 128M
InnoDB: Completed initialization of buffer pool
InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
InnoDB: Opened 3 undo tablespaces
InnoDB: 3 undo tablespaces made active
InnoDB: Will use system tablespace for all newly created rollback-segment as innodb_undo_tablespaces=0
InnoDB: Highest supported file format is Barracuda.
InnoDB: Log scan progressed past the checkpoint lsn 2456803
InnoDB: Doing recovery: scanned up to log sequence number 2456812 (0%)
InnoDB: Doing recovery: scanned up to log sequence number 2456812 (0%)
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: xtrabackup: Last MySQL binlog file position 2714, file name bin.000002
InnoDB: xtrabackup: Last MySQL binlog file position 2714, file name bin.000002
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown…
InnoDB: Shutdown completed; log sequence number 2456821
InnoDB: Number of pools: 1
171208 16:47:05 completed OK!
[mysql@mysql3 ~]$ rm -rf /opt/mysql/data/
[mysql@mysql3 ~]$ rm -f /opt/mysql/log/*
[mysql@mysql3 ~]$ ls /opt/mysql/
log tmp
[mysql@mysql3 ~]$ cp -rp /home/mysql/2017-12-08_16-44-22/ /opt/mysql/
[mysql@mysql3 ~]$ cd /opt/mysql/
[mysql@mysql3 mysql]$ ll
total 12
drwxr-x— 6 mysql mysql 4096 Dec 8 16:17 2017-12-08_16-44-22
drwxr-xr-x 2 mysql mysql 4096 Dec 8 14:56 log
drwxr-xr-x 2 mysql mysql 4096 Dec 8 14:56 tmp
[mysql@mysql3 mysql]$ mv 2017-12-08_16-44-22 data
[mysql@mysql3 mysql]$ ll
total 12
drwxr-xr-x 2 mysql mysql 4096 Dec 8 14:56 log
drwxr-x— 6 mysql mysql 4096 Dec 8 16:17 data
drwxr-xr-x 2 mysql mysql 4096 Dec 8 14:56 tmp
启动mysql!
[mysql@mysql3 ~]$ service mysqld start
Starting MySQL… [ OK ]
三、配置主从(使用gtid的方式)
1、查看mysql2和mysql3参数文件/etc/my.cnf,都已经配置:
gtid_mode = ON
enforce_gtid_consistency = 1
如果没有配置需要添加这两个参数,然后重启mysql实例。
2、mysql2上配置复制用户
[root@localhost][mysql][04:11:48]> GRANT REPLICATION SLAVE ON . TO ‘repl’@‘192.168.3.%’ IDENTIFIED BY ‘repl’;
Query OK, 0 rows affected, 1 warning (0.02 sec)
[root@localhost][mysql][04:11:50]> select host,user from user;
±------------±----------+
| host | user |
±------------±----------+
| 192.168.3.% | repl |
3、模拟备份以后生产库插入新的数据(mysql1上)
[root@localhost][eisoo][02:37:43]> insert into test1 values(6,‘ffffffffff’);
[root@localhost][eisoo][05:02:13]> insert into test1 values(7,‘ggggg’);
4、mysql3丛库配置
(1)查看备份的binlog点(mysql1或者mysql3上都可以)
[mysql@mysql1 ~]$ cat /db_backup/2017-12-08_16-44-22/xtrabackup_binlog_info
bin.000002 2714 a106631b-d8be-11e7-8bc7-000c29ca39fb:1-9
(2)mysql3上配置
[root@localhost][(none)][05:01:02]> reset master;
Query OK, 0 rows affected (0.02 sec)
[root@localhost][(none)][05:01:04]> set global gtid_purged=‘a106631b-d8be-11e7-8bc7-000c29ca39fb:1-9’;
Query OK, 0 rows affected (0.03 sec)
[root@localhost][(none)][05:03:10]> change master to master_host=‘192.168.3.202’,master_port=3306,master_user=‘repl’,master_password=‘repl’,master_auto_position=1;
[root@localhost][(none)][05:07:35]> start slave;
Query OK, 0 rows affected (0.04 sec)
[root@localhost][(none)][05:07:42]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.3.202
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: bin.000003
Read_Master_Log_Pos: 3638
Relay_Log_File: relay.000002
Relay_Log_Pos: 396
Relay_Master_Log_File: 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: 1094
Relay_Log_Space: 2861
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: 11
Master_UUID: b70617d1-db2d-11e7-b05c-000c29bdd9c2
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Waiting for dependent transaction to commit
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: a106631b-d8be-11e7-8bc7-000c29ca39fb:10-13,
b70617d1-db2d-11e7-b05c-000c29bdd9c2:1-3
Executed_Gtid_Set: a106631b-d8be-11e7-8bc7-000c29ca39fb:1-9
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
验证数据:
[root@localhost][(none)][05:07:59]> select * from eisoo.test1;
±-----±-----------+
| id | name |
±-----±-----------+
| 1 | aaaaaaa |
| 2 | bbbbbbbbb |
| 3 | ccccc |
| 4 | ddddddddd |
| 5 | eeeee |
| 6 | ffffffffff |
| 7 | ggggg |
问题处理:
1、启动mysql服务失败
[mysql@slave mysql]$ service mysqld start
Starting MySQL…The server quit without updating PID fil[FAILED]mysql/data/pid).
查看err日志发现如下信息:
2018-01-10T10:44:34.775899+08:00 0 [ERROR] Can’t start server: Bind on TCP/IP port: Address already in use
提示端口已经占用,查看3306端口确实已存在(原因:在替换data目录前没有停掉mysql服务)
[mysql@slave mysql]$ ps -ef | grep 3306
mysql 3350 2133 0 Jan09 pts/0 00:00:31 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/opt/mysql/data/error.log --pid-file=/opt/mysql/data/pid --socket=/opt/mysql/data/mysql.sock --port=3306
mysql 15389 12672 0 10:45 pts/0 00:00:00 grep 3306
解决:kill掉占用3306端口的进程,重新启动mysql
2、start slave时IO进程报错
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
从报错信息可以看到master和slave设置了相同的server_id(低级错误!)
解决:修改slave的server_id,重启slave的mysql服务。
3、重新设置slave的server_id后启动slave进程,再次报错:
[root@localhost][(none)][11:07:30]> start slave;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
解决:
[root@localhost][(none)][11:07:38]> reset slave;
Query OK, 0 rows affected (0.55 sec)
[root@localhost][(none)][11:12:12]> change master to master_host=‘192.168.3.241’,master_port=3306,master_user=‘repl’,master_password=‘repl’,master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
[root@localhost][(none)][11:12:14]> start slave;
Query OK, 0 rows affected (0.06 sec)