《Mysql


推荐一首歌

  - 《可不可以》张紫豪

好吧,随便从排行榜上找了一首

  第十一章《MySQL的复制特性》

总结

1:复制(Replication) 应用场景?

  - 提高性能 (通过主从特性,把写操作全部放在 Master 端,操作放在 Slave 端,提高整体的 写入/查询 效率)

  - 保存数据 (即使 Master 挂掉,从数据库还保存着数据)

  - 数据分析 (将数据挖掘等耗时吃资源的事,放到 Slave 去做) 

2:MySQL 的复制过程是什么样子的?

  - MySQL 的复制过程默认是异步的,也就是说,Master 和 Slave 是不需要实时连接的,甚至 Slave 可以很长时间都不连接 Master ,需要同步时,在连接 Master(一般没人这么做)

  - 但是面对 读写实时性要求很高的场景,明显就不合适了。

    - 在 MySQL5.0 Google 的 MySQL 团队提交了一个补丁,可以实现半同步的功能。

    - 在 MySQL5.5 之后,官方就自带了半同步功能

4:配置主从

  - 修改 Master 数据库

    - log-bin [必须] 二进制日志

    - server-id [必须] 唯一标识

  - 建立专用的复制用户 [授予权限]

    - grant replication slave on *.* to '用户名'@'允许主机' identified by '密码';

  - 查询 Master 主机 bin-log 日志位置,和开始起始位置

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| bin-log 日志位置  |  起始位置  |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

  - 配置 slave 机器

mysql> change master to
    -> master_host='主机IP',
    -> master_port='主机端口',
    -> master_user='用户名',
    -> master_password='密码',
    -> master_log_file='master主机bin-log日志位置',
    -> master_log_pos='master主机位置';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

  - 配置成功后,查看下 slave 状态

mysql> show slave status G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.27.27
                  Master_User: genee
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 688
               Relay_Log_File: 3c02e653a082-relay-bin.000002
                Relay_Log_Pos: 496
        Relay_Master_Log_File: mysql-bin.000002
             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: 688
              Relay_Log_Space: 710
              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: bb3e01f3-b739-11e8-bc2d-02b2c2845dd0
             Master_Info_File: /var/lib/mysql/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:
           Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

   - Last_IO_Error 如果这个字段没有信息的话,说明是成功的。

  

5:管理常用命令 ( slave )

  - Slave_IO_State 显示 slave 当前的状态

  - Slave_IO_Running 显示 I/O 状态是否正常运行,正常情况下应该在正常运行,除非手动停止或者出现错误。

  - Last_IO_ErrnoLast_SQL_Errno 正常情况下应该输出空值,在出错的时候回提示错误信息。

  - Seconds_Behind_Master 显示当前 slave 和 master 的主从延迟,注意这里的主从延迟指的并不是时间上的延迟

    - 其实是 slave 接受到的 master 节点日志,和已经应用到日志的节点数目。

    - 这么说不好理解,举个例子来说,比如 I/O 接受到的日志写入位置为 3306 ,而 SQL 线程才应用到 1106,中间差了 200 ,Seconds_Behind_Master 的值就会是 200。

    - 但是,此事 master 可能已经写到了 7200 ,所以说,这个值并不是完全精确的代表主从延迟。

    - 这个参数,最好是 0,当然,并不意味着为0了就代表主从的完全同步,还时需要通过其他参数来判断。

  - Master_Log_FileRead_Master_Log_Pos 显示当前读取 master 的二进制文件位置 / 读取位置

  - Relay_Log_FileRelay_Log_Pos 显示当前当前 slave 正在使用处理的中继文件位置 / 读取位置

  - Relay_Master_Log_FileExec_Master_Log_Pos 显示当前当前 slave 正在使用二进制文件位置 / 读取位置

6: Master 管理 slave 状态

  - 在本地搭建了两个 slave 从机,作为试验

mysql> show processlist G;
*************************** 1. row ***************************
     Id: 14
   User: root
   Host: localhost
     db: demo
Command: Query
   Time: 0
  State: starting
   Info: show processlist
*************************** 2. row ***************************
     Id: 21
   User: genee
   Host: 172.17.42.3:60222
     db: NULL
Command: Binlog Dump
   Time: 21932
  State: Master has sent all binlog to slave; waiting for more updates
   Info: NULL
*************************** 3. row ***************************
     Id: 23
   User: genee
   Host: 172.17.42.4:43398
     db: NULL
Command: Binlog Dump
   Time: 116
  State: Master has sent all binlog to slave; waiting for more updates
   Info: NULL
3 rows in set (0.00 sec)

  - 通过 show processlist 可以在 Master 主机上获取全部的 Slave 从机信息,包括他们的连接状态等

  - 但是如果我们只想获取 slave 的节点消息时,直接可以使用 show slave hosts 来获取全部的状态信息。

mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID                           |
+-----------+------+------+-----------+--------------------------------------+
|      9999 |      | 3306 |         1 | a34fad42-b7ca-11e8-a457-0242ac112a04 |
|        11 |      | 3006 |         1 | 898e0435-b753-11e8-99c5-0242ac112a03 |
+-----------+------+------+-----------+--------------------------------------+
2 rows in set (0.00 sec)

 7:复制特性的原理?

  - 复制的核心,就是基于 Master 节点对数据库的各项变更机制。

  - Slave 只要连接到 Master 就会不断的去请求 Master 的二进制文件,拿到二进制文件中,解析之后再本地处理。(这个过程中 Slave 是主导地位拉取日志)

7:MySQL的复制模式?(目前只做了解,之后如果有机会的话会详细说)

  - 基于语句复制 (SBR) 基于实际执行的SQL语句复制方案。[S]tatement [B]ase [R]eplication

  - 基于记录复制 (RBR) 基于修改列的复制模式。[R]ow [B]ase [R]eplication

  - 混合复制模式 (MBR) 。 [M]ixed [B]ase [R]eplication

  - 可以通过 查看 binlog_format 参数查看

mysql> show global variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)

    - statment 基于语句复制

    - row 基于行复制

    - mixed 混合复制

8:什么是中继日志?

  - 从文件类型上来看,中继日志(Relay Log) 和二进制日志及其相似,区别是逻辑上的。

    - 二进制日志用于保存自身节点产生的数据。

    - 中继日志则是保存接受其他节点的事件。

      - 可以在 my.cnf 中指定中继日志的位置和索引位置(不要随意更改 日志位置/和主机名称 -> 会导致文件位置和索引位置不符合报错)

      - relay-log=‘中继日志位置’

      - relay-log-index=‘中继日志索引位置’

  - MySQL 会在下列情况下 创建新的中继日志文件,并更新其索引

    - 启动 Slave 节点时候。

    - 日志文件达到最大值

      - 由 max_relay_log_size 和 控制。(如果没有 max_relay_log_size 则指定 max_binlog_size 来确定大小)

9:部署联级 Slave 增强性能

  - 总的来说,如果只有 Master 为全部的 Slave 提供节点服务,那么本身就会增加 Master 的负载

  - 所以,我们也可以让 Slave 承担起中转节点的作用。

    - 但是 Slave 只有中继日志,没有二进制日志怎么办

    - 设置 log-slave-updates 参数就好啦

  - 

10:复制环境中的主从切换

  - 在 MySQL 中 并没有很严格的定义 Master 和 Slave 定义。

  - 也就是说,在 MySQL 的复制环境中,随时都可以通过 change master to 来改变。随意性非常强

  - 主从的切换也就是如此,也是用过改变 Master 的状态来改变

原文地址:https://www.cnblogs.com/25-lH/p/9638755.html