[root@wallet01 ~]# wget https://www.percona.com/redir/downloads/percona-release/redhat/latest/percona-release-0.1-6.noarch.rpm
[root@wallet01 ~]# rpm -ivh percona-release-0.1-6.noarch.rpm
[root@wallet01 ~]# yum install -y percona-xtrabackup-24.x86_64
[root@wallet01 ~]# xtrabackup -v
xtrabackup version 2.4.13 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 3e7ca7c)
[root@wallet01 ~]# vi /etc/my.cnf
[mysqld]
server_id = 201
log_bin = mysql-bin
binlog_format = row
sync_binlog = 1
binlog_cache_size = 16M
max_binlog_cache_size = 32M
max_binlog_size = 128M
expire_logs_days = 7
[root@wallet01 ~]# su - mysql
[mysql@wallet01 ~]$ ssh-keygen
[mysql@wallet01 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub mysql@wallet02
[mysql@wallet01 ~]$ ssh wallet02 date
Mon Feb 18 10:41:16 CST 2019
[mysql@wallet01 ~]$ xtrabackup --backup --user=xtrabackup --password=xtrabackup \
--stream=tar | ssh mysql@wallet02 \ "gzip >/home/mysql/backup/`date '+%Y-%m-%d_%H-%M-%S'`.tar.gz"
[mysql@wallet01 ~]$ mysql -uroot -pabcd.1234
mysql> grant replication slave on *.* to 'repl'@'192.168.1.%' identified by 'repl';
Query OK, 0 rows affected (0.11 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)
[root@wallet02 ~]# wget https://www.percona.com/redir/downloads/percona-release/redhat/latest/percona-release-0.1-6.noarch.rpm
[root@wallet02 ~]# rpm -ivh percona-release-0.1-6.noarch.rpm
[root@wallet02 ~]# yum install -y percona-xtrabackup-24.x86_64
[root@wallet02 ~]# xtrabackup -v
xtrabackup version 2.4.13 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 3e7ca7c)
[root@wallet02 ~]# vi /etc/my.cnf
[mysqld]
server_id = 202
master_info_repository = table
relay_log_info_repository = table
relay_log = relay-bin
relay_log_recovery = 1
[root@wallet02 ~]# su - mysql
[mysql@wallet02 ~]$ cd backup
[mysql@wallet02 backup]$ tar izxvf 2019-02-18_10-42-09.tar.gz
[mysql@wallet02 backup]$ ls -lh
total 1.6G
-rw-rw-r-- 1 mysql mysql 515M Feb 18 10:43 2019-02-18_10-42-09.tar.gz
-rw-rw---- 1 mysql mysql 423 Feb 18 10:43 backup-my.cnf
-rw-rw---- 1 mysql mysql 1.0G Feb 18 10:42 ibdata1
drwxrwxr-x 2 mysql mysql 4.0K Feb 18 10:47 mysql
drwxrwxr-x 2 mysql mysql 4.0K Feb 18 10:47 performance_schema
drwxrwxr-x 2 mysql mysql 4.0K Feb 18 10:47 tpcc100
-rw-rw---- 1 mysql mysql 10M Feb 18 10:42 undo001
-rw-rw---- 1 mysql mysql 10M Feb 18 10:42 undo002
-rw-rw---- 1 mysql mysql 10M Feb 18 10:42 undo003
-rw-rw---- 1 mysql mysql 27 Feb 18 10:43 xtrabackup_binlog_info
-rw-rw---- 1 mysql mysql 121 Feb 18 10:43 xtrabackup_checkpoints
-rw-rw---- 1 mysql mysql 490 Feb 18 10:43 xtrabackup_info
-rw-rw---- 1 mysql mysql 21M Feb 18 10:43 xtrabackup_logfile
[mysql@wallet02 backup]$ cat xtrabackup_binlog_info
mysql-bin.000226 101300289
[mysql@wallet02 ~]$ xtrabackup --prepare --target-dir=/home/mysql/backup
[mysql@wallet02 ~]$ xtrabackup --copy-back --target-dir=/home/mysql/backup
[root@wallet02 ~]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/wallet02.err'.
. SUCCESS!
[mysql@wallet02 ~]$ mysql -uroot -pabcd.1234
mysql> change master to \
master_host='192.168.1.201', \
master_user='repl', \
master_password='repl', \
master_log_file='mysql-bin.000226', \
master_log_pos=101300289;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.40.34
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000228
Read_Master_Log_Pos: 5518995
Relay_Log_File: relay-bin.000008
Relay_Log_Pos: 5519158
Relay_Master_Log_File: mysql-bin.000228
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: 5518995
Relay_Log_Space: 5519372
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: 201
Master_UUID: b23f4533-2e78-11e9-91e4-000c296e09c1
Master_Info_File: mysql.slave_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:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
参数解析
log-bin:开启二进制日志
server-id:同一组主从服务器的唯一标识
read only:从库为只读状态
binlog_format:二进制日志的格式
log_slave_updates:将slave的IO线程读取的数据变更信息记录到slave的binlog文件中
binlog-do-db:只复制指定的库,在master上使用
binlog-ingore-db:不复制指定的库,在master上使用
relay log:将slave的IO线程读取的数据变更信息记录到slave的relay log文件中
replicate_do_table:只复制指定的表,在slave上使用
replicate_ingore_table:不复制指定的表,在slave上使用
replicate_do_db:只复制指定的库,在slave上使用
replicate_ingore_db:不复制指定的库,在slave上使用
replicate-wild-do-table:只复制通配符匹配的表,在slave上使用
replicate-wild-ingore-table:不复制通配符匹配的表,在slave上使用
master_info_repository:
记录IO线程读取到的master binlog pos记录到文件中或是记录到表中(slave_master_info),
用于slave宕机后根据文件中记录的pos向主库重新获取binlog
relay_info_repository:
记录SQL线程读取的master binlog pos记录到文件中或是记录到表中(slave_relay_log_info),
用于slave宕机后根据文件中记录的pos恢复sql线程
relay_log_recovery:
当slave重启之后会根据slave_relay_log_info重新创建一个文件,SQL线程会根据这个文件进行恢复复制,
IO线程会读取SQL线程的pos,根据这个pos向主库重新获取binlog
slave_net_timeout:slave在多少秒内未收到master的binlog,slave认为是网络超时,并重新连接master
[root@wallet01 ~]# mysql -uroot -pabcd.1234
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.16 sec)
[root@wallet01 ~]# vi /etc/my.cnf
[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=3000
[root@wallet02 ~]# mysql -uroot -pabcd.1234
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.11 sec)
[root@walle02 ~]# vi /etc/my.cnf
[mysqld]
rpl_semi_sync_slave_enabled=1
[root@wallet02 ~]# service mysqld restart
Shutting down MySQL.. [ OK ]
Starting MySQL......... [ OK ]
[root@wallet01 ~]# service mysqld restart
Shutting down MySQL.. [ OK ]
Starting MySQL.... [ OK ]
[root@wallet01 ~]# mysql -uroot -pabcd.1234
mysql> show status like 'rpl_semi_sync%';
+--------------------------------------------+---------+
| Variable_name | Value |
+--------------------------------------------+---------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 1542 |
| Rpl_semi_sync_master_net_wait_time | 538362 |
| Rpl_semi_sync_master_net_waits | 349 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 9083 |
| Rpl_semi_sync_master_tx_wait_time | 2779470 |
| Rpl_semi_sync_master_tx_waits | 306 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 357 |
+--------------------------------------------+---------+
14 rows in set (0.07 sec)
[root@wallet01 ~]# yum install -y percona-toolkit
[root@wallet01 ~]# pt-heartbeat --version
pt-heartbeat 3.0.13
参数:
--user=s User for login if not current user
--ask-pass Prompt for a password when connecting to MySQL
--host=s Connect to host
--port=i Port number to use for connection
--database=s The database to use for the connection
--create-table Create the heartbeat --table if it does not exist
--create-table-engine=s Sets the engine to be used for the heartbeat table
--table=s The table to use for the heartbeat (default heartbeat)
--update Update a master's heartbeat
--interval=f How often to update or check the heartbeat
--monitor Monitor slave delay continuously
--daemonize Fork to the background and detach from the shell
--log=s Print all output to this file when daemonized
--stop Stop running instances by creating the sentinel file
[root@wallet01 ~]# pt-heartbeat --user=root --ask-pass --host=localhost --port=3306 --database=tpcc100 \
--interval=2 --create-table --update --daemonize
[root@wallet01 ~]# mysql -uroot -pabcd.1234
mysql> select * from tpcc100.heartbeat;
+----------------------------+-----------+------------------+----------+-----------------------+---------------------+
| ts | server_id | file | position | relay_master_log_file | exec_master_log_pos |
+----------------------------+-----------+------------------+----------+-----------------------+---------------------+
| 2019-02-18T13:28:12.000630 | 201 | mysql-bin.000229 | 35655793 | NULL | NULL |
+----------------------------+-----------+------------------+----------+-----------------------+---------------------+
1 row in set (0.00 sec)
[root@wallet02 ~]# yum install -y percona-toolkit
[root@wallet02 ~]# pt-heartbeat --version
pt-heartbeat 3.0.13
[root@wallet02 ~]# pt-heartbeat --user=root --ask-pass --host=localhost --port=3306 --database=tpcc100 \
--interval=2 --monitor --daemonize --log=/tmp/slave-lag.log
[root@wallet02 ~]# tail -f /tmp/slave-lag.log
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
0.00s [ 0.00s, 0.00s, 0.00s ]
[root@wallet01 ~]# pt-table-checksum --version
pt-table-checksum 3.0.13
[root@wallet01 ~]# pt-table-checksum --user=root --ask-pass --host=192.168.1.201 --port=3306 --databases=tpcc100 \
--replicate=tpcc100.checksums --nocheck-replication-filters --nocheck-binlog-format
Enter MySQL password:
Checking if all tables can be checksummed ...
Starting checksum ...
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
02-18T14:45:35 0 0 300000 0 9 0 3.024 tpcc100.customer
02-18T14:45:35 0 0 100 0 1 0 0.149 tpcc100.district
02-18T14:45:35 0 0 1 0 1 0 0.148 tpcc100.heartbeat
02-18T14:45:37 0 0 340588 0 5 0 1.942 tpcc100.history
02-18T14:45:37 0 0 100000 0 1 0 0.457 tpcc100.item
02-18T14:45:38 0 0 89670 0 1 0 0.279 tpcc100.new_orders
02-18T14:45:49 0 0 3403055 0 20 0 11.224 tpcc100.order_line
02-18T14:45:50 0 0 340179 0 1 0 1.076 tpcc100.orders
02-18T14:45:57 0 0 1000000 0 11 0 6.767 tpcc100.stock
02-18T14:45:57 0 0 10 0 1 0 0.142 tpcc100.warehouse
[root@wallet02 ~]# mysql -uroot -pabcd.1234
mysql> use tpcc100
Database changed
mysql> set foreign_key_checks=0;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from customer where c_id=1 and c_d_id=1 and c_w_id=1;
Query OK, 1 row affected (0.07 sec)
[root@wallet01 ~]# pt-table-checksum --user=root --ask-pass --host=192.168.1.201 --port=3306 --databases=tpcc100 \
--replicate=tpcc100.checksums --nocheck-replication-filters --nocheck-binlog-format
Enter MySQL password:
Checking if all tables can be checksummed ...
Starting checksum ...
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
02-18T14:49:22 0 1 300000 0 9 0 3.065 tpcc100.customer
02-18T14:49:22 0 0 100 0 1 0 0.143 tpcc100.district
02-18T14:49:22 0 0 1 0 1 0 0.137 tpcc100.heartbeat
02-18T14:49:24 0 0 340579 0 5 0 1.930 tpcc100.history
02-18T14:49:25 0 0 100000 0 1 0 0.440 tpcc100.item
02-18T14:49:25 0 0 89670 0 1 0 0.239 tpcc100.new_orders
02-18T14:49:37 0 0 3403055 0 20 0 11.451 tpcc100.order_line
02-18T14:49:38 0 0 340179 0 1 0 1.056 tpcc100.orders
02-18T14:49:44 0 0 1000000 0 11 0 6.246 tpcc100.stock
02-18T14:49:44 0 0 10 0 1 0 0.146 tpcc100.warehouse
[root@wallet01 ~]# pt-table-sync --print --replicate=tpcc100.checksums h=192.168.1.201,u=root,p=abcd.1234 \
h=192.168.1.202,u=root,p=abcd.1234
REPLACE INTO `tpcc100`.`customer`(`c_id`, `c_d_id`, `c_w_id`, `c_first`, `c_middle`, `c_last`, `c_street_1`, `c_street_2`, `c_city`,
`c_state`, `c_zip`, `c_phone`, `c_since`, `c_credit`, `c_credit_lim`, `c_discount`, `c_balance`, `c_ytd_payment`, `c_payment_cnt`,
`c_delivery_cnt`, `c_data`) VALUES ('1', '1', '1', 'rfpXdvRGZ78lsPR', 'OE', 'BARBARBAR', 'sE74yrapn1A5mn7bRw64', 'VIPbPATmZLe',
'TikIJZ3Ii83T', 'cu', '582037381', '6083691478835012', '2019-02-18 10:28:52', 'GC', '50000', '0.17', '-10.00', '10.00', '1', '0',
'GtbAtoNGbznqOtSPmT6Om5Y9MQfemMW0F886uWMTV9Jr0lEmCL9zQh8m6lOs7sqKyyQsUkKyrdnsN2eZNlYnThXZQtPYjDsi9HYc1satWNlrPNQk7xVac072tWadhRJqgrS
hi0YCMH3jfRUmOxK0yR1pvZSa0jQeZrMHpuTBCUvpvNZIKrGGGH69rWjpG7Upyo6ogXxq1qduDCCW1TmHiqOXN7NdFpSBdWxH1v80lluxw5RxWBDE2bmPh72wwS5Xw3oxyux
hgpecu3XQFidHKPe1UevO4yv01hvxcqfGfHsaLNyY6bpOyRPS5Iq9Ej7GP2BzsoFX655PS2xYdkwaBJ2EZrLoYS2vUBuK07p4CtTdvO9W866HN7VvwqhUGhOisI2qQpuaiND
blN8rRE8mJ') /*percona-toolkit src_db:tpcc100 src_tbl:customer src_dsn:h=192.168.40.34,p=...,u=root dst_db:tpcc100 dst_tbl:customer
dst_dsn:h=wallet02,p=...,u=root lock:1 transaction:1 changing_src:tpcc100.checksums replicate:tpcc100.checksums bidirectional:0
pid:2396 user:root host:wallet01*/;
[root@wallet01 ~]# pt-table-sync --execute --replicate=tpcc100.checksums h=192.168.1.201,u=root,p=abcd.1234 \
h=192.168.1.202,u=root,p=abcd.1234 --noforeign-key-checks
[root@wallet01 ~]# pt-table-checksum --user=root --ask-pass --host=192.168.1.201 --port=3306 --databases=tpcc100 \
--replicate=tpcc100.checksums --nocheck-replication-filters --nocheck-binlog-format
Enter MySQL password:
Checking if all tables can be checksummed ...
Starting checksum ...
TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE
02-18T14:56:31 0 0 300000 0 9 0 2.950 tpcc100.customer
02-18T14:56:31 0 0 100 0 1 0 0.139 tpcc100.district
02-18T14:56:31 0 0 1 0 1 0 0.151 tpcc100.heartbeat
02-18T14:56:33 0 0 340579 0 5 0 1.933 tpcc100.history
02-18T14:56:34 0 0 100000 0 1 0 0.532 tpcc100.item
02-18T14:56:34 0 0 89670 0 1 0 0.250 tpcc100.new_orders
02-18T14:56:45 0 0 3403055 0 20 0 11.300 tpcc100.order_line
02-18T14:56:46 0 0 340179 0 1 0 1.058 tpcc100.orders
02-18T14:56:53 0 0 1000000 0 12 0 6.821 tpcc100.stock
02-18T14:56:53 0 0 10 0 1 0 0.147 tpcc100.warehouse
转载于:https://blog.51cto.com/13598811/2351239