Heartbeat builds a MySQL database dual-master Hot Standby High Availability Environment

Source: Internet
Author: User
1. IntroductionHeartbeat is used to build a dual-host Hot Standby environment. It can be connected through a dedicated heartbeat line or through a common network cable. Official Website: mongotmp] # Cat/etc/RedHat-releasered Hat Enterprise Linux as Release 4 (nahant update 6) heartbeat 2.1.3 2. Installation 2.1. Preparation

In this test, there are two hosts whose IP addresses are: Host Name IP address alssme_probe3 172.18.158.23alssme _ probe4 172.18.158.24. If/etc/hosts is not configured, configure the/etc/hosts file first. 2.2 create users and user groupsHeartbeat requires the haclient user group and the hacluster user. The two nodes perform the same operation and ensure that the IDs of haclient and hacluster are the same. Groupadd haclient-G 1001 useradd-G haclient hacluster-u 1002 2.3. InstallationYou can directly download the RPM package or download the source code package to compile the package. Here, I compile the source code by myself. Install the Libnet package before installing heartbeat. Tar zxvf heartbeat-2.1.3.tar.gzrpm-IVH libnet-1.1.2.1-2.2.el4.rf.rpm [root @ alsme_probe3 TMP] # cd heartbeat-2.1.3

[Root @ alsme_probe3 TMP] #./configure [root @ alsme_probe3 TMP] # Make [root @ alsme_probe3 TMP] # make install is also installed on another host. The Libnet package can be found in the http://rpmfind.net/linux/rpm2html/search.php here. 3. ConfigurationHeartbeat has three configuration files:-ha. CF-authkyes-haresources files must be consistent on each machine. the following is a one-to-one introduction. CP/usr/local/share/doc/heartbeat-2.1.3/ha. CF/usr/local/etc/ha. d CP/usr/local/share/doc/heartbeat-2.1.3/haresources/usr/local/etc/ha. d CP/usr/local/share/doc/heartbeat-2.1.3/authkeys/usr/local/etc/ha. d3.1 edit hertbeat master configuration file ha. CF, the content on the two hosts is the same. The configuration to be enabled is as follows. Detailed descriptions are provided in Ha. Cf. [Root @ alsme_probe3 TMP] # vi/usr/local/etc/ha. d/ha. Cf # Ha log file record location. If the directory does not exist, manually add

Logfile/var/log/ha-log # file to write other messages

Logfacility local0 # This is the log that sets heartbeat, here is the system log # Set the heartbeat (Monitoring) Time to 2 seconds

Keepalive 2 # How long does it take to detect

Warntime 5 # How long does it take to get in touch and start warning: deadtime 20 # How long does it take to get in touch and think the other party has crashed (the unit is wonderful) initdead 120 # Here is a period of ignore time reserved for the restart (for example, starting the network after the restart). If the network is not connected, the keepalive detection will fail, but it cannot be switched at this time.) # Use bond0 UDP broadcast to send heartbeat information # bcast bond0 # Use the UDP unicast of eth0 to notify heartbeat. The IP address should be the IP address of the other party. We recommend that you use unicast. If a CIDR block has multiple such clusters, you must use single-play. Otherwise, each group of clusters will see the other node and report an error. Ucast eth0 172.18.57.154 # Use UDP port 694 for heartbeat monitoring

Udpport 694

Auto_failback off # Whether to automatically switch back after it returns to normal, usually set to off. # The hostname of Node 1 must be consistent with the result obtained by the uname-N command.

Node alsme_probe3 # hostname of Node 2

Node alssme_probe4 # ping the gateway to check whether the heartbeat is normal

Ping 172.18.158.254hopfudge 1 deadping 5 # specify the process to be started and closed together with heartbeat

# Respawn hacluster/usr/local/lib64/heartbeat/ipfail

# Apiauth ipfail gid = haclient uid = hacluster # Whether the V2 style mode is used, which must be enabled when there are more than three nodes # CRM On 3.2 edit the file authkeys[Root @ alsme_probe3 heartbeat-2.1.3] # vi/usr/local/etc/ha. d/authkeys

Auth 1

1 CRC

[Root @ alsme_probe3 heartbeat-2.1.3] # chmod 600/usr/local/etc/ha. d/authkeys Note: authkeys permissions must be 600.3.3 edit the cluster resource file: haresources (what needs to be done by the slave when switching) [root @ alsme_probe3 heartbeat-2.1.3] # vi/usr/local/etc/ha. d/haresources

