>데이터 베이스 >MySQL 튜토리얼 >mysql高可用方案之主主架构(master-master)

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

WBOY
WBOY원래의
2016-06-07 14:55:391254검색

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

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

 

环境规划:

主机名:tong2   IP:192.168.1.248

主机名:tong3   IP:192.168.1.249

数据库:mysql-5.6.21

 

1.配置服务器网络环境

tong2节点:

[root@tong2 ~]# 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)

[root@tong2 ~]# vim /etc/hosts

192.168.1.248 tong2
192.168.1.249 tong3

[root@tong2 ~]#

 

tong3节点:

[root@tong3 ~]# 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)
[root@tong3 ~]# vim /etc/hosts

192.168.1.248 tong2
192.168.1.249 tong3

[root@tong3 ~]#

 

2.下载安装mysql-5.6.21软件

tong2节点和tong3节点:

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

[root@tong2 ~]# rpm -ivh MySQL-*                 --安装所有的mysql软件包

 

3.修改配置文件

tong2节点:

[root@tong2 ~]# 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

[root@tong2 ~]#

 

tong3节点:

[root@tong3 ~]# 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

[root@tong3 ~]#

 

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

tong2节点:

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

[root@tong2 ~]# 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节点:

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

[root@tong3 ~]# 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 tong3@'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节点:

[root@tong2 ~]# vim /etc/sysconfig/iptables

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

[root@tong2 ~]# /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  ]
[root@tong2 ~]#

 

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节点:

[root@tong2 ~]# 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节点:

[root@tong3 ~]# 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>

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.