主从介绍

MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的。
MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
主从过程大致有3个步骤:
1)主将更改操作记录到binlog里
2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里中继日志
3)从根据relaylog里面的sql语句按顺序执行
MySQL主从配置 - 图1

  • 主服务器上有一个log dump线程,用来和从的I/O线程传递binlog;
  • 从服务器上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地。

    使用场景

    1、数据的备份。
    单纯的读。当主服务器损坏,从服务器可以顶替
    2、主从同时被使用
    当主的服务器压力过大,从节点也被用来读数据。

    配置

    准备

    | 主机名 | IP | MySQL版本 | centos版本 | | —- | —- | —- | —- | | server | 192.168.100.10 | mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz | centos7.2-1511 | | client | 192.168.100.20 | | |

两台虚拟机均安装完成二进制免编译MySQL,关闭防火墙
参考:https://www.yuque.com/docs/share/525f338c-a471-4aa3-be85-da0f6d3919da?# 《二进制免编译安装MySQL》

过程

主节点操作

  1. # .err结尾的文件为错误日志
  2. [root@server ~]# ls -a /data/mysql/
  3. . ibdata1 localhost.localdomain.err server.err
  4. .. ib_logfile0 mysql server.pid
  5. auto.cnf ib_logfile1 performance_schema test
  6. [root@server ~]#
  7. # 修改配置文件 /etc/my.cnf 确定有log_bin 和server_id
  8. [root@server ~]# vim /etc/my.cnf
  9. log_bin=sxb
  10. server_id = 41
  11. [root@server ~]# service mysqld restart
  12. Shutting down MySQL.. SUCCESS!
  13. Starting MySQL. SUCCESS!
  14. # 可以看到多出log_bin名为开头的两个文件,sxb.000001、sxb.index
  15. # 这两个文件为bin_log文件和索引文件 可以参考流程图
  16. [root@server ~]# ls -a /data/mysql/
  17. . ibdata1 localhost.localdomain.err server.err sxb.index
  18. .. ib_logfile0 mysql server.pid test
  19. auto.cnf ib_logfile1 performance_schema sxb.000001
  20. [root@server ~]#
  21. # 创建主从用户 权限
  22. [root@server ~]# mysql -uroot -p12456
  23. Warning: Using a password on the command line interface can be insecure.
  24. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  25. [root@server ~]# mysql -uroot -p123456
  26. Warning: Using a password on the command line interface can be insecure.
  27. Welcome to the MySQL monitor. Commands end with ; or \g.
  28. Your MySQL connection id is 2
  29. Server version: 5.6.47-log MySQL Community Server (GPL)
  30. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  31. Oracle is a registered trademark of Oracle Corporation and/or its
  32. affiliates. Other names may be trademarks of their respective
  33. owners.
  34. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  35. mysql>
  36. # 创建主从用户 权限
  37. mysql> grant replication slave on *.* to 'repl'@192.168.100.20 identified by '123456';
  38. Query OK, 0 rows affected (0.00 sec)
  39. # 刷新权限
  40. mysql> flush privileges;
  41. Query OK, 0 rows affected (0.00 sec)
  42. # 查看server节点状态
  43. mysql> show master status ;
  44. +------------+----------+--------------+------------------+-------------------+
  45. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  46. +------------+----------+--------------+------------------+-------------------+
  47. | sxb.000001 | 410 | | | |
  48. +------------+----------+--------------+------------------+-------------------+
  49. 1 row in set (0.00 sec)
  50. mysql>

