MySQL5.7基于binary log的主从复制

                                          作者:尹正杰 

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  基于binary log 的复制是指主库将修改操作写入binary log 中,从库负责读取主库的binary log ,并且在本地复制一份,然后里面的操作在从库执行一遍。

  每个从库会保存目前读取主库日志的文件名和日志位置。

  主库和每个从库都必须有一个唯一ID,叫server-id配置在配置文件中。

一.部署mysql数据库

1>.操作系统环境(2台配置想用的虚拟机即可,配置如下)

[root@node101 ~]# hostname
node101.yinzhengjie.org.cn
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# cat /etc/hosts | grep yinzhengjie
172.30.1.101 node101.yinzhengjie.org.cn
172.30.1.102 node102.yinzhengjie.org.cn
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# cat /etc/redhat-release
CentOS Linux release 7.2. (Core)
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# free -h
total used free shared buff/cache available
Mem: .7G 127M .4G 8.5M 166M .4G
Swap: .0G 0B .0G
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# uname -r
3.10.-.el7.x86_64
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# uname -m
x86_64
[root@node101 ~]#
[root@node101 ~]#

2>.安装mysql数据库

  如下图所示,下载MySQL的tar包,下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

[root@node101 ~]# mkdir -pv /yinzhengjie/softwares
mkdir: created directory ‘/yinzhengjie’
mkdir: created directory ‘/yinzhengjie/softwares’
[root@node101 ~]#
[root@node101 ~]# ll
total
-rw-r--r--. root root Mar : mysql-5.7.-linux-glibc2.-x86_64.tar.gz
[root@node101 ~]#
[root@node101 ~]# tar -zxf mysql-5.7.-linux-glibc2.-x86_64.tar.gz -C /yinzhengjie/softwares/
[root@node101 ~]#
[root@node101 ~]# ln -s /yinzhengjie/softwares/mysql-5.7.-linux-glibc2.-x86_64 /yinzhengjie/softwares/mysql
[root@node101 ~]#
[root@node101 ~]# mkdir /yinzhengjie/softwares/mysql/data
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/mysql/
total
drwxr-xr-x. root root Mar : bin
-rw-r--r--. Dec : COPYING
drwxr-xr-x. root root Mar : data
drwxr-xr-x. root root Mar : docs
drwxr-xr-x. root root Mar : include
drwxr-xr-x. root root Mar : lib
drwxr-xr-x. root root Mar : man
-rw-r--r--. Dec : README
drwxr-xr-x. root root Mar : share
drwxr-xr-x. root root Mar : support-files
[root@node101 ~]#
[root@node101 ~]# useradd -s /sbin/nologin mysql
[root@node101 ~]#
[root@node101 ~]# id mysql
uid=(mysql) gid=(mysql) groups=(mysql)
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# chown mysql:mysql -R /yinzhengjie/softwares/mysql
[root@node101 ~]#
[root@node101 ~]# ll -d /yinzhengjie/softwares/mysql-5.7.-linux-glibc2.-x86_64/
drwxr-xr-x. mysql mysql Mar : /yinzhengjie/softwares/mysql-5.7.-linux-glibc2.-x86_64/
[root@node101 ~]#
[root@node101 ~]# cat ~/.bash_profile
# .bash_profile # Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi # User specific environment and startup programs PATH=$PATH:$HOME/bin:/yinzhengjie/softwares/mysql/bin/ export PATH
[root@node101 ~]#
[root@node101 ~]# source ~/.bash_profile
[root@node101 ~]#
[root@node101 ~]# mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data &
[]
[root@node101 ~]# --02T14::.217229Z [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
--02T14::.603930Z [Warning] InnoDB: New log files created, LSN=
--02T14::.794673Z [Warning] InnoDB: Creating foreign key constraint system tables.
--02T14::.862231Z [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b2127a6e-3cf8-11e9-ae0d-000c29fe9bef.
--02T14::.863307Z [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
--02T14::.864126Z [Note] A temporary password is generated for root@localhost: prxUpf-#P7su []+ Done mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server start
Starting MySQL.Logging to '/yinzhengjie/softwares/mysql/data/node101.yinzhengjie.org.cn.err'.
SUCCESS!
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server status
SUCCESS! MySQL running ()
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server status
SUCCESS! MySQL running ()
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7. Copyright (c) , , 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. mysql> alter user user() identified by 'yinzhengjie';
Query OK, rows affected (0.00 sec) mysql> quit
Bye
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7. MySQL Community Server (GPL) Copyright (c) , , 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. mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql> quit
Bye
[root@node101 ~]#

MySQL5.7安装过程笔记详解!戳这里!

  安装MySQL5.7时如果遇到报错可参考我的笔记:https://www.cnblogs.com/yinzhengjie/p/10322426.html

3>.关闭并禁用防火墙和selinux

[root@node101 ~]#
[root@node101 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Fri -- :: PST; 2h 24min ago
Main PID: (firewalld)
CGroup: /system.slice/firewalld.service
└─ /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Mar :: node101.yinzhengjie.org.cn systemd[]: Starting firewalld - dynamic firewall daemon...
Mar :: node101.yinzhengjie.org.cn systemd[]: Started firewalld - dynamic firewall daemon.
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@node101 ~]#
[root@node101 ~]# systemctl stop firewalld
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead) Mar :: node101.yinzhengjie.org.cn systemd[]: Starting firewalld - dynamic firewall daemon...
Mar :: node101.yinzhengjie.org.cn systemd[]: Started firewalld - dynamic firewall daemon.
Mar :: node101.yinzhengjie.org.cn systemd[]: Stopping firewalld - dynamic firewall daemon...
Mar :: node101.yinzhengjie.org.cn systemd[]: Stopped firewalld - dynamic firewall daemon.
[root@node101 ~]#
[root@node101 ~]#

[root@node101 ~]# systemctl stop firewalld

[root@node101 ~]# sed -i s'#SELINUX=enforcing#SELINUX=disabled#'  /etc/selinux/config
[root@node101 ~]# getenforce
Enforcing
[root@node101 ~]# setenforce
[root@node101 ~]#
[root@node101 ~]# getenforce
Permissive
[root@node101 ~]#
[root@node101 ~]#

[root@node101 ~]# sed -i s'#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config

4>.在主库(node101.yinzhengjie.org.cn)中添加测试数据

[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7. MySQL Community Server (GPL) Copyright (c) , , 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. mysql>
mysql>
mysql>
mysql> CREATE DATABASE course CHARACTER SET = utf8;
Query OK, row affected (0.00 sec) mysql>
mysql> USE course;
Database changed
mysql>
mysql>
mysql> CREATE TABLE dept(id INT PRIMARY KEY AUTO_INCREMENT,demt_name VARCHAR());
Query OK, rows affected (0.05 sec) mysql> CREATE TABLE students(
-> sid INT PRIMARY KEY AUTO_INCREMENT,
-> sname VARCHAR(),
-> gender VARCHAR(),
-> dept_id INT NOT NULL,
-> CONSTRAINT student_dept FOREIGN KEY(dept_id) REFERENCES dept(id)
-> );
Query OK, rows affected (0.01 sec) mysql>
mysql> CREATE TABLE teacher(
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(),
-> dept_id INT NOT NULL,
-> CONSTRAINT teacher_dept FOREIGN KEY(dept_id) REFERENCES dept(id)
-> );
Query OK, rows affected (0.01 sec) mysql>
mysql> CREATE TABLE course(
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> course_name VARCHAR(),
-> teacher_id INT,
-> CONSTRAINT course_teacher FOREIGN KEY(teacher_id) REFERENCES teacher(id)
-> );
Query OK, rows affected (0.00 sec) mysql> CREATE TABLE score(sid INT,course_id INT,score INT,PRIMARY KEY(sid,course_id));
Query OK, rows affected (0.04 sec) mysql>

创建数据库与对应的表结构

插入预置数据

二.MySQL基于binlog的复制的配置方法

1>.修改my.cnf配置文件

  主库需要开启bin-log,并且指定一个唯一的server-id,重启数据库。

  在同一个复制组下的所有server_id都必须是唯一的,而且取值必须是正整数,取值范围是1~(2^32)-1。

  确保主库的my.cnf中skip-networking参数为非开启状态,否则会导致主从库不能通信而复制失败

[root@node101 ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server restart                #修改配置后,需要重启服务
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/mysql/data/            #重启数据库后查看MySQL的数据目录
total
-rw-r-----. mysql mysql Mar : auto.cnf
drwxr-x---. mysql mysql Mar : course
-rw-r-----. mysql mysql Mar : ib_buffer_pool
-rw-r-----. mysql mysql Mar : ibdata1
-rw-r-----. mysql mysql Mar : ib_logfile0
-rw-r-----. mysql mysql Mar : ib_logfile1
-rw-r-----. mysql mysql Mar : ibtmp1
drwxr-x---. mysql mysql Mar : mysql
-rw-r-----. mysql mysql Mar : node101.yinzhengjie.org.cn.err
-rw-r-----. mysql mysql Mar : node101.yinzhengjie.org.cn.pid
drwxr-x---. mysql mysql Mar : performance_schema
drwxr-x---. mysql mysql Mar : sys
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.index
[root@node101 ~]#
[root@node101 ~]#

[root@node101 ~]# cat /etc/my.cnf

  使用MySQL专用的脚本查看bin-log日志

[root@node101 ~]# ll /yinzhengjie/softwares/mysql/data/
total
-rw-r-----. mysql mysql Mar : auto.cnf
drwxr-x---. mysql mysql Mar : course
-rw-r-----. mysql mysql Mar : ib_buffer_pool
-rw-r-----. mysql mysql Mar : ibdata1
-rw-r-----. mysql mysql Mar : ib_logfile0
-rw-r-----. mysql mysql Mar : ib_logfile1
-rw-r-----. mysql mysql Mar : ibtmp1
drwxr-x---. mysql mysql Mar : mysql
-rw-r-----. mysql mysql Mar : node101.yinzhengjie.org.cn.err
-rw-r-----. mysql mysql Mar : node101.yinzhengjie.org.cn.pid
drwxr-x---. mysql mysql Mar : performance_schema
drwxr-x---. mysql mysql Mar : sys
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.index
[root@node101 ~]#
[root@node101 ~]# mysqlbinlog -v /yinzhengjie/softwares/mysql/data/yinzhengjie-mysql-bin.
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at
# :: server id end_log_pos CRC32 0x8c40aa35 Start: binlog v , server v 5.7.-log created :: at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
l+R7XA8BAAAAdwAAAHsAAAABAAQANS43LjI1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACX5HtcEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
ATWqQIw=
'/*!*/;
# at
# :: server id end_log_pos CRC32 0xf9fa3f87 Previous-GTIDs
# [empty]
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node101 ~]#

[root@node101 ~]# mysqlbinlog -v /yinzhengjie/softwares/mysql/data/yinzhengjie-mysql-bin.000001        #在做修改之前的操作

[root@node101 ~]#
[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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. mysql>
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| course |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql>
mysql> USE course
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>
mysql> SHOW TABLES;
+------------------+
| Tables_in_course |
+------------------+
| course |
| dept |
| score |
| students |
| teacher |
+------------------+
rows in set (0.00 sec) mysql>
mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| | Jason Yin | | |
| | Andy | | |
| | Bob | | |
| | Ruth | | |
| | Mike | | |
| | John | | |
| | Cindy | | |
| | Susan | | |
+-----+-----------+--------+---------+
rows in set (0.00 sec) mysql>
mysql> UPDATE students SET sname='尹正杰' WHERE sid=;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql>
mysql> DELETE FROM students WHERE sid>;
Query OK, rows affected (0.00 sec) mysql>
mysql>
mysql> CREATE INDEX idx_sname ON students(sname);
Query OK, rows affected (0.05 sec)
Records: Duplicates: Warnings: mysql>
mysql>
mysql> SHOW CREATE TABLE students;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
`sid` int() NOT NULL AUTO_INCREMENT,
`sname` varchar() DEFAULT NULL,
`gender` varchar() DEFAULT NULL,
`dept_id` int() NOT NULL,
PRIMARY KEY (`sid`),
KEY `student_dept` (`dept_id`),
KEY `idx_sname` (`sname`),
CONSTRAINT `student_dept` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.00 sec) mysql>
mysql>

一系列DML语句操作

[root@node101 ~]#
[root@node101 ~]# mysqlbinlog -v /yinzhengjie/softwares/mysql/data/yinzhengjie-mysql-bin.
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at
# :: server id end_log_pos CRC32 0x8c40aa35 Start: binlog v , server v 5.7.-log created :: at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
l+R7XA8BAAAAdwAAAHsAAAABAAQANS43LjI1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACX5HtcEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
ATWqQIw=
'/*!*/;
# at
# :: server id end_log_pos CRC32 0xf9fa3f87 Previous-GTIDs
# [empty]
# at
# :: server id end_log_pos CRC32 0xf24a19ab Anonymous_GTID last_committed= sequence_number= rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at
# :: server id end_log_pos CRC32 0xb3462630 Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
SET @@session.pseudo_thread_id=/*!*/;
SET @@session.foreign_key_checks=, @@session.sql_auto_is_null=, @@session.unique_checks=, @@session.autocommit=/*!*/;
SET @@session.sql_mode=/*!*/;
SET @@session.auto_increment_increment=, @@session.auto_increment_offset=/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=,@@session.collation_connection=,@@session.collation_server=/*!*/;
SET @@session.lc_time_names=/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0x73a81747 Table_map: `course`.`students` mapped to number
# at
# :: server id end_log_pos CRC32 0x3fbc7a1b Update_rows: table id flags: STMT_END_F BINLOG '
7uV7XBMBAAAAPAAAAGEBAAAAAG8AAAAAAAEABmNvdXJzZQAIc3R1ZGVudHMABAMPDwMEwAAkAAZH
F6hz
7uV7XB8BAAAATgAAAK8BAAAAAG8AAAAAAAEAAgAE///wAQAAAAlKYXNvbiBZaW4BMAEAAADwAQAA
AAnlsLnmraPmnbABMAEAAAAberw/
'/*!*/;
### UPDATE `course`.`students`
### WHERE
### @=
### @='Jason Yin'
### @=''
### @=
### SET
### @=
### @='尹正杰'
### @=''
### @=
# at
# :: server id end_log_pos CRC32 0x3280146f Xid =
COMMIT/*!*/;
# at
# :: server id end_log_pos CRC32 0xb5a01913 Anonymous_GTID last_committed= sequence_number= rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at
# :: server id end_log_pos CRC32 0x9b6f868c Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0xe0eec4e9 Table_map: `course`.`students` mapped to number
# at
# :: server id end_log_pos CRC32 0xa9b72d9b Delete_rows: table id flags: STMT_END_F BINLOG '
MOZ7XBMBAAAAPAAAAJUCAAAAAG8AAAAAAAEABmNvdXJzZQAIc3R1ZGVudHMABAMPDwMEwAAkAAbp
xO7g
MOZ7XCABAAAAVQAAAOoCAAAAAG8AAAAAAAEAAgAE//AGAAAABEpvaG4BMAMAAADwBwAAAAVDaW5k
eQExAwAAAPAIAAAABVN1c2FuATEDAAAAmy23qQ==
'/*!*/;
### DELETE FROM `course`.`students`
### WHERE
### @=
### @='John'
### @=''
### @=
### DELETE FROM `course`.`students`
### WHERE
### @=
### @='Cindy'
### @=''
### @=
### DELETE FROM `course`.`students`
### WHERE
### @=
### @='Susan'
### @=''
### @=
# at
# :: server id end_log_pos CRC32 0x9539228a Xid =
COMMIT/*!*/;
# at
# :: server id end_log_pos CRC32 0x4879ed39 Anonymous_GTID last_committed= sequence_number= rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at
# :: server id end_log_pos CRC32 0x7cf1c0fd Query thread_id= exec_time= error_code=
use `course`/*!*/;
SET TIMESTAMP=/*!*/;
CREATE INDEX idx_sname ON students(sname)
/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node101 ~]#
[root@node101 ~]#

[root@node101 ~]# mysqlbinlog -v /yinzhengjie/softwares/mysql/data/yinzhengjie-mysql-bin.000001            #我们会发现操作日志都会被记录到bin log日志中!

2>.在主库创建一个专门用来复制的数据库用户,这样所有从库都可以用这个用户来连接主库,也可以确保这个用户只有复制的权限

  虽然可以用任何拥有复制权限的MySQL用户来复制关系,但由于被使用的用户名和密码会明文保存在备库的master.info文件中,所以为安全起见,最好是使用仅有复制权限的独立用户。

[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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. mysql>
mysql> CREATE USER 'copy'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie';
Query OK, 0 rows affected (0.00 sec) mysql>
mysql> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%';
Query OK, 0 rows affected (0.00 sec) mysql>
mysql> quit
Bye
[root@node101 ~]#
[root@node101 ~]#
[root@node102 ~]# mysql -u copy -pyinzhengjie -P 3306 -h node101.yinzhengjie.org.cn
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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. mysql>
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec) mysql>
mysql> quit
Bye
[root@node102 ~]#
[root@node102 ~]#

[root@node102 ~]# mysql -u copy -pyinzhengjie -P 3306 -h node101.yinzhengjie.org.cn        #从slave库验证远程连接主库是否正常

3>.获取主库的日志信息

  为了确保建立的备库能从正确的bin log位置开启复制,要首先获取主库的bin log信息,包括当前的日志文件名和日志文件内的位置。

mysql> FLUSH TABLES WITH READ LOCK;                        #对主库上所有表加锁,停止修改,即在从库复制的过程中主库不能执行UPDATA,DELETE,INSERT语句!
Query OK, rows affected (0.00 sec) mysql> SHOW MASTER STATUS;                              #获取主库的日志信息,file表示当前日志文件名称,position表示当前日志的位置。
+------------------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------------+----------+--------------+------------------+-------------------+
| yinzhengjie-mysql-bin. |    |         |           |            |
+------------------------------+----------+--------------+------------------+-------------------+
row in set (0.00 sec) mysql>
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| course |
+------------+
row in set (0.00 sec) mysql>
mysql> DROP TABLE dept;                                #我们对主库的所有表加锁后,我们是没法对表进行修改的,删除表也不行哟!
ERROR (HY000): Can't execute the query because you have a conflicting read lock
mysql>

4>.主库数据生成镜像并上传到从库

  有两种方式生成镜像,

    一种是用mysqldump,是innodb存储引擎的方式;

[root@node101 ~]# mysqldump --all-databases --master-data -u root -pyinzhengjie -P  > yinzhengjie-master.db                             #将主库的所有库的表结构和表数据都导出来
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@node101 ~]#
[root@node101 ~]# ll -h
total 784K
-rw-r--r--. root root 781K Mar : yinzhengjie-master.db
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# cat yinzhengjie-master.db | tail -
-- /*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE IF NOT EXISTS `slow_log` (
`start_time` timestamp() NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
`user_host` mediumtext NOT NULL,
`query_time` time() NOT NULL,
`lock_time` time() NOT NULL,
`rows_sent` int() NOT NULL,
`rows_examined` int() NOT NULL,
`db` varchar() NOT NULL,
`last_insert_id` int() NOT NULL,
`insert_id` int() NOT NULL,
`server_id` int() unsigned NOT NULL,
`sql_text` mediumblob NOT NULL,
`thread_id` bigint() unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on -- ::
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# hostname
node101.yinzhengjie.org.cn
[root@node101 ~]#
[root@node101 ~]# hostname -i
172.30.1.101
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# sftp -oPort= root@node102.yinzhengjie.org.cn
Connected to node102.yinzhengjie.org.cn.
sftp> put yinzhengjie-master.db /root/yinzhengjie-master.db
Uploading yinzhengjie-master.db to /root/yinzhengjie-master.db
yinzhengjie-master.db % 781KB .9KB/s :
sftp> quit
[root@node101 ~]#
[root@node101 ~]#

[root@node101 ~]# mysqldump --all-databases --master-data -u root -pyinzhengjie -P 3306 > yinzhengjie-master.db       #将主库的所有库的表结构和表数据都导出来并将该文件上传到从库节点中。

[root@node102 ~]# hostname
node102.yinzhengjie.org.cn
[root@node102 ~]#
[root@node102 ~]# hostname -i
172.30.1.102
[root@node102 ~]#
[root@node102 ~]#
[root@node102 ~]#
[root@node102 ~]# ll
total
-rw-r--r--. root root Mar : yinzhengjie-master.db
[root@node102 ~]#
[root@node102 ~]#
[root@node102 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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. mysql>
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql>
mysql> source yinzhengjie-master.db; ...... Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected, warning (0.00 sec) Query OK, rows affected (0.00 sec) ....... mysql>
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| course |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql>
mysql>
mysql> use course;
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>
mysql>
mysql> SHOW TABLES;
+------------------+
| Tables_in_course |
+------------------+
| course |
| dept |
| score |
| students |
| teacher |
+------------------+
rows in set (0.00 sec) mysql>
mysql>
mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| | 尹正杰 | | |
| | Andy | | |
| | Bob | | |
| | Ruth | | |
| | Mike | | |
+-----+-----------+--------+---------+
rows in set (0.00 sec) mysql>
mysql>

mysql> source yinzhengjie-master.db;                                                    #从库将主库的文件导入到从库中,使得2个库的数据保持一致!

    另一种是将数据文件从主库拷贝到从库,这种方式效率更高(省去了doum/import过程中insert语句执行导致的更新index的行为),但innodb不推荐使用。

[root@node101 ~]# hostname
node101.yinzhengjie.org.cn
[root@node101 ~]#
[root@node101 ~]# hostname -i
172.30.1.101
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/mysql/data/
total
-rw-r-----. mysql mysql Mar : auto.cnf
drwxr-x---. mysql mysql Mar : course
-rw-r-----. mysql mysql Mar : ib_buffer_pool
-rw-r-----. mysql mysql Mar : ibdata1
-rw-r-----. mysql mysql Mar : ib_logfile0
-rw-r-----. mysql mysql Mar : ib_logfile1
drwxr-x---. mysql mysql Mar : mysql
-rw-r-----. mysql mysql Mar : node101.yinzhengjie.org.cn.err
drwxr-x---. mysql mysql Mar : performance_schema
drwxr-x---. mysql mysql Mar : sys
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.index
[root@node101 ~]#
[root@node101 ~]# tar -zcf yinzhengjie-mysql-data.tar.gz /yinzhengjie/softwares/mysql/data
tar: Removing leading `/' from member names
[root@node101 ~]#
[root@node101 ~]# ls
yinzhengjie-master.db yinzhengjie-mysql-data.tar.gz
[root@node101 ~]#
[root@node101 ~]# sftp -oPort= root@node102.yinzhengjie.org.cn
Connected to node102.yinzhengjie.org.cn.
sftp> put yinzhengjie-mysql-data.tar.gz /root/yinzhengjie-mysql-data.tar.gz
Uploading yinzhengjie-mysql-data.tar.gz to /root/yinzhengjie-mysql-data.tar.gz
yinzhengjie-mysql-data.tar.gz % 1336KB .3MB/s :
sftp>
sftp> quit
[root@node101 ~]#

将主库的MySQL数据目录打包发送到从库中

[root@node102 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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. mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| course |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql>
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql>
mysql>
mysql> quit
Bye
[root@node102 ~]#
[root@node102 ~]# /etc/init.d/mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@node102 ~]#
[root@node102 ~]#
[root@node102 ~]# hostname
node102.yinzhengjie.org.cn
[root@node102 ~]#
[root@node102 ~]# hostname -i
172.30.1.102
[root@node102 ~]#
[root@node102 ~]# ll
total
-rw-r--r--. root root Mar : yinzhengjie-master.db
-rw-r--r--. root root Mar : yinzhengjie-mysql-data.tar.gz
[root@node102 ~]#
[root@node102 ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=
[root@node102 ~]#
[root@node102 ~]#
[root@node102 ~]# mv /yinzhengjie/softwares/mysql/data /yinzhengjie/softwares/mysql/data.`date +%F`
[root@node102 ~]#
[root@node102 ~]# ll
total
-rw-r--r--. root root Mar : yinzhengjie-master.db
-rw-r--r--. root root Mar : yinzhengjie-mysql-data.tar.gz
[root@node102 ~]#
[root@node102 ~]# tar -zxf yinzhengjie-mysql-data.tar.gz
[root@node102 ~]#
[root@node102 ~]# ll
total
drwxr-xr-x. root root Mar : yinzhengjie
-rw-r--r--. root root Mar : yinzhengjie-master.db
-rw-r--r--. root root Mar : yinzhengjie-mysql-data.tar.gz
[root@node102 ~]#
[root@node102 ~]# mv yinzhengjie/softwares/mysql/data /yinzhengjie/softwares/mysql/
[root@node102 ~]#
[root@node102 ~]# ll /yinzhengjie/softwares/mysql/
total
drwxr-xr-x. mysql mysql Mar : bin
-rw-r--r--. mysql mysql Mar : COPYING
drwxr-xr-x. mysql mysql Mar : data
drwxr-xr-x. mysql mysql Mar : data.--
drwxr-xr-x. mysql mysql Mar : docs
drwxr-xr-x. mysql mysql Mar : include
drwxr-xr-x. mysql mysql Mar : lib
drwxr-xr-x. mysql mysql Mar : man
-rw-r--r--. mysql mysql Mar : README
drwxr-xr-x. mysql mysql Mar : share
drwxr-xr-x. mysql mysql Mar : support-files
[root@node102 ~]#
[root@node102 ~]#
[root@node102 ~]# /etc/init.d/mysql.server start
Starting MySQL. SUCCESS!
[root@node102 ~]#
[root@node102 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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. mysql>
mysql>
mysql>
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| course |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql>
mysql> USE course
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>
mysql> SHOW TABLES;
+------------------+
| Tables_in_course |
+------------------+
| course |
| dept |
| score |
| students |
| teacher |
+------------------+
rows in set (0.00 sec) mysql>
mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| | 尹正杰 | | |
| | Andy | | |
| | Bob | | |
| | Ruth | | |
| | Mike | | |
+-----+-----------+--------+---------+
rows in set (0.00 sec) mysql>
mysql> quit
Bye
[root@node102 ~]#

将主库的数据目录放在从库的数据目录中,启动从库的MySQL即可看到和主库相同的数据啦!前提是要保证主库和从库的配置文件要一致哟!

温馨提示:
当你是将数据目录直接拷贝到从库时,需要关闭主库和从库的数据库实例。启动从库实例之前,我们最好是手动将数据目录中的“auto.cnf”删除掉,因为它也是唯一标识一个服务器实例的一个标志,因此我们让从库和主库的auto.cnf的内容一致!此时我们手动将其删除即可,并且我们应该保证哥哥数据的server-id的值应该是不同的!然后重启数据库后,会在从库的数据目录中自动生成新的’auto.cnf’文件具体操作如下所示。 [root@node102 ~]#
[root@node102 ~]# ll /yinzhengjie/softwares/mysql/data/auto.cnf
-rw-r-----. mysql mysql Mar : /yinzhengjie/softwares/mysql/data/auto.cnf
[root@node102 ~]#
[root@node102 ~]#
[root@node102 ~]# cat /yinzhengjie/softwares/mysql/data/auto.cnf
[auto]
server-uuid=b2127a6e-3cf8-11e9-ae0d-000c29fe9bef
[root@node102 ~]#
[root@node102 ~]# [root@node102 ~]# rm -f /yinzhengjie/softwares/mysql/data/auto.cnf
[root@node102 ~]# [root@node102 ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=
[root@node102 ~]#
[root@node102 ~]# /etc/init.d/mysql.server restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@node102 ~]#
[root@node102 ~]# cat /yinzhengjie/softwares/mysql/data/auto.cnf
[auto]
server-uuid=d4802ad0-3dc9-11e9-ae9a-000c29dc1634
[root@node102 ~]#

5>.在从库上建立复制关系

[root@node102 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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. mysql>
mysql> CHANGE MASTER TO
-> MASTER_HOST='node101.yinzhengjie.org.cn',
-> MASTER_PORT=,
-> MASTER_USER='copy',
-> MASTER_PASSWORD='yinzhengjie',
-> MASTER_LOG_FILE='yinzhengjie-mysql-bin.000001',
-> MASTER_LOG_POS=;
Query OK, rows affected, warnings (0.05 sec) mysql>
mysql> START SLAVE;                      #打开slave的同步进程
Query OK, rows affected (0.01 sec) mysql>
mysql> SHOW SLAVE STATUS\G                  #查看slave复制进程信息
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: node101.yinzhengjie.org.cn
Master_User: copy
Master_Port:
Connect_Retry:
Master_Log_File: yinzhengjie-mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: node102-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: yinzhengjie-mysql-bin.
Slave_IO_Running: Yes              #如果你看到了这个IO和下面一行的SQL都为yes则说明你的配置是没有问题的!还及得IO和SQL线程各自的功能是干嘛的么?
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
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和slave之间的差距!咱们理解数据的同步的延迟大小,若为0表示目前从库的数据和主库的数据是一样的!
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0      #如果上面的IO线程不是yes的话,我们就可以在这里查看抱错信息,根据相应的报错查找对应的解决方案即可,下面的SQL日志错误也是同样的道理。
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: b2127a6e-3cf8-11e9-ae0d-000c29fe9bef
Master_Info_File: /yinzhengjie/softwares/mysql/data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.00 sec) mysql>

6>.主库上更新数据

[root@node101 ~]# hostname -i
172.30.1.101
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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. mysql>
mysql> SELECT * FROM course.students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| | 尹正杰 | | |
| | Andy | | |
| | Bob | | |
| | Ruth | | |
| | Mike | | |
+-----+-----------+--------+---------+
rows in set (0.02 sec) mysql>
mysql>
mysql> UPDATE course.students SET sname='Jason Yin' WHERE sid=;
Query OK, row affected (0.01 sec)
Rows matched: Changed: Warnings: mysql>
mysql> SELECT * FROM course.students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| | Jason Yin | | |
| | Andy | | |
| | Bob | | |
| | Ruth | | |
| | Mike | | |
+-----+-----------+--------+---------+
rows in set (0.00 sec) mysql>

mysql> UPDATE course.students SET sname='Jason Yin' WHERE sid=1;          #主库更新数据

[root@node102 ~]#
[root@node102 ~]# hostname
node102.yinzhengjie.org.cn
[root@node102 ~]#
[root@node102 ~]# hostname -i
172.30.1.102
[root@node102 ~]#
[root@node102 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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. mysql>
mysql>
mysql> SELECT * FROM course.students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| | Jason Yin | | |
| | Andy | | |
| | Bob | | |
| | Ruth | | |
| | Mike | | |
+-----+-----------+--------+---------+
rows in set (0.00 sec) mysql>
mysql> quit
Bye
[root@node102 ~]#
[root@node102 ~]#

mysql> SELECT * FROM course.students;                          #从库查看数据已经被更改啦~

7>.查看bin log日志

  注意,主库和从库都是查看最新的日志文件,我们可以根据文件编号来确定,而且主库查看的是yinzhengjie-mysql-bin.000002文件,而从库查看的是node102-relay-bin.000003。

[root@node101 ~]# ll /yinzhengjie/softwares/mysql/data/
total
-rw-r-----. mysql mysql Mar : auto.cnf
drwxr-x---. mysql mysql Mar : course
-rw-r-----. mysql mysql Mar : ib_buffer_pool
-rw-r-----. mysql mysql Mar : ibdata1
-rw-r-----. mysql mysql Mar : ib_logfile0
-rw-r-----. mysql mysql Mar : ib_logfile1
-rw-r-----. mysql mysql Mar : ibtmp1
drwxr-x---. mysql mysql Mar : mysql
-rw-r-----. mysql mysql Mar : node101.yinzhengjie.org.cn.err
-rw-r-----. mysql mysql Mar : node101.yinzhengjie.org.cn.pid
drwxr-x---. mysql mysql Mar : performance_schema
drwxr-x---. mysql mysql Mar : sys
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.index
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# mysqlbinlog -v /yinzhengjie/softwares/mysql/data/yinzhengjie-mysql-bin.
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at
# :: server id end_log_pos CRC32 0x35bede44 Start: binlog v , server v 5.7.-log created :: at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
jfJ7XA8BAAAAdwAAAHsAAAABAAQANS43LjI1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACN8ntcEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AUTevjU=
'/*!*/;
# at
# :: server id end_log_pos CRC32 0x2d98f213 Previous-GTIDs
# [empty]
# at
# :: server id end_log_pos CRC32 0xc171b0f8 Anonymous_GTID last_committed= sequence_number= rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at
# :: server id end_log_pos CRC32 0xa72fa9e7 Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
SET @@session.pseudo_thread_id=/*!*/;
SET @@session.foreign_key_checks=, @@session.sql_auto_is_null=, @@session.unique_checks=, @@session.autocommit=/*!*/;
SET @@session.sql_mode=/*!*/;
SET @@session.auto_increment_increment=, @@session.auto_increment_offset=/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=,@@session.collation_connection=,@@session.collation_server=/*!*/;
SET @@session.lc_time_names=/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0x9149fef1 Table_map: `course`.`students` mapped to number
# at
# :: server id end_log_pos CRC32 0x9ae384b1 Update_rows: table id flags: STMT_END_F BINLOG '
+Pd7XBMBAAAAPAAAAFsBAAAAAGwAAAAAAAEABmNvdXJzZQAIc3R1ZGVudHMABAMPDwMEwAAkAAbx
/kmR
+Pd7XB8BAAAATgAAAKkBAAAAAGwAAAAAAAEAAgAE///wAQAAAAnlsLnmraPmnbABMAEAAADwAQAA
AAlKYXNvbiBZaW4BMAEAAACxhOOa
'/*!*/;
### UPDATE `course`.`students`
### WHERE
### @=
### @='尹正杰'
### @=''
### @=
### SET
### @=
### @='Jason Yin'
### @=''
### @=
# at
# :: server id end_log_pos CRC32 0x5bddb9c2 Xid =
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node101 ~]#
[root@node101 ~]#

