Mysql 异步复制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql 异步复制相关的知识,希望对你有一定的参考价值。

一:mysql AB复制的原理:(异步复制)

            1.MysqlAB 主机有三个线程相互联系mysql数据库,实施同步。Master主机(Binlog Dump线程):slave主机(IO线程与SQL线程)。首先,由slave上执行startslave创建以个I/O线程,I/O线程链接到master上,并请求发送二进制日志文件里面的语句。Master创建一个线程(BinglogDump线程),slave创建一个I/O线程,读取master上的BinglogDump线程,将其拷贝到slave上的数据目录的中继日志中(relay logs)。第三个就是SQL线程,SQL线程读取中继日志里面的语句,并实时进行数据更新。

技术分享

2. 常见的复制模型:

    1)一主一从:这种架构的优点就是比较简单,搭建和维护都比较容易,成本也比较低。对于一些负载量不是特别大、可靠性要求不是特别高的场合,完全可以采用这种模型。但是对于一些负载比较大站点,和对可用性要求比较高的场合,这种架构就不太适用了。因为如果访问量比较大,Master节点的压力会比较的,另外如果Master崩溃,也会导致业务的终止。

    2)一主多从模式:在绝大多数场景中,我们的应用都是读多写。我们使用这种架构,通过读写分离的技术,可以有效降低Master上读的压力。我们在后端的slave上可以做一些数据备份,数据挖掘等方面的工作。但是如果备库比较多,同时主库又要负责其他的请求时,主库的压力会明显增大,此时主库会成为整个系统的性能瓶颈。

 

3.安装mysql-5.6.24.tar.gz

Mysql-A:安装

[[email protected] ~]# tar -zxfmysql-5.6.17-linux-glibc2.5-x86_64.tar.gz

[[email protected] ~]# mvmysql-5.6.17-linux-glibc2.5-x86_64 /usr/local/mysql

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

[[email protected]]#./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/--datadir=/data/mysql/   #初始化数据库    --basedir=mysql安装目录           --datadir数据库存放数据的目录

[[email protected] mysql]# cpsupport-files/my-default.cnf /etc/my.cnf        #拷贝模板文件

[[email protected] mysql]# useradd mysql                                                            #添加mysql用户

[[email protected] mysql]# chown -R mysql:mysql/usr/local/mysql/              #修改目录所属主

[[email protected] mysql]# mkdir -p /data/mysql                                       #创建数据存放目录

[[email protected] mysql]# chown -R mysql:mysql/data/mysql/

 [[email protected]]# cp support-files/mysql.server /etc/init.d/mysql                  #拷贝启动脚本

 [[email protected]]# chkconfig --level 345 mysql on                                               #创建开机启动项


Mysql-B:安装:

同上

                

Mysql-A:配置文件

Vim /etc/my.cnf

[mysqld]

basedir=/usr/local/mysql

datadir=/data/mysql

log-bin=mysql-bin.log#主服务器设置bin-log 从服务器则不需要

server-id=1#设置服务器的ID ID号唯一

           

[[email protected] mysql]# /etc/init.d/mysqlstart

Starting MySQL..                                           [确定]


[[email protected] mysql]#/usr/local/mysql/bin/mysqladmin -uroot password ‘123456‘ #修改mysql账户密码



Mysql-B:配置文件

[mysqld]

basedir=/usr/local/mysql

datadir=/data/mysql

server-id= 2

relay-log=relay-bin   #开启中继日志  sql线程需要读取中继日志对数据进行更新

 

[[email protected] mysql]# /etc/init.d/mysqlstart

StartingMySQL..                                           [确定]

 

[[email protected] mysql]#  /usr/local/mysql/bin/mysqladmin -urootpassword ‘123456‘

 

Mysql-A服务器配置:

mysql>GRANT REPLICATION SLAVE,REPLICATION CLIENT,RELOAD,SUPER ON *.* TO‘backup‘@‘192.168.60.81‘ IDENTIFIED BY ‘backup‘;   #授权192.168.68.81服务器的backup用户对本服务器的所有库拥有拷贝的权限

Query OK,0 rows affected (0.00 sec)

 

mysql>flush privileges;                                                      #授权用户之后要刷新权限列表

Query OK,0 rows affected (0.00 sec)

 

mysql>show master status;

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

|File             | Position |Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

|mysql-bin.000001 |      667 |              |                  |                   |

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

1 row inset (0.00 sec)

Binlog_Do_DB:同步数据库的

Binlog_Ignore_DB:不同步的数据库名

       


Mysql-B服务器配置:

mysql>change master to master_host=‘192.168.60.80‘,master_user=‘backup‘,master_password=‘backup‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=764;

Query OK,0 rows affected, 2 warnings (0.04 sec)

 

mysql>start slave;

Query OK,0 rows affected (0.01 sec)

 

 

mysql>show slave status\G;

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

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 192.168.60.80

                  Master_User: backup

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 764

               Relay_Log_File: relay-bin.000002

                Relay_Log_Pos: 283

        Relay_Master_Log_File: mysql-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: 764

              Relay_Log_Space: 450

              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

                  Master_UUID:b9eb3ea3-f27f-11e4-b57c-000c2921542d

             Master_Info_File:/data/mysql/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has readall relay log; waiting for the slave I/O thread to update it

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

1 row inset (0.00 sec)

 

ERROR:

No queryspecified

 

mysql>




 

性能测试:

系统环境:

Vmware 虚拟机 Centos 6.5 X86 64位系统             windows 7 64

4内存     60G硬盘  2核虚拟CPU    IDE 硬盘  5400        

 

 

1.mysql 优化前测试 (自带mysql测试工具mysqlslap)

测试命令

/usr/local/mysql/bin/mysqlslap -a--auto-generate-sql-load-type=read --concurrency=1000 --number-of-queries50000  --engine=innodb--iterations=3  --debug-info  -uroot -p123456

技术分享

[[email protected] opt]#/usr/local/mysql/bin/mysqlslap -a --auto-generate-sql-load-type=write--concurrency=1000   --engine=innodb--iterations=3  --debug-info  -uroot -p123456

技术分享

/usr/local/mysql/bin/mysqlslap -a--auto-generate-sql-load-type=mixed --concurrency=900  --number-of-queries=9000-auto-generate-sql-add-autoincrement --engine=innodb --iterations=3 --debug-info  -uroot -p123456

技术分享

 

优化后的my.cnf

/usr/local/mysql/bin/mysqlslap -a--auto-generate-sql-load-type=read --concurrency=1000 --number-of-queries50000  --engine=innodb--iterations=3  --debug-info  -uroot -p123456

技术分享

 

/usr/local/mysql/bin/mysqlslap -a--auto-generate-sql-load-type=write --concurrency=1000   --engine=innodb --iterations=3  --debug-info -uroot -p123456

技术分享

 

/usr/local/mysql/bin/mysqlslap -a--auto-generate-sql-load-type=mixed --concurrency=900  --number-of-queries=9000-auto-generate-sql-add-autoincrement --engine=innodb --iterations=3 --debug-info  -uroot -p123456


技术分享

本文出自 “11641924” 博客,请务必保留此出处http://11651924.blog.51cto.com/11641924/1783412

以上是关于Mysql 异步复制的主要内容,如果未能解决你的问题,请参考以下文章

Mysql 异步复制

详解mysql复制机制--异步复制,半同步复制和并行复制

MySQL主从复制(异步模式)

mysql主从复制的异步复制与同步复制

MySQL主从异步复制配置

一文看懂MySQL的异步复制全同步复制与半同步复制