Two Ways for Alibaba Cloud Server to Manually Realize mysql Dual machine Hot Standby

  • 2021-12-12 10:10:19
  • OfStack

1. Concepts

1. The difference between hot backup and backup

Hot backup refers to High Available (HA), that is, high availability, while backup refers to Backup, one kind of data backup. These are two different concepts, and the products to be dealt with are two completely different products in function. Hot backup mainly guarantees the continuity of business, and the realization method is the transfer of fault points. Backup, the main purpose is to prevent data loss, and make a copy, so backup emphasizes data recovery rather than application failover.

2. What is dual-machine hot standby?  

In a broad sense, dual-machine hot standby is to use two servers to back up each other and execute the same service together for important services. When one server fails, another server can undertake the service task, thus automatically ensuring that the system can provide services continuously without manual intervention.

In a narrow sense, dual-machine hot standby is to use two servers that are backup to each other to jointly perform the same service, in which one host is a working machine (Primary Server) and the other is a backup host (Standby Server). Under normal system conditions, The working machine provides services for the application system, The backup machine monitors the operation of the working machine (generally, it is diagnosed by heartbeat, and the working machine also detects whether the backup machine is normal). When the working machine is abnormal and cannot support the operation of the application system, the backup machine actively takes over the work of the working machine, continues to support key application services, and ensures the uninterrupted operation of the system. Dual-machine hot standby is a high-availability solution for the failure of IT core server, storage and network routing switching.

Ok, let's introduce Alibaba Cloud Server to realize mysql dual-machine hot standby. The specific contents are as follows:

Because Alibaba Cloud Server does not support keepalive virtual ip, it is impossible to realize dual-machine hot standby of mysql through keepalive. There are two ways to realize Alibaba Cloud's dual-machine hot equipment here:

1. Buy Alibaba Cloud's Cloud Database Advanced Edition with its own slave library, and automatically switch to the slave library when the master library fails

2 This is mainly about the second way. Two Alibaba Cloud servers deploy mysql respectively to realize the master-master synchronization of mysql. I won't focus on the synchronization of mysql. You can Baidu by yourself. If necessary, an article may be published in the future. Assuming that mysql master-master synchronization has been implemented now, what to do with it

Because the use of springboot, so need to deal with the yml file, and then add a send mail to a number of people, here host running timing tasks, spare machine needs, you can also run 1 under the corresponding

Because I deployed mysql with docker, I used the docker ps Check the status of mysql. If you deploy normally, you can use ps or log in directly to mysql to see if it is successful


# coding: utf8
import subprocess
from email.mime.text import MIMEText
import smtplib
import os
# Because what's used here Java Adj. springboot If the backup machine is switched, the part needs to be modified yml Documents 
YML_PATH = ''
NEW_YML = ''
msg_from = '' #  Sender mailbox 
passwd = '' #  Fill in the authorization code of the sender's mailbox 
msg_to = []
def check_mysql():
 res = subprocess.Popen('docker ps |grep mysql', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
 stdout, stderr = res.communicate()
 if stdout:
  res_status = subprocess.Popen("docker ps |grep mysql| awk '{print $7}'", stdout=subprocess.PIPE,
          stderr=subprocess.PIPE, shell=True)
  out, err = res_status.communicate()
  if out.strip() == "Up":
   print 'mysql is ok'
  else:
   print 'mysql is broken, switch standby machine'
   flag = judge_mysql_string()
   if flag:
    print ' The standby machine has been connected, so there is no need to switch '
   else:
    switch_mysql()
 else:
  print 'mysql is broken, switch standby machine'
  flag = judge_mysql_string()
  if flag:
   print ' The standby machine has been connected, so there is no need to switch '
  else:
   switch_mysql()
def switch_mysql():
 print '---copy yml----'
 subprocess.call('cp {} {}'.format(NEW_YML, YML_PATH), shell=True)
 subprocess.call('docker restart tomcat', shell=True)
 subject = 'mysql Host failure '
 info = 'mysql Host failure has been switched to standby machine, please check the host problem and repair it in time , If you need to switch back, ' \
 content = '<html><meta charset="UTF-8"><body><p style="color: red">{}</p> </body></html>'.format(info)
 send_email(subject, content)
 print '----end switch---'
def send_email(subject, content):
 msg = MIMEText(content, 'html', 'utf-8')
 msg['Subject'] = subject
 msg['From'] = msg_from
 msg['To'] = ','.join(msg_to)
 try:
  s = smtplib.SMTP_SSL("smtp.qq.com", 465)
  s.login(msg_from, passwd)
  s.sendmail(msg_from, msg_to, msg.as_string())
 except Exception as e:
  print e
 finally:
  s.quit()
def judge_mysql_string():
 with open(os.path.join(YML_PATH, 'application.yml'),'r') as f:
  res = f.read()
  if 'your ip' in res:
   return True
  else:
   return False
check_mysql()

Summarize


Related articles: