一文搞懂MySQL主从复制

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 一文搞懂MySQL主从复制

1 为什么要主从复制

主从复制、读写分离一般是一起使用的。目的很简单,就是为了提高数据库的并发性能。你想,假设是单机,读写都在一台MySQL上面完成,性能肯定不高。如果有三台MySQL,一台mater只负责写操作,两台salve只负责读操作,性能就能大大提高了。

所以主从复制、读写分离就是为了数据库能支持更大的并发、提高数据库的可用性

2 主从复制步骤

2.1 环境准备

MySQL主机:

  • IP地址:10.0.0.1
  • 端口号:3306
  • 版本:5.5

MySQL从机:

  • IP地址:10.0.0.2
  • 端口号:3306
  • 版本:5.5

2.2 修改配置文件

MySQL主机:

[mysqld]
log-bin=/home/mysql/binlog
server-id=1
复制代码

MySQL从机:

[mysqld]
server-id=2
log-bin=/home/mysql/binlog
relay-log=/home/mysql/relaylog
复制代码

2.3 在MySQL中进行操作

首先重启两个MySQL

MySQL主机:

[root@iZ2ze4m2ri7irkf6h6n8zoZ mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.62-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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 USER ymx IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'ymx'@'10.0.0.2'  identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+---------------+----------+--------------+------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000003 |      594 |              |                  |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)
复制代码

MySQL从机:

[root@iZ1608aqb7ntn9Z ~]# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.62-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| college_work       |
| db_chat            |
| db_stu_work_mg     |
| mysql              |
| performance_schema |
| sysbooking         |
| test               |
| yn_db              |
+--------------------+
9 rows in set (0.00 sec)
mysql> change master to master_host='10.0.0.1',master_user='ymx',master_password='123456',master_port=3306,master_log_file='binlog.000003',master_log_pos=594;
Query OK, 0 rows affected (0.01 sec)file='binlog.000003',master_log_pos=594;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.1
                  Master_User: ymx
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000003
          Read_Master_Log_Pos: 594
               Relay_Log_File: relaylog.000002
                Relay_Log_Pos: 250
        Relay_Master_Log_File: binlog.000003
             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: 594
              Relay_Log_Space: 399
              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: 1
1 row in set (0.00 sec)
复制代码

2.4 测试

MySQL主机:

mysql> create database slave_db;
Query OK, 1 row affected (0.00 sec)
mysql> use slave_db;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table user( id int(20),name varchar(200));
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+--------------------+
| Tables_in_slave_db |
+--------------------+
| user               |
+--------------------+
1 row in set (0.00 sec)
复制代码

MySQL从机:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| college_work       |
| db_chat            |
| db_stu_work_mg     |
| mysql              |
| performance_schema |
| slave_db           |
| sysbooking         |
| test               |
| yn_db              |
+--------------------+
10 rows in set (0.00 sec)
mysql> use slave_db;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> show tables;
+--------------------+
| Tables_in_slave_db |
+--------------------+
| user               |
+--------------------+
1 row in set (0.00 sec)
复制代码

3 MySQL主从复制原理

网络异常,图片无法展示
|

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
SQL 存储 关系型数据库
MySQL的主从复制&主从同步
MySQL的主从复制&主从同步
30 0
|
4月前
|
SQL 关系型数据库 MySQL
面试官:说一下MySQL主从复制的原理?
面试官:说一下MySQL主从复制的原理?
94 0
面试官:说一下MySQL主从复制的原理?
|
4月前
|
SQL 关系型数据库 MySQL
MySQL主从复制
MySQL主从复制
|
17天前
|
负载均衡 容灾 关系型数据库
mysql主从复制
mysql主从复制
31 1
|
1月前
|
SQL 存储 运维
MySQL高可用性:主从复制和集群
MySQL高可用性:主从复制和集群
36 0
|
16天前
|
SQL 关系型数据库 MySQL
mysql主从复制
mysql主从复制
|
4月前
|
关系型数据库 MySQL Linux
Linux下搭建MySQL主从复制之一主一从架构
Linux下搭建MySQL主从复制之一主一从架构
59 0
|
1月前
|
SQL 网络协议 关系型数据库
【怒怼大厂面试官】听说你精通MySQL?来说说MySQL主从复制
面试官:MySQL主从复制了解吧?嗯嗯了解的。主要是利用了MySQL的Binary Log二进制文件。那我把二进制文件丢给从库,从库复制整个文件吗。噢噢不是的。
48 1
【怒怼大厂面试官】听说你精通MySQL?来说说MySQL主从复制
|
1月前
|
监控 负载均衡 关系型数据库
|
3月前
|
SQL 负载均衡 关系型数据库
MySQL(六)主从复制
MySQL(六)主从复制
27 0