MySQL 5.5 主从复制异步、半同步以及注意事项详解

大纲

一、前言

二、Mysql 基础知识

三、Mysql 复制(Replication)

四、Mysql 复制(Replication)类型

五、Mysql 主从复制基本步骤

六、Mysql 主从复制(异步)

七、Mysql 主从复制(半同步)

八、Mysql 复制工具

九、Mysql 复制注意事项

十、Mysql 复制过滤

一、前言

从这一篇博客开始我们就来学习mysql的高级课程,在前面的几篇博客我们讲解了mysql基础知识、mysql日志类型、mysql配置文件、mysql备份策略,这一篇博客中我们来讲解mysql的复制。

二、mysql基础知识

1.mysql日志类型

  • 二进制日志
  • 事务日志
  • 错误日志
  • 一般查询日志
  • 中继日志
  • 慢查询日志

注,有博友对mysql日志不怎么了解,可以参考这篇博客http://freeloda.blog.51cto.com/2033581/1253991

2.mysql二进制日志详解

说明:默认开启,精确的记录了用户对数据库中的数据进行操作的命令和操作的数据对象。  
二进制日志文件的作用:

  • 提供了增量备份的功能
  • 提供了数据基于时间点的恢复,这个恢复的时间点可以由用户控制
  • 为mysql的复制架构提供基础,将这主服务器的二进制日志复制到从服务器上并执行同样的操作,就可将数据进行同步
  • 实现数据的高可用
  • 分担负载

二进制日志默认存放位置:

  • 数据目录下,以mysql-bin.XXXXXX命名的日志

二进制日志格式:

  • 基于语句 statement
  • 基于行 row
  • 混合方式 mixed

二进制日志滚动方式:

  • 重启服务
  • 日志达到最大上限
  • 执行flush logs

二进制日志事件:

  • position 基于位置,也就是offset(偏移量)
  • datetime 基于时间
  • timestamp

二进制日志的查看与删除方式:


1

2

3

4

5

mysql>show master status; 查看当前正在使用的二进制日志

mysql>show binlog events in‘mysql-bin.000001‘; 查看二进制日志记录的事件[from position]

mysql>flush logs; 二进制日志滚动

mysql>show binary logs; 查看所有二进制日志

mysql>purge binary logs to ‘mysql-bin.000003‘; 删除二进制日志

文件系统中查看二进制日志的命令:


1

2

3

4

5

6

mysqlbinlog

相关选项,

--start-position #开始位置

--stop-position #结束位置

--start-datetime ‘yyyy-mm-dd hh:mm:ss‘#开始时间

--stop-datetime ‘‘#结束时间

配置mysql的主配置文件:


1

2

3

4

5

6

7

8

9

sql_log_bin = {ON|OFF} #用于控制二进制日志信息是否记录进日志文件。默认为ON,表示启用记录功能。用户可以在会话级别修改此变量的值,但其必须具有SUPER权限

binlog_cache_size = 32768 #默认值32768 Binlog Cache 用于在打开了二进制日志(binlog)记录功能的环境,是 MySQL 用来提高binlog的记录效率而设计的一个用于短时间内临时缓存binlog数据的内存区域。一般来说,如果我们的数据库中没有什么大事务,写入也不是特别频繁,2MB~4MB是一个合适的选择。但是如果我们的数据库大事务较多,写入量比较大,可与适当调高binlog_cache_size。同时,我们可以通过binlog_cache_use 以及 binlog_cache_disk_use来分析设置的binlog_cache_size是否足够,是否有大量的binlog_cache由于内存大小不够而使用临时文件(binlog_cache_disk_use)来缓存了

binlog_stmt_cache_size = 32768 #当非事务语句使用二进制日志缓存,但是超出binlog_stmt_cache_size时,使用一个临时文件来存放这些语句

log_bin = mysql-bin #指定binlog的位置,默认在数据目录下

binlog-format= {ROW|STATEMENT|MIXED} #指定二进制日志的类型,默认为MIXED。如果设定了二进制日志的格式,却没有启用二进制日志,则MySQL启动时会产生警告日志信息并记录于错误日志中。

sync_binlog = 10 #设定多久同步一次二进制日志至磁盘文件中,0表示不同步,任何正数值都表示对二进制每多少次写操作之后同步一次。当autocommit的值为1时,每条语句的执行都会引起二进制日志同步,否则,每个事务的提交会引起二进制日志同步

max_binlog_cache_size = {4096 .. 18446744073709547520} #二进定日志缓存空间大小,5.5.9及以后的版本仅应用于事务缓存,其上限由max_binlog_stmt_cache_size决定。

max_binlog_stmt_cache_size = {4096 .. 18446744073709547520} #二进定日志缓存空间大小,5.5.9及以后的版本仅应用于事务缓存

expire_log_days = {0..99} #设定二进制日志的过期天数,超出此天数的二进制日志文件将被自动删除。默认为0,表示不启用过期自动删除功能。如果启用此功能,自动删除工作通常发生在MySQL启动时或FLUSH日志时

注:一般建议将binlog日志与数据文件分开存放,不但可以提高mysql性能,还可以增加安全性!

三、Mysql 复制(Replication)

1.Mysql 复制作用

  • 负载平衡(load balancing)
  • 备份
  • 高可用性(high availability)和容错

2.Mysql 复制如何工作

主要有三步(如下图):

  • master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
  • slave将master的binary log events拷贝到它的中继日志(relay log);
  • slave重做中继日志中的事件,将改变反映它自己的数据。

具体说明:

该过程的第一部分就是master记录二进制日志。在每个事务更新数据完成之前,master在二日志记录这些改变。MySQL将事务串行的写入二进制日志,即使事务中的语句都是交叉执行的。在事件写入二进制日志完成后,master通知存储引擎提交事务。

下一步就是slave将master的binary log拷贝到它自己的中继日志。首先,slave开始一个工作线程——I/O线程。I/O线程在master上打开一个普通的连接,然后开始binlog dump process。Binlog dump process从master的二进制日志中读取事件,如果已经跟上master,它会睡眠并等待master产生新的事件。I/O线程将这些事件写入中继日志。   
SQL slave thread处理该过程的最后一步。SQL线程从中继日志读取事件,更新slave的数据,使其与master中的数据一致。只要该线程与I/O线程保持一致,中继日志通常会位于OS的缓存中,所以中继日志的开销很小。

此外,在master中也有一个工作线程:和其它MySQL的连接一样,slave在master中打开一个连接也会使得master开始一个线程。复制过程有一个很重要的限制——复制在slave上是串行化的,也就是说master上的并行更新操作不能在slave上并行操作。所以slave上数据一般要慢于master上数据。即master与slave之间的数据在一定时间内会不同步。

四、Mysql 复制(Replication)类型

1.mysql复制类型有以下一些基本原则

  • 每个slave只能有一个master;
  • 每个slave只能有一个唯一的服务器ID;
  • 每个master可以有很多slave;
  • 如果你设置log_slave_updates,slave可以是其它slave的master,从而扩散master的更新。

注,MySQL不支持多主服务器复制(Multimaster Replication)——即一个slave可以有多个master。但是,通过一些简单的组合,我们却可以建立灵活而强大的复制体系结构。

2.mysql复制类型

(1).一主多从模式

