mysql的简单主从复制

主从复制打开后,各自开启的线程:

master端
mysql> show processlist\G;
*************************** 1. row ***************************
Id: 4
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: starting
Info: show processlist
*************************** 2. row ***************************
Id: 5
User: kiosk
Host: server2:41404
db: NULL
Command: Binlog Dump #开启一个连接server2的Dump线程
Time: 714
State: Master has sent all binlog to slave; waiting for more updates
Info: NULL
*************************** 3. row ***************************
Id: 6
User: kiosk
Host: server3:60038
db: NULL
Command: Binlog Dump #开启一个连接server3的Dump线程
Time: 260
State: Master has sent all binlog to slave; waiting for more updates
Info: NULL
3 rows in set (0.00 sec) ERROR:
No query specified
slave端(两台slave开启的线程相同)
mysql> show processlist\G;
*************************** 1. row ***************************
Id: 6
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: starting
Info: show processlist
*************************** 2. row ***************************
Id: 7
User: system user
Host:
db: NULL
Command: Connect
Time: 736
State: Waiting for master to send event #开启一个IO线程等待master发送event
Info: NULL
*************************** 3. row ***************************
Id: 8
User: system user
Host:
db: NULL
Command: Connect mysql> show processlist\G;
*************************** 1. row ***************************
Id: 6
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: starting
Info: show processlist
*************************** 2. row ***************************
Id: 7
User: system user
Host:
db: NULL
Command: Connect #开启有一个IO线程
Time: 736
State: Waiting for master to send event #等待master发送event
Info: NULL
*************************** 3. row ***************************
Id: 8
User: system user
Host:
db: NULL
Command: Connect #开启一个SQL线程
Time: 150
State: Slave has read all relay log; waiting for more updates #relay log已经读完,等待更多的数据
Info: NULL
3 rows in set (0.00 sec) ERROR:
No query specified

日志文件

[root@server1 mysql]# pwd
/var/lib/mysql
[root@server1 mysql]# ls
auto.cnf client-cert.pem ibdata1 ibtmp1 mysql-bin.index mysql.sock.lock public_key.pem sys
ca-key.pem client-key.pem ib_logfile0 mysql mysqld_safe.pid performance_schema server-cert.pem test
ca.pem ib_buffer_pool ib_logfile1 mysql-bin.000001 mysql.sock private_key.pem server-key.pem
日志文件mysql-bin.000001的查看方式
[root@server1 mysql]# mysqlbinlog mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4 #pos号从4开始,到123结束(end_log_pos 123)
#170626 16:51:14 server id 1 end_log_pos 123 CRC32 0x81470229 Start: binlog v 4, server v 5.7.17-log created 170626 16:51:14 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
AstQWQ8BAAAAdwAAAHsAAAABAAQANS43LjE3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAACy1BZEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
ASkCR4E=
'/*!*/;
# at 123
#170626 16:51:15 server id 1 end_log_pos 154 CRC32 0x1b2dd997 Previous-GTIDs
# [empty]
# at 154
#170626 19:24:18 server id 1 end_log_pos 219 CRC32 0xfa85ae42 Anonymous_GTID last_committed=0 sequence_number=1
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#170626 19:24:18 server id 1 end_log_pos 448 CRC32 0x2f4c5c11 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1498476258/*!*/;
SET @@session.pseudo_thread_id=4/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
GRANT REPLICATION SLAVE ON *.* TO 'kiosk'@'172.25.44.%' IDENTIFIED WITH 'mysql_native_password' AS '*17C56DEB4E9DB39B6523B6E79C6E99F3462FE761'
/*!*/;
# at 448
#170626 19:25:28 server id 1 end_log_pos 513 CRC32 0xd5b74785 Anonymous_GTID last_committed=1 sequence_number=2
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 513
#170626 19:25:28 server id 1 end_log_pos 600 CRC32 0x90cfecc3 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1498476328/*!*/;
SET @@session.time_zone='SYSTEM'/*!*/;
flush privileges
/*!*/;
# at 600
#170626 19:37:46 server id 1 end_log_pos 665 CRC32 0x5c6c5e78 Anonymous_GTID last_committed=2 sequence_number=3
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 665
#170626 19:37:46 server id 1 end_log_pos 759 CRC32 0x432d0d44 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1498477066/*!*/;
create database test
/*!*/;
# at 759
#170626 19:38:55 server id 1 end_log_pos 824 CRC32 0x56adfbce Anonymous_GTID last_committed=3 sequence_number=4
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 824
#170626 19:38:55 server id 1 end_log_pos 968 CRC32 0x0dcfbcbf Query thread_id=4 exec_time=1 error_code=0
use `test`/*!*/;
SET TIMESTAMP=1498477135/*!*/;
create table test(
id varchar(10) not null,
name varchar(10) not null)
/*!*/;
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*/;
last_committed表示事务提交的时候,上次事务提交的编号。
如果事务的last_committed值一样,就表示这些事物是一起提交的操作,可以进行并行的回放。
同时一个组里的last committed对应上一个事务的sequence number。
组提交只与last committed有关,这也是MySQL基于组提交(logic clock)的并行复制即使在gtid关闭情形下也能生效的原因
[root@server1 mysql]# mysqlbinlog mysql-bin.000001 |grep last_committed
#170626 19:24:18 server id 1 end_log_pos 219 CRC32 0xfa85ae42 Anonymous_GTID last_committed=0 sequence_number=1
#170626 19:25:28 server id 1 end_log_pos 513 CRC32 0xd5b74785 Anonymous_GTID last_committed=1 sequence_number=2
#170626 19:37:46 server id 1 end_log_pos 665 CRC32 0x5c6c5e78 Anonymous_GTID last_committed=2 sequence_number=3
#170626 19:38:55 server id 1 end_log_pos 824 CRC32 0x56adfbce Anonymous_GTID last_committed=3 sequence_number=4

