mysql高可用方案之Keepalived+主主复制

环境规划:

node1:    192.168.1.250

node2:    192.168.1.251

vip:      192.168.1.201

数据库:   mysql-5.6.23

1.各节点的网络配置

node1节点:

[[email protected] ~]# hostname
node1
[[email protected] ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 10:78:d2:c9:50:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.250/24 brd 192.168.1.255 scope global eth0
    inet6 fe80::1278:d2ff:fec9:5028/64 scope link
       valid_lft forever preferred_lft forever
[[email protected] ~]#

node2节点:

[[email protected] ~]# hostname
node2
[[email protected] ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether d0:27:88:7d:83:9b brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.251/24 brd 192.168.1.255 scope global eth0
    inet6 fe80::d227:88ff:fe7d:839b/64 scope link
       valid_lft forever preferred_lft forever
[[email protected] ~]#

2.下载安装mysql数据库

node1节点和node2节点一样安装(下面红色部分不一样)

[[email protected] ~]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz

[[email protected] ~]# tar xvf mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz  -C /usr/local/

[[email protected] ~]# cd /usr/local/
[[email protected] local]# mv mysql-5.6.23-linux-glibc2.5-x86_64  mysql-5.6.23   
[[email protected] local]# chown  -R root:mysql mysql-5.6.23/
[[email protected] local]# chown  -R mysql:mysql mysql-5.6.23/data/
[[email protected] local]# cd mysql-5.6.23/
[[email protected] mysql-5.6.23]# ./scripts/mysql_install_db  --user=mysql --group=mysql --database=/usr/local/mysql-5.6.23/data --basedir=/usr/local/mysql-5.6.23
[[email protected] mysql-5.6.23]# cp -a my.cnf  /etc/
[[email protected] mysql-5.6.23]# cp -a support-files/mysql.server  /etc/init.d/mysqld
[[email protected] mysql-5.6.23]# vim /etc/my.cnf

basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23
port = 3306
server_id = 10                       --将另一台主修改为20
socket = /tmp/mysql.sock

log-bin=mysql-bin
log-bin-index=mysql-bin-index

replicate-do-db=tong
replicate-ignore-db=mysql

auto_increment_offset=1              --将另一台主修改为2
auto_increment_increment=2

relay-log=relay-log
relay-log-index=relay-log-inde

log_slave_updates
sync-binlog=1

[[email protected] mysql-5.6.23]# /etc/init.d/mysqld restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL. SUCCESS!
[[email protected] mysql-5.6.23]#

3.配置主主复制

node1节点:

[[email protected] mysql-5.6.23]# /usr/local/mysql-5.6.23/bin/mysqladmin -u root password ‘system‘  --修改初始密码为system
Warning: Using a password on the command line interface can be insecure.
[[email protected] mysql-5.6.23]# /usr/local/mysql-5.6.23/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.23-log MySQL Community Server (GPL)

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> create database tong;
Query OK, 1 row affected (0.05 sec)

mysql> grant replication slave,replication client on *.* to [email protected]‘192.168.1.251‘ identified by ‘system!#%246‘;         --创建复制用户
Query OK, 0 rows affected (0.05 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)

mysql> show master status;    --查看node1节点的二进制位置
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      690 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

node2节点:

[[email protected] mysql-5.6.23]# /usr/local/mysql-5.6.23/bin/mysqladmin  -u root password ‘system‘
Warning: Using a password on the command line interface can be insecure.

[[email protected] mysql-5.6.23]# /usr/local/mysql-5.6.23/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.23-log MySQL Community Server (GPL)

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> create database tong;
Query OK, 1 row affected (0.03 sec)

mysql> grant replication slave,replication client on *.* to [email protected]‘192.168.1.250‘ identified by ‘system!#%246‘;
Query OK, 0 rows affected (0.06 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

mysql> change master to master_host=‘192.168.1.250‘,master_port=3306,master_user=‘repl_user‘,master_password=‘system!#%246‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=690;    --同步node1的数据
Query OK, 0 rows affected, 2 warnings (0.39 sec)

mysql> show master status;         --查看node2节点的二进制日志
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      690 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

mysql>

node1节点:

mysql> change master to master_host=‘192.168.1.251‘,master_port=3306,master_user=‘repl_user‘,master_password=‘system!#%246‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=690;    --同步node2节点的数据
Query OK, 0 rows affected, 2 warnings (0.49 sec)

mysql> start slave;
Query OK, 0 rows affected (0.08 sec)

mysql>

4.测试主主同步是否正常(在两个节点各写一行数据,在两个节点查看数据)

node1节点:

mysql> \u tong
Database changed
mysql> create table t (a int);
Query OK, 0 rows affected (0.33 sec)

mysql> insert into t values(1);
Query OK, 1 row affected (0.08 sec)

mysql>

node2节点:

mysql> \u tong
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from t;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> insert into t values(2);
Query OK, 1 row affected (0.12 sec)

mysql> select * from t;
+------+
| a    |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

mysql>

node1节点:

mysql> select * from t;
+------+
| a    |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

mysql>

5.下载安装keepalived软件(node1和node2是一样)

node1节点:

[[email protected] ~]# wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

[[email protected] ~]# tar xvf keepalived-1.2.15.tar.gz
[[email protected] ~]# cd keepalived-1.2.15

[[email protected] keepalived-1.2.15]# ./configure --prefix=/usr/local/keepalived-1.2.15 --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64

[[email protected] keepalived-1.2.15]# make && make install

[[email protected] keepalived-1.2.15]# echo $?
0
[[email protected] keepalived-1.2.15]# cd /usr/local/keepalived-1.2.15/

[[email protected] keepalived-1.2.15]# ll
total 16
drwxr-xr-x. 2 root root 4096 Apr 30 11:46 bin
drwxr-xr-x. 5 root root 4096 Apr 30 11:46 etc
drwxr-xr-x. 2 root root 4096 Apr 30 11:46 sbin
drwxr-xr-x. 3 root root 4096 Apr 30 11:46 share

[[email protected] keepalived-1.2.15]# mkdir /etc/keepalived       --创建文件夹不能少,否则出错

[[email protected] keepalived-1.2.15]# cp -a etc/rc.d/init.d/keepalived  /etc/init.d/

[[email protected] keepalived-1.2.15]# cp -a etc/keepalived/keepalived.conf  /etc/keepalived

[[email protected] keepalived-1.2.15]# cp -a etc/sysconfig/keepalived  /etc/sysconfig/

[[email protected] keepalived-1.2.15]# cd /etc/keepalived/                  
[[email protected] keepalived]# vim keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]           --邮件报警
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1        --邮件服务器
   smtp_connect_timeout 30      --连接超时30秒报警
   router_id mysql-ha          
}

vrrp_instance VI_1 {
    state MASTER            --主节点是MASTER,备节点是BACKUP
    interface eth0
    virtual_router_id 50    --id值在两台服务器必须一至
    priority 100            --优先级,node1是100,node2是90
    advert_int 1
    nopreempt               --不抢占资源
    authentication {
        auth_type PASS      --两个节点认证的权限
        auth_pass 1111
    }
    virtual_ipaddress {     --VIP地址
        192.168.1.201
    }
}

virtual_server 192.168.1.201 3306 {    --VIP地址和端口
    delay_loop 6
    lb_algo wrr              --权纵
    lb_kind DR               --DR模式
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP             --协议

real_server 192.168.1.250 3306 {    --node1的IP地址和端口
        weight 1
        notify_down /usr/local/mysql-5.6.23/bin/mysql.sh    --检测mysql宕机后执行的脚本
        TCP_CHECK {
            connect_timeout 3    --连接超时
            nb_get_retry 3       --重试3秒
            connect_port 3306    --连接端口

}
    }
}

[[email protected] keepalived]# cat /usr/local/mysql-5.6.23/bin/mysql.sh    --脚本内容
#!/bin/bash
pkill keepalived
/usr/bin/keepalived -D
[[email protected] keepalived]#

node2节点:

安装keepalived软件是一样

[[email protected] keepalived]# vim keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id mysql-ha
}

vrrp_instance VI_1 {
    state BACKUP                 --与node1不一样
    interface eth0
    virtual_router_id 50
    priority 99                   --与node1不一样
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.201
    }
}

