MySQL的主从复制


点击前往查看MySQL的安装

1、主库操作

vim /etc/my.cnf

添加如下配置

log-bin=mysql-bin #[必须]启用二进制日志
server-id=128     #[必须]服务器唯一id

保存后重启MySQL

systemctl restart mysqld
mysql -uroot -pczx123
mysql> grant replication slave on *.* to 'czx'@'%' identified by 'root@czx123';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      740 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

主节点别操作,从库slave进行配置


如果grant添加用户报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements,执行下面两个命令即可再添加即可

mysql >  set global validate_password_policy=LOW; #设置密码复杂度为低
mysql > set global validate_password_length=4; #密码长度为4

2、从库操作

注意这是另一台子节点的虚拟机的操作

vim /etc/my.cnf

添加如下配置并保存。

server-id=129     #[必须]服务器唯一id

保存成功厚要重启MySQL

systemctl restart mysqld
mysql -uroot -pczx123
mysql> change master to master_host='192.168.64.128',master_user='czx',master_password='root@czx123',master_log_file='mysql-bin.000001',master_log_pos=740;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
# 其中master_host为主库IP地址,master_log_file和master_log_pos为主库执行“show master status;”查找到的
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status;
+----------------+----------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+-------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State | Master_Host    | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File             | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error                                                                                                                                          | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------+----------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+-------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
|                | 192.168.64.128 | czx         |        3306 |            60 | mysql-bin.000001 |                 740 | localhost-relay-bin.000001 |             4 | mysql-bin.000001      | No               | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 740 |             154 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                  NULL | No                            |          1593 | Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. |              0 |                |                             |              128 |             | /var/lib/mysql/master.info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             | 220527 02:15:23         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------+----------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+-------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.00 sec)
# 虽然显示很多,可以将结果复制到文本编辑器上查看从数据库的状态

在编辑器查找发现从库备份数据失败,原因就是两个虚拟机是克隆的,所以MySQL的uuid一样,更改从库的uuid即可

mysql> select uuid();#生成新的uuid
+--------------------------------------+
| uuid()                               |
+--------------------------------------+
| bb3980df-dd25-11ec-b3e4-000c297e18d4 |
+--------------------------------------+
1 row in set (0.00 sec)mysql> show variables like 'datadir';# 查找auto.cnf配置文件所在目录
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| datadir       | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.01 sec)
mysql>exit;vim  /var/lib/mysql/auto.cnf

修改uuid为新生成的uuid

[auto]
server-uuid=bb3980df-dd25-11ec-b3e4-000c297e18d4

保存后重启从库的MySQL

systemctl restart mysqld
mysql -uroot -pczx123
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> show slave status;
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State                   | Master_Host    | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File             | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | 192.168.64.128 | czx         |        3306 |            60 | mysql-bin.000002 |                 154 | localhost-relay-bin.000004 |           320 | mysql-bin.000002      | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 154 |             531 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |              128 | 29ca9845-d5a7-11ec-9de0-000c29f65e07 | /var/lib/mysql/master.info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------------------------+----------------+-------------+-------------+---------------+------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.00 sec)

发现两个都是yes即启动成功,后续可以继续添加MySQL的从库,操作如同第二个从库一样

3、验证是否配置好

使用navicat链接两个配置好的库,在主库新建数据库,刷新一下从库,看是否生成主库新建的数据库。若有则主从配置好。
如果出现10060或1045错误,请查看:navicat连接服务器的MySQL的10060和1045错误_czxboys的博客-CSDN博客

4、SpringBoot的MySQL读写分离配置

4.1 导入依赖

pom.xml引入shardingsphere依赖

 <!--MySql读写分离引入依赖--><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>4.0.0-RC1</version></dependency>

4.2 添加配置

application.yml添加MySQL的主从库信息,从库可以多个,这里以一主一从为例。

spring:shardingsphere:datasource:names:master,slave# 主数据源master:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.64.128:3306/reggie?characterEncoding=utf-8&serverTimezone=Asia/Shanghaiusername: rootpassword: czx123# 从数据源slave:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.64.129:3306/reggie?characterEncoding=utf-8&serverTimezone=Asia/Shanghaiusername: rootpassword: czx123masterslave:# 读写分离配置load-balance-algorithm-type: round_robin #轮询# 最终的数据源名称name: dataSource# 主库数据源名称master-data-source-name: master# 从库数据源名称列表,多个逗号分隔slave-data-source-names: slave

添加完重启项目即配置完MySQL主从数据库的读写分离