基于gtid的mysql主从复制

server-uuid是对server的唯一标识,是用来生成gtid的,因为gtid的有一部分就是uuid。
gtid的组成:uuid+序列号
全局唯一的 server_uuid 的一个好处是:可以解决由 server_id 配置冲突带来的 MySQL 主备复制的异常终止。
[root@server1 mysql]# cat auto.cnf
[auto]
server-uuid=2b368933-5a43-11e7-86b6-525400670fe5
修改配置文件/etc/my.cnf(master和slave端都要配)
gtid-mode=ON
enforce-gtid-consistency=ON
slave端:
  • stop slave;
mysql> change master to master_host='172.25.44.1',master_user='kiosk',master_password='Lt@@0404',master_log_file='mysql-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (1.43 sec)
  • start slave;
关于sync_binlog参数

  • sync_binlog=0 ## 表示在事物提交之后,MySQL不会调用fsync指令将binlog_cache中的信息刷新到磁盘,而是交给文件系统,决定是将数据刷新到磁盘。这种之后的性能是最好的,当然风险也最大。一旦系统Crash,binlog信息就都会丢失。
  • sync_binlog=n ## 表示每体提交n次事物,MySQL就会强制调用fsync指令将binlog_cache中的信息写入磁盘。当然n=1的时,最安全同时系统性能损耗也最大,这样的话:即使系统Crash,也最多丢失binlog_cache中未完成的一个事务,对实际数据没有任何实质性影响。

mysql> show variables like 'sync_binlog';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sync_binlog | 1 |
+---------------+-------+
1 row in set (0.00 sec)

mysql的并行复制

配置slave端的配置文件
	slave-parallel-type=DATABASE			 基于库的并行复制方式
slave-parallel-type=LOGICAL_CLOCK 基于组提交的并行复制方式
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
查看开启的并行复制的线程

[root@server2 software]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@server2 software]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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 processlist\G; #除了原有的线程之外,还开启了16个并行线程
*************************** 1. row ***************************
Id: 1
User: system user
Host:
db: NULL
Command: Connect
Time: 56
State: Slave has read all relay log; waiting for more updates
Info: NULL
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 3
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 4. row ***************************
Id: 5
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 5. row ***************************
Id: 6
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 6. row ***************************
Id: 7
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 7. row ***************************
Id: 8
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 8. row ***************************
Id: 9
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 9. row ***************************
Id: 10
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 10. row ***************************
Id: 12
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 11. row ***************************
Id: 14
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 12. row ***************************
Id: 15
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 13. row ***************************
Id: 16
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 14. row ***************************
Id: 17
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 15. row ***************************
Id: 18
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 16. row ***************************
Id: 19
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 17. row ***************************
Id: 20
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 18. row ***************************
Id: 21
User: system user
Host:
db: NULL
Command: Connect
Time: 58
State: Waiting for an event from Coordinator
Info: NULL
*************************** 19. row ***************************
Id: 22
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: starting
Info: show processlist
19 rows in set (0.00 sec) ERROR:
No query specified

