MySQL 5.7.9主从及Enhanced Multi-Threaded Slave配置

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

1.master/slave hosts文件


root login

hosts(master/slave)

127.0.0.1 localhost

10.8.1.5 mdb01

10.8.1.6 sdb01


cp soft/mysql-5.7.9/support-files/my-default.cnf /etc/my.cnf


2.master server-id


root login


vi /etc/my.cnf


[mysqld]


log-bin=mysql-bin(DATADIR/mysql-bin)

server-id=1



3.Slave server-id


vi /etc/my.cnf

[mysqld]

server-id=2



4.master 复制用户,锁表禁止插入


mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'repl';

Query OK, 0 rows affected (0.18 sec)


mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

Query OK, 0 rows affected (0.05 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.09 sec)


锁定表

mysql> FLUSH TABLES WITH READ LOCK;

Query OK, 0 rows affected (0.44 sec)


在master一个不同的session


mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000006 |      154 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)



mysql> 



5.如果存在同步主从数据


master

mysql> UNLOCK TABLES;


6.Slave 设置日志开始复制位置


mysql> CHANGE MASTER TO

    ->         MASTER_HOST='mdb01',

    ->         MASTER_USER='repl',

    ->         MASTER_PASSWORD='repl',

    ->         MASTER_LOG_FILE='mysql-bin.000006',

    ->         MASTER_LOG_POS=154;

Query OK, 0 rows affected, 2 warnings (0.09 sec)


7.启动slave

mysql> START SLAVE;

Query OK, 0 rows affected (0.07 sec)


8.检查slave状态

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: mdb01

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000006

          Read_Master_Log_Pos: 319

               Relay_Log_File: sdb01-relay-bin.000003

                Relay_Log_Pos: 485

        Relay_Master_Log_File: mysql-bin.000006

             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: 319

              Relay_Log_Space: 692

              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

                  Master_UUID: 42ad8740-7e88-11e5-83de-000c29270868

             Master_Info_File: /opt/mysql/data/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           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

         Replicate_Rewrite_DB: 

                 Channel_Name: 

1 row in set (0.00 sec)


ERROR: 

No query specified


mysql> 


配置可能遇到的问题:


主从如果是同一个虚拟机复制生成的两台auto.cnf文件中server-uuid相同,Slave_IO_Running: Null 进程可能无法启动。


error日志提示:

[ERROR] Slave I/O for channel '': Fatal error: The slave I/O thread stops because master and slave hav

e equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593


需要修改其中任何一个MySQL数据库DataDir目录下的auto.cnf文件中修改为不同值,然后停止slave后,重新启动MySQL服务,然后再启动Slave便正常运行了。


[auto]

server-uuid=42ad8740-7e88-11e5-83de-000c29270869



9、主从测试


1).master


mysql> create database testdb;


mysql> use testdb;


mysql> create table user ( 

    -> uid int auto_increment,  

    ->    data json,primary key(uid)

    -> );

Query OK, 0 rows affected (0.27 sec)


mysql> insert into user values (NULL,'{"name":"David","mail":"jiangchengyao@gmail.com","address":"Shangahai"}');

Query OK, 1 row affected (0.64 sec)


mysql> insert into user values (NULL,'{"name":"Amy","mail":"amy@gmail.com"}'); 

Query OK, 1 row affected (0.14 sec)


mysql> select data->"$.name" from user;

+----------------+

| data->"$.name" |

+----------------+

| "David"        |

| "Amy"          |

+----------------+

2 rows in set (0.16 sec)


mysql> 




2).slave


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

| testdb             |

+--------------------+

5 rows in set (0.00 sec)


mysql> use testdb

Database changed

mysql> show tables;

+------------------+

| Tables_in_testdb |

+------------------+

| user             |

+------------------+

1 row in set (0.00 sec)


mysql> desc user;

+-------+---------+------+-----+---------+----------------+

| Field | Type    | Null | Key | Default | Extra          |

+-------+---------+------+-----+---------+----------------+

| uid   | int(11) | NO   | PRI | NULL    | auto_increment |

| data  | json    | YES  |     | NULL    |                |

+-------+---------+------+-----+---------+----------------+

2 rows in set (0.01 sec)


mysql> SHOW CREATE TABLE user\G;

*************************** 1. row ***************************

       Table: user

