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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
日志服务 SLS,月写入数据量 50GB 1个月
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

     mysql主从在目前企业中很常见的方案之一,主是将DML,DDL,DML语句用mysql dump进程将二进制日志记录,从是用thread_io线程读取主上的制日志存放中继日志中,再用thread_sql线程读取中继日志将数据写入到数据库,实现数据同步.


mysql dba技术群 378190849

武汉-linux运维群 236415619

 

架构规划:

tong1:192.168.1.247

tong2:192.168.1.248

 

1.配置网络

tong1节点:

[root@tong1 ~]# hostname 
tong1
[root@tong1 ~]# cat /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2

[root@tong1 ~]#

 

tong2节点:

[root@tong2 ~]# hostname 
tong2
[root@tong2 ~]# cat /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2

[root@tong2 ~]#

 

2.下载安装mysql数据库

在两个节点安装mysql数据库

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

[root@tong1 ~]# tar xvf mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz  -C /usr/local/

[root@tong1 ~]# cd /usr/local/

[root@tong1 ~]# mv mysql-5.6.23-linux-glibc2.5-x86_64/ mysql-5.6.23

[root@tong1 ~]# cd mysql-5.6.23/

[root@tong1 ~]# ./scripts/mysql_install_db --user=mysql --group=mysql --basedir=/usr/local/mysql-5.6.23 --datadir=/usr/local/mysql-5.6.23/data

[root@tong1 ~]# cp -a my.cnf  /etc/

[root@tong1 ~]# cp -a support-files/mysql.server  /etc/init.d/mysqld

[root@tong1 ~]# chkconfig  --add mysqld

[root@tong1 ~]# chkconfig  mysqld on

[root@tong1 ~]# vim /etc/my.cnf

basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 20
socket = /tmp/mysql.sock

[root@tong1 ~]# pkill  mysqld

[root@tong1 ~]# /etc/init.d/mysqld  restart

[root@tong1 ~]# /usr/local/mysql-5.6.23/bin/mysqladmin  -u root password 'system'

[root@tong1 ~]# /usr/local/mysql-5.6.23/bin/mysql -u root -p  --输入密码system
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.23 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> exit
Bye
[root@tong1 ~]# 


3.在主库上开启二进制日志和防火墙

tong1节点:

[root@tong1 ~]# vim /etc/my.cnf

basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 20
socket = /tmp/mysql.sock


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

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

-A INPUT -m state --state NEW -m tcp -p tcp -s 192.168.1.248 --dport 3306 -j ACCEPT

[root@tong1 ~]# /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@tong1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

[root@tong1 ~]# /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 1
Server version: 5.6.23 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 replication slave ,replication client on *.* to repl_user@'192.168.1.248' identified by 'system!#%246';
Query OK, 0 rows affected (0.00 sec)

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

mysql> create database tong;
Query OK, 1 row affected (0.00 sec)

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

mysql> 


4.在从库上配置复制主库的数据

[root@tong2 mysql-5.6.23]# vim /etc/my.cnf

basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 10
socket = /tmp/mysql.sock

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

[root@tong2 mysql-5.6.23]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@tong2 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 1
Server version: 5.6.23 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.00 sec)

mysql> change master to  master_host='192.168.1.247',master_user='repl_user',master_password='system!#%246',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=528;
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.247
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 528
               Relay_Log_File: tong2-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: mysql
           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: 528
              Relay_Log_Space: 456
              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: 3de2c8c6-ed4c-11e4-9392-1078d2c78303
             Master_Info_File: /usr/local/mysql-5.6.23/data/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> 


5.验证主从同步

tong1主库:

