前言

随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求。此时数据库集群就很好的解决了这个问题了。采用MySQL分布式集群,能够搭建一个高并发、负载均衡的集群服务器。在此之前我们必须要保证每台MySQL服务器里的数据同步。数据同步我们可以通过MySQL内部配置就可以轻松完成,主要有主从复制和主主复制。

演示:实现mysql主从复制

配置主节点

1.修改主节点配置文件

 ...
#binlog54 #binlog_format = STATEMENT55 binlog_format = row         <<主从节点保持一致,要么都是row,要么都是mixed56 server-id = 1003306         <<主从节点的server_id必须保证不能一致57 log-bin = /data/mysql/mysql3306/logs/mysql-bin  <<定义二进制日志文件保存文件和格式58 binlog_cache_size = 4M59 max_binlog_size = 256M60 max_binlog_cache_size = 1M61 sync_binlog = 062 expire_logs_days = 1063 #procedure 64 log_bin_trust_function_creators=165
...

2.重启mysql

[root@ken home]# mysqladmin -uroot -pxx shutdown
[root@ken home]# mysqld &
[root@ken home]# ss -tnl | grep 3306
LISTEN     0      70          :::3306                    :::*   

3.主节点上授权具有复制权限的用户

[root@ken home]# mysql -uroot -p                  <<登录mysql
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.23-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> grant replication slave on *.* to ken@'%' identified by 'xx'; <<创建ken用户,并设置密码xx

配置从节点

1.修改从节点和主机点时间同步

可以使用NTP服务或者date直接设置。此步省略。

2.修改丛节点配置文件

 ...
#binlog54 #binlog_format = STATEMENT55 binlog_format = row         <<主从节点保持一致,要么都是row,要么都是mixed56 server-id = 1003307        <<主从节点的server_id必须保证不能一致57 log-bin = /data/mysql/mysql3306/logs/mysql-bin  <<定义二进制日志文件保存文件和格式58 binlog_cache_size = 4M59 max_binlog_size = 256M60 max_binlog_cache_size = 1M61 sync_binlog = 062 expire_logs_days = 1063 #procedure 64 log_bin_trust_function_creators=165
...

3.重启mysql

[root@ken home]# mysqladmin -uroot -pxx shutdown
[root@ken home]# mysqld &
[root@ken home]# ss -tnl | grep 3306
LISTEN     0      70          :::3306                    :::*   

4.连接主服务器

链接主服务器命令详解

 1 格式:CHANGE MASTER TO 选项
 2 选项:
 3 MASTER_HOST = 'host_name'         指定主服务的ip或者主机名
 4 MASTER_USER = 'user_name'         指定主服务器的用户名
 5 MASTER_PASSWORD = 'password'      指定用户名的密码
 6 MASTER_PORT = port_num            指定连接的端口,默认是3306
 7 MASTER_CONNECT_RETRY = interval   指定连接失败的时候的重试间隔时间

MySQL [(none)]> change master to master_host='10.220.5.137',master_user='root',master_password='xx'

5.启动 从节点

MySQL [(none)]> start salve;

6.查看从节点链接状态

主要看IO以及SQL线程是否启动

MySQL [(none)]> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 10.220.5.137Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000003Read_Master_Log_Pos: 928Relay_Log_File: relay-bin.000005Relay_Log_Pos: 1141Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: Yes             <<<IO线程启动成功Slave_SQL_Running: Yes             <<<SQL线程启动成功Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 928Relay_Log_Space: 1508Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1003306Master_UUID: e2357094-d6d9-11e8-ba06-000c292218ecMaster_Info_File: /data/mysql/mysql3306/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: 2a88f089-cd97-11e8-a862-000c29492f7b:1-45Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)

7.测试

在主服务器端建库建表,查看从服务器端是否同步

主服务器建库建表

