MySQL 官方 Auto-Failover 功能測試
Jun 07, 2016 pm 04:39 PM參考資料: http://www.clusterdb.com/mysql/replication-and-auto-failover-made-easy-with-mysql-utilities 環(huán)境介紹: master: demoenv-trial-1 slaves: demoenv-trial-2 demoenv-trial-3 1. 安裝 Percona Server,在所有服務(wù)器上: $ sudo yum install ht
參考資料:
http://www.clusterdb.com/mysql/replication-and-auto-failover-made-easy-with-mysql-utilities
環(huán)境介紹:
master: demoenv-trial-1
slaves: demoenv-trial-2 demoenv-trial-3
1. 安裝 Percona Server,在所有服務(wù)器上:
$ sudo yum install http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
$ sudo yum install Percona-Server-shared-compat
$ sudo yum install Percona-Server-server-56
$ sudo yum install http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
$ sudo yum install mysql-utilities
2. 配置 /etc/my.cnf,在所有服務(wù)器上:
注意:確保 server-id 不同且 report-host 與自身主機(jī)名相同
$ sudo vim /etc/my.cnf
[mysqld] # basic setting datadir = /opt/mysql/data tmpdir = /opt/mysql/tmp socket = /opt/mysql/run/mysqld.sock port = 3306 pid-file = /opt/mysql/run/mysqld.pid # innodb setting default-storage-engine = INNODB innodb_file_per_table = 1 log-bin = /opt/mysql/binlogs/bin-log-mysqld log-bin-index = /opt/mysql/binlogs/bin-log-mysqld.index innodb_data_home_dir = /opt/mysql/data innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /opt/mysql/data binlog-do-db = testdb # server id server-id=1 # gtids setting binlog-format = ROW log-slave-updates = true gtid-mode = on enforce-gtid-consistency = true report-host = demoenv-trial-1 report-port = 3306 master-info-repository = TABLE relay-log-info-repository = TABLE sync-master-info = 1 # other settings [mysqld_safe] log-error = /opt/mysql/log/mysqld.log pid-file = /opt/mysql/run/mysqld.pid open-files-limit = 8192 [mysqlhotcopy] interactive-timeout [client] port = 3306 socket = /opt/mysql/run/mysqld.sock default-character-set = utf8
3. 創(chuàng)建所需目錄,在所有服務(wù)器上:
$ sudo mkdir -p /opt/mysql/{data,tmp,run,binlogs,log}
$ sudo chown mysql:mysql /opt/mysql/{data,tmp,run,binlogs,log}
4. 初始化數(shù)據(jù)庫,在所有服務(wù)器上:
$ sudo -i
# su - mysql
$ mysql_install_db --user=mysql --datadir=/opt/mysql/data/
$ exit
# exit
$ sudo /etc/init.d/mysql start
5. 創(chuàng)建授權(quán)用戶 root@'%' 以便通過 mysqlreplicate 來進(jìn)行主從復(fù)制的配置,在所有服務(wù)器上:
$ mysql -uroot
mysql> grant all on *.* to root@'%' identified by 'pass' with grant option; mysql> quit;
6. 創(chuàng)建復(fù)制所需的用戶,在所有服務(wù)器上:
$ mysql -uroot
mysql> grant replication slave on *.* to 'rpl'@'%' identified by 'rpl'; mysql> quit;
7. 配置主從復(fù)制,可選擇任意一臺(tái)服務(wù)器操作:
[dong.guo@demoenv-trial-1 ~]$ mysql -uroot
mysql> use mysql; mysql> drop user root@'demoenv-trial-1'; mysql> quit;
[dong.guo@demoenv-trial-1 ~]$ mysqlreplicate --master=root:pass@'demoenv-trial-1':3306 --slave=root:pass@'demoenv-trial-2':3306 --rpl-user=rpl:rpl
# master on demoenv-trial-1: ... connected. # slave on demoenv-trial-2: ... connected. # Checking for binary logging on master... # set up replication... # ...done.
[dong.guo@demoenv-trial-1 ~]$ mysqlreplicate --master=root:pass@'demoenv-trial-1':3306 --slave=root:pass@'demoenv-trial-3':3306 --rpl-user=rpl:rpl
# master on demoenv-trial-1: ... connected. # slave on demoenv-trial-3: ... connected. # Checking for binary logging on master... # set up replication... # ...done.
8. 驗(yàn)證主從復(fù)制的數(shù)據(jù)完整性
[dong.guo@demoenv-trial-1 ~]$ mysql -uroot
mysql> create database testdb; mysql> quit;
[dong.guo@demoenv-trial-1 ~]$ mysql -uroot -ppass -h'demoenv-trial-2' -e 'show databases;'
+-------------------+ | Database | +-------------------+ | information_schema| | mysql | | performance_schema| | test | | testdb | +-------------------+
[dong.guo@demoenv-trial-1 ~]$ mysql -uroot -ppass -h'demoenv-trial-3' -e 'show databases;'
+-------------------+ | Database | +-------------------+ | information_schema| | mysql | | performance_schema| | test | | testdb | +-------------------+
[dong.guo@demoenv-trial-1 ~]$ mysqlrplshow --master=rpl:rpl@'demoenv-trial-1':3306 --discover-slaves-login=root:pass;
# master on demoenv-trial-1: ... connected. # Finding slaves for master: demoenv-trial-1:3306 # Replication Topology Graph demoenv-trial-1:3306 (MASTER) | +--- demoenv-trial-2:3306 - (SLAVE) | +--- demoenv-trial-3:3306 - (SLAVE)
[dong.guo@demoenv-trial-1 ~]$ mysqlrplcheck --master=root:pass@'demoenv-trial-1' --slave=root:pass@'demoenv-trial-2'
# master on demoenv-trial-1: ... connected. # slave on demoenv-trial-2: ... connected. Test Description Status --------------------- Checking for binary logging on master [pass] Are there binlog exceptions? [WARN] +--------+--------+-----------+ | server | do_db | ignore_db | +--------+--------+-----------+ | master | testdb | | +--------+--------+-----------+ Replication user exists? [pass] Checking server_id values [pass] Checking server_uuid values [pass] Is slave connected to master? [pass] Check master information file [pass] Checking InnoDB compatibility [pass] Checking storage engines compatibility [pass] Checking lower_case_table_names setting [pass] Checking slave delay (seconds behind master) [pass] # ...done.
9. 關(guān)閉 master 測試 auto-failover
[dong.guo@demoenv-trial-1 ~]$ mysqlfailover --master=root:pass@'demoenv-trial-1':3306 --discover-slaves-login=root:pass --rediscover
# Discovering slaves for master at demoenv-trial-1:3306 # Discovering slave at demoenv-trial-2:3306 # Found slave: demoenv-trial-2:3306 # Discovering slave at demoenv-trial-3:3306 # Found slave: demoenv-trial-3:3306 # Checking privileges. # Discovering slaves for master at demoenv-trial-1:3306 MySQL Replication Failover Utility Failover Mode = auto Next Interval = Sun Oct 20 06:58:52 2013 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB bin-log-mysqld.00000 299 testdb GTID Executed Set 92df196b-3906-11e3-b6b6-000c290d14d7:1 Replication Health Status +-----------------+------+--------+-------+-----------+---------------+ | host | port | role | state | gtid_mode | health | +-----------------+------+--------+-------+-----------+---------------+ | demoenv-trial-1 | 3306 | MASTER | UP | ON | OK | | demoenv-trial-2 | 3306 | SLAVE | UP | ON | OK | | demoenv-trial-3 | 3306 | SLAVE | UP | ON | OK | +-----------------+------+--------+-------+-----------+---------------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs
然后,執(zhí)行命令的終端就掛起了,需要另外開啟一個(gè)終端來關(guān)閉master:
[dong.guo@demoenv-trial-1 ~]$ mysqladmin -uroot -ppass shutdown
接著,在剛剛掛起的終端上,可以看到:
Failover starting in 'auto' mode... # Candidate slave demoenv-trial-2:3306 will become the new master. # Checking slaves status (before failover). # Preparing candidate for failover. # Creating replication user if it does not exist. # Stopping slaves. # Performing STOP on all slaves. # Switching slaves to new master. # Disconnecting new master as slave. # Starting slaves. # Performing START on all slaves. # Checking slaves for errors. # Failover complete. # Discovering slaves for master at demoenv-trial-2:3306 Failover console will restart in 5 seconds. MySQL Replication Failover Utility Failover Mode = auto Next Interval = Sun Oct 20 07:01:25 2013 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB bin-log-mysqld.00000 299 testdb GTID Executed Set 92df196b-3906-11e3-b6b6-000c290d14d7:1 Replication Health Status +-----------------+------+--------+-------+-----------+---------------+ | host | port | role | state | gtid_mode | health | +-----------------+------+--------+-------+-----------+---------------+ | demoenv-trial-2 | 3306 | MASTER | UP | ON | OK | | demoenv-trial-3 | 3306 | SLAVE | UP | ON | OK | +-----------------+------+--------+-------+-----------+---------------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs
服務(wù)器 'demoenv-trial-2' 變成了新的master。
10. 插入數(shù)據(jù)測試主從復(fù)制
[dong.guo@demoenv-trial-2 ~]$ mysql -uroot
mysql> use testdb; Database changed mysql> CREATE TABLE `hostgroup` ( -> `hostgroup_id` tinyint(4) NOT NULL AUTO_INCREMENT, -> `hostgroup_name` char(20) DEFAULT NULL, -> `hostgroup_next` tinyint(4) NOT NULL, -> `colo_name` char(4) NOT NULL, -> PRIMARY KEY (`hostgroup_id`) -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; Query OK, 0 rows affected (0.10 sec) mysql> quit;
[dong.guo@demoenv-trial-2 binlogs]$ mysql -uroot -ppass -h'demoenv-trial-3' testdb -e 'show tables;'
+-----------------+ | Tables_in_testdb| +-----------------+ | hostgroup | +-----------------+
11. 嘗試將舊的master恢復(fù)
[dong.guo@demoenv-trial-1 ~]$ sudo /etc/init.d/mysql start
[dong.guo@demoenv-trial-1 ~]$ mysqlreplicate --master=root:pass@'demoenv-trial-2':3306 --slave=root:pass@'demoenv-trial-1':3306
# master on demoenv-trial-2: ... connected. # slave on demoenv-trial-1: ... connected. # Checking for binary logging on master... # setting up replication... # ...done.
在剛剛掛起的終端上,可以看到:
... Replication Health Status +-----------------+------+--------+-------+-----------+---------------+ | host | port | role | state | gtid_mode | health | +-----------------+------+--------+-------+-----------+---------------+ | demoenv-trial-2 | 3306 | MASTER | UP | ON | OK | | demoenv-trial-1 | 3306 | SLAVE | UP | ON | OK | | demoenv-trial-3 | 3306 | SLAVE | UP | ON | OK | +-----------------+------+--------+-------+-----------+---------------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs
將舊的master恢復(fù)
[dong.guo@demoenv-trial-1 ~]$ mysqlrpladmin --master=root:pass@'demoenv-trial-2':3306 --new-master=root:pass@'demoenv-trial-1':3306 --demote-master --discover-slaves-login=root:pass switchover
# Discovering slaves for master at demoenv-trial-2:3306 # Discovering slave at demoenv-trial-1:3306 # Found slave: demoenv-trial-1:3306 # Discovering slave at demoenv-trial-3:3306 # Found slave: demoenv-trial-3:3306 # Checking privileges. # Performing switchover from master at demoenv-trial-2:3306 to slave at demoenv-trial-1:3306. # Checking candidate slave prerequisites. # Checking slaves configuration to master. # Waiting for slaves to catch up to old master. # Stopping slaves. # Performing STOP on all slaves. # Demoting old master to be a slave to the new master. # Switching slaves to new master. # Starting all slaves. # Performing START on all slaves. # Checking slaves for errors. # Switchover complete. # # Replication Topology Health: +-----------------+------+--------+-------+-----------+---------------+ | host | port | role | state | gtid_mode | health | +-----------------+------+--------+-------+-----------+---------------+ | demoenv-trial-1 | 3306 | MASTER | UP | ON | OK | | demoenv-trial-2 | 3306 | SLAVE | UP | ON | OK | | demoenv-trial-3 | 3306 | SLAVE | UP | ON | OK | +-----------------+------+--------+-------+-----------+---------------+ # ...done.
在剛剛掛起的終端上,可以看到:
MySQL Replication Failover Utility Failover Mode = auto Next Interval = Sun Oct 20 07:30:07 2013 Master Information ------------------ Binary Log File Position Binlog_Do_DB Binlog_Ignore_DB bin-log-mysqld.00000 710 testdb GTID Executed Set 8a58172b-1efd-11e3-8cf1-000c2950fe0c:1 [...] Replication Health Status +-----------------+------+--------+-------+-----------+----------------------------------+ | host | port | role | state | gtid_mode | health | +-----------------+------+--------+-------+-----------+----------------------------------+ | demoenv-trial-2 | 3306 | MASTER | UP | ON | OK | | demoenv-trial-1 | 3306 | SLAVE | UP | WARN | Slave is not connected to master.| | demoenv-trial-3 | 3306 | SLAVE | UP | WARN | Slave is not connected to master.| +-----------------+------+--------+-------+-----------+----------------------------------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs
可以看到failover在手動(dòng)恢復(fù)了舊的master之后已經(jīng)停止了工作。
按下Q,然后重啟auto-failover:
[dong.guo@demoenv-trial-1 ~]$ mysqlfailover --master=root:pass@'demoenv-trial-1':3306 --discover-slaves-login=root:pass --rediscover
... Replication Health Status +-----------------+------+--------+-------+-----------+---------------+ | host | port | role | state | gtid_mode | health | +-----------------+------+--------+-------+-----------+---------------+ | demoenv-trial-1 | 3306 | MASTER | UP | ON | OK | | demoenv-trial-2 | 3306 | SLAVE | UP | ON | OK | | demoenv-trial-3 | 3306 | SLAVE | UP | ON | OK | +-----------------+------+--------+-------+-----------+---------------+ Q-quit R-refresh H-health G-GTID Lists U-UUIDs
它又可以工作了。
12. 對于 auto-failover 的簡短總結(jié):
它包含在mysql-utilities這個(gè)軟件包中;
它只能工作在MySQL 5.6 版本上,因?yàn)樾枰狦ITDs的支持;
它可以自動(dòng)選取一個(gè)slave作為新的master,當(dāng)現(xiàn)有的master死掉以后;
但是它不能自動(dòng)將舊的master恢復(fù),并且在手動(dòng)恢復(fù)了舊的master之后也會(huì)停止工作。
原文地址:MySQL 官方 Auto-Failover 功能測試, 感謝原作者分享。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

