主从介绍

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

  • 主服务器上有一个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.30

两台虚拟机均安装完成二进制免编译MySQL,关闭防火墙
配置过程

主节点操作

  1. # .err结尾的文件为错误日志
  2. [root@server ~]# ls -a /data/mysql/
  3. . auto.cnf ib_logfile0 mysql server.err test
  4. .. ibdata1 ib_logfile1 performance_schema server.pid
  5. # 修改配置文件 /etc/my.cnf 确定有log_bin 和server_id
  6. [root@server ~]# vim /etc/my.cnf
  7. log_bin=wsw
  8. server_id = 41
  9. [root@server ~]# service mysqld restart
  10. Shutting down MySQL.. SUCCESS!
  11. Starting MySQL. SUCCESS!
  12. # 可以看到多出log_bin名为开头的两个文件,wsw.000001、wsw.index
  13. # 这两个文件为bin_log文件和索引文件 可以参考流程图
  14. [root@server ~]# ls -a /data/mysql/
  15. . ibdata1 mysql server.pid wsw.index
  16. .. ib_logfile0 performance_schema test
  17. auto.cnf ib_logfile1 server.err lhn.000001
  18. # 创建主从用户 权限
  19. mysql> grant replication slave on *.* to 'repl'@192.168.200.42 identified by '000000';
  20. Query OK, 0 rows affected (0.00 sec)
  21. # 刷新权限
  22. mysql> flush privileges;
  23. Query OK, 0 rows affected (0.00 sec)
  24. # 查看server节点状态
  25. mysql> show master status ;
  26. +------------+----------+--------------+------------------+-------------------+
  27. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  28. +------------+----------+--------------+------------------+-------------------+
  29. | lhn.000001 | 410 | | | |
  30. +------------+----------+--------------+------------------+-------------------+
  31. 1 row in set (0.00 sec)

从节点配置

  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.200.41',master_user='repl',master_password='000000',master_log_file='wsw.000001',master_log_pos=410;
  9. Query OK, 0 rows affected, 2 warnings (0.04 sec)
  10. mysql> start slave;
  11. Query OK, 0 rows affected (0.00 sec)
  12. mysql> show slave status\G;
  13. *************************** 1. row ***************************
  14. Slave_IO_State: Waiting for master to send event
  15. Master_Host: 192.168.200.41
  16. Master_User: repl
  17. Master_Port: 3306
  18. Connect_Retry: 60
  19. Master_Log_File: lhn.000001
  20. Read_Master_Log_Pos: 410
  21. Relay_Log_File: client-relay-bin.000002
  22. Relay_Log_Pos: 277
  23. Relay_Master_Log_File: lhn.000001
  24. Slave_IO_Running: Yes
  25. Slave_SQL_Running: Yes
  26. Replicate_Do_DB: # 同步那些库 这些配置都是可以写在著配置文件的my.cnf
  27. Replicate_Ignore_DB: # 不同步哪些库
  28. Replicate_Do_Table: # 同步哪些表
  29. Replicate_Ignore_Table:
  30. Replicate_Wild_Do_Table: # 同步哪个库.表 常用
  31. Replicate_Wild_Ignore_Table: # 忽略哪个库的哪个表
  32. Last_Errno: 0 # 错误信息
  33. Last_Error:
  34. Skip_Counter: 0
  35. Exec_Master_Log_Pos: 410
  36. Relay_Log_Space: 451
  37. Until_Condition: None
  38. Until_Log_File:
  39. Until_Log_Pos: 0
  40. Master_SSL_Allowed: No
  41. Master_SSL_CA_File:
  42. Master_SSL_CA_Path:
  43. Master_SSL_Cert:
  44. Master_SSL_Cipher:
  45. Master_SSL_Key:
  46. Seconds_Behind_Master: 0
  47. Master_SSL_Verify_Server_Cert: No
  48. Last_IO_Errno: 0 # 线程错误信息
  49. Last_IO_Error:
  50. Last_SQL_Errno: 0
  51. Last_SQL_Error:
  52. Replicate_Ignore_Server_Ids:
  53. Master_Server_Id: 41
  54. Master_UUID: 197dfb61-1310-11ec-af29-000c29b5c42c
  55. Master_Info_File: /data/mysql/master.info
  56. SQL_Delay: 0
  57. SQL_Remaining_Delay: NULL
  58. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  59. Master_Retry_Count: 86400
  60. Master_Bind:
  61. Last_IO_Error_Timestamp:
  62. Last_SQL_Error_Timestamp:
  63. Master_SSL_Crl:
  64. Master_SSL_Crlpath:
  65. Retrieved_Gtid_Set:
  66. Executed_Gtid_Set:
  67. Auto_Position: 0
  68. 1 row in set (0.00 sec)
  69. ERROR:
  70. No query specified

测试

  1. # 主节点创建一个库,测试操作在这个库进行
  2. mysql> create database wsw;
  3. Query OK, 1 row affected (0.00 sec)
  4. # 从节点查看同步信息
  5. mysql> show databases;
  6. +--------------------+
  7. | Database |
  8. +--------------------+
  9. | information_schema |
  10. | mysql |
  11. | performance_schema |
  12. | test |
  13. | lhn |
  14. +--------------------+
  15. 5 rows in set (0.00 sec)
  16. # 000001文件大小为主节点操作命令组成
  17. # 可以通过bin_log文件恢复数据

注意
不可以在从节点对数据库进行写入删除操作