从节点配置

  1. # 从节点修改配置文件,确保有server_id 这个字段,
  2. # 字段id不要一样,一般以ip最后一位结尾
  3. [root@client ~]# vim /etc/my.cnf
  4. server_id = 42
  5. mysql> stop slave;
  6. Query OK, 0 rows affected, 1 warning (0.00 sec)
  7. # 创建连接, 主节点信息,bin_log文件名及大小
  8. mysql> change master to master_host='192.168.100.10',master_user='repl',master_password='123456',master_log_file='sxb.000001',master_log_pos=410;
  9. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  10. mysql>
  11. mysql> start slave;
  12. Query OK, 0 rows affected (0.00 sec)
  13. mysql>
  14. mysql> show slave status\G;
  15. *************************** 1. row ***************************
  16. Slave_IO_State: Waiting for master to send event
  17. Master_Host: 192.168.100.10
  18. Master_User: repl
  19. Master_Port: 3306
  20. Connect_Retry: 60
  21. Master_Log_File: sxb.000002
  22. Read_Master_Log_Pos: 120
  23. Relay_Log_File: server-relay-bin.000004
  24. Relay_Log_Pos: 277
  25. Relay_Master_Log_File: sxb.000002
  26. Slave_IO_Running: Yes
  27. Slave_SQL_Running: Yes
  28. Replicate_Do_DB:
  29. Replicate_Ignore_DB:
  30. Replicate_Do_Table:
  31. Replicate_Ignore_Table:
  32. Replicate_Wild_Do_Table:
  33. Replicate_Wild_Ignore_Table:
  34. Last_Errno: 0
  35. Last_Error:
  36. Skip_Counter: 0
  37. Exec_Master_Log_Pos: 120
  38. Relay_Log_Space: 608
  39. Until_Condition: None
  40. Until_Log_File:
  41. Until_Log_Pos: 0
  42. Master_SSL_Allowed: No
  43. Master_SSL_CA_File:
  44. Master_SSL_CA_Path:
  45. Master_SSL_Cert:
  46. Master_SSL_Cipher:
  47. Master_SSL_Key:
  48. Seconds_Behind_Master: 0
  49. Master_SSL_Verify_Server_Cert: No
  50. Last_IO_Errno: 0
  51. Last_IO_Error:
  52. Last_SQL_Errno: 0
  53. Last_SQL_Error:
  54. Replicate_Ignore_Server_Ids:
  55. Master_Server_Id: 29
  56. Master_UUID: 797e5ab1-1508-11ec-bc02-000c29df5533
  57. Master_Info_File: /data/mysql/master.info
  58. SQL_Delay: 0
  59. SQL_Remaining_Delay: NULL
  60. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  61. Master_Retry_Count: 86400
  62. Master_Bind:
  63. Last_IO_Error_Timestamp:
  64. Last_SQL_Error_Timestamp:
  65. Master_SSL_Crl:
  66. Master_SSL_Crlpath:
  67. Retrieved_Gtid_Set:
  68. Executed_Gtid_Set:
  69. Auto_Position: 0
  70. 1 row in set (0.00 sec)
  71. ERROR:
  72. No query specified
  73. mysql>

测试

  1. # 主节点创建一个库,测试操作在这个库进行
  2. mysql> show databases;
  3. +--------------------+
  4. | Database |
  5. +--------------------+
  6. | information_schema |
  7. | mysql |
  8. | performance_schema |
  9. | test |
  10. +--------------------+
  11. 4 rows in set (0.00 sec)
  12. mysql> create database sxb;
  13. Query OK, 1 row affected (0.00 sec)
  14. mysql> show databases;
  15. +--------------------+
  16. | Database |
  17. +--------------------+
  18. | information_schema |
  19. | mysql |
  20. | performance_schema |
  21. | sxb |
  22. | test |
  23. +--------------------+
  24. 5 rows in set (0.00 sec)
  25. mysql>
  26. # 从节点查看同步信息
  27. mysql> show databases;
  28. +--------------------+
  29. | Database |
  30. +--------------------+
  31. | information_schema |
  32. | mysql |
  33. | performance_schema |
  34. | sxb |
  35. | test |
  36. +--------------------+
  37. 5 rows in set (0.01 sec)
  38. mysql>
  39. # 000001文件大小为主节点操作命令组成
  40. # 可以通过bin_log文件恢复数据

注意

1、不要在从节点对数据库进行写入删除操作

拓展

读写分离
分库分表
不停机进行主从