percona-xtrabackup工具实现mysql5.6.34的主从同步复制
percona-xtrabackup工具实现mysql5.6.34的主从同步复制
下载并安装percona-xtrabackup工具
# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.7/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm # yum localinstall -y percona-xtrabackup-24-2.4.7-1.el6.x86_64.rpm
1.备份,将mysql数据库整个备份到/opt/目录下
# innobackupex --defaults-file="/etc/my.cnf" --user=root -proot --socket=/tmp/mysql.sock /opt
2.预处理,进行事物检查(也可以拷贝到从库后再进行检查)
# innobackupex --defaults-file="/etc/my.cnf" --user=root -proot --socket=/tmp/mysql.sock --apply-log --use-memory=1G /opt/2017-05-18_00-13-42/
3.scp到从库
[root@centossz008 ~]# scp -r /opt/2017-05-18_01-34-42/ 192.168.3.13:/opt
4.关闭从库,清理从库数据,恢复数据到从库
/etc/init.d/mysqld stop
删除从库的数据和日志信息
[root@node5 ~]# rm -rf /data/mydata/* [root@node5 ~]# rm -rf /data/binlogs/* [root@node5 ~]# rm -rf /data/relaylogs/*
在从库上执行(将数据恢复到数据库中)
[root@node5 ~]# innobackupex --defaults-file="/etc/my.cnf" --user=root --socket=/tmp/mysql.sock --move-back /opt/2017-05-18_01-34-42/
5.修改权限,启动从库
[root@node5 mydata]# chown -R mysql.mysql /data [root@node5 mydata]# /etc/init.d/mysqld start
查看主库中master位置
[root@node5 mydata]# cat /opt/2017-05-18_01-34-42/xtrabackup_binlog_info master-bin.000002 191 4c6237f8-a7da-11e6-9966-000c29f333f8:1-2
6.主库中创建建salve同步用户
mysql> grant replication slave,reload,super on *.* to repluser@192.168.3.13 identified by 'replpass'; mysql> FLUSH PRIVILEGES;
7.从库执行同步
mysql> change master to master_host='192.168.3.12',master_user='repluser',master_password='replpass',master_log_file='master-bin.000002',master_log_pos=191; mysql> start slave; mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.3.12 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000002 Read_Master_Log_Pos: 586 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 710 Relay_Master_Log_File: master-bin.000002 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: 586 Relay_Log_Space: 908 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: 100 Master_UUID: 4c6237f8-a7da-11e6-9966-000c29f333f8 Master_Info_File: /data/mydata/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 4c6237f8-a7da-11e6-9966-000c29f333f8:3-4 Executed_Gtid_Set: 4c6237f8-a7da-11e6-9966-000c29f333f8:3-4 Auto_Position: 0 1 row in set (0.00 sec)
延迟复制:
启用方法:
mysql> stop slave; mysql> change master to master_delay=600; mysql> start slave;
应用场景:
1.误删除恢复
2.延迟测试(当有延迟时业务是否会受影响)
3.历史查询
***************************
mysql master 配置
[root@centossz008 ~]# cat /etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8mb4 [mysqld] port = 3306 innodb_file_per_table = 1 binlog-format=ROW log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=file relay-log-info-repository=file sync-master-info=1 slave-parallel-workers=4 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 server-id=100 report-port=3306 log-bin=/data/binlogs/master-bin max_binlog_size = 200M datadir=/data/mydata socket=/tmp/mysql.sock sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES init-connect = 'SET NAMES utf8mb4' character-set-server = utf8mb4 skip-name-resolve skip-external-locking back_log = 300 max_connections = 1024 max_connect_errors = 6000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 4M binlog_cache_size = 1M max_heap_table_size = 8M tmp_table_size = 16M read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 8M join_buffer_size = 8M key_buffer_size = 4M thread_cache_size = 8 query_cache_type = 1 query_cache_size = 16M query_cache_limit = 2M ft_min_word_len = 4 expire_logs_days = 10 performance_schema = 0 explicit_defaults_for_timestamp default_storage_engine = InnoDB innodb_open_files = 500 innodb_buffer_pool_size = 64M innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 4 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 512M myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M
******************
mysql slave 配置
[root@node5 src]# cat /etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock default-character-set = utf8mb4 [mysqld] port = 3306 innodb_file_per_table = 1 binlog-format=ROW log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=file relay-log-info-repository=file sync-master-info=1 slave-parallel-workers=4 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 server-id=200 report-port=3306 log-bin=/data/binlogs/master-bin relay-log=/data/relaylogs/relay-bin max_binlog_size = 200M datadir=/data/mydata socket=/tmp/mysql.sock sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES init-connect = 'SET NAMES utf8mb4' character-set-server = utf8mb4 skip-name-resolve skip-external-locking back_log = 300 max_connections = 1024 max_connect_errors = 6000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 4M binlog_cache_size = 1M max_heap_table_size = 8M tmp_table_size = 16M read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 8M join_buffer_size = 8M key_buffer_size = 4M thread_cache_size = 8 query_cache_type = 1 query_cache_size = 16M query_cache_limit = 2M ft_min_word_len = 4 expire_logs_days = 10 performance_schema = 0 explicit_defaults_for_timestamp default_storage_engine = InnoDB innodb_open_files = 500 innodb_buffer_pool_size = 64M innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 4 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 512M myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M
*********************
cenos6上启动mysql服务报错:
070517 23:08:52 mysqld_safe Starting mysqld daemon with databases from /data/mydata
2107-05-17 23:08:56 0 [ERROR] This MySQL server doesn't support dates later then 2038
2107-05-17 23:08:56 0 [ERROR] Aborting
将时间修改为1年前,即可启动,启动完成后改回时间即可