mysqldump is a common tool for performing logical backups of MySQL databases. It generates SQL files containing CREATE and INSERT statements to rebuild the database. 1. It does not back up the original file, but converts the database structure and content into portable SQL commands; 2. It is suitable for small databases or selective recovery, and is not suitable for fast recovery of TB-level data; 3. Common options include --single-transaction, --databases, --all-databases, --routines, etc.; 4. Use mysql command to import during recovery, and can turn off foreign key checks to improve speed; 5. It is recommended to test backup regularly, use compression, and automatic adjustment.

When handling NULL values ??in MySQL, please note: 1. When designing the table, the key fields are set to NOTNULL, and optional fields are allowed NULL; 2. ISNULL or ISNOTNULL must be used with = or !=; 3. IFNULL or COALESCE functions can be used to replace the display default values; 4. Be cautious when using NULL values ??directly when inserting or updating, and pay attention to the data source and ORM framework processing methods. NULL represents an unknown value and does not equal any value, including itself. Therefore, be careful when querying, counting, and connecting tables to avoid missing data or logical errors. Rational use of functions and constraints can effectively reduce interference caused by NULL.

GROUPBY is used to group data by field and perform aggregation operations, and HAVING is used to filter the results after grouping. For example, using GROUPBYcustomer_id can calculate the total consumption amount of each customer; using HAVING can filter out customers with a total consumption of more than 1,000. The non-aggregated fields after SELECT must appear in GROUPBY, and HAVING can be conditionally filtered using an alias or original expressions. Common techniques include counting the number of each group, grouping multiple fields, and filtering with multiple conditions.

