mysql简单主从复制(二)
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简单主从复制(二)的更多相关文章
- mysql简单主从复制(一)
MYSQL简单主从复制 master:172.25.44.1 slave:172.25.44.2 mysql5.7安装 master和slave均操作 准备rpm包:mysql-5.7.17-1.el ...
- Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(二)
然后是项目下的文件:完整的项目请看 上一篇 Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(一) 项目下的springmvc-servlet.xml配置文件: ...
- Gin实战:Gin+Mysql简单的Restful风格的API(二)
上一篇介绍了Gin+Mysql简单的Restful风格的API,但代码放在一个文件中,还不属于restful风格,接下来将进行进一步的封装. 目录结构 ☁ gin_restful2 tree . ├─ ...
- liinux安装 mysql 及主从复制
mariadb其实就是mysqlmysql已经被oracle收购,它即将闭源,马上要开始收费了因此还想免费试用开源的数据库mysql,就在centos7上,将mysql分支为mariadb 安装mar ...
- Linux系统——MySQL基础(二)
# MySQL数据库完全备份与恢复## 数据库备份的分类1. 从物理与逻辑的角度,备份可以分为物理备份和逻辑备份.(1)物理备份:对数据库操作系统的物理文件(数据文件.日志文件)的备份.物理备份又可分 ...
- mysql (主从复制)(proxy , Amoeba)
原址如下: http://heylinux.com/archives/1004.html Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中 ...
- mysql的主从复制原理与实现
关于mysql的主从复制,之前一直在听说这个话题,一直没有实现,昨天学习了下,原来是这么回事: 既然是主从复制,那么肯定有主有从,也就说一个主数据库(一般为写库),一个从数据库(读库).主数据库更新了 ...
- mysql ab主从复制出错及解决过程
一.mysql主从服务器报错描述:Slave_IO_Running=NO,Slave_SQL_Running=YES,Last_Errno=0 mysql slave stop ; mysql sla ...
- Linux 下的mysql+centos7+主从复制
mysql+centos7+主从复制 MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公 ...
随机推荐
- 配置mabatis,报Could not load driverClass ${jdbc.driverClassName}
<!-- 扫描mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" ...
- MySQL_第三方数据库引擎_tokudb
前阵子迁移zabbix到tokudb,整理部分操作笔记到这篇博文. 如果转载,请注明博文来源: www.cnblogs.com/xinysu/ ,版权归 博客园 苏家小萝卜 所有.望各 ...
- ETL作业调度软件TASKCTL4.1单机部署
单机部署,实际上就是将EM节点和一个Server节点安装到同一个地方.EM节点是TASKCTL服务端的最顶层,主要负责客户端与服务端之间的通信.Server节点是TASKCTL的调度服务控制层,也有A ...
- Android学习笔记-TextView(文本框)(一)
1.基础属性详解: id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeL ...
- C——Network Saboteur (POJ2531)
题目: A university network is composed of N computers. System administrators gathered information on t ...
- 各种排序算法及其java程序实现
各种排序算法:冒择路(入)兮(稀)快归堆,桶式排序,基数排序 冒泡排序,选择排序,插入排序,稀尔排序,快速排序,归并排序,堆排序,桶式排序,基数排序 一.冒泡排序(BubbleSort)1. 基本思想 ...
- swift3.0 移除当前页面的前一个页面
用的是以下的跳转方式 self.navigationController?.pushViewController(viewController, animated: true) 在当前页面移除方式 s ...
- Modelsim调用用do脚本自动化仿真
前言 EDA发展的趋势是自动化,使用脚本自动化仿真可以减少不必要的时间浪费. 流程 在windows下新建批处理脚本bat文件(linux下可用shell脚本或者其他,注意给脚本运行权限即可:chmo ...
- 普通程序员如何入门AI
毫无疑问,人工智能是目前整个互联网领域最火的行业,随着AlphaGo战胜世界围棋冠军,以及各种无人驾驶.智能家居项目的布道,人们已经意识到了AI就是下一个风口.当然,程序员是我见过对于新技术最敏感的一 ...
- 《Java从入门到放弃》入门篇:springMVC数据校验
昨天我们扯完了数据传递,今天我们来聊聊数据校验的问题.来,跟着我一起读:计一噢叫,一按艳. 在springMVC中校验数据也非常简单,spring3.0拥有自己独立的数据校验框架,同时支持JSR303 ...