mysql高可用方案之主主架构(master-master)

mysql的主从是将主服务器操作记录写入二进制日志文件,然后通过mysql dump线程将日志传到从服务器中继日志中,从服务器在中继日志读取信息并执行.mysql主主架构原理和主从差不多,只是两台服务器都要开启二进制日志文件,并互相传送给对方读取日志中的内容,使数据同步.两台服务器可以同时读也可以同时写,但不能解决单点故障.

环境规划:

主机名:tong2   IP:192.168.1.248

主机名:tong3   IP:192.168.1.249

数据库:mysql-5.6.21

1.配置服务器网络环境

tong2节点:

[[email protected] ~]# ifconfig  eth0
eth0      Link encap:Ethernet  HWaddr 10:78:D2:C7:17:E8 
          inet addr:192.168.1.248  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::1278:d2ff:fec7:17e8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3118346 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16271 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:201242719 (191.9 MiB)  TX bytes:1754452 (1.6 MiB)

[[email protected] ~]# vim /etc/hosts

192.168.1.248 tong2
192.168.1.249 tong3

[[email protected] ~]#

tong3节点:

[[email protected] ~]# ifconfig  eth0
eth0      Link encap:Ethernet  HWaddr 10:78:D2:C8:F7:50 
          inet addr:192.168.1.249  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::1278:d2ff:fec8:f750/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5055594 errors:0 dropped:0 overruns:0 frame:0
          TX packets:96641 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:580382285 (553.4 MiB)  TX bytes:7055569 (6.7 MiB)
[[email protected] ~]# vim /etc/hosts

192.168.1.248 tong2
192.168.1.249 tong3

[[email protected] ~]#

2.下载安装mysql-5.6.21软件

tong2节点和tong3节点:

http://mirrors.sohu.com/mysql/MySQL-5.6/       --下载关于mysql的软件

[[email protected] ~]# rpm -ivh MySQL-*                 --安装所有的mysql软件包

3.修改配置文件

tong2节点:

[[email protected] ~]# vim /usr/my.cnf     --在配置文件中添加如下内容

server_id = 10            --服务器的id号

log-bin=mysql-bin       --二进制日志文件
log-bin-index=mysql-bin-index     --索引文件
relay-log=relay-bin                      --中继日志文件
relay-log-index=relay-bin-index     --中继索引文件
replicate-do-db=tong                   --要同步的数据库

auto_increment_offset=1               --设置服务器交叉读写数据
auto_increment_increment=2

[[email protected] ~]#

tong3节点:

[[email protected] ~]# vim /usr/my.cnf

server_id = 20

log-bin=mysql-bin
log-bin-index=mysql-bin-index
relay-log=relay-bin
relay-log-index=relay-bin-index
replicate-do-db=tong

auto_increment_offset=2              
auto_increment_increment=2

[[email protected] ~]#

3.登陆数据库并创建用户

tong2节点:

[[email protected] ~]# /etc/init.d/mysql restart     --重启服务
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[[email protected] ~]# netstat -antup | grep 3306     
tcp        0      0 :::3306                     :::*                        LISTEN      8508/mysqld

[[email protected] ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.21 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> create database tong;      --创建要同步的数据库

Query OK, 1 row affected (0.00 sec)

mysql> grant replication slave,replication client on *.* to ‘tong2‘@‘192.168.1.249‘ identified by ‘system‘;       --创建复制用户
Query OK, 0 rows affected (0.00 sec)

mysql> flush tables;
Query OK, 0 rows affected (0.00 sec)

mysql>

tong3节点:

[[email protected] ~]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[[email protected] ~]# netstat -antup | grep 3306     
tcp        0      0 :::3306                     :::*                        LISTEN      3484/mysqld

[[email protected] ~]# 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.21 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> create database tong;

Query OK, 1 row affected (0.00 sec)

mysql> grant replication slave,replication client on *.* to [email protected]‘192.168.1.248‘ identified by ‘system‘;
Query OK, 0 rows affected (0.00 sec)

mysql> flush tables;
Query OK, 0 rows affected (0.00 sec)

mysql>

4.iptable包过滤

tong2节点和tong3节点:

[[email protected] ~]# vim /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp -s 192.168.1.249--dport 3306 -j ACCEPT   --添加规则,放行对mysql的通行

[[email protected] ~]# /etc/init.d/iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
[[email protected] ~]#

5.查看各节点的二进制日志的位置

tong2节点:

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

tong3节点:

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

6.服务器互相连接

tong2节点:

[[email protected] ~]# 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.21-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> change master to master_host=‘192.168.1.249‘,master_user=‘tong3‘,master_password=‘system‘,master_port=3306,master_log_file=‘mysql-bin.000004‘,master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.43 sec)

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

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.249
                  Master_User: tong3
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 120
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes          --io线程启动正常
            Slave_SQL_Running: Yes        --sql线程启动正常
              Replicate_Do_DB: tong         --要同步的数据库
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 120
              Relay_Log_Space: 450
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 20
                  Master_UUID: 6009ee25-7f93-11e4-8817-1078d2c8f750
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

mysql>

tong3节点:

[[email protected] ~]# 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.21-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> change master to master_host=‘192.168.1.248‘,master_user=‘tong2‘,master_password=‘system‘,master_port=3306,master_log_file=‘mysql-bin.000001‘,master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.43 sec)

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

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.248
                  Master_User: tong2
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 120
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: tong
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 120
              Relay_Log_Space: 450
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 10
                  Master_UUID: 2cb52da5-759f-11d6-bc85-1078d2c717e8
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

mysql>

7.测试服务是否正常

tong2节点:

mysql> create table a (a int);
Query OK, 0 rows affected (0.26 sec)
mysql> insert into a values(1),(2),(3),(4);
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0

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

mysql>

tong3节点:

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 a;
+------+
| a    |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)

mysql> create table a1 (a1 int);
Query OK, 0 rows affected (0.27 sec)

mysql> insert into a1 values(11),(12),(13),(14);
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from a1;
+------+
| a1   |
+------+
|   11 |
|   12 |
|   13 |
|   14 |
+------+
4 rows in set (0.00 sec)

mysql>

tong2节点:

mysql> select * from a1;
+------+
| a1   |
+------+
|   11 |
|   12 |
|   13 |
|   14 |
+------+
4 rows in set (0.00 sec)

mysql>

时间: 2024-10-22 00:58:33

mysql高可用方案之主主架构(master-master)的相关文章

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 addr1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKN

[转载] 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的各种高可用方案,大多是基于以下几种基础来部署的: 基于主从复制:

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

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

MySQL高可用方案选型参考

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

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高可用方案MHA自动Failover与手动Failover的实践及原理

集群信息 角色                             IP地址                 ServerID      类型 Master                         192.168.244.10   1                 写入 Candicate master          192.168.244.20   2                 读 Slave                           192.168.244.

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

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

MySQL高可用方案——双主

MySQL的高可用方案有很多种,双主.MHA.MMM等等,这里只是写下最简单的双主这种高可用方案. 一.配置MySQL互为主从 1.环境准备 系统 IP 主机名 服务 Centos 7.5 192.168.20.2 mysql01 MySQL+keepalived Centos 7.5 192.168.20.3 mysql02 MySQL+keepalived 注:MySQL已部署完成,可参考博文Centos部署MySQL 5.7进行部署. 2.开启二进制日志及中继日志 #主机mysql01配置