为什么80%的码农都做不了架构师?>>>   

MySQL的Master-Slave Replication是一个异步的复制过程,从一个 MySQL instance(Master)复制到另一个 instance(Slave)。在Master与Slave之间的实现整个复制过程主要由三个线程来完成,其中两个线程(SQL线程和IO线程)在 Slave 端,另外一个线程(IO线程)在Master端。 在MySQL Multi-Master兴起的今天,Master-Slave依然有着不少应用场景。
MySQL 复制的基本过程如下(http://machael.blog.51cto.com/829462/239112/):
1. Slave上面的IO线程连接上 Master,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容;
2. Master 接收到来自 Slave 的 IO 线程的请求后,通过负责复制的 IO 线程根据请求信息读取指定日志指定位置之后的日志信息,返回给 Slave 端的 IO 线程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息在 Master 端的 Binary Log 文件的名称以及在 Binary Log 中的位置;
3. Slave 的 IO 线程接收到信息后,将接收到的日志内容依次写入到 Slave 端的Relay Log文件(mysql-relay-bin.xxxxxx)的最末端,并将读取到的Master端的bin-log的文件名和位置记录到master-
info文件中,以便在下一次读取的时候能够清楚的高速Master“我需要从某个bin-log的哪个位置开始往后的日志内容,请发给我”
​4. Slave 的 SQL 线程检测到 Relay Log 中新增加了内容后,会马上解析该 Log 文件中的内容成为在 Master 端真实执行时候的那些可执行的 Query 语句,并在自身执行这些 Query。这样,实际上就是在 Master 端和 Slave 端执行了同样的 Query,所以两端的数据是完全一样的。
下面就在CentOS 6.5 两节点的环境中配置一个简单的Percona Server 5.6.16 Master-Slave复制:
OStack01 ​192.168.46.132
OStack02 ​192.168.46.133

1. 安装Percona

在Master, Slave安装Percona Server

[root@localhost Desktop]# rpm -ivh http://www.percona.com/downloads/Percona-Server-5.6/LATEST/binary/redhat/6/x86_64/Percona-Server-shared-56-5.6.16-rel64.2.el6.x86_64.rpm
Retrieving http://www.percona.com/downloads/Percona-Server-5.6/LATEST/binary/redhat/6/x86_64/Percona-Server-shared-56-5.6.16-rel64.2.el6.x86_64.rpm
Preparing...                ########################################### [100%]1:Percona-Server-shared-5########################################### [100%]
[root@localhost Desktop]# rpm -ivh http://www.percona.com/downloads/Percona-Server-5.6/LATEST/binary/redhat/6/x86_64/Percona-Server-client-56-5.6.16-rel64.2.el6.x86_64.rpm
Retrieving http://www.percona.com/downloads/Percona-Server-5.6/LATEST/binary/redhat/6/x86_64/Percona-Server-client-56-5.6.16-rel64.2.el6.x86_64.rpm
Preparing...                ########################################### [100%]1:Percona-Server-client-5########################################### [100%]
[root@localhost Desktop]# rpm -ivh http://www.percona.com/downloads/Percona-Server-5.6/LATEST/binary/redhat/6/x86_64/Percona-Server-server-56-5.6.16-rel64.2.el6.x86_64.rpm
Retrieving http://www.percona.com/downloads/Percona-Server-5.6/LATEST/binary/redhat/6/x86_64/Percona-Server-server-56-5.6.16-rel64.2.el6.x86_64.rpm
Preparing...                ########################################### [100%]1:Percona-Server-server-5########################################### [100%]
2014-04-30 00:39:07 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2014-04-30 00:39:07 19100 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-04-30 00:39:07 19100 [Note] InnoDB: The InnoDB memory heap is disabled
2014-04-30 00:39:07 19100 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-04-30 00:39:07 19100 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-04-30 00:39:07 19100 [Note] InnoDB: Using Linux native AIO
2014-04-30 00:39:07 19100 [Note] InnoDB: Using CPU crc32 instructions
2014-04-30 00:39:07 19100 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-04-30 00:39:07 19100 [Note] InnoDB: Completed initialization of buffer pool
2014-04-30 00:39:07 19100 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2014-04-30 00:39:07 19100 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2014-04-30 00:39:07 19100 [Note] InnoDB: Database physically writes the file full: wait...
2014-04-30 00:39:07 19100 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2014-04-30 00:39:07 19100 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2014-04-30 00:39:07 19100 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2014-04-30 00:39:07 19100 [Warning] InnoDB: New log files created, LSN=45781
2014-04-30 00:39:07 19100 [Note] InnoDB: Doublewrite buffer not found: creating new
2014-04-30 00:39:07 19100 [Note] InnoDB: Doublewrite buffer created
2014-04-30 00:39:07 19100 [Note] InnoDB: 128 rollback segment(s) are active.
2014-04-30 00:39:08 19100 [Warning] InnoDB: Creating foreign key constraint system tables.
2014-04-30 00:39:08 19100 [Note] InnoDB: Foreign key constraint system tables created
2014-04-30 00:39:08 19100 [Note] InnoDB: Creating tablespace and datafile system tables.
2014-04-30 00:39:08 19100 [Note] InnoDB: Tablespace and datafile system tables created.
2014-04-30 00:39:08 19100 [Note] InnoDB: Waiting for purge to start
2014-04-30 00:39:08 19100 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.16-64.2 started; log sequence number 0
2014-04-30 00:39:08 19100 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2014-04-30 00:39:08 19100 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
2014-04-30 00:39:08 19100 [Note] Binlog end
2014-04-30 00:39:08 19100 [Note] InnoDB: FTS optimize thread exiting.
2014-04-30 00:39:08 19100 [Note] InnoDB: Starting shutdown...
2014-04-30 00:39:10 19100 [Note] InnoDB: Shutdown completed; log sequence number 16259772014-04-30 00:39:10 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2014-04-30 00:39:10 19125 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-04-30 00:39:10 19125 [Note] InnoDB: The InnoDB memory heap is disabled
2014-04-30 00:39:10 19125 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-04-30 00:39:10 19125 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-04-30 00:39:10 19125 [Note] InnoDB: Using Linux native AIO
2014-04-30 00:39:10 19125 [Note] InnoDB: Using CPU crc32 instructions
2014-04-30 00:39:10 19125 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-04-30 00:39:10 19125 [Note] InnoDB: Completed initialization of buffer pool
2014-04-30 00:39:10 19125 [Note] InnoDB: Highest supported file format is Barracuda.
2014-04-30 00:39:10 19125 [Note] InnoDB: 128 rollback segment(s) are active.
2014-04-30 00:39:10 19125 [Note] InnoDB: Waiting for purge to start
2014-04-30 00:39:10 19125 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.16-64.2 started; log sequence number 1625977
2014-04-30 00:39:10 19125 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2014-04-30 00:39:10 19125 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
2014-04-30 00:39:10 19125 [Note] Binlog end
2014-04-30 00:39:10 19125 [Note] InnoDB: FTS optimize thread exiting.
2014-04-30 00:39:10 19125 [Note] InnoDB: Starting shutdown...
2014-04-30 00:39:12 19125 [Note] InnoDB: Shutdown completed; log sequence number 1625987PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:/usr/bin/mysqladmin -u root password 'new-password'/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'Alternatively you can run:/usr/bin/mysql_secure_installationwhich will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.See the manual for more instructions.Please report any problems at http://bugs.mysql.com/The latest information about MySQL is available on the web athttp://www.mysql.comSupport MySQL by buying support/licenses at http://shop.mysql.comWARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the serverPercona Server is distributed with several useful UDF (User Defined Function) from Maatkit.
Run the following commands to create these functions:
mysql -e "CREATE FUNCTION fnv1a_64 RETURNS INTEGER SONAME 'libfnv1a_udf.so'"
mysql -e "CREATE FUNCTION fnv_64 RETURNS INTEGER SONAME 'libfnv_udf.so'"
mysql -e "CREATE FUNCTION murmur_hash RETURNS INTEGER SONAME 'libmurmur_udf.so'"
See http://code.google.com/p/maatkit/source/browse/trunk/udf for more details

2. 配置Master,Slave的数据库参数文件

master:
# vi /etc/my.cnf
#在[mysqld]中添加
explicit_defaults_for_timestamp=true
log_bin = mysql-bin
server_id  = 1
innodb_buffer_pool_size = 128M
join_buffer_size = 128M
sort_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

log-bin is the binary log basename to generate binary log file names.
server-id option is used in replication to enable master and slave servers to identify themselves uniquely. On the master and each slave, you must use the server-id option to establish a unique replication ID in the range from 1 to 231.
explicit_defaults_for_timestamp是5.6关于timestamp默认行为的设置,请参考:http://www.williamsang.com/archives/818.html
sqlmode – STRICT_TRANS_TABLES – 为事务引擎启用严格模式
sqlmode – NO_ENGINE_SUBSTITUTION 如果需要的存储引擎被禁用或未编译,可以防止自动替换存储引擎。

slave:

vi /etc/my.cnf
#在[mysqld]中添加
explicit_defaults_for_timestamp=true
log_bin = mysql-bin
server_id = 2
relay_log = /var/lib/mysql/mysql-relay-bin
log_slave_updates = 1
read_only         =1
innodb_buffer_pool_size = 128M
join_buffer_size = 128M
sort_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

Out of the settings described above, the only essential one is the server-id part, but I have enabled log-bin too and named it similar to the one on master, so that slave can be promoted to master if and when required.
relay-log specifies the location and name of the relay log
log-slave-updates makes the slave log the replicated events to its binary log
read-only prevents data changes on the slaves, because slave is really usually used for reads

3. 启动并配置数据库

分别启动master、slave数据库:

<SHELL># service mysql start
Starting MySQL (Percona Server)......[  OK  ]

检查监听端口

<SHELL># netstat -nat
# netstat -lnap | grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      20771/mysqld

设置root密码

<SHELL># /usr/bin/mysqladmin -u root password 'debugo'
<SHELL># mysql -u root

登录并检查:

# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.16-64.2-56-log Percona Server (GPL), Release 64.2, Revision 569
......mysql> show master status-> G
*************************** 1. row ***************************File: mysql-bin.000001Position: 262Binlog_Do_DB: Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

下面做一个test数据库的完整备份,并复制到slave所在的节点中:

<SHELL>#  mysqldump -uroot -pdebugo  --master-data=2 test > /root/dbdump.db

Note the user of master-data option. Use this option to dump a master replication server to produce a dump file that can be used to set up another server as a slave of the master. With the master-data value set to 2, the“change master” to statement (that indicates binlog name and position) is written as a comment at the start of the dump file for information purposes. The master-data option also turns on lock-all-tables option, which acquires a global read lock across all tables which ensures that the dump is consistent.
拷贝并导入到slave中:

<SHELL># scp /root/dbdump.db OStack02:/root
dbdump.db                                                               100%  203     0.2KB/s   00:00
<SHELL># mysql -uroot -p test < dbdump.db
Enter password: 

配置master数据库用户:

mysql> grant replication slave,replication client on *.* to rep@'192.168.46.%' identified by 'debugo';
Query OK, 0 rows affected (0.05 sec)
mysql> grant all on test.* to rep@'192.168.46.%';
Query OK, 0 rows affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)mysql> show master status G
*************************** 1. row ***************************File: mysql-bin.000003Position: 349Binlog_Do_DB: Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

