Keepalived build highly available MySQL-HA

Source: Internet
Author: User

There are many solutions for MySQL-HA, such as heartbeat, drbd, mmm, and shared storage, but they have their own advantages and disadvantages. The heartbeat and drbd configurations are complex. You need to write your own scripts to achieve automatic MySQL switching. For those who do not know the scripting language, this is undoubtedly a split-brain problem. For mmm, few people use the production environment, and the mmm management end needs to run a server separately. To achieve high availability, You need to implement HA on the mmm management end, this will undoubtedly increase hardware spending. For shared storage, I personally think that MySQL Data is safer to be stored locally. After all, there is a single point of failure in storage devices. Using MySQL dual master + keepalived is a very good solution. In the MySQL-HA environment, MySQL is mutually master-slave, which ensures data consistency between the two MySQL databases, then, use keepalived to implement virtual IP addresses, and use the service monitoring function provided by keepalived to automatically switch MySQL during failure.

Next, I will share with you the architecture in a production environment to be launched. Let's take a look at how MySQL-HA is implemented in this architecture. The environment topology is as follows:

 

 
 
  1. MySQL-VIP: 192.168.1.200
  2. MySQL-master1: 192.168.1.201
  3. MySQL-master2: 192.168.1.202
  4. OS Version: CentOS 5.4
  5. MySQL version: 5.0.89
  6. Keepalived version: 1.1.20

1. MySQL master-master Configuration

1. Modify the MySQL configuration file

To enable the binlog function for both MySQL instances, enable the function by adding log-bin = MySQL-bin in the [MySQLd] section of the MySQL configuration file.

The server-ID of the two MySQL instances cannot be the same. By default, the serverID of the two MySQL instances is 1 and you need to change one of them to 2.

2. Set 192.168.1.201 to the master server of 192.168.1.202.

Create an authorized user on 192.168.1.201

 

 
 
  1. MySQL> grant replication slave on *.* to replication@% identified by replication;    
  2. Query OK, 0 rows affected (0.00 sec)    
  3.    
  4. MySQL> show master status;    
  5. +------------------+----------+--------------+------------------+    
  6. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |    
  7. +------------------+----------+--------------+------------------+    
  8. | MySQL-bin.000003 |      374 |              |                  |     
  9. +------------------+----------+--------------+------------------+    
  10. 1 row in set (0.00 sec)  

Set 192.168.1.201 as the master server on 192.168.1.202.

 

 
 
  1. MySQL> change master to master_host=192.168.1.201,master_user=replication,master_password=replication,master_log_file=MySQL-bin.000003,master_log_pos=374;    
  2. Query OK, 0 rows affected (0.05 sec)    
  3.    
  4. MySQL> start slave;    
  5. Query OK, 0 rows affected (0.00 sec)    
  6.    
  7. MySQL> show slave statusG    
  8. *************************** 1. row ***************************    
  9.              Slave_IO_State: Waiting for master to send event    
  10.                 Master_Host: 192.168.1.201    
  11.                 Master_User: replication    
  12.                 Master_Port: 3306    
  13.               Connect_Retry: 60    
  14.             Master_Log_File: MySQL-bin.000003    
  15.         Read_Master_Log_Pos: 374    
  16.              Relay_Log_File: MySQL-master2-relay-bin.000002    
  17.               Relay_Log_Pos: 235    
  18.       Relay_Master_Log_File: MySQL-bin.000003    
  19.            Slave_IO_Running: Yes    
  20.           Slave_SQL_Running: Yes    
  21.             Replicate_Do_DB:     
  22.         Replicate_Ignore_DB:     
  23.          Replicate_Do_Table:     
  24.      Replicate_Ignore_Table:     
  25.     Replicate_Wild_Do_Table:     
  26. Replicate_Wild_Ignore_Table:     
  27.                  Last_Errno: 0    
  28.                  Last_Error:     
  29.                Skip_Counter: 0    
  30.         Exec_Master_Log_Pos: 374    
  31.             

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.