virtual_server 192.168.1.201 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.1.251 3306 {       --node2的IP地址和端口
        weight 1
        notify_down /usr/local/mysql-5.6.23/bin/mysql.sh
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            connect_port 3306
        }
    }

}

[[email protected] keepalived]# cat /usr/local/mysql-5.6.23/bin/mysql.sh    --脚本内容
#!/bin/bash
pkill keepalived
/usr/bin/keepalived -D
[[email protected] keepalived]#

6.启动服务和测试状态

node1节点和node1节点:

[[email protected] keepalived]# /etc/init.d/mysqld restart    --两个节点启动服务
 ERROR! MySQL server PID file could not be found!
Starting MySQL. SUCCESS!
[[email protected] keepalived]# /etc/init.d/keepalived restart   --两个节点启动服务
Stopping keepalived:                                       [FAILED]
Starting keepalived:                                       [  OK  ]
[[email protected] etc]# /usr/local/mysql-5.6.23/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.23-log MySQL Community Server (GPL)

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> grant all privileges on *.* to [email protected]‘%‘ identified by ‘system‘;     --在两个节点创建相同的远程登陆用户
Query OK, 0 rows affected (0.07 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

mysql>

node2节点:

[[email protected] keepalived]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether d0:27:88:7d:83:9b brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.251/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.201/32 scope global eth0           --VIP地址在node2已经启动
    inet6 fe80::d227:88ff:fe7d:839b/64 scope link
       valid_lft forever preferred_lft forever
[[email protected] keepalived]#

用mysql客户端登陆VIP地址:

[[email protected] keepalived]# /usr/local/mysql-5.6.23/bin/mysql -u remote -p -h 192.168.1.201
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.6.23-log MySQL Community Server (GPL)

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> \u tong
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table g (a int);
Query OK, 0 rows affected (0.29 sec)

mysql> insert into g values(1);
Query OK, 1 row affected (0.09 sec)

mysql> select * from g;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> exit
Bye
[[email protected] keepalived]# /etc/init.d/mysqld stop    --关闭node2节点的mysql服务
Shutting down MySQL.... SUCCESS!
[[email protected] keepalived]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether d0:27:88:7d:83:9b brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.251/24 brd 192.168.1.255 scope global eth0   --VIP不见了
    inet6 fe80::d227:88ff:fe7d:839b/64 scope link
       valid_lft forever preferred_lft forever
[[email protected] keepalived]#

node1节点:

[[email protected] keepalived]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 10:78:d2:c9:50:28 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.250/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.201/32 scope global eth0           --VIP在node1启动了
    inet6 fe80::1278:d2ff:fec9:5028/64 scope link
       valid_lft forever preferred_lft forever
[[email protected] keepalived]#

用mysql客户端登陆VIP地址:

[[email protected] bin]# ./mysql -u remote -p -h 192.168.1.201 -P 3306
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.6.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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> \u tong
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from g;      --数据同步了
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql>

时间: 2024-11-16 02:36:01

mysql高可用方案之Keepalived+主主复制的相关文章

MySQL高可用基础之keepalived+双主复制【转】

环境:MySQL-VIP:192.168.1.3MySQL-master1:192.168.1.1MySQL-master2:192.168.1.2 OS版本:CentOS release 6.4 (Final) Linux 2.6.32-358.el6.x86_64MySQL版本:5.6.14Keepalived版本:1.2.13 一.MySQL master-master配置 1.修改MySQL配置文件/etc/my.cnf   # Server1配置[mysqld]basedir = /u

负载均衡高可用之LVS+Keepalived(DR/主备)+apache

负载均衡高可用之LVS+Keepalived(DR/主备)+apache 介绍: LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. LVS集群采用IP负载均衡技术和基于内容请求分发技术.调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一个高性能的.高可用的虚拟服务器.整个服务器集群的结构对客户

MySQL高可用方案探究

来自 http://bbs.chinaunix.net/thread-3769165-1-1.html 最近花了点时间研究了一下mysql的高可用,总结成文档,希望对初学这有帮助. Lvs+Keepalived+Mysql单点写入主主同步高可用方案 http://blog.chinaunix.net/uid-20639775-id-3337448.html Lvs+Keepalived+Mysql单点写入读负载均衡主主同步高可用方案 http://blog.chinaunix.net/uid-2

Heartbeat+DRBD+MySQL高可用方案

Heartbeat+DRBD+MySQL高可用方案 =============================================================================== 概述: =============================================================================== 方案介绍  1.方案介绍及优缺点 ★方案介绍 本方案采用Heartbeat双机热备软件来保证数据库的高稳定性和连续性,数

[转]MYSQL高可用方案探究(总结)

前言 http://blog.chinaunix.net/uid-20639775-id-3337432.htmlLvs+Keepalived+Mysql单点写入主主同步高可用方案 http://blog.chinaunix.net/uid-20639775-id-3337448.htmlLvs+Keepalived+Mysql单点写入读负载均衡主主同步高可用方案http://blog.chinaunix.net/uid-20639775-id-3337471.htmlHeartbeat高可用M

[转载] MySQL高可用方案选型参考

原文: http://imysql.com/2015/09/14/solutions-of-mysql-ha.shtml?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io 本次专题是 MySQL高可用方案选型,这个专题想必有很多同学感兴趣. 高可用的意义以及各种不同高可用等级相应的停机时间我就不必多说了,直接进入主题. 可选MySQL高可用方案 MySQL的各种高可用方案,大多是基于以下几种基础来部署的: 基于主从复制:

Heartbeat+DRBD+MySQL高可用方案【转】

转自Heartbeat+DRBD+MySQL高可用方案 - yayun - 博客园 http://www.cnblogs.com/gomysql/p/3674030.html 1.方案简介 本方案采用Heartbeat双机热备软件来保证数据库的高稳定性和连续性,数据的一致性由DRBD这个工具来保证.默认情况下只有一台mysql在工作,当主mysql服务器出现问题后,系统将自动切换到备机上继续提供服务,当主数据库修复完毕,又将服务切回继续由主mysql提供服务. 2.方案优缺点 优点:安全性高.稳

五大常见的MySQL高可用方案【转】

1. 概述 我们在考虑MySQL数据库的高可用的架构时,主要要考虑如下几方面: 如果数据库发生了宕机或者意外中断等故障,能尽快恢复数据库的可用性,尽可能的减少停机时间,保证业务不会因为数据库的故障而中断. 用作备份.只读副本等功能的非主节点的数据应该和主节点的数据实时或者最终保持一致. 当业务发生数据库切换时,切换前后的数据库内容应当一致,不会因为数据缺失或者数据不一致而影响业务. 关于对高可用的分级在这里我们不做详细的讨论,这里只讨论常用高可用方案的优缺点以及高可用方案的选型. 2. 高可用方

MySQL高可用方案选型参考

本次专题是 MySQL高可用方案选型,这个专题想必有很多同学感兴趣. 高可用的意义以及各种不同高可用等级相应的停机时间我就不必多说了,直接进入主题. 可选MySQL高可用方案 MySQL的各种高可用方案,大多是基于以下几种基础来部署的: 基于主从复制: 基于Galera协议: 基于NDB引擎: 基于中间件/proxy: 基于共享存储: 基于主机高可用: 在这些可选项中,最常见的就是基于主从复制的方案,其次是基于Galera的方案,我们重点说说这两种方案.其余几种方案在生产上用的并不多,我们只简单