slave数据库:

[root@localhost ~]# mysql -u root
...
mysql> grant replication slave,replication client on *.* to rep@'192.168.46.%' identified by 'debugo';
Query OK, 0 rows affected (0.05 sec)
mysql> grant all on test.* to rep@'192.168.46.%';
Query OK, 0 rows affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)mysql> change master to master_host='192.168.46.132', master_user='rep',master_password='debugo',master_log_file='mysql-bin.000001',master_log_pos=0;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)

4. 测试

在master端的test数据库中,创建test.t表并插入数据。

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      623 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)mysql> use test
Database changedmysql> create table t (name varchar(10));
Query OK, 0 rows affected (0.02 sec)mysql> insert into t values ('debugo');
Query OK, 1 row affected (0.00 sec)mysql> commit;
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 |      945 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

slave端,检查数据

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1126 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t              |
+----------------+
1 row in set (0.00 sec)mysql> select * from t;
+--------+
| name   |
+--------+
| debugo |
+--------+
1 row in set (0.00 sec)mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1448 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

可见两边binlog的position增量是一致的,数据被成功传送过来。

转载于:https://my.oschina.net/boltwu/blog/719838

Percona Server 5.6 Master-Slave Replication测试相关推荐

  1. mongoDB-3.x Master Slave Replication

    mongoDB-3.x Master Slave Replication 官方文档: https://docs.mongodb.org/manual/core/master-slave/ https: ...

  2. Mysql的master,slave的配置

    MYSQL的master,slave环境的搭建<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:off ...

  3. master slave mysql_MYSQL高可用之复制(MASTER/SLAVE)

    MYSQL高可用之复制(MASTER/SLAVE) 随着互联网的快速发展,对数据库的访问已经越来越频繁,仅凭单个服务器已经无法应对高并发的访问,也无法满足数据库提供7*24的服务,这个时候我们就必须考 ...

  4. mysql 部署master slave_MySQL Master Slave 数据同步,集群。

    该文章为原创,作者:梁健,QQ:15141739,时间:2011年5月11日17:55:26 转载注明作者.最近学习了一下MySQL的集群,发现MySQL的集群有很多种方式,下面来介绍下学到的MySQ ...

  5. Master/Slave知识

    1.master上授权给slave mysql>grant all on *.* to repadmin@'218.6.67.75' identified by 'backup'; mysql& ...

  6. mysql 查看slave状态_Mysql master/slave测试过程

    1.测试环境:gt;gt;Master:物理机,Debian(testing) + mysql5.0 + ip:192.168.40.223gt;gt;slave: 虚拟机,CentOS(4.0) + ...

  7. Percona Server for MySQL 5.5.30-30.2

    Percona 很高兴的发布了 Percona Server for MySQL 5.5.30-30.2 版本,该版本基于 MySQL 5.5.30 ,这是 5.5 分支的最新稳定版本. 该版本包含如 ...

  8. 搭建主从数据库出现的错误 error connecting to master ‘slave@172.17.0.2:3306‘ - retry-time: 30 retries: 1

    在搭建主从数据库的时候出现了报错 出现错误的截图: 解决办法: 重新授权 CREATE USER 'slave'@'%' IDENTIFIED BY '123456'; GRANT REPLICATI ...

  9. mysql配置master_mysql 主从配置(master/slave)

    1.  在每台服务器上创建复制账号(也可以只在master上创建用户,这里配置两个是为了方便以后切换) 备库运行的I/O县城需要建立一个到主库的TCP/IP连接,所以必须在主库创建一个用户,并赋予合适 ...

