mysql主从同步的配置记录

2018-01-13 16:24:49   mysql相关

  主从同步  

主库


  1. [mysqld]
  2. server_id = 1
  3. binlog-do-db = test #需要同的数据库
  4. log_bin = mysql-bin # 开启log_bin

然后需要创建从库同步时所需要的账户

  1. grant replication slave on *.* to 'repl'@'192.168.1.%' identified by 'repl..2017';

查看master状态

  1. mysql> show master status \G;
  2. *************************** 1. row ***************************
  3. File: mysql-bin.000002
  4. Position: 4707
  5. Binlog_Do_DB: test
  6. Binlog_Ignore_DB:
  7. Executed_Gtid_Set:
  8. 1 row in set (0.00 sec)

重启mysql


在从库中配置

  1. [mysqld]
  2. server_id = 2 #服务器id,必须唯一
  3. replicate-rewrite-db=callcenter->dkht_official
  4. replicate-do-table=dkht_official.plt_service_list1000 # 需要同步的数据库
  5. expire_logs_days=10
  6. //linux下出现table名大小写异常的问题,设置大小写不敏感
  7. lower_case_table_names=1

重启mysql


连接主库

  1. mysql > change master to master_host='192.168.1.151',master_port=3306,master_user='repl',master_password='repl',master_log_file='mysql-bin.000002',master_log_pos=3703;
  1. master_host 主库地址
  2. master_port 主库端口
  3. master_user 主库提供的同步用户
  4. master_password 主库提供的同步用户密码
  5. master_log_file 在主库中查看 master 状态中的 file
  6. master_log_pos 在主库中查看 master 状态中的 position
    启动主从同步

  1. mysql > start slave;

查看同步状态

  1. mysql> show slave status \G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.1.151
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin.000002
  9. Read_Master_Log_Pos: 4707
  10. Relay_Log_File: localhost-relay-bin.000004
  11. Relay_Log_Pos: 1503
  12. Relay_Master_Log_File: mysql-bin.000002
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB: test
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. 1 row in set (0.00 sec)
  19. # 状态中,`Slave_IO_Running: Yes` 和 `Slave_SQL_Running: Yes` 同时保证为 yes 即可判断成功。