mysql简单主从复制(二)的更多相关文章

  1. mysql简单主从复制(一)

    MYSQL简单主从复制 master:172.25.44.1 slave:172.25.44.2 mysql5.7安装 master和slave均操作 准备rpm包:mysql-5.7.17-1.el ...

  2. Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(二)

    然后是项目下的文件:完整的项目请看  上一篇 Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(一) 项目下的springmvc-servlet.xml配置文件: ...

  3. Gin实战:Gin+Mysql简单的Restful风格的API(二)

    上一篇介绍了Gin+Mysql简单的Restful风格的API,但代码放在一个文件中,还不属于restful风格,接下来将进行进一步的封装. 目录结构 ☁ gin_restful2 tree . ├─ ...

  4. liinux安装 mysql 及主从复制

    mariadb其实就是mysqlmysql已经被oracle收购,它即将闭源,马上要开始收费了因此还想免费试用开源的数据库mysql,就在centos7上,将mysql分支为mariadb 安装mar ...

  5. Linux系统——MySQL基础(二)

    # MySQL数据库完全备份与恢复## 数据库备份的分类1. 从物理与逻辑的角度,备份可以分为物理备份和逻辑备份.(1)物理备份:对数据库操作系统的物理文件(数据文件.日志文件)的备份.物理备份又可分 ...

  6. mysql (主从复制)(proxy , Amoeba)

    原址如下: http://heylinux.com/archives/1004.html Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中 ...

  7. mysql的主从复制原理与实现

    关于mysql的主从复制,之前一直在听说这个话题,一直没有实现,昨天学习了下,原来是这么回事: 既然是主从复制,那么肯定有主有从,也就说一个主数据库(一般为写库),一个从数据库(读库).主数据库更新了 ...

  8. mysql ab主从复制出错及解决过程

    一.mysql主从服务器报错描述:Slave_IO_Running=NO,Slave_SQL_Running=YES,Last_Errno=0 mysql slave stop ; mysql sla ...

  9. Linux 下的mysql+centos7+主从复制

    mysql+centos7+主从复制   MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公 ...

随机推荐

  1. effective java 第2章-创建和销毁对象 读书笔记

    背景 去年就把这本javaer必读书--effective java中文版第二版 读完了,第一遍感觉比较肤浅,今年打算开始第二遍,顺便做一下笔记,后续会持续更新. 1.考虑用静态工厂方法替代构造器 优 ...

  2. (转)log4j(三)——如何控制不同级别的日志信息的输出?

    一:测试环境与log4j(一)——为什么要使用log4j?一样,这里不再重述 1 老规矩,先来个栗子,然后再聊聊感受 package test.log4j.test3; import org.apac ...

  3. (转)Spring事务配置的五种方式

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

  4. Java 9 揭秘(17. Reactive Streams)

    Tips 做一个终身学习的人. 在本章中,主要介绍以下内容: 什么是流(stream) 响应式流(Reactive Streams)的倡议是什么,以及规范和Java API 响应式流在JDK 中的AP ...

  5. iOS开发实战-时光记账Demo 本地数据库版

    现在记账APP也是用途比较广泛 自己写了个简单的demo 欢迎指正 效果 分析 1.思维推导 首先简单的做了下思维推导 2.文件结构 大致框架想好后就可以着手开始准备了 数据库管理:coreData ...

  6. C语言编译过程(转)

    内容摘要 : C语言编译的整个过程是非常复杂的,里面涉及到的编译器知识.硬件知识.工具链知识都是非常多的,深入了解整个编译过程对工程师理解应用程序的编写是有很大帮助的,希望大家可以多了解一些,在遇到问 ...

  7. 关于ALTERA生成IP核卡住

    最近搞fft,用的quartus13.1版本,发现quartus和modelism存在各种各样的坑啊,fftIP核大家可能也遇到过ip核 生成到一半就卡住的问题,之前我是通过换系统解决的,但是好景不长 ...

  8. 初学Python(二)——数组

    初学Python(二)——数组 初学Python,主要整理一些学习到的知识点,这次是数组. # -*- coding:utf-8 -*- list = [2.0,3.0,4.0] #计算list长度 ...

  9. JavaSE中线程与并行API框架学习笔记——线程为什么会不安全?

    前言:休整一个多月之后,终于开始投简历了.这段时间休息了一阵子,又病了几天,真正用来复习准备的时间其实并不多.说实话,心里不是非常有底气. 这可能是学生时代遗留的思维惯性--总想着做好万全准备才去做事 ...

  10. javascript编程解决黑化的牛牛问题

    问题描述 时间限制:1秒 空间限制:32768K 牛牛变得黑化了,想要摧毁掉地球.但他忘记了开启地球毁灭器的密码.牛牛手里有一个字符串S,牛牛还记得从S中去掉一个字符就恰好是正确的密码,请你帮牛牛求出 ...