MySQL主从架构

本文涉及的产品
云服务器 ECS,每月免费额度280元 3个月
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云服务器ECS,u1 2核4GB 1个月
简介:

   MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的。它基于binlog,主设备上须开启binlog才能进行主从。

   主从过程大致有3个步骤:

     1)主设备将更改操作记录到binlog里;

     2)从将主设备的binlog事件(sql语句)同步到从本机上并记录在relaylog里;

     3)从根据relaylog里面的sql语句按顺序执行。

   主设备上有一个log dump线程,用来和从的I/O线程传递binlog。从设备上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地。

wKiom1mmxVLDrgOOAAHlMAz2Xnk817.png


准备工作

1、准备两台装有mysql的服务器,并启动mysql服务。

2、分配角色,确定设备主从。


配置主设备

1、编辑配置文件

1
2
3
4
[root@plinuxos ~] # vim /etc/my.cnf
[mysqld]
server- id =88
log_bin=bin01

2、重启mysql

1
2
3
[root@plinuxos ~] # /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS!

3、检查log_bin

1
2
[root@plinuxos ~] # ls /data/mysql/bin01.*
/data/mysql/bin01 .000001   /data/mysql/bin01 .index

4、创建数据库

1
2
[root@plinuxos mysql] # cd /data/mysql
[root@plinuxos mysql] # /usr/local/mysql/bin/mysql -uroot -e 'create database db01'

5、增加测试数据

1
2
[root@plinuxos mysql] # /usr/local/mysql/bin/mysqldump -uroot zrlog >/tmp/zrlog.sql
[root@plinuxos mysql] # /usr/local/mysql/bin/mysql -uroot db01 </tmp/zrlog.sql

6、备份所有数据库

1
2
[root@plinuxos mysql] # /usr/local/mysql/bin/mysqldump -uroot db01 > /tmp/db01.sql
[root@plinuxos mysql] # /usr/local/mysql/bin/mysqldump -uroot zrlog > /tmp/zrlog.sql

7、创建用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@plinuxos mysql] # /usr/local/mysql/bin/mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 414
Server version: 5.6.35-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, 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 on *.* to  'repl' @ '122.112.197.192'  identified by  '123456' ;
Query OK, 0 rows affected (0.01 sec)

8、锁表并查看状态

1
2
3
4
5
6
7
8
9
10
mysql> flush tables with  read  lock;   ##锁表,防止写入
Query OK, 0 rows affected (0.01 sec)
 
mysql> show master status;    ##下方两个参数需要在从设备上配置
+--------------+----------+--------------+------------------+-------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------+----------+--------------+------------------+-------------------+
| bin01.000001 |    10472 |              |                  |                   |
+--------------+----------+--------------+------------------+-------------------+
1 row  in  set  (0.00 sec)


配置从设备

1、编辑配置文件

1
2
3
[root@ecs-89c1 mysql] # vi /etc/my.cnf
[mysqld]
server- id =192

2、重启mysql

1
2
3
[root@ecs-89c1 mysql] # /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

3、复制主设备数据库备份文件

1
2
3
4
5
6
7
8
[root@ecs-89c1 mysql] # scp 122.112.253.88:/tmp/*.sql /tmp/
The authenticity of host  '122.112.253.88 (122.112.253.88)'  can't be established.
ECDSA key fingerprint is 2e:6e:90:32:87:05:9e:63:63:d6:2d:44:a5:5f:be:51.
Are you sure you want to  continue  connecting ( yes /no )?  yes
Warning: Permanently added  '122.112.253.88'  (ECDSA) to the list of known hosts.
root@122.112.253.88's password: 
db01.sql                                    100% 9877     9.7KB /s    00:00          
zrlog.sql                                   100% 9878     9.7KB /s    00:00

4、创建对应数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@ecs-89c1 mysql] # /usr/local/mysql/bin/mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 1
Server version: 5.6.35 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, 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 db01;
Query OK, 1 row affected (0.00 sec)
 
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
 
[root@ecs-89c1 mysql] # /usr/local/mysql/bin/mysql -uroot db01 </tmp/db01.sql
[root@ecs-89c1 mysql] # /usr/local/mysql/bin/mysql -uroot zrlog </tmp/zrlog.sql