[root@tong1 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 1
Server version: 5.6.23 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
Database changed
mysql> create table t (a int);
Query OK, 0 rows affected (0.29 sec)

mysql> insert into t values(1),(2),(3);
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

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

mysql> 



tong2从库验证数据:

[root@tong2 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 1
Server version: 5.6.23 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

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

mysql> 











本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1340214,如需转载请自行联系原作者
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
2月前
|
运维 监控 关系型数据库
MySQL高可用方案:MHA与Galera Cluster对比
本文深入对比了MySQL高可用方案MHA与Galera Cluster的架构原理及适用场景。MHA适用于读写分离、集中写入的场景,具备高效写性能与简单运维优势;而Galera Cluster提供强一致性与多主写入能力,适合对数据一致性要求严格的业务。通过架构对比、性能分析及运维复杂度评估,帮助读者根据自身业务需求选择最合适的高可用方案。
|
2月前
|
SQL 监控 关系型数据库
MySQL主从复制:构建高可用架构
本文深入解析MySQL主从复制原理与实战配置,涵盖复制架构、监控管理、高可用设计及性能优化,助你构建企业级数据库高可用方案。
|
2月前
|
网络协议 NoSQL API
转转客服IM系统的WebSocket集群架构设计和部署方案
客服IM系统是转转自研的在线客服系统,是用户和转转客服沟通的重要工具,主要包括机器人客服、人工客服、会话分配、技能组管理等功能。在这套系统中,我们使用了很多开源框架和中间件,今天讲一下客服IM系统中WebSocket集群的的实践和应用。
214 0
|
2月前
|
消息中间件 监控 Cloud Native
高效设计:支持亿级用户社交关系的100W QPS架构方案
面对亿级用户与百万QPS的高并发场景,性能测试成为系统稳定的关键。本文剖析真实业务痛点,详解从接口压测、全链路监控到瓶颈定位的完整性能体系,助你掌握大厂级性能优化能力,从容应对卡顿、宕机等线上挑战。
|
2月前
|
存储 监控 NoSQL
Redis高可用架构全解析:从主从复制到集群方案
Redis高可用确保服务持续稳定,避免单点故障导致数据丢失或业务中断。通过主从复制实现数据冗余,哨兵模式支持自动故障转移,Cluster集群则提供分布式数据分片与水平扩展,三者层层递进,保障读写分离、容灾切换与大规模数据存储,构建高性能、高可靠的Redis架构体系。
|
3月前
|
存储 关系型数据库 MySQL
修复.net Framework4.x连接MYSQL时遇到utf8mb3字符集不支持错误方案。
通过上述步骤大多数情况下能够解决由于UTF-encoding相关错误所带来影响,在实施过程当中要注意备份重要信息以防止意外发生造成无法挽回损失,并且逐一排查确认具体原因以采取针对性措施解除障碍。
251 12
|
边缘计算 Kubernetes 物联网
Kubernetes 赋能边缘计算:架构解析、挑战突破与实践方案
在物联网和工业互联网快速发展的背景下,边缘计算凭借就近处理数据的优势,成为解决云计算延迟高、带宽成本高的关键技术。而 Kubernetes 凭借统一管理、容器化适配和强大生态扩展性,正逐步成为边缘计算的核心编排平台。本文系统解析 Kubernetes 适配边缘环境的架构分层、核心挑战与新兴解决方案,为企业落地边缘项目提供实践参考。
287 0
|
4月前
|
SQL 关系型数据库 MySQL
解决MySQL "ONLY_FULL_GROUP_BY" 错误的方案
在实际操作中,应优先考虑修正查询,使之符合 `ONLY_FULL_GROUP_BY`模式的要求,从而既保持了查询的准确性,也避免了潜在的不一致和难以预测的结果。只有在完全理解查询的业务逻辑及其后果,并且需要临时解决问题的情况下,才选择修改SQL模式或使用 `ANY_VALUE()`等方法作为短期解决方案。
633 8
|
3月前
|
监控 NoSQL 关系型数据库
保障Redis与MySQL数据一致性的强化方案
在设计时,需要充分考虑到业务场景和系统复杂度,避免为了追求一致性而过度牺牲系统性能。保持简洁但有效的策略往往比采取过于复杂的方案更加实际。同时,各种方案都需要在实际业务场景中经过慎重评估和充分测试才可以投入生产环境。
235 0
|
4月前
|
数据采集 边缘计算 定位技术
ar景区导航导览开发方案:核心技术架构与功能设计
本方案针对传统景区导航吸引力弱、互动性差等问题,融合三维建模、多源定位与AR引擎技术,实现室内外精准导航与AR互动体验。支持AR寻宝等功能,提升游客体验与景区竞争力。
356 0

推荐镜像

更多