[root@node101 ~]# mysqlbinlog -v /yinzhengjie/softwares/mysql/data/yinzhengjie-mysql-bin.000002        #查看主库的最新日志

[root@node102 ~]# ll /yinzhengjie/softwares/mysql/data
total
-rw-r-----. mysql mysql Mar : auto.cnf
drwxr-x---. mysql mysql Mar : course
-rw-r-----. mysql mysql Mar : ib_buffer_pool
-rw-r-----. mysql mysql Mar : ibdata1
-rw-r-----. mysql mysql Mar : ib_logfile0
-rw-r-----. mysql mysql Mar : ib_logfile1
-rw-r-----. mysql mysql Mar : ibtmp1
-rw-r-----. mysql mysql Mar : master.info
drwxr-x---. mysql mysql Mar : mysql
-rw-r-----. mysql mysql Mar : node101.yinzhengjie.org.cn.err
-rw-r-----. mysql mysql Mar : node102-relay-bin.
-rw-r-----. mysql mysql Mar : node102-relay-bin.
-rw-r-----. mysql mysql Mar : node102-relay-bin.index
-rw-r-----. mysql mysql Mar : node102.yinzhengjie.org.cn.err
-rw-r-----. mysql mysql Mar : node102.yinzhengjie.org.cn.pid
drwxr-x---. mysql mysql Mar : performance_schema
-rw-r-----. mysql mysql Mar : relay-log.info
drwxr-x---. mysql mysql Mar : sys
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.
-rw-r-----. mysql mysql Mar : yinzhengjie-mysql-bin.index
[root@node102 ~]#
[root@node102 ~]#
[root@node102 ~]# mysqlbinlog -v /yinzhengjie/softwares/mysql/data/node102-relay-bin.
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at
# :: server id end_log_pos CRC32 0x93c4b3af Start: binlog v , server v 5.7.-log created ::
# This Format_description_event appears in a relay log and was generated by the slave thread.
# at
# :: server id end_log_pos CRC32 0xeeedf574 Previous-GTIDs
# [empty]
# at
# :: server id end_log_pos CRC32 0x3c8e65f1 Rotate to yinzhengjie-mysql-bin. pos:
# at
# :: server id end_log_pos CRC32 0x35bede44 Start: binlog v , server v 5.7.-log created :: at startup
ROLLBACK/*!*/;
BINLOG '
jfJ7XA8BAAAAdwAAAHsAAAAAAAQANS43LjI1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACN8ntcEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AUTevjU=
'/*!*/;
# at
# :: server id end_log_pos CRC32 0xfd49277d Rotate to yinzhengjie-mysql-bin. pos:
# at
# :: server id end_log_pos CRC32 0xc171b0f8 Anonymous_GTID last_committed= sequence_number= rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at
# :: server id end_log_pos CRC32 0xa72fa9e7 Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
SET @@session.pseudo_thread_id=/*!*/;
SET @@session.foreign_key_checks=, @@session.sql_auto_is_null=, @@session.unique_checks=, @@session.autocommit=/*!*/;
SET @@session.sql_mode=/*!*/;
SET @@session.auto_increment_increment=, @@session.auto_increment_offset=/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=,@@session.collation_connection=,@@session.collation_server=/*!*/;
SET @@session.lc_time_names=/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0x9149fef1 Table_map: `course`.`students` mapped to number
# at
# :: server id end_log_pos CRC32 0x9ae384b1 Update_rows: table id flags: STMT_END_F BINLOG '
+Pd7XBMBAAAAPAAAAFsBAAAAAGwAAAAAAAEABmNvdXJzZQAIc3R1ZGVudHMABAMPDwMEwAAkAAbx
/kmR
+Pd7XB8BAAAATgAAAKkBAAAAAGwAAAAAAAEAAgAE///wAQAAAAnlsLnmraPmnbABMAEAAADwAQAA
AAlKYXNvbiBZaW4BMAEAAACxhOOa
'/*!*/;
### UPDATE `course`.`students`
### WHERE
### @=
### @='尹正杰'
### @=''
### @=
### SET
### @=
### @='Jason Yin'
### @=''
### @=
# at
# :: server id end_log_pos CRC32 0x5bddb9c2 Xid =
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node102 ~]#
[root@node102 ~]#

