1.环境
----------------------------------------------------------|
|mysql版本 | 5.7.14 | 5.7.14 |
|----------|----------------------------------------------|
|ip |192.168.26.233(master) | 192.168.26.75 (slave)|
|port | 3306 | 3306 |
|---------------------------------------------------------|
master端操作
1. 主库必备参数:
server-id=3306100
log-bin = /data/mysql/mysql_3306/logs/mysql-bin
binlog_format=row
可选: row
2. 获取position和binlog位置
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000016
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
3.创建复制账号
mysql> create user 'repl'@'192.168.26.%' identified by 'repl';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'repl'@'192.168.26.%';
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+-----------+--------------+
| user | host |
+-----------+--------------+
| repl | 192.168.26.% |
| admin | localhost |
| mysql.sys | localhost |
| root | localhost |
+-----------+--------------+
4 rows in set (0.00 sec)
4.在slave机器上测试账号能不能登陆
[root@zw_test_26_75 ~]# mysql -h 192.168.26.233 -urepl -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.14-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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.
slave端操作
1. 从库必备参数:
server-id=3306101 (server-id不能重复)
log-bin = /data/mysql/mysql_3306/logs/mysql-bin
log_slave_updates
如果没加log_slave_updates 参数复制的时候 a->b->c不行的
2. 生成一个 change master to 语句,执行
root@localhost [(none)]>change master to master_host='192.168.26.233', master_port=3306, master_user='repl',master_password='repl',master_log_file='mysql-bin.000016',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.77 sec)
3.查看slve的状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000016
Read_Master_Log_Pos: 154
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000016
Slave_IO_Running: No --正常应该是yes
Slave_SQL_Running: No --正常应该是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: 154
Relay_Log_Space: 154
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: NULL
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: 0
Master_UUID:
Master_Info_File: /data/mysql/mysql_3306/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
4.启动slave
root@localhost [(none)]>start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 154
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000017
Slave_IO_Running: Yes --yes状态正常
Slave_SQL_Running: Yes --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: 154
Relay_Log_Space: 1197
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: 3306100
Master_UUID: 7e354a2c-6f5f-11e6-997d-005056a36f08
Master_Info_File: /data/mysql/mysql_3306/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
常用命令:
start slvae; 开始复制
stop slave; 停止复制
start slave io_thread/sql_thread 针对复制线程开启关闭:
测试:
1. 查看master的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zw |
+--------------------+
2. 查看slave的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
3.master 创建test数据库
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
4.查看slave的数据库,可以看到test数据库同步过来了
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
5. master test数据库上创建一个表t1
mysql> use test
Database changed
mysql> create table t1(id int(5));
Query OK, 0 rows affected (0.16 sec)
mysql> insert into t1 values(1);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
6. 查看slave的数据库
mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
mysql> select * from t1;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
可以看到 t1表和里面的一条数据也同步过来了
---------------------------------------------------------
可以看到新的数据都同步过来了,但是搭建主从复制之前的maser上 数据库zw没有复制到从库,这就需要对元数据的迁移
1. master端 mysqldump把数据导出:
[root@zw-test-db mysql_3306]# mysqldump -S /tmp/mysql3306.sock -uroot -p --single-transaction --master-data=2 -A > /tmp/full_backup.sql
Enter password:
-A 导出全库
--single-transaction 来保证备份的一致性
--master=2 可以得到binlog和pos的值
加上master=2 dump出来的有这一句话: -- change master to master_log_file='mysql-bin.000017', master_log_pos=731;
2.拷贝到slave端
3.slave停止同步
mysql> stop slave;
Query OK, 0 rows affected (0.11 sec)
4.清空slave信息
mysql> reset slave all;
Query OK, 0 rows affected (0.12 sec)
5.恢复到slave
[root@llmj-svn-26-81 mysql]# mysql -S /tmp/mysql3306.sock -uroot -p < /tmp/full_backup.sql
Enter password:
6. 查看master同步之前的表有没有恢复到slave
mysql> use zw;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+--------------+
| Tables_in_zw |
+--------------+
| mycode |
+--------------+
1 row in set (0.00 sec)
mysql> select * from mycode;
+----+---------+
| id | content |
+----+---------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
| 6 | fff |
| 7 | ggg |
| 8 | hhh |
+----+---------+
8 rows in set (0.00 sec)
可以看到master之前的库和表都同步到slave了
7. 执行完后,找到/tmp/full_backup.sql,查看log-bin 和pos的位置
-- change master to master_log_file='mysql-bin.000017', master_log_pos=731;
8. 重新生成change master to
root@localhost [(none)]>change master to master_host='192.168.26.233', master_port=3306, master_user='repl',master_password='repl',master_log_file='mysql-bin.000017',master_log_pos=731;
9.查看slave的状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 731
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000017
Slave_IO_Running: No
Slave_SQL_Running: No
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: 731
Relay_Log_Space: 154
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: NULL
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: 0
Master_UUID:
Master_Info_File: /data/mysql/mysql_3306/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
10.启动slave
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 731
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000017
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: 731
Relay_Log_Space: 521
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: 3306100
Master_UUID: 7e354a2c-6f5f-11e6-997d-005056a36f08
Master_Info_File: /data/mysql/mysql_3306/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
11. master zw库的 mycode表删除id为单数的表
mysql> delete from mycode where id in (2,4,6,8);
Query OK, 4 rows affected (0.00 sec)
mysql> select * from mycode;
+----+---------+
| id | content |
+----+---------+
| 1 | aaa |
| 3 | ccc |
| 5 | eee |
| 7 | ggg |
+----+---------+
4 rows in set (0.00 sec)
12. 查看slave 对应的表,可以看到数据同步过来了
mysql> select * from mycode;
+----+---------+
| id | content |
+----+---------+
| 1 | aaa |
| 3 | ccc |
| 5 | eee |
| 7 | ggg |
+----+---------+
4 rows in set (0.00 sec)
到此binlog+position搭建结束。