mysql主从同步io_mysql 主从同步

记录mysql主从同步的实验过程。

环境:win10(主库) centos7(从库)

在win10上用Navicat访问centos的mysql,发现无法访问。

先用ping命令发现可以ping通,telnet 3306端口不通,想到可能是防火墙的问题。

centos7默认使用firewall,不是iptables

查看firewall状态 $ firewall-cmd --state 结果:running

关闭防火墙:systemctl stop firewalld.service

禁止防火墙开机启动:systemctl disable firewalld.service

关闭防火墙之后Navicat可以访问了。

设置主库mysql配置文件 my.ini(我的路径是**/MySQL Server 5.7/my.ini)

server-id=1  --这个指定的是server-id一定要唯一

log-bin=mysql-bin  --开启二进制日志

binlog-do-db = test  --要同步的库

还有一个参数是binlog-ignore-db  --不同步哪些库,我这里没有用。

3b452aaad95fec474360186bae8a03e6.png

设置完成后重启MySql服务

0bc3509515135182fe66833824c1fbff.png

在主库上给从库建一个用来同步数据的用户

CREATE USER 'synchro'@'192.168.3.109' IDENTIFIED BY 'slavepass';GRANT REPLICATION SLAVE ON *.* TO 'synchro'@'192.168.3.109';

flushPRIVILEGES

执行 SHOW MASTER STATUS 查看一下当前的二进制日志文件和文件内容的当前位置

54939a4fe46dd3782e720e791d5c7d74.png

配置从库

修改mysql配置文件my.cnf (我的路径是/etc/my.cnf)

添加 server_id = 2

重启mysql

systemctl stop mysqld

systemctl start mysqld

登录到mysql

mysql -u root -p 输入密码后登录成功。

mysql> CHANGE MASTER TO

-> MASTER_HOST='192.168.3.109',MASTER_USER='synchro',MASTER_PASSWORD='slavepass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=2122;

MASTER_USER是在主库建的那个用户,MASTER_PASSWORD是登录密码,MASTER_LOG_FILE是上图中的File列的值,MASTER_LOG_POS=2122是上图中Position的值。

启动主从同步

START slave

查看主从同步状态(不加\G的话输出的内容会很乱)

show slave status\G

返回结果*************************** 1. row ***************************Slave_IO_State: Waitingfor master tosend event

Master_Host:192.168.3.109Master_User: synchro

Master_Port:3306Connect_Retry:60Master_Log_File: mysql-bin.000003Read_Master_Log_Pos:2122Relay_Log_File: localhost-relay-bin.000002Relay_Log_Pos:320Relay_Master_Log_File: mysql-bin.000003Slave_IO_Running: Yes  //这两个都为YES时,才可以同步数据

Slave_SQL_Running: Yes  //这两个都为YES时,才可以同步数据

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno:0Last_Error:

Skip_Counter:0Exec_Master_Log_Pos:2122Relay_Log_Space:531Until_Condition: None

Until_Log_File:

Until_Log_Pos:0Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No

Last_IO_Errno:0Last_IO_Error:

Last_SQL_Errno:0Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id:1Master_UUID: eb2966eb-4184-11e8-a79a-4ccc6af9c3f6

Master_Info_File:/var/lib/mysql/master.info

SQL_Delay:0SQL_Remaining_Delay:NULLSlave_SQL_Running_State: Slave hasread all relay log; waiting formore updates

Master_Retry_Count:86400Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position:0Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:1 row in set (0.00 sec)

Slave_IO_Running这个状态可能是Connecting。此时要仔细检查上边change master部分的参数有没有写错。

此时可以尝试在主库插入一条数据,刷新从库看到插入的数据即为成功。

测试:

stop slave 关闭主从同步后,向主库插入数据。再启动主从同步,数据会同步,不会丢失。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值