注,由一个master和一个slave组成复制系统是最简单的情况。Slave之间并不相互通信,只能与master进行通信。

具体说明:

如果写操作较少,而读操作很时,可以采取这种结构。你可以将读操作分布到其它的slave,从而减小master的压力。但是,当slave增加到一定数量时,slave对master的负载以及网络带宽都会成为一个严重的问题。这种结构虽然简单,但是,它却非常灵活,足够满足大多数应用需求。一些建议:

  • 不同的slave扮演不同的作用(例如使用不同的索引,或者不同的存储引擎)
  • 用一个slave作为备用master,只进行复制
  • 用一个远程的slave,用于灾难恢复

发送复制事件到其它slave,当设置log_slave_updates时,你可以让slave扮演其它slave的master。此时,slave把SQL线程执行的事件写进行自己的二进制日志(binary log),然后,它的slave可以获取这些事件并执行它

(2).双主模式

注,Master-Master复制的两台服务器,既是master,又是另一台服务器的slave。

具体说明:

主动的Master-Master复制有一些特殊的用处。例如,地理上分布的两个部分都需要自己的可写的数据副本。这种结构最大的问题就是更新冲突。假设一个表只有一行(一列)的数据,其值为1,如果两个服务器分别同时执行如下语句:

在第一个服务器上执行:


1

2

3

mysql> UPDATE tbl SET col=col + 1; 

在第二个服务器上执行: 

mysql> UPDATE tbl SET col=col * 2;

那么结果是多少呢?一台服务器是4,另一个服务器是3,但是,这并不会产生错误。

实际上,MySQL并不支持其它一些DBMS支持的多主服务器复制(Multimaster Replication),这是MySQL的复制功能很大的一个限制(多主服务器的难点在于解决更新冲突),但是,如果你实在有这种需求,你可以采用MySQL Cluster,以及将Cluster和Replication结合起来,可以建立强大的高性能的数据库平台。但是,可以通过其它一些方式来模拟这种多主服务器的复制。

(3).主从模式

注,这是master-master结构变化而来的,它避免了M-M的缺点,实际上,这是一种具有容错和高可用性的系统。它的不同点在于其中一个服务只能进行只读操作。

(4).带从服务器的Master-Master结构(Master-Master with Slaves)

注,这种结构的优点就是提供了冗余。在地理上分布的复制结构,它不存在单一节点故障问题,而且还可以将读密集型的请求放到slave上。

总结:一般常用的两种复制类型一种是主从模式,另一种是一主多从模式。在这一篇博客中我们主要讲解主从模式复制。

五、Mysql 主从复制基本步骤

1.master 配置

  • 启用二进制日志
  • 配置一个唯一的server-id
  • 创建具有复制权限的用户

2.slave 配置

  • 启用中继日志
  • 配置一个唯一的server-id
  • 连接主服务器,并开始复制数据
  • 启动数据复制

注,基本步骤我们就说到这里,下面我们来具体演示一下主从复制。

六、Mysql 主从复制(异步)

1.mysql异步复制

异步复制:MySQL本身支持单向的、异步的复制。异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时,最重要的是这意味着当应用系统的事务在主服务器上提交并确认时数据并不能在同一时刻拷贝或应用到从服务器上。通常这个延时是由网络带宽、资源可用性和系统负载决定的。然而,使用正确的组件并且调优,复制能做到接近瞬时完成。    
       当主库有更新的时候,主库会把更新操作的SQL写入二进制日志(Bin log),并维护一个二进制日志文件的索引,以便于日志文件轮回(Rotate)。在从库启动异步复制的时候,从库会开启两个I/O线程,其中一个线程连接主库,要求主库把二进制日志的变化部分传给从库,并把传回的日志写入本地磁盘。另一个线程则负责读取本地写入的二进制日志,并在本地执行,以反映出这种变化。较老的版本在复制的时候只启用一个I/O线程,实现这两部分的功能。下面我们来具体演示一下mysql的异步复制。

2.实验拓扑

注,Active (master,node1) 192.168.1.201 , Passive (slave,node2)192.168.1.202

3.环境配置

时间同步


1

2

[[email protected] ~]# ntpdate 202.120.2.101

[[email protected] ~]# ntpdate 202.120.2.101

4.操作系统

  • CentOS 6.4 X86_64

5.软件版本

  • Mysql 5.5.33 (注,这里用的是mysql 5.5.33二进制通用安装包,解压就能用)

6.安装并配置mysql

master:

(1).解压并链接


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

[[email protected] src]# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/

[[email protected] src]# cd /usr/local/ 

[[email protected] local]# ln -sv /usr/local/mysql-5.5.33-linux2.6-x86_64 mysql 

"mysql" -> "/usr/local/mysql-5.5.33-linux2.6-x86_64"

[[email protected] local]# cd mysql 

[[email protected] mysql]# ll 

总用量 200 

drwxr-xr-x  2 root root    4096 8月  24 17:58 bin 

-rw-r--r--  1 7161 wheel  17987 7月  15 20:01 COPYING 

drwxr-xr-x  3 root root    4096 8月  24 17:58 data 

drwxr-xr-x  2 root root    4096 8月  24 17:58 docs 

drwxr-xr-x  3 root root    4096 8月  24 17:58 include 

-rw-r--r--  1 7161 wheel 134493 7月  15 20:01 INSTALL-BINARY 

drwxr-xr-x  3 root root    4096 8月  24 17:58 lib 

drwxr-xr-x  4 root root    4096 8月  24 17:58 man

drwxr-xr-x 10 root root    4096 8月  24 17:58 mysql-test

-rw-r--r--  1 7161 wheel   2496 7月  15 20:01 README 

drwxr-xr-x  2 root root    4096 8月  24 17:58 scripts 

drwxr-xr-x 27 root root    4096 8月  24 17:58 share 

drwxr-xr-x  4 root root    4096 8月  24 17:58 sql-bench 

drwxr-xr-x  3 root root    4096 8月  24 17:58 support-files

(2).新建mysql用户


1

2

3

4

[[email protected] mysql]# groupadd -g 3306 mysql

[[email protected] mysql]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql 

[[email protected] mysql]# id mysql 

uid=3306(mysql) gid=3306(mysql) 组=3306(mysql)

(3).修改mysql安装目录所有者与所属组


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

[[email protected] mysql]# chown -R root.mysql /usr/local/mysql/*

[[email protected] mysql]# ll 

总用量 200 

drwxr-xr-x  2 root mysql   4096 8月  24 17:58 bin 

-rw-r--r--  1 root mysql  17987 7月  15 20:01 COPYING 

drwxr-xr-x  3 root mysql   4096 8月  24 17:58 data 

drwxr-xr-x  2 root mysql   4096 8月  24 17:58 docs 

drwxr-xr-x  3 root mysql   4096 8月  24 17:58 include 

-rw-r--r--  1 root mysql 134493 7月  15 20:01 INSTALL-BINARY 

drwxr-xr-x  3 root mysql   4096 8月  24 17:58 lib 

drwxr-xr-x  4 root mysql   4096 8月  24 17:58 man

drwxr-xr-x 10 root mysql   4096 8月  24 17:58 mysql-test

-rw-r--r--  1 root mysql   2496 7月  15 20:01 README 