最新文章

  1. JAVA NIO学习一:NIO简介、NIOIO的主要区别
  2. DCMTK:从DICOM封装的PDF存储对象中提取PDF文件
  3. Algs4-1.4.42问题规模
  4. r语言ggplot2 多线图绘制图例_plotnine: Python版的ggplot2作图库
  5. Vue封装hbuilder热更新
  6. numpy支持比python更多的数据类型_NumPy数据类型
  7. (39) gulp开发服务器
  8. H323plus的学习使用(2)——simple实现音视频通话
  9. angular引用bootstrap_angular怎么引入bootstrap?
  10. 参数化命令相关知识点之==================防止SQl的注入
  11. mysql 事件的作用_MySQL 事件
  12. Web API-BOM- 操作浏览器
  13. Windows下vim方式操作软件+Gvim使用
  14. Silverlight实例教程 - 自定义扩展Validation类,验证框架的总结和建议
  15. arcmap shp导出cad无反应_软件技巧如何利用OSM提取场地CAD以及LANDUSE属性
  16. 超级HOOK技术无需分析基址
  17. 搞科研必须了解的五十个学术网站
  18. 计算机word大作业,计算机操作基础--Word大作业要求.doc
  19. VSCode 使用 StandardJS 自动格式化代码
  20. 代码审计——你是如何发现那些有缺陷的代码的

热门文章

  1. JavaScript数据结构与算法——数组详解(上)
  2. POI导出word带图片及本地测试没问题,在服务器上找不到模板的问题
  3. 找回消失的ubuntu启动选项
  4. [c++] volatile关键字
  5. cocos 新工程遇到的问题
  6. Android笔记——在布局文件中插入另一个布局文件
  7. C# 中的 lock的陷阱
  8. 【原创】MIPS中断系统的板级验证及实例测试
  9. eclipse中显示jquery或extjs自动提示
  10. [导入]Dreamweaver插入Flash参数