MySQL5.7基于mysqldump的主从复制

1创建账号
创建用于复制的账号
GRANT REPLICATION SLAVE ON . TO ‘repl’@’192.168.1.%’ IDENTIFIED BY PASSWORD ‘repl4salve’;
创建用于监控的账号
grant replication client on . to ‘monitor’@’192.168.1.%’ identified by’m0n1tor’;
2 备份数据
[root@node1 backup]# mysqldump -uroot -hlocalhost -p -S /tmp/3306.sock –single-transaction –master-data=2 -A >3306.sql
Enter password:
[root@node1 backup]# ls
3306.sql
[root@node1 backup]# more 3306.sql

– MySQL dump 10.13 Distrib 5.7.9, for linux-glibc2.5 (x86_64)

– Host: localhost Database:


– Server version 5.7.9-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.000006’, MASTER_LOG_POS=154;


记录下这个位置在slave中有用到

将生成的文件导入到node2中
[root@node1 backup]# scp 3306.sql node2:/data/mysql/backup
root@node2’s password:
3306.sql 100% 703KB 703.0KB/s 00:00

在node2中讲数据导入到db中
(product)root@localhost [(none)]> source /data/mysql/backup/3306.sql

(product)root@localhost [mysql]> flush privileges; 刷新一下权限,因为user表也会到过来的 需要flush权限才会有效
Query OK, 0 rows affected (0.00 sec)

编写change master 语句:

(product)root@localhost [mysql]> change master to master_host='192.168.1.101',
                                                                                   master_port=3306,
                                                                                   master_user='repl',
                                                                                   master_password='repl4slave',
                                                                                   master_log_file='mysql-bin.000006',
                                                                                   master_log_pos=154;

上面的logfile和logpos的的值就是 dump出来的值。

(product)root@localhost [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

(product)root@localhost [(none)]> show slave status \G

***************** 1. row *****************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 154
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000006
**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: 154
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: 1013306
Master_UUID: 5671c0ed-8297-11e5-a0c3-000c2972e7f3
Master_Info_File: /data/mysql/mysql3306/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:
1 row in set (0.00 sec)

这里都是yes的表示执行成功
然后测试一下:
在node1中插入数据:
[root@node1 mysql3306]# sh start_mysql.sh
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.9-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.

(product)root@localhost [(none)]> use db_1;
Database changed
(product)root@localhost [db_1]> select * from tb_1;
+—-+——+
| id | name |
+—-+——+
| 1 | aa |
| 2 | bb |
| 3 | c |
| 4 | dd |
| 5 | ee |
| 6 | ff |
| 7 | g |
| 8 | hh |
+—-+——+
8 rows in set (0.00 sec)

(product)root@localhost [db_1]> insert into tb_1 (name) values(‘cc’);
Query OK, 1 row affected (0.01 sec)
在node2中查询是否有数据进入:
[root@node2 mysql3306]# sh start_mysql.sh
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.9-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.

(product)root@localhost [(none)]> use db_1;
Database changed
(product)root@localhost [db_1]> select * from tb_1;
+—-+——+
| id | name |
+—-+——+
| 1 | aa |
| 2 | bb |
| 3 | c |
| 4 | dd |
| 5 | ee |
| 6 | ff |
| 7 | g |
| 8 | hh |
| 9 | cc |
+—-+——+
9 rows in set (0.00 sec)

主从复制创建成功

需要注意的是在这个复制过程中binlog的格式必须是一致的。
还有要把机器的 防火墙和selinux关闭掉。否则会报错的!!
在5.7中防火墙关闭:
systemctl start firewalld.service#启动firewall
systemctl stop firewalld.service#停止firewall
systemctl disable firewalld.service#禁止firewall开机启动
原来的iptables被firewall代替了。

你可能感兴趣的:(MySQL,Linux)