MySQL的主从配置+SpringBoot的MySQL读写分离配置相关推荐

  1. docker部署django项目、mysql主从搭建、django实现读写分离

    目录 docker部署django项目 1.1 基于python基础镜像 将本地django项目打包并发到远程服务器上 将服务器上的.zip django项目解压 部署的具体流程 1.2 基于dock ...

  2. Mysql一主多从和读写分离配置简记

    Mysql一主多从和读写分离配置简记 标签: mysql数据库服务器class数据库servermanager 2012-05-30 16:44 14981人阅读 评论(1) 收藏 举报  分类: 数 ...

  3. mysql 配置文件在哪_MySQL+MyCat分库分表 读写分离配置

    一. MySQL+MyCat分库分表 1 MyCat简介 java编写的数据库中间件 Mycat运行环境需要JDK. Mycat是中间件,运行在代码应用和MySQL数据库之间的应用. 前身: corb ...

  4. MySQL主从复制与读写分离配置及实操

    MySQL主从复制与读写分离 一.MySQL主从复制 1.复制类型 2.工作过程 二.MySQL读写分离 1.定义 2.存在意义 3.作用时间 4.原理 5.类型 基于程序代码内部实现 基于中间代理层 ...

  5. MySQL使用Mycat实现分库分表-读写分离

    MySQL使用Mycat实现分库分表-读写分离 Mycat Mycat介绍 什么是Mycat? Mycat架构 Mycat核心概念 MyCat主要解决的问题 MyCat对多数据库的支持 Mycat分片 ...

  6. springboot 多数据源 读写分离 AOP方式

    大家好,我是烤鸭: 今天分享springboot读写分离配置.    环境:  springboot  2.1.0.RELEASE          场景说明,目前的需求是 读数据源 * 2 + 写数 ...

  7. ProxySQL 配置详解及读写分离(+GTID)等功能说明2 (完整篇)

    1. 实验环境 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 ...

  8. ProxySQL 配置详解及读写分离(+GTID)等功能说明 (完整篇)

    ProxySQL是灵活强大的MySQL代理层, 是一个能实实在在用在生产环境的MySQL中间件,可以实现读写分离,支持 Query 路由功能,支持动态指定某个 SQL 进行 cache,支持动态加载配 ...

  9. MyCat:开源分布式数据库中间件之数据库分片和读写分离配置

    mycat权威文档指南下载 1.   MyCAT介绍 1.1. 什么是MyCAT? 简单的说,MyCAT就是: 一个彻底开源的,面向企业应用开发的"大数据库集群" 支持事务.ACI ...

最新文章

  1. 【开源方案共享】三维点云快速分割算法
  2. 人工智能技术给教育行业带来哪些主要影响?
  3. bootstrap的两种在input框后面增加一个图标的方式
  4. 非此即彼的逻辑错误_MBA逻辑攻略-逻辑知识大全,快来收藏吧!
  5. Spring MVC 4.1 支持jsonp
  6. oracle从一个表insert语句,Oracle 使用PLSQL 导出 一个表的insert 语句
  7. python简单语法_python的基本语法(一)
  8. django查询mysql 区分大小写_Django+MySQL查询不区分大小写问题
  9. js中删除数组中某一项的方法
  10. you need python_Life is short,you need python!(1)
  11. java常见抛出异常
  12. 基础篇:4.1)规范化:3d工程图纸出图步骤详解
  13. 图贴图软件 ——— Snipaste
  14. m7405d粉盒清零方法_联想打印机 多功能一体机全系列硒鼓清零方法
  15. 小米+线刷+android,小米10 Android 11 Beta1刷机包发布,线刷体验,国内首批
  16. 摩托罗拉linux软件下载,摩托罗拉手机软件驱动下载
  17. CT重建学习笔记(一)
  18. 关闭Win 11自动更新工具
  19. python 画彩虹_python – 在matplotlib中,我如何绘制多色线,如彩虹
  20. 经典Python视频教程

热门文章

  1. nopi 缩小字体填充_我在超市里改字体
  2. eclipse导入工程报错Faceted Project Problem(1 item)
  3. TIA WinCC Professional入门经典(2) 创建画面与联合仿真
  4. 数据库导入表时出错:Importing tables....errors detected(see log page)
  5. IT大学生成长周报 | 第 5 期
  6. Docker定制化Python基础镜像
  7. 如何解决“504 Gateway Time-out”错误
  8. 二进制,CPU,操作系统
  9. Google Dremel 理解
  10. Revit插件建模助手尺寸定位标注和标注避让操作