Mysql5.7主从复制配置

准备两台服务器,一主一从。

IP 服务ID 角色
192.168.1.110 110 master
192.168.1.111 111 slave

Mysql主从软件版本尽量保持一致

配置主数据

修改配置文件

  1. > vim /etc/my.cnf
  2. [mysqld]
  3. basedir=/usr/local/mysql
  4. datadir=/usr/local/mysql/data
  5. #要给从库同步的库
  6. binlog-do-db=rumenz
  7. #不给从机同步的库(多个写多行)
  8. binlog-ignore-db=mysql
  9. binlog-ignore-db=information_schema
  10. binlog-ifnore-db=performance_schema
  11. binlog-ignore-db=sys
  12. log-error=/usr/local/mysql/data/error.log
  13. #自动清理 7 天前的log文件,可根据需要修改
  14. expire_logs_days=7
  15. #启动binlog
  16. log-bin=mysql-bin
  17. 服务器唯一ID,一般取IP最后一段,必填
  18. server-id=110

重启Mysql

  1. > systemctl restart mysql

建立账户并授权slave

  1. > grant file,replication slave on *.* to 'slave'@'%' identified by '123456';
  2. > flush privileges;
  3. > show master status;

img

配置从库

将主库的所有数据拷贝一份,保证主从数据一致

修改my.cnf配置文件

  1. > vim /etc.my.cnf
  2. [mysqld]
  3. basedir=/usr/local/mysql
  4. datadir=/usr/local/mysql/data
  5. server-id=111
  6. #加上以下参数可以避免更新不及时,SLAVE 重启后导致的主从复制出错。
  7. read_only=1
  8. master_info_repository=TABLE
  9. relay_log_info_repository=TABLE
  10. log-error=/usr/local/mysql/data/error.log

在从库的服务器上关联主服务器

  1. > stop slave;
  2. > change master to
  3. > master_host='192.168.1.110',
  4. > master_port=3306,
  5. > master_user='slave', #备份用户名
  6. > master_password='123456',#密码
  7. > master_log_file='mysql3306-bin.000001', #上面截图,且要与master的参数一致
  8. > master_log_pos=1155; #上面截图,且要与master的参数一致
  9. > start slave;

master_log_filemaster_log_pos的值要和查看主库的内容要一致,一个指的是二进制文件,一个指的是二进制日志的位置

查看从库状态

  1. show slave status \G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: localhost
  5. Master_User: slave
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql3306-bin.000001
  9. Read_Master_Log_Pos: 604
  10. Relay_Log_File: BOTCHNFOSDAF06-relay-bin.000002
  11. Relay_Log_Pos: 324
  12. Relay_Master_Log_File: mysql3306-bin.000001
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 604
  25. Relay_Log_Space: 540
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 110
  43. Master_UUID: b7f6033a-c74e-11eb-a1ab-000c291b6073
  44. Master_Info_File: mysql.slave_master_info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0
  57. Replicate_Rewrite_DB:
  58. Channel_Name:
  59. Master_TLS_Version:
  60. 1 row in set (0.00 sec)

返回笔记列表
入门小站