需求:生产环境同步sakila
(root@localhost) [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
+--------------------+
4 rows in set (0.00 sec)
(root@localhost) [(none)]> show variables like '%log%bin%';
+---------------------------------+--------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------+
| log_bin | ON | #主库binlog日志开启
| log_bin_basename | /data/mysql5.6/mysql-bin |
| log_bin_index | /data/mysql5.6/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+--------------------------------+
6 rows in set (0.00 sec)
(root@localhost) [(none)]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 711 |
+---------------+-------+
1 row in set (0.02 sec)
确认端口号
(root@localhost) [(none)]> select @@port;
+--------+
| @@port |
+--------+
| 33061 |
+--------+
1 row in set (0.01 sec)
在主库,对sakila库,做逻辑备份
[root@vhost1 ~]# mysqldump -uroot -S /data/mysql5.6/mysql.sock -B sakila --single-transaction --master-data=2 |gzip>/tmp/sakila.$(date +%F).sql.gz
[root@vhost1 ~]# ls /tmp/
sakila.2016-12-03.sql.gz
创建用户,可以到主库上复制binlog
(root@localhost) [(none)]> grant replication slave on *.* to 'rep'@'192.168.10.%' identified by 'rep';
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [(none)]> flush privileges;
Query OK, 0 rows affected (0.06 sec)
从库编辑my.cnf文件,添加过滤条件白名单
[root@vhost2~]# vi /data/mysql_5.6/my.cnf
binlog-do-db=sakila
同时确认,从库开启binlog
mysql>> show variables like '%log%bin%';
+---------------------------------+--------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------+
| log_bin | ON | #主库binlog日志开启
| log_bin_basename | /data/mysql_5.6/mysqlbin |
| log_bin_index | /data/mysql_5.6/mysqlbin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+--------------------------------+
6 rows in set (0.00 sec)
确保从库server_id与主库不相同
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 712 |
+---------------+-------+
1 row in set (0.00 sec)
重启从库
[root@vhost2~]# mysqladmin shutdown --socket=/data/mysql_5.6/mysql.sock --port=33062
[root@vhost2~]#mysqld_safe --defaults-file=/data/mysql_5.6/my.cnf
导入备份文件
[root@vhost2 ~]# scp root@192.168.10.71:/tmp/sakila.2016-12-03.sql.gz /tmp/
[root@vhost2~]# gzip -d sakila.2016-12-03.sql.gz
[root@vhost2 ~]# mysql -uroot -p -S /data/mysql_5.6/mysql.sock </tmp/sakila.2016-12-03.sql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
+--------------------+
4 rows in set (0.06 sec)
查看备份文件中,binlog和偏移量值CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=313;
[root@vhost2 tmp]# more sakila.2016-12-03.sql
-- MySQL dump 10.13 Distrib 5.6.37, for linux-glibc2.12 (x86_64)
--
-- Host: localhost Database: sakila
-- ------------------------------------------------------
-- Server version 5.6.37-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Position to start replication or point-in-time recovery from
--
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=313;
mysql> help change master to;
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.10.71',
-> MASTER_USER='rep',
-> MASTER_PASSWORD='rep',
-> MASTER_PORT=33061,
-> MASTER_LOG_FILE='mysql-bin.000003',
-> MASTER_LOG_POS=313;
Query OK, 0 rows affected, 2 warnings (0.23 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave statusG;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.71
Master_User: rep
Master_Port: 33061
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 600
Relay_Log_File: vhost1-relay-bin.000002
Relay_Log_Pos: 570
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
replicate-do-db = ljk #白名单,允许复制哪个库
binlog-ignore-db=ljk #黑名单,这个库的修改不记录二进制日志。