[root@node102 ~]# mysqlbinlog -v /yinzhengjie/softwares/mysql/data/node102-relay-bin.000003          #查看从库的最新日志

三.MySQL基于bin log的复制常见报错汇总

1>.Last_IO_Errno:1593

Last_IO_Errno: 1593
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).

  报错原因:

    服务器ID和复制集群其他的机器重复了。

  解决办法:

    删除备库的auto.cnf文件,重启mysql,生成新的UUID。

2>.Last_IO_Errno:2003

Last_IO_Errno: 2003
Last_IO_Error: error connecting to master 'copy@172.30.1.101:3306' - retry-time: 60 retries: 86400

  报错原因:

    连接MySQL的master节点失败!

  解决方案:

    备库(slave)连接主库(master)失败,检查防火墙,用户密码端口是否设置正确。

四.MySQL基于binlog的多slave环境

1>.当第一个slave创建好之后,如果还想要创建其他的slave,则可以直接使用先前使用的备份文件,分别执行以下几个步骤:

    在slave的my.cnf上分配新的server_id

    从库应用主库的数据镜像

    利用相同的change master命令将从库指定主库的日志信息和链接信息

    slave start

  这样第二个slave也就创建起来了。在主库上执行“show processlist”即可查看slave信息。

2>.如果想在事后再增加一个slave,但之前的备份文件已经不存在,或者主库的日志文件已经被清楚了的情况下,考虑使用如下方法:

  在已经建立好的复制环境中新增一个从库,则不需要关闭主库复制数据,而是用已有的从库复制数据即可。

  关闭现有的从库。(mysqladmin shutdown)

  拷贝从库的文件到新的从库,包括log文件和relay log文件,其中如果relay log使用了从库的主机名,则需要调relay-log-index参数。

  拷贝master info和reg log info文件到新的从库。

  为新的从库分配一个唯一的seriver-id。

  新的从库启动slave进程。