mysql> create database ken;                  <<创建数据库ken
Query OK, 1 row affected (0.00 sec)mysql> use ken;                              <<进入数据库
Database changed
mysql> create table ken1 as select * from jobs.teachers; <<创建ken1表
Query OK, 6 rows affected (0.04 sec)
Records: 6  Duplicates: 0  Warnings: 0mysql> select * from ken1;            <<查看ken1表数据
+-----+------------+-----+--------+
| TID | Name       | Age | Gender |
+-----+------------+-----+--------+
|   1 | Song Jiang |  45 | M      |
|   2 | ken        |  25 | M      |
|   3 | ken        |  45 | M      |
|   4 | hah        |  67 | M      |
|   5 | ken2       |  76 | M      |
|   6 | ken2       |  76 | M      |
+-----+------------+-----+--------+
6 rows in set (0.00 sec)

8.检查从服务器

MySQL [(none)]> show databases;    <<查看数据库,已经同步过来ken
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jobs               |
| ken                |
| liu                |
| mysql              |
| performance_schema |
| sys                |
| test               |
| test5              |
+--------------------+
9 rows in set (0.00 sec)MySQL [(none)]> use ken;         <<进入到ken数据库中
Database changed
MySQL [ken]> show tables;        <<查看库中的表,可以看到ken1表
+---------------+
| Tables_in_ken |
+---------------+
| ken1          |
+---------------+
1 row in set (0.00 sec)MySQL [ken]> select * from ken1;  <<检查ken1表中的数据完整
+-----+------------+-----+--------+
| TID | Name       | Age | Gender |
+-----+------------+-----+--------+
|   1 | Song Jiang |  45 | M      |
|   2 | ken        |  25 | M      |
|   3 | ken        |  45 | M      |
|   4 | hah        |  67 | M      |
|   5 | ken2       |  76 | M      |
|   6 | ken2       |  76 | M      |
+-----+------------+-----+--------+
6 rows in set (0.00 sec)

到这里主从复制的演示就结束了,接下来再演示一个慢同步的架构

演示:实现mysql慢同步演示

半同步:N多个从节点中,只要任意一个从节点给主节点返回信息告知自己已经将数据存储成功,那么主节点会立刻给客户端反向执行结果信息。

配置主节点

1.安装主模块

mysql> install plugin rpl_semi_sync_master soname "semisync_master.so";

2.查看主端有关semi的变量

mysql> show global variables like '%semi%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled              | OFF         |
| rpl_semi_sync_master_timeout              | 10000      |
| rpl_semi_sync_master_trace_level          | 32         |
| rpl_semi_sync_master_wait_for_slave_count | 1          |
| rpl_semi_sync_master_wait_no_slave        | ON         |
| rpl_semi_sync_master_wait_point           | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.02 sec)

3.启动rpl_semi_sync_master_enabled

mysql> set global  rpl_semi_sync_master_enabled=1;

4.安装从模块

mysql> install plugin rpl_semi_sync_slave soname "semisync_slave.so";

5.启动rpl_semi_sync_slave_enabled

mysql> set global  rpl_semi_sync_slave_enabled=1;

6.重启从端slave

MySQL [ken]> stop slave;
MySQL [ken]> start slave;

7.查看IO/SQL启动状态

MySQL [ken]> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 10.220.5.137Master_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 154Relay_Log_File: relay-bin.000003Relay_Log_Pos: 367Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: Yes            <<IO线程启动成功Slave_SQL_Running: Yes            <<SQL线程启动成功Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 568Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1003306Master_UUID: e2357094-d6d9-11e8-ba06-000c292218ecMaster_Info_File: /data/mysql/mysql3306/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: 2a88f089-cd97-11e8-a862-000c29492f7b:1-45Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)

8.测试

略。详情见上主从复制。

转载于:https://www.cnblogs.com/kenken2018/p/9839501.html

