坏气十足 发表于 2018-10-7 12:07:41

Mysql数据库高可用

  Mysql数据库高可用
  Mysql(5.7.19)+keepalive(1.3.0)    +RHEL6.5
  Mysql下载地址
  https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz
  Keepalived地址
  http://www.keepalived.org/software/keepalived-1.3.0.tar.gz
  规划如下
  ip
  端口
  备注
  Mysql1
  172.25.254.120
  3306
  Mysql2
  172.25.254.130
  3306
  keepalived
  172.25.254.111
  一:首先搭建主主数据库
  拷贝压缩包到系统/mnt下
  cd /mnt/
  tar xvf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz
  cp -a mysql-5.7.19-linux-glibc2.12-x86_64 /usr/local/mysql
  useradd mysql
  cd /usr/local/mysql/
  cd bin/
  ./mysqld--initialize
  ./mysql_install_db --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data
  chown -R mysql/var/lib/mysql/data/
  ./mysqld_safe --user=mysql
  cd ..
  cp support-files/mysql.server /etc/init.d/mysqld
  rm -rf /etc/my.cnf
  cat set password = password('123456');
  sed-i '/\$/alog-bin=/var/lib/mysql/log-bin' /etc/my.cnf
  sed-i '/\$/aserver_id=130' /etc/my.cnf #两个id不一样即可
  service mysqld restart
  至此两个数据库搭建完成
  二:主主搭建
  mysql -uroot -p123456#登陆数据库
  grant   replication    slaveon*.*    to   slaveuser@"172.25.254.120"   identified by "123456";#130上配置
  grant   replication    slaveon*.*    to   slaveuser@"172.25.254.130"   identified by "123456";#120上配置
  登陆测试是否成功
  mysql> show master status\G;#分别查看两个节点
  ****************** 1. row ***************************
  File: log-bin.000001
  Position: 154
  change   master   to   master_host="172.25.254.120",master_user="slaveuser",master_password="123456",master_log_file="log-bin.000001",master_log_pos=154;#130上操作
  change   master   to   master_host="172.25.254.130",master_user="slaveuser",master_password="123456",master_log_file="log-bin.000001",master_log_pos=154;#120上操作
  start   slave;#两节点分别开启
  showslave status\G;
  Slave_IO_Running: YES
  Slave_SQL_Running: YES
  可以自己测试
  三:keepalived搭建
  cd /mnt/
  tar xvf keepalived-1.3.0.tar.gz
  yum -y install gcc gcc-c++ openssl-devel
  cd keepalived-1.3.0
  ./configure
  make&& make install
  mkdir /etc/keepalived
  cd /usr/local/etc/
  cp -a keepalived/keepalived.conf /etc/keepalived/
  cp sysconfig/keepalived /etc/sysconfig/
  cat /etc/keepalived/keepalived.conf
  ! Configuration File for keepalived
  global_defs {
  router_id
  }
  vrrp_sync_group VGM{
  group{
  HA
  }
  }
  vrrp_script check_mysql {
  script "/root/check_mysql.sh"#脚本文件
  interval 5
  }
  vrrp_instance HA {
  state MASTER#从BACKUP
  interface eth0
  virtual_router_id 51
  priority 100#从99
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  172.25.254.111/24 dev eth0
  }
  track_script {
  check_mysql
  }
  }
  cat /root/check_mysql.sh #脚本文件
  b=`ps aux | grep mysql | grep sqld | wc -l`
  if [ $b == 0 ]
  then
  service mysqld restart
  c=`ps aux | grep mysql | grep sqld | wc -l`
  if [ $c == 0 ]
  then
  kill -9 `ps aux | grep keep | grep lived | awk '{print $2}'`
  fi
  fi
  a=`ps aux | grep keep | grep lived |wc -l`
  if [ $a == 0 ]
  then
  sleep 300
  reboot
  fi
  测试,与开机启动不在次次试验中,可自行测试

页: [1]
查看完整版本: Mysql数据库高可用