mysql 主从配置

1.两台机器上开启mysql服务,机器ip分别为192.168.0.1(master) 192.168.0.2(slave)
2.master上的mysql开启bin-log服务,在my.cnf 的 [mysqld]模块添加

server-id=200  #设置主服务器的ID
innodb_flush_log_at_trx_commit=2  #
sync_binlog=1  #开启binlog日志同步功能
log-bin=mysql-bin-200  #binlog日志文件名
3.重启mysql,登录mysql服务器后输入
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;

 如果输出master的binlog信息则证明成功开启

| mysql-bin-200.000002 |      120 |              |                  |     

 在master添加slave的数据库账号

mysql> create user 'slave'@'x.x.x.x' identified by 'pass'

 赋予slave账号replication权限

mysql>grant replication slave ON *.* TO 'slave'@'X.X.X.X' identified by 'password';

  至此主库的配置全部完成

4.登录slave的服务器,在my.cnf中进行相同配置

server-id=201
innodb_flush_log_at_trx_commit=2
sync_binlog=1
log-bin=mysql-bin-201

 5.登录slave的mysql数据库,进行从库账号的配置,这里master-log-file 以及master-log-pos从主库的 show master status获取

mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.0.1',
    -> MASTER_USER='slave',
    -> MASTER_PASSWORD='mysql',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysql-bin-200.000002',
    -> MASTER_LOG_POS=120,
    -> MASTER_CONNECT_RETRY=10;

6.验证从库配置情况,如果发现有如下两项是ok则表明从库配置完成

mysql> show slave status \G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

7.原理理解:

从库每次通过Master_Log_File和Read_Master_Log_Pos去主库找文件获取最新的的操作,完成同步操作后更新Master_Log_File和Read_Master_Log_Pos。下次同步时继续以上流程

 Master_Log_File: mysql-bin-200.000005
 Read_Master_Log_Pos: 120

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值