MySQL5.7基于binary log的主从复制的更多相关文章

  1. mysql5.7基于gtid进行搭建主从复制过程

    gtid_mode = onenforce-gtid-consistency = onskip_name_resolve # 去掉域名解析二进制日志必须开启,且格式为ROWserver-id必须配置成 ...

  2. Centos7.5部署MySQL5.7基于GTID主从复制+并行复制+半同步复制+读写分离(ProxySQL) 环境- 运维笔记 (完整版)

    之前已经详细介绍了Mysql基于GTID主从复制的概念,原理和配置,下面整体记录下MySQL5.7基于GTID主从复制+并行复制+增强半同步复制+读写分离环境的实现过程,以便加深对mysql新特性GT ...

  3. 7.5 Point-in-Time (Incremental) Recovery Using the Binary Log 使用binay log 基于时间点恢复

    7.5 Point-in-Time (Incremental) Recovery Using the Binary Log 使用binay log 基于时间点恢复 7.5.1 Point-in-Tim ...

  4. 【MySQL】通过Binary Log简单实现数据回滚(一)

    一.前言 对,没错,我又水了好一阵子,深刻反思寄几.前段时间,工作项目上出于对excel等批量操作可能出现误操作的问题,要求提供一个能够根据操作批次进行数据回滚的能力.在开发的过程中接触到了MySQL ...

  5. MySQL二进制日志(binary log)总结

    本文出处:http://www.cnblogs.com/wy123/p/7182356.html (保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错 ...

  6. mysql5.7.18的安装与主从复制

    CentOS6.7安装mysql5.7.18 1.  解压到/usr/local目录 # tar -zxvf mysql-5.7.18-linux-glibc2.5-i686.tar.gz -C /u ...

  7. 基于GTID模式MySQL主从复制

    基于GTID模式MySQL主从复制 GTID复制原理:基于GTID的复制是MySQL 5.6后新增的复制方式GTID (global transaction identifier) 即全局事务ID, ...

  8. 【MySQL】MySQL同步报错-> Last_IO_Error: Got fatal error 1236 from master when reading data from binary log

    这个报错网上搜索了一下,大部分是由于MySQL意外关闭或强制重启造成的binlog文件事务点读取异常造成的主从同步报错 Last_IO_Error: Got fatal error 1236 from ...

  9. Group Commit of Binary Log

    160222 09:19:26 mysqld_safe Starting mysqld daemon with databases from /data01/mysql 2016-02-22 09:1 ...

