1、在主库配置文件添加如下配置:

    1. #Server ID,一般设置成IP地址的最后一位,如下测试就按后两位
    2. server_id=93
    3. #开启log bin,名字最好有意义用来区分
    4. log-bin=/data/br/base/my_3306/binlog/binlog
    5. #需要进行复制的数据库,可以指定数据库
    6. #binlog-do-db=DB_master
    7. #不需要备份的数据库,可以设置多个数据库,一般不会同步mysql这个库
    8. binlog-ignore-db=mysql
    9. binlog-ignore-db=information_schema
    10. binlog-ignore-db=performance_schema
    11. #为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存
    12. binlog_cache_size=1m
    13. #二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。
    14. expire_logs_days=7
    15. # 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
    16. # 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
    17. slave_skip_errors=1062

    2、重启数据库
    3、新建同步用户,并授权

    1. CREATE USER 'replication'@'192.168.1.51' IDENTIFIED BY 'bonree365';
    2. GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%';
    3. GRANT REPLICATION SLAVE ON *.* TO 'replication'@'10.240.49.94';
    4. flush privileges;

    4、查看主库状态:

    1. show master status;

    5、在从库机器上面验证同步用户是否能够登陆主库,能够正常登陆说明同步用户创建没问题
    image.png
    6、在从库配置文件添加如下配置,并重启数据库

    1. #add slave-server
    2. server_id=94
    3. #binlog-ignore-db=mydql
    4. #binlog-ignore-db=information_schema
    5. #binlog-ignore-db=performance_schema
    6. #log-bin=dev-slave-bin
    7. binlog_cache_size=1M
    8. binlog_format=mixed
    9. expire_logs_days=7
    10. slave_skip_errors=1062
    11. relay_log=/data/br/base/my_3306/binlog/dev-relay-bin
    12. #log_slave_updates=1
    13. read_only=1

    7、添加链接到主库同步复制的账户

    1. change master to master_host='10.240.49.93',master_user='replication',master_password='Bonree@365',master_port=33306,master_log_file='binlog.000001',master_log_pos=0;

    8、启动从库同步
    start slave;
    9、查看从库同步状态

    1. mysql> show slave status\G;
    2. *************************** 1. row ***************************
    3. Slave_IO_State: Waiting for master to send event
    4. Master_Host: 10.240.49.93
    5. Master_User: replication
    6. Master_Port: 33306
    7. Connect_Retry: 60
    8. Master_Log_File: binlog.000001
    9. Read_Master_Log_Pos: 1281
    10. Relay_Log_File: dev-relay-bin.000003
    11. Relay_Log_Pos: 1488
    12. Relay_Master_Log_File: binlog.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: 1281
    25. Relay_Log_Space: 1870
    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: 93
    43. Master_UUID: d50ed5d5-9a29-11e8-ac8a-02420a630002
    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: d50ed5d5-9a29-11e8-ac8a-02420a630002:1-5
    55. Executed_Gtid_Set: d50ed5d5-9a29-11e8-ac8a-02420a630002:1-5
    56. Auto_Position: 0
    57. Replicate_Rewrite_DB:
    58. Channel_Name:
    59. Master_TLS_Version:
    60. 1 row in set (0.00 sec)

    验证同步状态,在主库创建表,插入数据。然后在从库上面查看是否同步过来

    1. create table slave_t(id int(10) not null, name varchar(20)) ;
    2. insert into slave_t values(1,'name01');
    3. mysql> select * from slave_t;
    4. +----+--------+
    5. | id | name |
    6. +----+--------+
    7. | 1 | name01 |
    8. +----+--------+
    9. 1 row in set (0.00 sec)

    错误排查:
    如果Slave_IO_Running状态为No,查看日志出现Error_code:1593报错,说明主从库的UUID一样。
    image.png
    删除从库data目录下的auto.cnf,然后重启数据库,发现两边的uuid不一样了,就可以了
    image.png