Alssme_probe3 ipaddr: 172.18.158.111/24/bond0 MySQL # the configuration of 172.18.158.111 is VIP. For details, see haresources. There are several Notes: 1). the first column of the Resource Group is one of the nodes in the HA. cf configuration file, and it should be the node currently prepared as the primary node. 2 ). each row represents a resource group. If a row cannot be written, use "\" to wrap it. 3 ). the Resource Group start sequence is from left to right, and the close sequence is from right to left 4 ). the script parameters are separated and passed by: 5 ). different resources in a resource group are separated by spaces. 6 ). different resource groups are not necessarily related. 7 ). each resource is an angular, which can be in/etc/init. under the d directory, it can also be/usr/local/etc/ha. d/resource. d directory. These corners must support the xxx start; XXX stop; mode. 8) for details about the service IP resource setting format, see the haresources file. 9 ). if MySQL is compiled and installed, You need to modify/etc/init. the basedir and datadir parameters in the D/MySQL file are as follows: [root @ alsme_probe3 heartbeat-2.1.3] # vi/usr/local/etc/ha. d/resource. d/drbd

#! /Bin/sh

Case "$1" in

Start)

# Declared as the primary node of drbd

Drbdadm primary DB

# Mount a File System

Mount/dev/drbd0/Data

# Start Related Services

Service MySQL start

;;

Stop)

# Reverse operation

Service MySQL stop

Umount/dev/drbd0/Data

Drbdadm secondary DB

; Esac

Exit 0 the MySQL we tested is provided when MySQL is installed. In the/etc/init. d directory, it can start and close MySQL. If we use the heartbeat + dual master mode, we do not need to add a MySQL Resource Group. If we use the heartbeat + Master/Slave Mode, we need to re-create the MySQL ry and change the slave to the master at start. The master is changed to slave during stop. We recommend that you use the heartbeat + dual master mode to minimize data loss. 4. test and test switchover:

1 ). manually call heartbeat's node switchover Script 2 ). unplug the network cable and test the switchover after the network of the primary node is disconnected. After the network is unplugged, the master node cannot communicate with the standby node.
The system will record the warn information in the log. If the duration reaches the length set in Ha. Cf, the resources will be released, and the standby node will find that it cannot communicate with the master node for a period of time.
(Set by HA. CF), start to start the resource and turn itself into a primary node. The switching period is also very short except for the period set in Ha. Cf. Easy to use
The problem of Custer split brain occurs. If dual master is used, this problem does not exist. 3) shut down the primary host and test whether the switchover is normal.