随机推荐

  1. a标签实现锚点功能

    a标签实现锚点功能 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  2. linq之左连接 + group by

    var list = from item in (from s in _sysBll.GetList(s => s.ParamID == "TraSchType" & ...

  3. Nginx+uwsgi部署 Diango(生产环境)

    环境:CentOS6.5 + Nginx1.11.5 + Python3.5.2 1. 安装基础软件包 yum install -y zlib-devel bzip2-devel \ pcre-dev ...

  4. centos 7创建ss服务(方式一)

    一:安装PIP,由于安装的是python 版本的ss,所以需要先安装PIP: $ curl "https://bootstrap.pypa.io/get-pip.py" -o &q ...

  5. Number Sequence POJ - 1019 递推 数学

    题意 1 12 123 1234 12345 ....这样的序列 问第n位数字是几   是数字! 1-9! 思路:递推关系 主要是位数的计算   用a[i]=a[i-1]+(int)log10((do ...

  6. AHOI2013 差异 【后缀数组】

    题目分析: 求出height以后很明显跨越最小height的一定贡献是最小height,所以对于区间找出最小height再将区间对半分. 代码: #include<bits/stdc++.h&g ...

  7. [HDU5536] Chip Factory

    传送门:>Here< 题意:给出一个长度为N的序列,求$Max\{ (a_i + a_j) ⊕ a_k \}$ (i,j,k均不相同)  ($N \leq 1000$) 解题思路 既然$O ...

  8. Matplotlib学习---用matplotlib画饼图/面包圈图(pie chart, donut chart)

    我在网上随便找了一组数据,用它来学习画图.大家可以直接把下面的数据复制到excel里,然后用pandas的read_excel命令读取.或者直接在脚本里创建该数据. 饼图: ax.pie(x,labe ...

  9. 【支付宝】支付 系统繁忙,请稍后再试(ALIN10146)

    https://openclub.alipay.com/read.php?tid=6918&fid=60 我碰到的是这个问题导致 把signType 改为  RSA2   改成      

  10. Eslint相关知识和配置大全

    ESLint最初是由Nicholas C. Zakas 于2013年6月创建的开源项目.它的目标是提供一个插件化的javascript代码检测工具. 代码检查是一种静态的分析,常用于寻找有问题的模式或 ...