mysql 主从复制

2018-10-10 04:16:05   Mysql

  MySQL主从复制  

基础信息

主: 192.168.0.1

从: 192.168.0.2

备份

复制 两台服务器的/etc/mysql/mysql.conf.d/mysqld.cnf 到 /etc/mysql/mysql.conf.d/mysqld.cnf.bak 便于数据恢复

主服务器配置

mysqld.cnf

  1. # bind-address = 127.0.0.1
  2. server-id = 1
  3. log_bin = /var/log/mysql/mysql-bin.log
  4. 注释掉bind-address127.0.0.1,允许远程连接,打开server-idlog二进制文件的注释

设置好 记得重启 mysql服务
centos服务器

  1. service mysqld restart

ubuntu服务器

  1. service mysql restart

创建从库的登录用户,并且授予slave权限

  1. mysql> grant replication slave on *.* to 'iwhao'@'192.168.0.2' identified by '123456';

刷新权限

  1. mysql> FLUSH PRIVILEGES;
  2. mysql> show master status;

获取mysql-bin的file信息和position信息 例如: mysql-bin.000030 154

从服务器配置

  1. server-id = 2
  2. // 默认不配置的话 是 复制全部 以下配置为 只复制 buildings单库
  3. replicate-do-db=buildings

设置好 记得重启 mysql服务
centos

  1. service mysqld restart

ubuntu

  1. service mysql restart

mysql-bin的值和master_log_pos值 在主服务器中查看 myqsl 中执行 show master status;

  1. change master to master_host='192.168.0.1', master_user='iwhao', master_password='123456', master_log_file='mysql-bin.000030', master_log_pos=154;
  2. mysql> show slave status \G;
  3. *************************** 1. row ***************************
  4. Slave_IO_State: Waiting for master to send event
  5. Master_Host: 192.168.0.1
  6. Master_User: iwhao
  7. Master_Port: 3306
  8. Connect_Retry: 60
  9. Master_Log_File: mysql-bin.000030
  10. Read_Master_Log_Pos: 154
  11. Relay_Log_File: relay-bin.000002
  12. Relay_Log_Pos: 320
  13. Relay_Master_Log_File: mysql-bin.000030
  14. Slave_IO_Running: Yes
  15. Slave_SQL_Running: Yes
  16. Replicate_Do_DB: buildings
  17. Replicate_Ignore_DB:
  18. Replicate_Do_Table:
  19. Replicate_Ignore_Table:
  20. Replicate_Wild_Do_Table:
  21. Replicate_Wild_Ignore_Table:
  22. Last_Errno: 0
  23. Last_Error:
  24. Skip_Counter: 0
  25. Exec_Master_Log_Pos: 154
  26. Relay_Log_Space: 521
  27. Until_Condition: None
  28. Until_Log_File:
  29. Until_Log_Pos: 0
  30. Master_SSL_Allowed: No
  31. Master_SSL_CA_File:
  32. Master_SSL_CA_Path:
  33. Master_SSL_Cert:
  34. Master_SSL_Cipher:
  35. Master_SSL_Key:
  36. Seconds_Behind_Master: 0
  37. Master_SSL_Verify_Server_Cert: No
  38. Last_IO_Errno: 0
  39. Last_IO_Error:
  40. Last_SQL_Errno: 0
  41. Last_SQL_Error:
  42. Replicate_Ignore_Server_Ids:
  43. Master_Server_Id: 1
  44. Master_UUID: 5f51ec81-7c33-11e8-80f0-00163e06250b
  45. Master_Info_File: /var/lib/mysql/master.info
  46. SQL_Delay: 0
  47. SQL_Remaining_Delay: NULL
  48. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  49. Master_Retry_Count: 86400
  50. Master_Bind:
  51. Last_IO_Error_Timestamp:
  52. Last_SQL_Error_Timestamp:
  53. Master_SSL_Crl:
  54. Master_SSL_Crlpath:
  55. Retrieved_Gtid_Set:
  56. Executed_Gtid_Set:
  57. Auto_Position: 0
  58. Replicate_Rewrite_DB:
  59. Channel_Name:
  60. Master_TLS_Version:
  61. 1 row in set (0.00 sec)

Slave_IO_Running 和 Slave_SQL_Running 值 都为 Yes 说明就成功了

https://www.cnblogs.com/gomysql/p/4991197.html

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

感谢你的支持,我会继续努力!
扫码打赏,建议金额1-10元