Percona Server 5.6 Master-Slave Replication测试
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
MySQL的Master-Slave Replication是一個(gè)異步的復(fù)制過(guò)程,從一個(gè) MySQL instance(Master)復(fù)制到另一個(gè) instance(Slave)。在Master與Slave之間的實(shí)現(xiàn)整個(gè)復(fù)制過(guò)程主要由三個(gè)線程來(lái)完成,其中兩個(gè)線程(SQL線程和IO線程)在 Slave 端,另外一個(gè)線程(IO線程)在Master端。 在MySQL Multi-Master興起的今天,Master-Slave依然有著不少應(yīng)用場(chǎng)景。
MySQL 復(fù)制的基本過(guò)程如下(http://machael.blog.51cto.com/829462/239112/):
1. Slave上面的IO線程連接上 Master,并請(qǐng)求從指定日志文件的指定位置(或者從最開(kāi)始的日志)之后的日志內(nèi)容;
2. Master 接收到來(lái)自 Slave 的 IO 線程的請(qǐng)求后,通過(guò)負(fù)責(zé)復(fù)制的 IO 線程根據(jù)請(qǐng)求信息讀取指定日志指定位置之后的日志信息,返回給 Slave 端的 IO 線程。返回信息中除了日志所包含的信息之外,還包括本次返回的信息在 Master 端的 Binary Log 文件的名稱(chēng)以及在 Binary Log 中的位置;
3. Slave 的 IO 線程接收到信息后,將接收到的日志內(nèi)容依次寫(xiě)入到 Slave 端的Relay Log文件(mysql-relay-bin.xxxxxx)的最末端,并將讀取到的Master端的bin-log的文件名和位置記錄到master-
info文件中,以便在下一次讀取的時(shí)候能夠清楚的高速M(fèi)aster“我需要從某個(gè)bin-log的哪個(gè)位置開(kāi)始往后的日志內(nèi)容,請(qǐng)發(fā)給我”
?4. Slave 的 SQL 線程檢測(cè)到 Relay Log 中新增加了內(nèi)容后,會(huì)馬上解析該 Log 文件中的內(nèi)容成為在 Master 端真實(shí)執(zhí)行時(shí)候的那些可執(zhí)行的 Query 語(yǔ)句,并在自身執(zhí)行這些 Query。這樣,實(shí)際上就是在 Master 端和 Slave 端執(zhí)行了同樣的 Query,所以?xún)啥说臄?shù)據(jù)是完全一樣的。
下面就在CentOS 6.5 兩節(jié)點(diǎn)的環(huán)境中配置一個(gè)簡(jiǎn)單的Percona Server 5.6.16 Master-Slave復(fù)制:
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 details2. 配置Master,Slave的數(shù)據(jù)庫(kù)參數(shù)文件
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_TABLESlog-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關(guān)于timestamp默認(rèn)行為的設(shè)置,請(qǐng)參考:http://www.williamsang.com/archives/818.html
sqlmode – STRICT_TRANS_TABLES – 為事務(wù)引擎啟用嚴(yán)格模式
sqlmode – NO_ENGINE_SUBSTITUTION 如果需要的存儲(chǔ)引擎被禁用或未編譯,可以防止自動(dòng)替換存儲(chǔ)引擎。
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_TABLESOut 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. 啟動(dòng)并配置數(shù)據(jù)庫(kù)
分別啟動(dòng)master、slave數(shù)據(jù)庫(kù):
<SHELL># service mysql start Starting MySQL (Percona Server)......[ OK ]檢查監(jiān)聽(tīng)端口
<SHELL># netstat -nat # netstat -lnap | grep 3306 tcp 0 0 :::3306 :::* LISTEN 20771/mysqld設(shè)置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)下面做一個(gè)test數(shù)據(jù)庫(kù)的完整備份,并復(fù)制到slave所在的節(jié)點(diǎn)中:
<SHELL># mysqldump -uroot -pdebugo --master-data=2 test > /root/dbdump.dbNote 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.
拷貝并導(dǎo)入到slave中:
配置master數(shù)據(jù)庫(kù)用戶:
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數(shù)據(jù)庫(kù):
[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. 測(cè)試
在master端的test數(shù)據(jù)庫(kù)中,創(chuàng)建test.t表并插入數(shù)據(jù)。
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端,檢查數(shù)據(jù)
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)可見(jiàn)兩邊binlog的position增量是一致的,數(shù)據(jù)被成功傳送過(guò)來(lái)。
轉(zhuǎn)載于:https://my.oschina.net/boltwu/blog/719838
總結(jié)
以上是生活随笔為你收集整理的Percona Server 5.6 Master-Slave Replication测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何在Mac上安装win10正版系统
- 下一篇: 转载:使用sklearn进行数据挖掘