4) Whether the master node can be switched normally when the power is down. The first and third test methods must be tested. 5. manage how to start and disable heartbeat: #/etc/init. d/hearbeat start or service heartbeat start #/etc/init. d/hearbeat stop or service heartbeat stop ..... Command sorting 6. Other actual test results show that if the MyISAM storage engine is used, data will always be lost when the primary node fails. If the InnoDB Storage engine is used
If the primary node itself does not have a program to write data to the local machine, there will be basically no data loss. If the primary node itself has a program to write data, then the local program will
Some data may be written in sequence, but if it is connected by other host programs, data will not be lost. Suggestion: In the drbd + HB environment, it is best to use the InnoDB Storage engine and set innodb_flush_log_at_trx_commit =
1. This allows almost every committed transaction to be recorded in ib_logfile * and can be recovered on the secondary node to reduce the loss. Heartbeat has two modes. The default mode only supports two nodes. If there are more than two nodes in the cluster, the CIB mode is required, that is, V2 style: 1 ). add all three nodes to ha. CF, and then In Ha. add "CRM 1" 2) to CF ). then use/usr/local/lib64/heartbeat/haresources2cib. PY
Convert the original/usr/local/etc/ha. d/haresources
/Usr/local/var/lib/heartbeat/CRM/CIB. XML. "/usr/local/lib64/heartbeat/haresources2cib. PY/usr/local/etc/ha. d/haresources/usr/local/var/lib/heartbeat/CRM/CIB. XML "3 ). clear the haresources. If only one node is started, the Service IP address takes effect in 2 minutes. I don't know how to control this time. By default, heartbeat cannot monitor resources. That is to say, if a resource crashes, no action will occur. It will only take action when it deems the other machine dead. That is, crashed, network disconnection, and so on. This obviously cannot achieve our goal. To achieve our goal, we need to adopt the CRM (cluster resource management) mode. First, configure heartbeat in the default mode. (For more information, see heartbeat ). After the default mode is successfully configured, perform the following operations: 1) in the HA. add CRM on2 in CF) to convert the haresources resource file to CIB. XML file, 2.1.3 self-contained conversion script/usr/local/lib64/heartbeat/haresources2cib. PY/usr/local/etc/ha. d/haresources output file in/usr/local/var/lib/heartbeat/CRM/CIB. xml3) if the hacluster and haclient users and user groups are created after heartbeat is installed, run the following command to modify the permission to modify the heartbeat directory permission: find/-type D-name "heartbeat"-exec chown-r hacluster {}\; find/-type D-name "heartbeat"-exec chgrp -R haclient {}\; 4) ipfail conflicts with CRM mode in version 2.0, so ipfail cannot be enabled in Ha. Cf. 5) CIB. if the modification to the XML file contains the following two lines in ipaddr, delete: <nvpair id = "ipaddr_172_18_57_83_attr_1" name = "Nic" value = "24"/> <nvpair id = "symbol" name = "cidr_netmask" value = "bond0"/> Version 2.1.3 the generated CIB. in the XML file, MySQL resources are in the OCF format, and its built-in MySQL angle cannot start mysql. Therefore, you need to modify the format in either of the following ways. Before modification, we will first introduce the differences between OCF and LSB formats: The angle book in LSB format must support the status function, and must be able to receive the start, stop, status, three parameters; in OCF format, the start, stop, and monitor parameters must be supported. among them, the status and monitor parameters are used to monitor resources, which is very important. for example, an LSB-style script is run. /When MySQL status is returned, if the returned value contains "OK" or "running", the normal returned value of the resource contains "STOPPED" or "no", indicating that the resource is abnormal. Run the OCF-style script. /In MySQL monitor, if 0 is returned, the resource is normal. If 7 is returned, the resource is faulty. the start point in the OCF format is/usr/lib/OCF/resource. d/HEARTBEAT (maybe this is not the directory on your machine, you can search for OCF). The starting angle of the LSB format is in/usr/lib/LSB/resource. d/heartbeat directory. Two modification methods: 1. Modify CIB. xml and change MySQL OCF to LSB. Then in/usr/lib/LSB/resource. d/HEARTBEAT (if
If the directory does not exist, create it manually and grant the permission to hacluster: haclient. Execute ln-S/etc/init. d/MySQL. # Rm/usr/heartbeat/var/lib/heartbeat/CRM/CIb. xml. *-F

# Vi/usr/heartbeat/var/lib/heartbeat/CRM/CIB. xml2. Modify/usr/lib/OCF/resource. d/heartbeat's MySQL corner to make it work normally. Or copy/etc/init. d/MySQL and modify it to support the monitor operation 6) and then start heartbeat. Service heartbeat start.7) If MySQL uses dual master nodes, after stopping the resource, remember to manually start MySQL. Heartbeat CRM Mode Management1) view all resources [root @ alssme_probe3 sbin] # crm_resource-lresource group: group_1 ipaddr_172_18_158_111 (Heartbeat: OCF: ipaddr) mysql_2 (LSB: MySQL) 2) check the node on which the resource runs [root @ alssme_probe3 sbin] # crm_resource-w-r mysql_2resource mysql_2 is running on: alssme_probe34) to start/stop the resource (the cluster will not switch, if you manually stop MySQL, it will restart or switch) [root @ alssme_probe4 CRM] # crm_resource-r mysql_2-P target_role-V started [root @ alssme_probe3 sbin] # cr M_resource-r mysql_2-P target_role-V stopped5) view the resource in CIB. definition in XML [root @ alssme_probe3 sbin] # crm_resource-X-r mysql_2mysql_2 (LSB: MySQL): started alssme_probe3raw XML: <primitive provider = "heartbeat" type = "MySQL" id = "mysql_2"> <operations> <op id = "mysql_2_mon" interval = "60s" name = "monitor" timeout = "30s"/> </Operations> <instance_attributes id = "mysql_2"> <attributes> <nvpair name = "target_role" id = "Mysql_2-target_role" value = "started"/> </attributes> </instance_attributes> </primitive> checks the resource running status every 60 seconds, then, start the resource. If the instance is not started after 30 s, the resource is switched to another node. Time can be modified (MySQL generally recommends this time value ). 6) Move the resource to the specified node crm_resource-m-r mysql_2-H alssme_probe4
Related Article

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.