MySQL paging is commonly implemented using LIMIT and OFFSET, but its performance is poor under large data volume. 1. LIMIT controls the number of each page, OFFSET controls the starting position, and the syntax is LIMITNOFFSETM; 2. Performance problems are caused by excessive records and discarding OFFSET scans, resulting in low efficiency; 3. Optimization suggestions include using cursor paging, index acceleration, and lazy loading; 4. Cursor paging locates the starting point of the next page through the unique value of the last record of the previous page, avoiding OFFSET, which is suitable for "next page" operation, and is not suitable for random jumps.

To view the size of the MySQL database and table, you can query the information_schema directly or use the command line tool. 1. Check the entire database size: Execute the SQL statement SELECTtable_schemaAS'Database',SUM(data_length index_length)/1024/1024AS'Size(MB)'FROMinformation_schema.tablesGROUPBYtable_schema; you can get the total size of all databases, or add WHERE conditions to limit the specific database; 2. Check the single table size: use SELECTta

To set up asynchronous master-slave replication for MySQL, follow these steps: 1. Prepare the master server, enable binary logs and set a unique server-id, create a replication user and record the current log location; 2. Use mysqldump to back up the master library data and import it to the slave server; 3. Configure the server-id and relay-log of the slave server, use the CHANGEMASTER command to connect to the master library and start the replication thread; 4. Check for common problems, such as network, permissions, data consistency and self-increase conflicts, and monitor replication delays. Follow the steps above to ensure that the configuration is completed correctly.

MySQL supports transaction processing, and uses the InnoDB storage engine to ensure data consistency and integrity. 1. Transactions are a set of SQL operations, either all succeed or all fail to roll back; 2. ACID attributes include atomicity, consistency, isolation and persistence; 3. The statements that manually control transactions are STARTTRANSACTION, COMMIT and ROLLBACK; 4. The four isolation levels include read not committed, read submitted, repeatable read and serialization; 5. Use transactions correctly to avoid long-term operation, turn off automatic commits, and reasonably handle locks and exceptions. Through these mechanisms, MySQL can achieve high reliability and concurrent control.

Character set and sorting rules issues are common when cross-platform migration or multi-person development, resulting in garbled code or inconsistent query. There are three core solutions: First, check and unify the character set of database, table, and fields to utf8mb4, view through SHOWCREATEDATABASE/TABLE, and modify it with ALTER statement; second, specify the utf8mb4 character set when the client connects, and set it in connection parameters or execute SETNAMES; third, select the sorting rules reasonably, and recommend using utf8mb4_unicode_ci to ensure the accuracy of comparison and sorting, and specify or modify it through ALTER when building the library and table.