5、配置主备同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@ecs-89c1 mysql] # /usr/local/mysql/bin/mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 4
Server version: 5.6.35 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, 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> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> change master to master_host= '122.112.253.88' ,master_user= 'repl' ,master_password= '123456' ,master_log_file= 'bin01.000001' ,master_log_pos=10472;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
 
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

6、查看主从状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
mysql> show slave status\G;   ##如果出现错误,可能是云主机策略没放过3306端口
*************************** 1. row ***************************
                Slave_IO_State: Waiting  for  master to send event
                   Master_Host: 122.112.253.88
                   Master_User: repl
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: bin01.000001
           Read_Master_Log_Pos: 10708
                Relay_Log_File: ecs-89c1-relay-bin.000003
                 Relay_Log_Pos: 515
         Relay_Master_Log_File: bin01.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
               Replicate_Do_DB: 
           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: 10708
               Relay_Log_Space: 691
               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: 88
                   Master_UUID: 79b66469-8cc8-11e7-ae36-fa163eb51d35
              Master_Info_File:  /data/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)
 
ERROR: 
No query specified

7、主设备解锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@plinuxos mysql] # /usr/local/mysql/bin/mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection  id  is 875
Server version: 5.6.35-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, 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> unlock tables;
Query OK, 0 rows affected (0.00 sec)


测试主从同步

1、在主设备上删除db01数据库的表;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql> show tables;
+----------------+
| Tables_in_db01 |
+----------------+
| comment        |
| link           |
| log            |
| lognav         |
| plugin         |
| tag            |
type            |
| user           |
+----------------+
8 rows  in  set  (0.00 sec)
 
mysql> drop table tag;
Query OK, 0 rows affected (0.01 sec)

2、在从设备查看对应的表也已经不存在了。

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> show tables;
+----------------+
| Tables_in_db01 |
+----------------+
| comment        |
| link           |
| log            |
| lognav         |
| plugin         |
type            |
| user           |
+----------------+
7 rows  in  set  (0.00 sec)

扩展学习

▎配置参数

1. 主服务器上:

binlog-do-db=      //仅同步指定的库(其他库不同步)

binlog-ignore-db= //忽略指定库(其他库都同步)

2. 从服务器上:

replicate_do_db=   //(不常用)

replicate_ignore_db=   //(不常用)

replicate_do_table=   //(不常用)

replicate_ignore_table=   //(不常用)

replicate_wild_do_table=   //如aming.%, (支持通配符%)

replicate_wild_ignore_table=












本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1961233,如需转载请自行联系原作者


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
关系型数据库 MySQL 开发工具
MySQL5.7主从配置(Docker)
MySQL5.7主从配置(Docker)
726 0
|
1月前
|
网络协议 Linux
Linux DNS服务详解——DNS主从架构配置
Linux DNS服务详解——DNS主从架构配置
405 4
|
1月前
|
SQL 关系型数据库 MySQL
解决MySQL主从慢同步问题的常见的解决方案:
解决MySQL主从慢同步问题的方法有很多,以下是一些常见的解决方案: 1. 检查网络连接:确保主从服务器之间的网络连接稳定,避免网络延迟或丢包导致数据同步缓慢。 2. 优化数据库配置:调整MySQL的配置参数,如增大binlog文件大小、调整innodb_flush_log_at_trx_commit等参数,以提高主从同步性能。 3. 检查IO线程和SQL线程状态:通过SHOW SLAVE STATUS命令检查IO线程和SQL线程的状态,确保它们正常运行并没有出现错误。 4. 检查主从日志位置:确认主从服务器的binlog文件和位置是否正确,避免由于错误的日志位置导致同步延迟。 5.
116 1
|
9天前
|
SQL 关系型数据库 MySQL
mysql主从同步出错解决办法
mysql主从同步出错解决办法
7 0
|
20天前
|
canal 消息中间件 关系型数据库
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
66 0
|
1月前
|
架构师 算法 关系型数据库
数据库架构师之道:MySQL安装与系统整合指南
数据库架构师之道:MySQL安装与系统整合指南
44 0
|
2月前
|
存储 监控 关系型数据库
ELK架构监控MySQL慢日志
ELK架构监控MySQL慢日志
|
10天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)
|
1月前
|
关系型数据库 MySQL 数据库连接
关于MySQL-ODBC的zip包安装方法
关于MySQL-ODBC的zip包安装方法
|
28天前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
81 1