Percona Server 5.6 Master-Slave Replication测试

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 1625977
 
2014-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 1625987
 
PLEASE 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_installation
 
which 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 at
 
  http://www.mysql.com
 
Support MySQL by buying support/licenses at http://shop.mysql.com
 
WARNING: 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 server
 
Percona 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.000001
         Position: 262
     Binlog_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.000003
         Position: 349
     Binlog_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 changed
 
mysql> 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 -A
 
Database 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值