主从介绍

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.200.50 | mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz | centos7.2-1511 | | client | 192.168.200.40 | | |

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

过程

主节点配置

  1. # .err结尾的文件为错误日志
  2. [root@server ~]# ls -a /data/mysql/
  3. . ibdata1 lnmp.err performance_schema test
  4. .. ib_logfile0 lnmp.pid server.err
  5. auto.cnf ib_logfile1 mysql server.pid
  6. # 修改配置文件 /etc/my.cnf 确定有log_bin 和server_id
  7. [root@server ~]# vim /etc/my.cnf
  8. log_bin=zzx
  9. server_id = 50
  10. [root@server ~]# service mysqld restart
  11. Shutting down MySQL.. SUCCESS!
  12. Starting MySQL. SUCCESS!
  13. # 可以看到多出log_bin名为开头的两个文件,zzx.000001、zzx.index
  14. # 这两个文件为bin_log文件和索引文件 可以参考流程图
  15. [root@server ~]# ls -a /data/mysql/
  16. . ibdata1 lnmp.err performance_schema test
  17. .. ib_logfile0 lnmp.pid server.err zzx.000001
  18. auto.cnf ib_logfile1 mysql server.pid zzx.index
  19. # 备份mysql库(加入环境变量)
  20. [root@lnmp ~]# mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
  21. Warning: Using a password on the command line interface can be insecure.
  22. # 创建一个库保存数据
  23. [root@lnmp ~]# mysql -uroot -p123456 -e "create database kei"
  24. Warning: Using a password on the command line interface can be insecure.
  25. # 将mysql库恢复成新建的库,作为测试数据
  26. [root@lnmp ~]# mysql -uroot -p123456 kei < /tmp/mysql.sql
  27. Warning: Using a password on the command line interface can be insecure.
  28. # 创建主从用户 权限
  29. [root@lnmp ~]# mysql -uroot -p123456
  30. mysql> grant replication slave on *.* to 'repl'@192.168.200.40 identified by '000000';
  31. mysql> flush privileges;
  32. mysql> show master status;
  33. +------------+----------+--------------+------------------+-------------------+
  34. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  35. +------------+----------+--------------+------------------+-------------------+
  36. | zzx.000001 | 410 | | | |
  37. +------------+----------+--------------+------------------+-------------------+

image.png
image.png
image.png

从节点配置

  1. # 从节点修改配置文件,确保有server_id 这个字段,我这里直接克隆主节点
  2. # 字段id不要一样,一般以ip最后一位结尾
  3. [root@client ~]# vim /etc/my.cnf
  4. server_id = 50
  5. [root@lnmp ~]# mysql -uroot -p123456
  6. mysql> stop slave;
  7. # 在主上将文件拷贝到从上,并在从上查看文件大小是否一致
  8. [root@lnmp ~]# scp /tmp/mysql.sql root@192.168.200.40:/tmp/
  9. The authenticity of host '192.168.200.40 (192.168.200.40)' can't be established.
  10. ECDSA key fingerprint is 36:87:11:e2:ab:8e:59:29:54:40:82:00:04:35:c8:88.
  11. Are you sure you want to continue connecting (yes/no)? yes
  12. Warning: Permanently added '192.168.200.40' (ECDSA) to the list of known hosts.
  13. root@192.168.200.40's password:
  14. mysql.sql 100% 683KB 683.3KB/s 00:00
  15. # 创建一个和主一样的库
  16. [root@lnmp ~]# mysql -uroot -p123456 -e "create database kei"
  17. Warning: Using a password on the command line interface can be insecure.
  18. # 将文件内容导入库
  19. [root@lnmp ~]# mysql -uroot -p123456 kei < /tmp/mysql.sql
  20. Warning: Using a password on the command line interface can be insecure.
  21. # 创建连接, 主节点信息,bin_log文件名及大小
  22. mysql> change master to master_host='192.168.200.50',master_user='repl',master_password='000000',master_log_file='zzx.000001',master_log_pos=410;
  23. mysql> start slave;
  24. mysql> show slave status\G
  25. *************************** 1. row ***************************
  26. Slave_IO_State:
  27. Master_Host: 192.168.200.50
  28. Master_User: repl
  29. Master_Port: 3306
  30. Connect_Retry: 60
  31. Master_Log_File: zzx.000001
  32. Read_Master_Log_Pos: 699937
  33. Relay_Log_File: lnmp-relay-bin.000001
  34. Relay_Log_Pos: 4
  35. Relay_Master_Log_File: zzx.000001
  36. Slave_IO_Running: No
  37. Slave_SQL_Running: No
  38. Replicate_Do_DB:
  39. Replicate_Ignore_DB:
  40. Replicate_Do_Table:
  41. Replicate_Ignore_Table:
  42. Replicate_Wild_Do_Table:
  43. Replicate_Wild_Ignore_Table:
  44. Last_Errno: 0
  45. Last_Error:
  46. Skip_Counter: 0
  47. Exec_Master_Log_Pos: 699937
  48. Relay_Log_Space: 120
  49. Until_Condition: None
  50. Until_Log_File:
  51. Until_Log_Pos: 0
  52. Master_SSL_Allowed: No
  53. Master_SSL_CA_File:
  54. Master_SSL_CA_Path:
  55. Master_SSL_Cert:
  56. Master_SSL_Cipher:
  57. Master_SSL_Key:
  58. Seconds_Behind_Master: NULL
  59. Master_SSL_Verify_Server_Cert: No
  60. Last_IO_Errno: 0
  61. Last_IO_Error:
  62. Last_SQL_Errno: 0
  63. Last_SQL_Error:
  64. Replicate_Ignore_Server_Ids:
  65. Master_Server_Id: 0
  66. Master_UUID:
  67. Master_Info_File: /data/mysql/master.info
  68. SQL_Delay: 0
  69. SQL_Remaining_Delay: NULL
  70. Slave_SQL_Running_State:
  71. Master_Retry_Count: 86400
  72. Master_Bind:
  73. Last_IO_Error_Timestamp:
  74. Last_SQL_Error_Timestamp:
  75. Master_SSL_Crl:
  76. Master_SSL_Crlpath:
  77. Retrieved_Gtid_Set:
  78. Executed_Gtid_Set:
  79. Auto_Position: 0
  80. 1 row in set (0.00 sec)

测试

  1. # 主节点创建一个库,测试操作在这个库进行
  2. [root@lnmp ~]# mysql -uroot -p123456
  3. mysql> create database zzx;
  4. mysql> show databases;
  5. +--------------------+
  6. | Database |
  7. +--------------------+
  8. | information_schema |
  9. | kei |
  10. | mysql |
  11. | performance_schema |
  12. | test |
  13. | zzx |
  14. +--------------------+
  15. 6 rows in set (0.00 sec)
  16. # 从节点查看同步信息
  17. [root@lnmp ~]# mysql -uroot -p123456
  18. mysql> show databases;
  19. +--------------------+
  20. | Database |
  21. +--------------------+
  22. | information_schema |
  23. | kei |
  24. | mysql |
  25. | performance_schema |
  26. | test |
  27. | zzx |
  28. +--------------------+
  29. 6 rows in set (0.01 sec)

image.png
image.png

Bash
复制代码