MySQL系列详解六:MySQL主从复制/半同步演示-技术流ken相关推荐

  1. mysql 流复制_MySQL系列详解六:MySQL主从复制/半同步演示-技术流ken

    前言 随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求.此时数据库集群就很好的解决了这个问题了.采用MySQL分布式集群,能够搭建一个高并发.负载均衡的集群服务器.在 ...

  2. mysql 事务排他锁_[数据库事务与锁]详解六: MySQL中的共享锁与排他锁

    注明: 本文转载自http://www.hollischuang.com/archives/923 在MySQL中的行级锁,表级锁,页级锁中介绍过,行级锁是Mysql中锁定粒度最细的一种锁,行级锁能大 ...

  3. php mysql 操作函数_PHP操作mysql函数详解,mysql和php交互函数

    1. 建立和关闭连接1) mysql_connect() resource mysql_connect([string hostname [:port][:/path/to/socket][,stri ...

  4. 史上最简单MySQL教程详解(进阶篇)之存储过程(一)

    史上最简单MySQL教程详解(进阶篇)之存储过程(一) 史上最简单MySQL教程详解(进阶篇)之存储过程(一) 什么是存储过程 存储过程的作用 如何使用存储过程 创建存储过程 DELIMITER改变分 ...

  5. 十六、MySQL 视图详解

    文章目录 一.常见的数据库对象 二.视图概述 2.1 为什么要使用视图? 2.2 视图的理解 2.3 视图的作用 三.视图常用操作 3.1 查看创建视图的权限 3.2 创建视图 3.3 查看视图 3. ...

  6. mysql 实例复制_MYSQL教程MySQL 复制详解及简单实例

    <MysqL教程MysqL 复制详解及简单实例>要点: 本文介绍了MysqL教程MysqL 复制详解及简单实例,希望对您有用.如果有疑问,可以联系我们. MysqL 复制详解及简单实例 主 ...

  7. mysql explain详解_数据库mysql(1)——B+TREE索引原理

    一.B+Tree索引详解 1.什么是索引? 索引:加速查询的数据结构. 2.索引常见数据结构: #1.顺序查找: 最基本的查询算法-复杂度O(n),大数据量此算法效率糟糕. #2.二叉树查找(bina ...

  8. MySQL Explain详解,分析语句为何运行慢

    MySQL Explain详解 在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain这个命令来查看一个这些SQL语 ...

  9. 详解:MySQL数据库的权限管理和运维实操

    详解:MySQL数据库的权限管理 一.MYSQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你权利以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执行updat ...

最新文章

  1. couchdb 垂直权限绕过漏洞(cve-2017-12635)
  2. tcp/ip 协议栈Linux内核源码分析15 udp套接字接收流程二
  3. 【ActiveMQ】消息生产者自动注入报错:Could not autowire. No beans of 'JmsMessagingTemplate' type found...
  4. html标签object和embed,html标签object和embed的区别
  5. android signalr 自动重连,.net-何时在signalR中重新连接?
  6. java循环的嵌套执行
  7. Intel 64/x86_64/IA-32/x86处理器 - SIMD指令集 - SSE扩展(9) - 64位整型指令(MMX指令集扩展)
  8. oracle全文检索 分区表,oracle全文检索
  9. Web前端工作笔记005---浏览器内核介绍
  10. JMeter 检查点之响应断言(Response Assertion)
  11. Rsync服务及搭建备份服务器
  12. 差点被开除:一次订单号重复的事故
  13. 冰点密码破解 — 强悍的调试器 SOFTICE 1
  14. 存算一体——后摩尔时代的AI芯片架构
  15. java注册机软件_myeclipse 8.5 注册机
  16. 如何制作伪原创视频?呆头鹅批量视频剪辑软件一键处理10万个视频
  17. 太极图正确画法_太极图唯一正确的画法
  18. 亚马逊AWS使用计费问题
  19. 7-4 出圈游戏 (c 语言)PTA
  20. Linux内核(十五)sysrq 详解 I —— 使用手册

热门文章

  1. VGGNet论文翻译-Very Deep Convolutional Networks for Large-Scale Image Recognition
  2. 【问题】父套子时,‘阻止子元素的外边距传递给父元素’与闭合浮动
  3. puppet连载八:linux优化模块
  4. 计网学习第一章:概述
  5. 趋势:“无人化”的未来,这些事情你需要知道!
  6. LeetCode刷题(39)--Set Matrix Zeros
  7. 字段 密码有效期_你知道Linux中用户们的密码藏在哪儿吗?
  8. 用计算机术语写诗,用电脑软件写诗——电子诗人
  9. 年夜饭之 -- 辣椒炒圣子
  10. java 代码压缩javascript_利用Java来压缩 JavaScript 代码详解