1.个人对于主从的理解

当数据较多的时候,搞一台额外的数据库当做从(副)节点。当主服务器损坏,从服务器可以顶替,并且从服务器也被用来读取数据。

2.准备

准备两台主机,均停止防火墙,selinux并且完成二进制免编译mysql的安装。

主机名 IP Mysql版本
server(主节点) 192.168.148.156 mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
other mysql(从节点) 192.168.148.171 (版本要一致)

3.主节点操作

server(192.168.148.156) 进行操作

  1. #vim /etc/my.cnf //修改配置文件
  2. log_bin=liao //可自定义
  3. server_id = 56 //(这里也可以自定义,但是不能与从节点相同)

Mysql主从配置 - 图1

  1. #Mysql //进入数据库
  2. >grant replication slave on *.* to 'liao'@192.168.148.171 identified by '000000'; //创建主从用户,给予权限
  3. >flush privileges; //刷新权限
  4. >show master status; //查看server节点状态
  • ‘liao’:用户名
  • ‘000000’:密码
  • @后跟的是从节点IP,因为到时候要在从节点的mysql中登录。

Mysql主从配置 - 图2

4.从节点操作

othermysql(192.168.148.171) 进行操作

  1. #vim /etc/my.cnf //修改配置文件
  2. server_id = 10 (别和主节点相同即可)
  3. 这里的 log_bin 可以不用配置
  4. #mysql //进入数据库
  5. >stop slave; //关闭主从同步服务
  6. >change master to master_host='192.168.148.156',master_user='liao',master_password='000000',master_log_file='liao1.000006',master_log_pos=1384; //创建连接,主节点信息,bin_log文件以及大小
  7. >start slave; //开启主从同步服务
  8. >show slave status\G; //查看主从同步服务配置
  • master_host:主节点IP
  • master_user:主节点用户名
  • master_password:主节点用户密码
  • master_log_file:log文件,show master status中的第一个文件
  • master_log_pos:log的位置号码

Mysql主从配置 - 图3

  1. *************************** 1. row ***************************
  2. Slave_IO_State: Waiting for master to send event
  3. Master_Host: 192.168.148.156
  4. Master_User: liao
  5. Master_Port: 3306
  6. Connect_Retry: 60
  7. Master_Log_File: liao1.000006
  8. Read_Master_Log_Pos: 1384
  9. Relay_Log_File: localhost-relay-bin.000002
  10. Relay_Log_Pos: 279
  11. Relay_Master_Log_File: liao1.000006
  12. Slave_IO_Running: Yes #这里要为yes
  13. Slave_SQL_Running: Yes #这里要为yes
  14. Replicate_Do_DB:
  15. Replicate_Ignore_DB:
  16. Replicate_Do_Table:
  17. Replicate_Ignore_Table:
  18. Replicate_Wild_Do_Table:
  19. Replicate_Wild_Ignore_Table:
  20. Last_Errno: 0
  21. Last_Error:
  22. Skip_Counter: 0
  23. Exec_Master_Log_Pos: 1384
  24. Relay_Log_Space: 456
  25. Until_Condition: None
  26. Until_Log_File:
  27. Until_Log_Pos: 0
  28. Master_SSL_Allowed: No
  29. Master_SSL_CA_File:
  30. Master_SSL_CA_Path:
  31. Master_SSL_Cert:
  32. Master_SSL_Cipher:
  33. Master_SSL_Key:
  34. Seconds_Behind_Master: 0
  35. Master_SSL_Verify_Server_Cert: No
  36. Last_IO_Errno: 0 #如果有问题,在这里看报错
  37. Last_IO_Error:
  38. Last_SQL_Errno: 0 #如果有问题,在这里看报错
  39. Last_SQL_Error:
  40. Replicate_Ignore_Server_Ids:
  41. Master_Server_Id: 56
  42. Master_UUID: a79ad7b9-1cf5-11ec-afb2-000c294a072c
  43. Master_Info_File: /data/mysql/master.info
  44. SQL_Delay: 0
  45. SQL_Remaining_Delay: NULL
  46. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  47. Master_Retry_Count: 86400
  48. Master_Bind:
  49. Last_IO_Error_Timestamp:
  50. Last_SQL_Error_Timestamp:
  51. Master_SSL_Crl:
  52. Master_SSL_Crlpath:
  53. Retrieved_Gtid_Set:
  54. Executed_Gtid_Set:
  55. Auto_Position: 0
  56. 1 row in set (0.00 sec)
  57. ERROR:
  58. No query specified

5.测试

server(192.168.148.156) 进行操作

Mysql主从配置 - 图4