drwxr-xr-x  2 root mysql   4096 8月  24 17:58 scripts 

drwxr-xr-x 27 root mysql   4096 8月  24 17:58 share 

drwxr-xr-x  4 root mysql   4096 8月  24 17:58 sql-bench 

drwxr-xr-x  3 root mysql   4096 8月  24 17:58 support-files

(4).为mysql提供配置文件并修改


1

2

3

4

5

[[email protected] mysql]# cp support-files/my-large.cnf /etc/my.cnf

[[email protected] mysql]# vim /etc/my.cnf

[mysqld]

#增加一行

datadir = /mydata/data

(5).为mysql提供启动脚本


1

2

[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[[email protected] mysql]# chmod +x /etc/init.d/mysqld

(6).初始化mysql数据库


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

[[email protected] ~]# mkdir -pv /mydata/data

mkdir: 已创建目录 "/mydata"

mkdir: 已创建目录 "/mydata/data"

[[email protected] ~]# chown -R mysql.mysql /mydata/data/ 

[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql 

Installing MySQL system tables... 

/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

Installation of system tables failed!  Examine the logs in

/mydata/data/ for more information.

You can try to start the mysqld daemon with:

    shell> /usr/local/mysql/bin/mysqld --skip-grant &

and use the command line tool /usr/local/mysql/bin/mysql

to connect to the mysql database and look at the grant tables:

    shell> /usr/local/mysql/bin/mysql -u root mysql

    mysql> show tables

Try ‘mysqld --help‘ if you have problems with paths.  Using --log

gives you a log in /mydata/data/ that may be helpful.

Please consult the MySQL manual section

‘Problems running mysql_install_db‘, and the manual section that 

describes problems on your OS.  Another information source are the 

MySQL email archives available at http://lists.mysql.com/.

Please check all of the above before mailing us!  And remember, if

you do mail us, you MUST use the /usr/local/mysql/scripts/mysqlbug script!

[[email protected] ~]# cd /mydata/data/

[[email protected] data]# ll 

总用量 8 

drwx------ 2 mysql root 4096 8月  24 18:21 mysql 

drwx------ 2 mysql root 4096 8月  24 18:21 test

(7).启动并测试


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

启动报错

[[email protected] data]# service mysqld start

Starting MySQL. ERROR! The server quit without updating PID file (/mydata/data/node1.test.com.pid).

查看一下错误日志

[[email protected] data]# vim node1.test.com.err

130824 18:21:44 mysqld_safe Starting mysqld daemon with databases from /mydata/data

/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory 

130824 18:21:44 mysqld_safe mysqld from pid file /mydata/data/node1.test.com.pid ended

注,从错误日志中我们看到差一个库文件libaio,我们用yum安装一下即可。

[[email protected] data]# yum install -y libaio

重新初始化mysql

[[email protected] data]# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql

启动mysql

[[email protected] data]# service mysqld start

Starting MySQL... SUCCESS!

环境变量配置

[[email protected] data]# vim /etc/profile.d/mysql.sh

export PATH=$PATH:/usr/local/mysql/bin

[[email protected] data]# source /etc/profile

测试一下

[[email protected] data]# mysql 

Welcome to the MySQL monitor.  Commands end with ; or \g. 

Your MySQL connection id is 1 

Server version: 5.5.33-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.

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mysql              | 

| performance_schema | 

test               

+--------------------+ 

4 rows in set (0.03 sec)

mysql>

好了,到这里master的mysql配置完成,下面我们进行slave的mysql配置。

slave:

(1).解压并链接


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

[[email protected] ~]# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/

[[email protected] ~]# cd /usr/local/ 

[[email protected] local]# ln -sv /usr/local/mysql-5.5.33-linux2.6-x86_64 mysql 

"mysql" -> "/usr/local/mysql-5.5.33-linux2.6-x86_64"

[[email protected] local]# cd mysql 

[[email protected] mysql]# ll 

总用量 200 

drwxr-xr-x  2 root root    4096 8月  24 18:41 bin 

-rw-r--r--  1 7161 wheel  17987 7月  15 20:01 COPYING 

drwxr-xr-x  3 root root    4096 8月  24 18:41 data 

drwxr-xr-x  2 root root    4096 8月  24 18:41 docs 

drwxr-xr-x  3 root root    4096 8月  24 18:41 include 

-rw-r--r--  1 7161 wheel 134493 7月  15 20:01 INSTALL-BINARY 

drwxr-xr-x  3 root root    4096 8月  24 18:41 lib 

drwxr-xr-x  4 root root    4096 8月  24 18:41 man

drwxr-xr-x 10 root root    4096 8月  24 18:41 mysql-test

-rw-r--r--  1 7161 wheel   2496 7月  15 20:01 README 

drwxr-xr-x  2 root root    4096 8月  24 18:41 scripts 

drwxr-xr-x 27 root root    4096 8月  24 18:41 share 

drwxr-xr-x  4 root root    4096 8月  24 18:41 sql-bench 

drwxr-xr-x  3 root root    4096 8月  24 18:41 support-files

(2).新建mysql用户


1

2

3

4

[[email protected] mysql]# groupadd -g 3306 mysql

[root[email protected] mysql]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql 

[[email protected] mysql]# id mysql 

uid=3306(mysql) gid=3306(mysql) 组=3306(mysql)

(3).修改mysql安装目录所有者与所属组


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

[[email protected] mysql]# chown -R root.mysql /usr/local/mysql/*

[[email protected] mysql]# ll 

总用量 200 

drwxr-xr-x  2 root mysql   4096 8月  24 18:41 bin 

-rw-r--r--  1 root mysql  17987 7月  15 20:01 COPYING 

drwxr-xr-x  3 root mysql   4096 8月  24 18:41 data 

drwxr-xr-x  2 root mysql   4096 8月  24 18:41 docs 

drwxr-xr-x  3 root mysql   4096 8月  24 18:41 include 

-rw-r--r--  1 root mysql 134493 7月  15 20:01 INSTALL-BINARY 

drwxr-xr-x  3 root mysql   4096 8月  24 18:41 lib 

drwxr-xr-x  4 root mysql   4096 8月  24 18:41 man

drwxr-xr-x 10 root mysql   4096 8月  24 18:41 mysql-test

-rw-r--r--  1 root mysql   2496 7月  15 20:01 README 

drwxr-xr-x  2 root mysql   4096 8月  24 18:41 scripts 

drwxr-xr-x 27 root mysql   4096 8月  24 18:41 share 

drwxr-xr-x  4 root mysql   4096 8月  24 18:41 sql-bench 

drwxr-xr-x  3 root mysql   4096 8月  24 18:41 support-files

(4).为mysql提供配置文件并修改


1

2

3

4

[[email protected] mysql]# cp support-files/my-large.cnf /etc/my.cnf 

[[email protected] mysql]# vim /etc/my.cnf

[mysqld]

datadir = /mydata/data

(5).为mysql提供启动脚本


1

2

[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld 

[[email protected] mysql]# chmod +x /etc/init.d/mysqld

(6).初始化mysql数据库


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

先安装libaio库文件

[[email protected] mysql]# yum install  -y libaio

[[email protected] mysql]# mkdir -pv /mydata/data

mkdir: 已创建目录 "/mydata"

mkdir: 已创建目录 "/mydata/data"

[[email protected] mysql]# cd /mydata/data 

[[email protected] data]# chown -R mysql.mysql /mydata/data/

[[email protected] data]# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql

[[email protected] data]# ll

总用量 1084 

drwx------ 2 mysql root     4096 8月  24 18:49 mysql 

-rw-rw---- 1 mysql mysql   27698 8月  24 18:49 mysql-bin.000001 

-rw-rw---- 1 mysql mysql 1061358 8月  24 18:49 mysql-bin.000002 

-rw-rw---- 1 mysql mysql      38 8月  24 18:49 mysql-bin.index 

drwx------ 2 mysql mysql    4096 8月  24 18:49 performance_schema 

drwx------ 2 mysql root     4096 8月  24 18:49 test

(7).启动并测试


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

启动mysql

[[email protected] data]# service mysqld start

Starting MySQL... SUCCESS!

环境变量配置

[[email protected] data]# vim /etc/profile.d/mysql.sh

export PATH=$PATH:/usr/local/mysql/bin

[[email protected] data]# source /etc/profile

测试一下

[[email protected] data]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g. 

Your MySQL connection id is 1 

Server version: 5.5.33-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.

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mysql              | 

| performance_schema | 

test               

+--------------------+ 

4 rows in set (0.06 sec)

mysql>

好了,slave的mysql也配置完成了,下面我们来配置主从复制。

7.配置master

(1).修改配置文件


1

2

3

4

5

6

7

[[email protected] ~]# vim /etc/my.cnf

#增加下面几行

[mysqld]

log-bin=master-bin

log-bin-index=master-bin.index

server-id       = 1

innodb_file_per_table = 1

(2).授权复制用户


1

2

3

4

mysql> grant replication slave on *.* to ‘repluser‘@‘192.168.1.%‘ identified by ‘replpass‘;

Query OK, 0 rows affected (0.61 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.41 sec)

(3).重启一下mysql服务


1

2

3

[[email protected] ~]# service mysqld restart

Shutting down MySQL....... SUCCESS!  

Starting MySQL............. SUCCESS!

8.配置slave

(1).修改配置文件


1

2

3

4

5

6

[[email protected] ~]# vim /etc/my.cnf

#增加下面几行

[mysqld]

relay-log = relay-log

relay-log-index = relay-log.index

server-id       = 10

(2).重启mysql服务


1

2

3

[[email protected] ~]# service mysqld restart

Shutting down MySQL..... SUCCESS!  

Starting MySQL........... SUCCESS!

(3).连接主服务器并复制


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

查看master上二进制日志

mysql> show master status;

+-------------------+----------+--------------+------------------+ 

| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | 

+-------------------+----------+--------------+------------------+ 

| master-bin.000001 |      107 |              |                  | 

+-------------------+----------+--------------+------------------+ 

1 row in set (0.00 sec)

连接master服务器

mysql> change master to master_host=‘192.168.1.201‘,master_user=‘repluser‘,master_password=‘replpass‘,master_log_file=‘master-bin.000001‘,master_log_pos=107;

Query OK, 0 rows affected (0.07 sec)

查看一下slave状态

mysql> show slave status\G

*************************** 1. row *************************** 

               Slave_IO_State:  

                  Master_Host: 192.168.1.201 

                  Master_User: repluser 

                  Master_Port: 3306 

                Connect_Retry: 60 

              Master_Log_File: master-bin.000001 

          Read_Master_Log_Pos: 107 

               Relay_Log_File: relay-log.000001 

                Relay_Log_Pos: 4 

        Relay_Master_Log_File: master-bin.000001 

             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: 107 

              Relay_Log_Space: 107 

              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 

1 row in set (0.00 sec)

启动复制并查看状态

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G

*************************** 1. row *************************** 

               Slave_IO_State: Waiting for master to send event 

                  Master_Host: 192.168.1.201 

                  Master_User: repluser 

                  Master_Port: 3306 

                Connect_Retry: 60 

              Master_Log_File: master-bin.000001 

          Read_Master_Log_Pos: 107 

               Relay_Log_File: relay-log.000002 

                Relay_Log_Pos: 254 

        Relay_Master_Log_File: master-bin.000001 

             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: 107 

              Relay_Log_Space: 404 

              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: 1 

1 row in set (0.00 sec)

9.主从复制测试

master:


1

2

3

4

5

6

7

8

9

10

11

12

13

mysql> create database mydb;

Query OK, 1 row affected (0.00 sec)

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mysql              | 

| performance_schema | 

test               

+--------------------+ 

5 rows in set (0.00 sec)

slave:


1

2

3

4

5

6

7

8

9

10

11

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mysql              | 

| performance_schema | 

test               

+--------------------+ 

5 rows in set (0.03 sec)

好了,到这里异步的主从复制到这里配置完成。下面我们来说一下什么是半同步复制(或说是同步也行)。

七、Mysql 主从复制(半同步)

1.半同步复制

在说明半同步复制之前我们先来了解一下,什么是同步复制?同步复制:同步复制可以定义为数据在同一时刻被提交到一台或多台机器,通常这是通过众所周知的“两阶段提交”做到的。虽然这确实给你在多系统中保持一致性,但也由于增加了额外的消息交换而造成性能下降。使用MyISAM或者InnoDB存储引擎的MySQL本身并不支持同步复制,然而有些技术,例如分布式复制块设备(简称DRBD),可以在下层的文件系统提供同步复制,允许第二个MySQL服务器在主服务器丢失的情况下接管(使用第二服务器的复本)。了解了同步复制我们正下面来说一下,什么是半同步复制?

MYSQL 5.5开始,支持半自动复制。之前版本的MySQL Replication都是异步(asynchronous)的,主库在执行完一些事务后,是不会管备库的进度的。如果备库不幸落后,而更不幸的是主库此时又出现Crash(例如宕机),这时备库中的数据就是不完整的。简而言之,在主库发生故障的时候,我们无法使用备库来继续提供数据一致的服务了。Semisynchronous Replication(半同步复制)则一定程度上保证提交的事务已经传给了至少一个备库。Semi synchronous中,仅仅保证事务的已经传递到备库上,但是并不确保已经在备库上执行完成了。

此外,还有一种情况会导致主备数据不一致。在某个session中,主库上提交一个事务后,会等待事务传递给至少一个备库,如果在这个等待过程中主库Crash,那么也可能备库和主库不一致,这是很致命的。如果主备网络故障或者备库挂了,主库在事务提交后等待10秒(rpl_semi_sync_master_timeout的默认值)后,就会继续。这时,主库就会变回原来的异步状态。

MySQL在加载并开启Semi-sync插件后,每一个事务需等待备库接收日志后才返回给客户端。如果做的是小事务,两台主机的延迟又较小,则Semi-sync可以实现在性能很小损失的情况下的零数据丢失。

2.异步与半同步异同

默认情况下MySQL的复制是异步的,Master上所有的更新操作写入Binlog之后并不确保所有的更新都被复制到Slave之上。异步操作虽然效率高,但是在Master/Slave出现问题的时候,存在很高数据不同步的风险,甚至可能丢失数据。

MySQL5.5引入半同步复制功能的目的是为了保证在master出问题的时候,至少有一台Slave的数据是完整的。在超时的情况下也可以临时转入异步复制,保障业务的正常使用,直到一台salve追赶上之后,继续切换到半同步模式。

3.具体配置

注,mysql5.5半同步插件是由谷歌提供,具体位置/usr/local/mysql/lib/plugin/下,一个是master用的semisync_master.so,一个是slave用的semisync_slave.so,下面我们就来具体配置一下。

master:

(1).安装插件


1

2

3

4

5

6

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME ‘semisync_master.so‘

Query OK, 0 rows affected (0.39 sec)

mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;

Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;

Query OK, 0 rows affected (0.00 sec)

(2).修改配置文件


1

2

3

4

[[email protected] ~]# vim /etc/my.cnf

[mysqld]

rpl_semi_sync_master_enabled=1 #启用半同步

rpl_semi_sync_master_timeout=1000 #超时时间为1s

(3).重新启动服务


1

2

3

[[email protected] ~]# service mysqld restart

Shutting down MySQL... SUCCESS!  

Starting MySQL.. SUCCESS!

slave:

(1).安装插件


1

2

3

4

5

6

7

8

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so‘

Query OK, 0 rows affected (0.38 sec)

mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1; 

Query OK, 0 rows affected (0.00 sec)

mysql> STOP SLAVE IO_THREAD;

Query OK, 0 rows affected (0.00 sec)

mysql> START SLAVE IO_THREAD;

Query OK, 0 rows affected (0.01 sec)

(2).修改配置文件


1

2

3

[[email protected] ~]# vim /etc/my.cnf

[mysqld]

rpl_semi_sync_slave_enabled=1  #启用半同步复制

(3).重新启动服务


1

2

3

[[email protected] ~]# service mysqld restart

Shutting down MySQL. SUCCESS!  

Starting MySQL.. SUCCESS!

4.查看一下状态

master:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

mysql> SHOW GLOBAL STATUS LIKE ‘rpl_semi%‘;

+--------------------------------------------+-------+ 

| Variable_name                              | Value | 

+--------------------------------------------+-------+ 

| Rpl_semi_sync_master_clients               | 1     | 

| Rpl_semi_sync_master_net_avg_wait_time     | 0     | 

| Rpl_semi_sync_master_net_wait_time         | 0     | 

| Rpl_semi_sync_master_net_waits             | 0     | 

| Rpl_semi_sync_master_no_times              | 0     | 

| Rpl_semi_sync_master_no_tx                 | 0     | 

| Rpl_semi_sync_master_status                | ON    | 

| Rpl_semi_sync_master_timefunc_failures     | 0     | 

| Rpl_semi_sync_master_tx_avg_wait_time      | 0     | 

| Rpl_semi_sync_master_tx_wait_time          | 0     | 

| Rpl_semi_sync_master_tx_waits              | 0     | 

| Rpl_semi_sync_master_wait_pos_backtraverse | 0     | 

| Rpl_semi_sync_master_wait_sessions         | 0     | 

| Rpl_semi_sync_master_yes_tx                | 0     | 

+--------------------------------------------+-------+ 

14 rows in set (0.00 sec)

slave:


1

2

3

4

5

6

7

mysql> SHOW GLOBAL STATUS LIKE ‘rpl_semi%‘;

+----------------------------+-------+ 

| Variable_name              | Value | 

+----------------------------+-------+ 

| Rpl_semi_sync_slave_status | ON    | 

+----------------------------+-------+ 

1 row in set (0.01 sec)

5.测试一下

master:


1

2

3

4

5

6

7

8

9

10

11

mysql> create table user (id int(10));

Query OK, 0 rows affected (0.42 sec)

mysql> show tables;

+----------------+ 

| Tables_in_mydb | 

+----------------+ 

| user           | 

+----------------+ 

1 row in set (0.00 sec)

mysql> insert user value (1);

Query OK, 1 row affected (0.34 sec)

注,大家可以看到创建一个表的插入一个数据的时间都很长,说明半同步配置完成。

6.模拟一下故障

slave:


1

2

3

4

5

mysql> STOP SLAVE IO_THREAD;

Query OK, 0 rows affected (0.01 sec)

master:

mysql> create table user1 (id int(10));

Query OK, 0 rows affected (1.03 sec)

注,大家可以看到主服务器会卡1s,我们超时时间设置的为1s。

7.查看一下状态


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

mysql> SHOW GLOBAL STATUS LIKE ‘rpl_semi%‘;

+--------------------------------------------+-------+ 

| Variable_name                              | Value | 

+--------------------------------------------+-------+ 

| Rpl_semi_sync_master_clients               | 1     | 

| Rpl_semi_sync_master_net_avg_wait_time     | 1560  | 

| Rpl_semi_sync_master_net_wait_time         | 10920 | 

| Rpl_semi_sync_master_net_waits             | 7     | 

| Rpl_semi_sync_master_no_times              | 1     | 

| Rpl_semi_sync_master_no_tx                 | 1     | 

| Rpl_semi_sync_master_status                | OFF   | 

| Rpl_semi_sync_master_timefunc_failures     | 0     | 

| Rpl_semi_sync_master_tx_avg_wait_time      | 985   | 

| Rpl_semi_sync_master_tx_wait_time          | 985   | 

| Rpl_semi_sync_master_tx_waits              | 1     | 

| Rpl_semi_sync_master_wait_pos_backtraverse | 0     | 

| Rpl_semi_sync_master_wait_sessions         | 0     | 

| Rpl_semi_sync_master_yes_tx                | 6     | 

+--------------------------------------------+-------+ 

14 rows in set (0.00 sec)

mysql> STOP SLAVE IO_THREAD;

Query OK, 0 rows affected (0.01 sec)

mysql> SHOW GLOBAL STATUS LIKE ‘rpl_semi%‘;

+----------------------------+-------+ 

| Variable_name              | Value | 

+----------------------------+-------+ 

| Rpl_semi_sync_slave_status | OFF   | 

+----------------------------+-------+ 

1 row in set (0.00 sec)

好了,到这里我们就配置完成了半同步复制。希望大家有所收获。下面我们来简单说一下mysql复制的工具。

八、Mysql 复制工具

1.percona-toolkit简介

percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括:

  • 检查master和slave数据的一致性
  • 有效地对记录进行归档
  • 查找重复的索引
  • 对服务器信息进行汇总
  • 分析来自日志和tcpdump的查询
  • 当系统出问题的时候收集重要的系统信息

percona-toolkit源自Maatkit 和Aspersa工具,这两个工具是管理mysql的最有名的工具,现在Maatkit工具已经不维护了,请大家还是使用percona-toolkit吧!这些工具主要包括开发、性能、配置、监控、复制、系统、实用六大类,作为一个优秀的DBA,里面有的工具非常有用,如果能掌握并加以灵活应用,将能极大的提高工作效率。

2.安装percona-toolkit


1

2

3

4

5

6

7

8

9

10

11

12

[[email protected] ~]# wget http://www.percona.com/downloads/percona-toolkit/2.2.4/RPM/percona-toolkit-2.2.4-1.noarch.rpm

[[email protected] ~]# yum install -y percona-toolkit-2.2.4-1.noarch.rpm

[[email protected] ~]# pt #以pt开头命令 

pt-agent                  pt-fingerprint            pt-pmp                    pt-table-checksum 

pt-align                  pt-fk-error-logger        pt-query-digest           pt-table-sync

pt-archiver               pt-heartbeat              pt-show-grants            pt-table-usage 

pt-config-diff            pt-index-usage            pt-sift                   pt-upgrade 

pt-deadlock-logger        pt-ioprofile              pt-slave-delay            pt-variable-advisor 

pt-diskstats              pt-kill                   pt-slave-find             pt-visual-explain 

pt-duplicate-key-checker  pt-mext                   pt-slave-restart          ptx 

pt-fifo-split             pt-mysql-summary          pt-stalk               

pt-find                   pt-online-schema-change   pt-summary

3.简单使用

常用工具:

(1).服务器摘要


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

[[email protected] ~]# pt-summary 

# Percona Toolkit System Summary Report ###################### 

        Date | 2013-08-24 15:15:14 UTC (local TZ: CST +0800) 

    Hostname | node1.test.com 

      Uptime | 10:37,  3 users,  load average: 0.00, 0.08, 0.07 

    Platform | Linux 

     Release | CentOS release 6.4 (Final) 

      Kernel | 2.6.32-358.el6.x86_64 

Architecture | CPU = 64-bit, OS = 64-bit 

   Threading | NPTL 2.12 

     SELinux | Disabled 

 Virtualized | VMWare 

# Processor ################################################## 

  Processors | physical = 2, cores = 0, virtual = 2, hyperthreading = no 

      Speeds | 2x2261.309 

      Models | 2xIntel(R) Core(TM) i5 CPU M 430 @ 2.27GHz 

      Caches | 2x3072 KB 

# Memory ##################################################### 

       Total | 230.8M 

        Free | 45.5M 

        Used | physical = 185.3M, swap allocated = 1000.0M, swap used = 40.5M, virtual = 225.9M 

     Buffers | 4.3M 

      Caches | 75.0M 

       Dirty | 104 kB 

     UsedRSS | 69.0M 

  Swappiness | 60 

 DirtyPolicy | 20, 10 

 DirtyStatus | 0, 0 

# Mounted Filesystems ######################################## 

  Filesystem  Size Used Type  Opts Mountpoint 

  /dev/sda1   194M  14% ext4  rw   /boot

  /dev/sda2   9.7G  27% ext4  rw   / 

  /dev/sda3   4.9G   3% ext4  rw   /data

  tmpfs       116M   0% tmpfs rw   /dev/shm

# Disk Schedulers And Queue Size ############################# 

         sda | [cfq] 128 

         sdb | [cfq] 128 

         sr0 | [cfq] 128 

# Disk Partioning ############################################ 

Device       Type      Start        End               Size 

============ ==== ========== ========== ================== 

/dev/sda     Disk                              21474836480 

/dev/sda1    Part          1         26          205632000 

/dev/sda2    Part         26       1301        10487232000 

/dev/sda3    Part       1301       1938         5239503360 

/dev/sda4    Part       1938       2611         5535613440 

/dev/sda5    Part       1939       2066         1044610560 

/dev/sdb     Disk                              21474836480 

/dev/sr0     Disk                               4353378304 

# Kernel Inode State ######################################### 

dentry-state | 8709    3069    45    0    0    0 

     file-nr | 736    0    21159 

    inode-nr | 9018    568 

# LVM Volumes ################################################ 

Unable to collect information 

# LVM Volume Groups ########################################## 

Unable to collect information 

# RAID Controller ############################################ 

  Controller | No RAID controller detected 

# Network Config ############################################# 

 FIN Timeout | 60 

  Port Range | 61000 

# Interface Statistics ####################################### 

  interface  rx_bytes rx_packets  rx_errors   tx_bytes tx_packets  tx_errors 

  ========= ========= ========== ========== ========== ========== ========== 

  lo                0          0          0          0          0          0 

  eth0      225000000     225000          0  200000000     225000          0 

# Network Devices ############################################ 

  Device    Speed     Duplex 

  ========= ========= ========= 

  eth0       1000Mb/s   Full   

# Network Connections ######################################## 

  Connections from remote IP addresses 

    192.168.1.102       2 

  Connections to local IP addresses 

    192.168.1.201       2 

  Connections to top 10 local ports 

    22                  2 

  States of connections 

    ESTABLISHED         2 

    LISTEN             10 

# Top Processes ############################################## 

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 

    1 root      20   0 19228  648  444 S  0.0  0.3   0:01.71 init 

    2 root      20   0     0    0    0 S  0.0  0.0   0:00.04 kthreadd 

    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.51 migration/0

    4 root      20   0     0    0    0 S  0.0  0.0   0:00.14 ksoftirqd/0

    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0

    6 root      RT   0     0    0    0 S  0.0  0.0   0:00.13 watchdog/0

    7 root      RT   0     0    0    0 S  0.0  0.0   0:00.17 migration/1

    8 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/1

    9 root      20   0     0    0    0 S  0.0  0.0   0:00.09 ksoftirqd/1

# Notable Processes ########################################## 

  PID    OOM    COMMAND 

 1037    -17    sshd 

# Simplified and fuzzy rounded vmstat (wait please) ########## 

  procs  ---swap-- -----io---- ---system---- --------cpu-------- 

   r  b    si   so    bi    bo     ir     cs  us  sy  il  wa  st 

   1  0     0    1    15    15      9     15   0   0 100   0   0 

   0  0     0    0     0     0   1000   1000   5  18  77   0   0 

   0  0     0    0     0     0     10     40   0   0 100   0   0 

   0  0     0    0     0     0     15     40   0   0 100   0   0 

   0  0     0    0     0     0     15     40   0   0 100   0   0 

# The End ####################################################

(2).服务器磁盘监测


1

2

3

4

5

6

7

8

9

10

11

[[email protected] ~]# pt-diskstats 

  #ts device    rd_s rd_avkb rd_mb_s rd_mrg rd_cnc   rd_rt    wr_s wr_avkb wr_mb_s wr_mrg wr_cnc   wr_rt busy in_prg    io_s  qtime stime 

  1.0 sda        0.0     0.0     0.0     0%    0.0     0.0     2.0     6.0     0.0    33%    0.0     0.3   0%      0     2.0    0.0   0.3 

  1.0 sda2       0.0     0.0     0.0     0%    0.0     0.0     2.0     6.0     0.0    33%    0.0     0.3   0%      0     2.0    0.0   0.3

  1.0 sda        0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0

  1.0 sda2       0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0

  1.0 sda        0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0

  1.0 sda2       0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0

  1.0 sda        0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0

  1.0 sda2       0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0

  1.0 sda        0.0     0.0     0.0     0%    0.0     0.0     0.0     0.0     0.0     0%    0.0     0.0   0%      0     0.0    0.0   0.0

(3).mysql服务状态摘要


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

[[email protected] ~]# pt-mysql-summary -- --user=root 

# Percona Toolkit MySQL Summary Report ####################### 

              System time | 2013-08-24 15:16:55 UTC (local TZ: CST +0800) 

# Instances ################################################## 

  Port  Data Directory             Nice OOM Socket 

  ===== ========================== ==== === ====== 

   3306 /mydata/data               0    0   /tmp/mysql.sock 

# MySQL Executable ########################################### 

       Path to executable | /usr/local/mysql/bin/mysqld

              Has symbols | Yes 

# Report On Port 3306 ######################################## 

                     User | [email protected] 

                     Time | 2013-08-24 23:16:55 (CST) 

                 Hostname | node1.test.com 

                  Version | 5.5.33-log MySQL Community Server (GPL) 

                 Built On | linux2.6 x86_64 

                  Started | 2013-08-24 22:37 (up 0+00:39:17) 

                Databases | 5 

                  Datadir | /mydata/data/

                Processes | 2 connected, 2 running 

              Replication | Is not a slave, has 1 slaves connected 

                  Pidfile | /mydata/data/node1.test.com.pid (exists) 

# Processlist ################################################

  Command                        COUNT(*) Working SUM(Time) MAX(Time)

  ------------------------------ -------- ------- --------- --------- 

  Binlog Dump                           1       1      2250      2250 

  Query                                 1       1         0         0

  User                           COUNT(*) Working SUM(Time) MAX(Time)

  ------------------------------ -------- ------- --------- --------- 

  repluser                              1       1      2250      2250 

  root                                  1       1         0         0

  Host                           COUNT(*) Working SUM(Time) MAX(Time)

  ------------------------------ -------- ------- --------- --------- 

  192.168.1.202                         1       1      2250      2250 

  localhost                             1       1         0         0

  db                             COUNT(*) Working SUM(Time) MAX(Time)

  ------------------------------ -------- ------- --------- --------- 

  NULL                                  2       2      2250      2250

  State                          COUNT(*) Working SUM(Time) MAX(Time)

  ------------------------------ -------- ------- --------- --------- 

  Master has sent all binlog to         1       1      2250      2250 

  NULL                                  1       1         0         0

# Status Counters (Wait 10 Seconds) ##########################

Variable                                Per day  Per second     10 secs 

Aborted_clients                              70                     

Binlog_cache_use                            175                     

Bytes_received                            90000           1         200 

Bytes_sent                               700000           7        2000 

Com_admin_commands                          100                     

Com_change_db                                70                     

Com_create_table                            100                     

Com_insert                                  175                     

Com_select                                  400                       1 

Com_set_option                              250                     

Com_show_databases                           70                     

Com_show_status                             100                     

Com_show_tables                              70                     

Com_show_variables                          250                     

Connections                                 350                       1 

Created_tmp_files                           225                     

Created_tmp_tables                          500                       4 

Flush_commands                               35                     

Handler_commit                              350                     

Handler_prepare                             350                     

Handler_read_first                          100                     

Handler_read_rnd_next                     15000                      35 

Handler_write                             12500                      35 

Innodb_buffer_pool_bytes_data          90000000        1000         

Innodb_buffer_pool_pages_flushed           1500                     

Innodb_buffer_pool_read_requests          22500                     

Innodb_buffer_pool_reads                   5000                     

Innodb_buffer_pool_write_requests          5000                     

Innodb_data_fsyncs                         1250                     

Innodb_data_read                      175000000        2000         

Innodb_data_reads                          6000                     

Innodb_data_writes                         2500                     

Innodb_data_written                    50000000         600         

Innodb_dblwr_pages_written                 1500                     

Innodb_dblwr_writes                         150                     

Innodb_log_write_requests                  1250                     

Innodb_log_writes                           500                     

Innodb_os_log_fsyncs                        700                     

Innodb_os_log_written                    700000           7         

Innodb_pages_created                        500                     

Innodb_pages_read                          5000                     

Innodb_pages_written                       1500                     

Innodb_rows_inserted                        175                     

Open_table_definitions                     1250                     

Opened_files                               3500                       4 

Opened_table_definitions                   1250                     

Opened_tables                              1250                     

Qcache_not_cached                           400                       1 

Queries                                    2250                       4 

Questions                                  2250                       4 

Rpl_semi_sync_master_clients                 35                     

Rpl_semi_sync_master_net_avg_wait_time       60000                     

Rpl_semi_sync_master_net_wait_time       400000           4         

Rpl_semi_sync_master_net_waits              250                     

Rpl_semi_sync_master_no_times                35                     

Rpl_semi_sync_master_no_tx                   35                     

Rpl_semi_sync_master_tx_avg_wait_time       35000                     

Rpl_semi_sync_master_tx_wait_time         35000                     

Rpl_semi_sync_master_tx_waits                35                     

Rpl_semi_sync_master_yes_tx                 225                     

Select_scan                                 500                     

Table_locks_immediate                      1500                     

Threads_created                              70                     

Uptime                                    90000           1           1 

# Table cache ################################################ 

                     Size | 256 

                    Usage | 10% 

# Key Percona Server features ################################ 

      Table & Index Stats | Not Supported 

     Multiple I/O Threads | Enabled 

     Corruption Resilient | Not Supported 

      Durable Replication | Not Supported 

     Import InnoDB Tables | Not Supported 

     Fast Server Restarts | Not Supported 

         Enhanced Logging | Not Supported 

     Replica Perf Logging | Not Supported 

      Response Time Hist. | Not Supported 

          Smooth Flushing | Not Supported 

      HandlerSocket NoSQL | Not Supported 

           Fast Hash UDFs | Unknown 

# Percona XtraDB Cluster ##################################### 

# Plugins #################################################### 

       InnoDB compression | ACTIVE 

# Query cache ################################################ 

         query_cache_type | ON 

                     Size | 16.0M 

                    Usage | 0% 

         HitToInsertRatio | 0% 

# Semisynchronous Replication ################################ 

   master semisync status |  

       master trace level | 32, net wait (more information about network waits) 

master timeout in milliseconds | 1000 

  master waits for slaves | ON 

           master clients |  

 master net_avg_wait_time |  

     master net_wait_time |  

         master net_waits |  

          master no_times |  

             master no_tx |  

 master timefunc_failures |  

  master tx_avg_wait_time |  

      master tx_wait_time |  

          master tx_waits |  

master wait_pos_backtraverse |  

     master wait_sessions |  

            master yes_tx |  

                    Slave | Disabled 

# Schema #####################################################

(4).慢查询日志分析统计


1

[[email protected] ~]# pt-query-digest /mydata/data/mysql-slow.log

(5).主从状态监测,提供给它一台mysql服务器的IP用户名密码,就可以分析出整个主从架构中每台服务器的信息,包括但不限于mysql版本,IP地址,server ID,mysql服务的启动时间,角色(主/从),Slave Status(落后于主服务器多少秒,有没有错误,slave有没有在运行)。


1

2

3

4

5

6

7

8

9

10

11

12

13

[[email protected] ~]# pt-slave-find --host=localhost --user=root

Cannot connect to h=192.168.1.202,u=root 

localhost 

Version         5.5.33-log 

Server ID       1 

Uptime          42:09 (started 2013-08-24T22:37:38) 

Replication     Is not a slave, has 1 slaves connected, is not read_only 

Filters      

Binary logging  MIXED 

Slave status 

Slave mode      STRICT 

Auto-increment  increment 1, offset 1 

InnoDB version  5.5.33

(6).mysql死锁监测


1

[[email protected] ~]# pt-duplicate-key-checker --database=world h=‘127.0.0.1‘ --user=root --password=123456

(7).监测从库的复制延迟


1

[[email protected] ~]# pt-slave-delay --host 192.168.1.202 --user=root --password=123456

注,简单说明就到这里,想学习更详细的内容,命令的使用可以通过--help获知

九、Mysql 复制注意事项

注,在主-从架构上建议使用的配置

master:


1

2

3

4

5

sync_binlog=1 # 立刻同步binlog

innodb_flush_logs_at_trx_commit=1 #立刻刷新innodb日志

slave:

skip_slave_start=1 #设置开机不同步

read_only=1 #设置为只读

十、Mysql 复制过滤

master:


1

2

3

4

5

6

7

8

9

binlog-do-db=mydb

binlog-ignore-db=mysql

slave:

replicate_do_db

rpplicate_ignore_db

replicate_do_table

replicate_ignore_table

replicate_wild_do_table

replicate_wild_ignore_table

测试一下:

在从服务器上只复制testdb一个数据库

slave:


1

2

3

4

5

6

7

[[email protected] ~]# vim /etc/my.cnf

[mysqld]

replicate_do_db=testdb

replicate_do_db=mysql

[[email protected] ~]# service mysqld restart

Shutting down MySQL. SUCCESS!  

Starting MySQL.. SUCCESS!

master:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

mysql> create database mydb1;

Query OK, 1 row affected (0.34 sec)

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mydb1              | 

| mysql              | 

| performance_schema | 

test               

+--------------------+ 

6 rows in set (0.00 sec)

slave:


1

2

3

4

5

6

7

8

9

10

11

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mysql              | 

| performance_schema | 

test               

+--------------------+ 

5 rows in set (0.00 sec)

注,大家可以看到没有同步mydb1,再测试一下。

master:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

mysql> create database testdb;

Query OK, 1 row affected (0.01 sec)

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mydb1              | 

| mysql              | 

| performance_schema | 

test               

| testdb             | 

+--------------------+ 

7 rows in set (0.00 sec)

slave:

mysql> show databases;

+--------------------+ 

| Database           | 

+--------------------+ 

| information_schema | 

| mydb               | 

| mysql              | 

| performance_schema | 

test               

| testdb             | 

+--------------------+ 

6 rows in set (0.00 sec)

注,大家可以看到同步了testdb,好了到这里所有演示全部完成,希望大家有所收获。^_^……

本文出自 “Share your knowledge …” 博客,请务必保留此出处http://freeloda.blog.51cto.com/2033581/1282329

时间: 2024-08-27 10:54:53

MySQL 5.5 主从复制异步、半同步以及注意事项详解的相关文章

第四阶段 (七)MySQL REPLICATION(主从复制、半同步复制、复制过滤)

Linux运维 第四阶段 (七)MySQL REPLICATION(主从复制.半同步复制.复制过滤) 一.MySQL Replication相关概念: 1.复制的作用:辅助实现备份:高可用HA:异地容灾:分摊负载(scaleout):rw-spliting(mysql proxy工作在应用层). 2.master有多个CPU允许事务并行执行,但往二进制日志文件只能一条条写:slave比master要慢:master-slave默认异步方式传送. 3.半同步:仅负责最近一台slave同步成功,其它

mysql主从复制异步半同步实例

建立mysql的复制 node1: mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+-----

MySQL数据的主从复制、半同步复制和主主复制详解

一.MySQL复制概述 ⑴.MySQL数据的复制的基本介绍 目前MySQL数据库已经占去数据库市场上很大的份额,其一是由于MySQL数据的开源性和高性能,当然还有重要的一条就是免费~不过不知道还能免费多久,不容乐观的未来,但是我们还是要能熟练掌握MySQL数据的架构和安全备份等功能,毕竟现在它还算是开源界的老大吧! MySQL数据库支持同步复制.单向.异步复制,在复制的过程中一个服务器充当主服务,而一个或多个服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环

MySQL主从复制:半同步、异步

MySQL主从复制:半同步.异步 大纲 前言 如何对MySQL进行扩展? MySQL Replication WorkFlow MySQL主从复制模式 实战演练 MySQL异步复制实现 MySQL半同步复制实现 实验中的思考 总结 前言 本篇我们介绍MySQL Replication的相关内容, 我们首先介绍MySQL CLuster的实现原理和如何一步步构建一个MySQL Replication Cluster 看懂本文需要了解: MySQL基本操作,MySQL日志类型及其作用 如何对MySQ

Mariadb主从复制,半同步复制,主主复制

文章内容概览 主从复制介绍 主从复制的作用 复制工作流程 复制时应该注意的问题 主从复制配置 半同步复制配置 复制过滤器方法说明和配置 双主(主主复制)配置 文章环境说明 操作系统: [[email protected] ~]# cat/etc/redhat-release CentOS release 6.6 (Final) [[email protected] ~]# uname -r 2.6.32-504.el6.x86_64 [[email protected] ~]# uname -m

MySQL5.6 数据库主从同步安装与配置详解(Master/Slave)

MySQL5.6 数据库主从同步安装与配置详解(Master/Slave)本篇文章主要介绍了MySQL5.6 数据库主从同步安装与配置详解,具有一定的参考价值,有兴趣的可以了解一下.安装环境 操作系统 :CentOS 6.5 数据库版本:MySQL 5.6.27 主机A:192.168.1.1 (Master) 主机B:192.168.1.2 (Slave) 这里强调的数据库的版本,是因为MySQL在5.6之前和之后的安装方式是不一样的. 本人在进行配置的时候,也遇到了这个坑,这里提前说明,希望

MySQL Server 5.0–安装及配置/MySQLInstanceConfig.exe用法详解

MySQL Server 5.0–安装及配置/MySQLInstanceConfig.exe用法详解 http://blog.csdn.net/feihong247/article/details/7791105 配置MySQL步骤: 1.       运行MySQL Server安装目录下bin/MySQLInstanceConfig.exe.出现如下所示的向导界面 . 点击"Next"进入下一步. 2.       如果MySQLInstanceConfig在MySQL Serve

SQL Server 2000 复制同步配置及常见问题详解(下)

SQL Server 2000 复制同步配置及常见问题详解(下) (二)分发.发布服务器端配置及问题 1.启动配置发布.订阅服务区器和分发向导 2.指定网络位置上的快照共享文件夹 3.配置完成4.启动新建发布向导 5.选择要发布的数据库6.选择合并发布,这样无论哪一端数据发生变化都会全范围同步7.这里可以选择兼容老版本的SQL Server,默认只选20008.选择要发布的数据库中的对象9. 11.设置新建发布属性 12.勾选“允许匿名订阅”问题五:若此处没有勾选“允许匿名订阅”,则会在订阅配置

SQL Server 2000 复制同步配置及常见问题详解

SQL Server 2000 复制同步配置及常见问题详解(上) 最近因为要用SQL Server2000的同步复制功能,配置了一台分发.发布服务器和订阅服务器间的数据库同步,其中也碰到不少的问题,省去其中理论的说明,重点说明配置步骤和问题解决,现总结如下: 环境配置要求: SQL Server 2000需要打上SP4补丁,补丁可以去网上搜,地址很多,那么如何判断SP4补丁是否打上了呢? 打开查询分析器,键入SQL语句“select @@version”,按F5执行,如果结果显示 Microso