wheat 发表于 2018-10-6 12:44:20

keepalived打造mysql主主高可用

  我的配置:
  # rpm -qa keepalived
  keepalived-1.2.13-5.el6_6.x86_64
  # cat /etc/issue

  CentOS>  Kernel \r on an \m
  # mysql -uroot -p123 -h192.168.31.132
  Welcome to the MySQL monitor.Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.5.46-log MySQL Community Server (GPL) by Remi
  # mysql -u root -p123 -h192.168.31.136
  Welcome to the MariaDB monitor.Commands end with ; or \g.

  Your MariaDB connection>  Server version: 5.5.32-MariaDB-log MariaDB Server
mysql服务器ip地址mysqlVIPmysql版本192.168.31.132192.168.31.131 5.5.46-log MySQL Community Server192.168.31.136192.168.31.1315.5.32-MariaDB-log MariaDB Server  这里mysql的安装和配置主主复制就不提了:
  mysql的安装:网上很多不一一复制了
  mysql主主复制配置案例: http://blog.chinaunix.net/uid-30234663-id-5192230.html
  开始配置安装:
  1:下载安装keepalived(所有节点都要安装):
  # yum install -y keepalived
  然后配置如下:
  192.168.31.136配置:
  # cat /etc/keepalived/keepalived.conf
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  hz7726@163.com
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 60
  priority 90
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  192.168.31.131
  }
  }
  virtual_server 192.168.31.131 3306 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  nat_mask 255.255.255.0
  persistence_timeout 50
  protocol TCP
  real_server 192.168.31.136 3306 {
  weight 3
  notify_down /tmp/mysql.sh
  TCP_CHECK {
  connect_timeout 3
  nb_get_retry 3
  delay_before_retry 3
  connect_port 3306
  }
  }
  }
  192.168.31.132的配置如下:
  # cat /etc/keepalived/keepalived.conf
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  hz7726@163.com
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 60
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  192.168.31.131
  }
  }
  virtual_server 192.168.31.131 3306 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  nat_mask 255.255.255.0
  persistence_timeout 50
  protocol TCP
  real_server 192.168.31.132 3306 {
  weight 3
  notify_down /tmp/mysql.sh
  TCP_CHECK {
  connect_timeout 3
  nb_get_retry 3
  delay_before_retry 3
  connect_port 3306
  }
  }
  }
  注意:要在两个服务器都要给其用户授权:不然报错如下
  # mysql -uroot -p123 -h192.168.31.131
  ERROR 1045 (28000): Access denied for user 'root'@'192.168.31.131' (using password: YES)
  解决办法:

  mysql> grant all on *.* to root@'192.168.31.%'>  Query OK, 0 rows affected (0.05 sec)
  mysql>flush privileges
  -> ;
  Query OK, 0 rows affected (0.00 sec)
  授权之后再在192.168.31.132和192。168.31.136上写mysql.sh检测脚本
  如下:
  #!/bin/sh
  pkill keepalived #检测资源放置脑裂
  在测试随便一台服务器:
  # mysql -uroot -p123 -h 192.168.31.131
MariaDB [(none)]> show databases;  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | han                |
  | hanye            |
  | mysql            |
  | performance_schema |
  | test               |
  +--------------------+
  6 rows in set (0.00 sec)
  停掉一台服务器在测试:
  valid_lft forever preferred_lft forever
  # pkill keepalived
  # mysql -uroot -p123 -h 192.168.31.131
  Welcome to the MariaDB monitor.Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.5.46-log MySQL Community Server (GPL) by Remi
  Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  MySQL [(none)]> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | blog_article02   |
  | db1                |
  | db2                |
  | db3                |
  | dbtest1            |
  | dbtest2            |
  | dbtest3            |
  | mycat            |
  | mysql            |
  | performance_schema |
  | syslog             |
  +--------------------+
  12 rows in set (0.07 sec)
  注意:我这边两台服务器的数据是不一致的:更容易发现高可用成功与否!!!
  # ifconfig eth0 | awk -F'[: ]+' '/inet addr/{print $4}'
  192.168.31.132
  # mysql -u root -p123
  Welcome to the MySQL monitor.Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.5.46-log MySQL Community Server (GPL) by Remi
  Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
  Oracle is a registered trademark of Oracle Corporation and/or its
  affiliates. Other names may be trademarks of their respective
  owners.
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | blog_article02   |
  | db1                |
  | db2                |
  | db3                |
  | dbtest1            |
  | dbtest2            |
  | dbtest3            |
  | mycat            |
  | mysql            |
  | performance_schema |
  | syslog             |
  +--------------------+
  12 rows in set (0.00 sec
  # ifconfig eth0 | awk -F'[: ]+' '/inet addr/{print $4}'
  192.168.31.136
  # mysql -uroot -p123
  Welcome to the MariaDB monitor.Commands end with ; or \g.

  Your MariaDB connection>  Server version: 5.5.32-MariaDB-log MariaDB Server
  Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  MariaDB [(none)]> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | han                |
  | hanye            |
  | mysql            |
  | performance_schema |
  | test               |
  +--------------------+
  6 rows in set (0.00 sec)
  ok了配置完成

页: [1]
查看完整版本: keepalived打造mysql主主高可用