主从复制第一篇之异步复制

主从复制主要用途:
1.灾备切换
2.读写分离
3.备份,避免影响业务
4.高可用和故障切换
5.mysql测试升级
首先,异步主从复制主要步骤:
1.主库中开启log_bin
2.全备份主库
3.授权(grant replication slave on .
4.配置复制,并启动(change master to)
5.查看主从复制信息
异步主从复制存在的主要问题:
1.主机宕机后,数据可能丢失
2.从库只有一个sql thread,当主库有大量写入时候,从库容易丢失数据
异步复制的演示:
第一步:主库中开启log_bin=1,在my.cnf文件中设置,这里就不详细介绍了。
第二步:用mysqldump进行主库全备份
  1. [root@VM_85_42_centos downloads]# ps -ef |grep mysqld #查看配置位置
  2. root 5307 1 0 Apr03 ? 00:00:00 /bin/sh /usr/local/mysql56/bin/mysqld_safe --defaults-file=/mysqldata/my.cnf
  3. mysql 5539 5307 0 Apr03 ? 00:00:50 /usr/local/mysql56/bin/mysqld --defaults-file=/mysqldata/my.cnf --basedir=/usr/local/mysql56 --datadir=/mysqldata/node1 --plugin-dir=/usr/local/mysql56/lib/plugin --user=mysql --log-error=/mysqldata/node1/VM_85_42_centos.err --pid-file=/mysqldata/node1/VM_85_42_centos.pid --socket=/tmp/mysql.sock --port=6000
  4. root 13332 20569 0 23:19 pts/0 00:00:00 grep --color=auto mysqld
  5. [root@VM_85_42_centos downloads]# mysqldump -uroot -p --socket=/tmp/mysql.sock --single-transaction -A --master-data=1 > all_data.sql
  6. [root@VM_85_42_centos downloads]# mysqldump -uroot -p --socket=/tmp/mysql.sock --single-transaction -A --master-data=1 > all_data.sql
  7. Enter password:
  8. [root@VM_85_42_centos downloads]# ls
  9. all_data.sql
使用远程登入从库myqsl
  1. mysql -utest -ptest -h(从库IP) -P(端口号)
  2. 登入从库后用
  3. mysql>source all_data.sql; # 导入主库
第三步:主库授权用户
grant replication slave on *.* to replication@'(从库IP)' identified by 'replication';
从库配置复制
  1. CHANGE MASTER TO
  2. MASTER_HOST='主库IP',
  3. MASTER_USER='replication',
  4. MASTER_PASSWORD='replication',
  5. MASTER_PORT=(根据my.cnf配置端口),
  6. MASTER_LOG_FILE='master2-bin.XXX',
  7. MASTER_LOG_POS=X,
  8. MASTER_CONNECT_RETRY=X; #等一系列参数
我的实例配置
  1. mysql> change master to
  2. MASTER_HOST='119.29.215.56',
  3. MASTER_USER='replication',
  4. MASTER_PASSWORD='replication',
  5. MASTER_PORT=6000,
  6. MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=630;
  7. #MASTER_LOG_FILE,MASTER_LOG_POS参数记录在刚才的 all_data.sql,可以用less命令查看。
配置完成以后启动:
  1. mysql> start stave;
  2. mysql> show slave status \G #是否启动看running是yes
  3. *************************** 1. row ***************************
  4. Slave_IO_State: Waiting for master to send event
  5. Master_Host: 119.29.215.56
  6. Master_User: repl
  7. Master_Port: 6000
  8. Connect_Retry: 60
  9. Master_Log_File: mysql-bin.000002
  10. Read_Master_Log_Pos: 952
  11. Relay_Log_File: VM_91_3_centos-relay-bin.000002
  12. Relay_Log_Pos: 605
  13. Relay_Master_Log_File: mysql-bin.000002
  14. Slave_IO_Running: Yes
  15. Slave_SQL_Running: Yes
  16. Replicate_Do_DB:
  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: 952
  26. Relay_Log_Space: 787
  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: 6000
  44. Master_UUID: cf441ef0-181b-11e7-b566-525400ef16de
  45. Master_Info_File: /mysqldata/node1/master.info
  46. SQL_Delay: 0
  47. SQL_Remaining_Delay: NULL
  48. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  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. 1 row in set (0.00 sec)
  59. 上面各类参数都是可以配置的,需要查看文档

评论