Create Table: CREATE TABLE `user` (

  `uid` int(11) NOT NULL AUTO_INCREMENT,

  `data` json DEFAULT NULL,

  PRIMARY KEY (`uid`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

1 row in set (0.00 sec)


ERROR: 

No query specified


mysql> select data ->"$.name" from user;

+-----------------+

| data ->"$.name" |

+-----------------+

| "David"         |

| "Amy"           |

+-----------------+

2 rows in set (0.03 sec)


mysql> 


10.Slave并行复制配置


Enhanced Multi-Threaded Slave配置,配置后重启MySQL服务器。


说了这么多,要开启enhanced multi-threaded slave其实很简单,只需根据如下设置:


# slave

slave-parallel-type=LOGICAL_CLOCK

slave-parallel-workers=16

master_info_repository=TABLE

relay_log_info_repository=TABLE

relay_log_recovery=ON



并行复制监控


复制的监控依旧可以通过SHOW SLAVE STATUS\G,但是MySQL 5.7在performance_schema架构下多了以下这些元数据表,用户可以更细力度的进行监控:


mysql> show tables like 'replication%';

+---------------------------------------------+

| Tables_in_performance_schema (replication%) |

+---------------------------------------------+

| replication_applier_configuration           |

| replication_applier_status                  |

| replication_applier_status_by_coordinator   |

| replication_applier_status_by_worker        |

| replication_connection_configuration        |

| replication_connection_status               |

| replication_group_member_stats              |

| replication_group_members                   |

+---------------------------------------------+

8 rows in set (0.00 sec)



slave状态:


mysql> SHOW SLAVE STATUS\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: mdb01

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000006

          Read_Master_Log_Pos: 1196

               Relay_Log_File: sdb01-relay-bin.000005

                Relay_Log_Pos: 320

        Relay_Master_Log_File: mysql-bin.000006

             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: 1196

              Relay_Log_Space: 527

              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

                  Master_UUID: 42ad8740-7e88-11e5-83de-000c29270868

             Master_Info_File: mysql.slave_master_info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           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

         Replicate_Rewrite_DB: 

                 Channel_Name: 

1 row in set (0.00 sec)


ERROR: 

No query specified


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

| testdb             |

+--------------------+

5 rows in set (0.01 sec)


mysql> use performance_schema

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> show tables like 'replication%';

+---------------------------------------------+

| Tables_in_performance_schema (replication%) |

+---------------------------------------------+

| replication_applier_configuration           |

| replication_applier_status                  |

| replication_applier_status_by_coordinator   |

| replication_applier_status_by_worker        |

| replication_connection_configuration        |

| replication_connection_status               |

| replication_group_member_stats              |

| replication_group_members                   |

+---------------------------------------------+

8 rows in set (0.00 sec)


mysql>


参考文献

http://www.innomysql.net/article/16317.html


本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1708255

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
在Ubuntu 22.04上配置和安装MySQL
以上就是在Ubuntu 22.04上配置和安装MySQL的步骤。这个过程可能看起来有点复杂,但只要按照步骤一步步来,你会发现其实并不难。记住,任何时候都不要急于求成,耐心是解决问题的关键。
53 30
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
199 26
MySQL 8.4 配置SSL组复制(八个步骤)
MySQL 8.4 配置SSL组复制(八个步骤)
37 0
seatunnel配置mysql2hive
本文介绍了SeaTunnel的安装与使用教程,涵盖从安装、配置到数据同步的全过程。主要内容包括: 1. **SeaTunnel安装**:详细描述了下载、解压及配置连接器等步骤。 2. **模拟数据到Hive (fake2hive)**:通过编辑测试脚本,将模拟数据写入Hive表。 3. **MySQL到控制台 (mysql2console)**:创建配置文件并执行命令,将MySQL数据输出到控制台。 4. **MySQL到Hive (mysql2hive)**:创建Hive表,配置并启动同步任务,支持单表和多表同步。
179 15
mysql主从复制概述和配置
【10月更文挑战第22天】MySQL 主从复制是一种将主服务器的数据复制到一个或多个从服务器的技术,实现读写分离,提高系统性能和可用性。主服务器记录变更日志,从服务器通过 I/O 和 SQL 线程读取并应用这些变更。适用于读写分离、数据备份和恢复、数据分析等场景。配置步骤包括修改配置文件、创建复制用户、配置从服务器连接主服务器并启动复制进程。
292 1
2024Mysql And Redis基础与进阶操作系列(1)作者——LJS[含MySQL的下载、安装、配置详解步骤及报错对应解决方法]
Mysql And Redis基础与进阶操作系列(1)之[MySQL的下载、安装、配置详解步骤及报错对应解决方法]
在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。
本文介绍了在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。同时,文章还对比了编译源码安装与使用 RPM 包安装的优缺点,帮助读者根据需求选择最合适的方法。通过具体案例,展示了编译源码安装的灵活性和定制性。
525 2
Mysql中搭建主从复制原理和配置
主从复制在数据库管理中广泛应用,主要优点包括提高性能、实现高可用性、数据备份及灾难恢复。通过读写分离、从服务器接管、实时备份和地理分布等机制,有效增强系统的稳定性和数据安全性。主从复制涉及I/O线程和SQL线程,前者负责日志传输,后者负责日志应用,确保数据同步。配置过程中需开启二进制日志、设置唯一服务器ID,并创建复制用户,通过CHANGE MASTER TO命令配置从服务器连接主服务器,实现数据同步。实验部分展示了如何在两台CentOS 7服务器上配置MySQL 5.7主从复制,包括关闭防火墙、配置静态IP、设置域名解析、配置主从服务器、启动复制及验证同步效果。
291 0
Mysql中搭建主从复制原理和配置
下一篇
oss创建bucket
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等