mysql设置主从同步

本文主要解决 mysql主从两台服务器之间,数据实时同步。环境是debain系统,已安装mariadb。

主机192.168.0.1,从机192.168.0.5

1、在一台服务器配置为从机

在linux终端找到 /etc/mysql/mariadb.conf.d/50-server.cnf

将其 server-id 设为 2(默认为1),

然后重启MYSQL服务

systemctl restart mariadb.service

2、在主机上加一个从机可以登录的用户

root@# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 172138
Server version: 10.0.36-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>grant replication slave on *.* on 'sally'@'192.168.0.5' identified by 'ilovesally';

flush privileges;

3、建好后,在从机192.168.0.5 服务器上执行以下语句,如果可以连上,则进行下一步,

root@# mysql -h 192.168.0.1 -u sally -P 3306 -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 172155
Server version: 10.0.36-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 

4、在主机执行以下语句,查看master状态

show master status;

5、在从机执行如下sql语句:

其中MASTER_LOG_FILE='mysql-bin.000020',MASTER_LOG_POS=1441;在第4步中取到

CHANGE MASTER TO
MASTER_HOST='192.168.0.1',
MASTER_USER='sally',
MASTER_PASSWORD='ilovesally',
MASTER_LOG_FILE='mysql-bin.000020',
MASTER_LOG_POS=1441;

6、在从库上继续执行如下sql语句:

slave start; show slave status;

这样,查看从服务器的状态,如果状态中的用红线标出来两个参数的值都为YES,那证明配置已经成功,否则可以检查一下具体问题出现在什么地方。

7、这样,就算配置完成了。在主库中新建数据库,新建一张表,插几条数据,到从库上查询一下看是否已经同步过来。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个高效工作的家伙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值