【十七】MySQL-主从(Master/Slave)同步安装与配置详解

MySQL5.6 数据库主从(Master/Slave)同步安装与配置详解

一、安装环境

操作系统 Red Hat 4.8.5-28

MySQL 版本:MySQL-5.6.45-1.el7.x86_64.rpm-bundle.tar

主节点 IP:192.168.1.49     

从节点 IP:192.168.1.52

二、Master 配置

1、修改 Master 的配置文件

[root@localhost  ~]# vi  /etc/my.cnf

# 在 [mysqld] 中增加以下配置项

# 设置 server_id,一般设置为 IP

 server_id=49

# 复制过滤:需要备份的数据库,输出 binlog

 binlog-do-db=test

# 复制过滤:不需要备份的数据库,不输出(mysql 库一般不同步)

 binlog-ignore-db=mysql

# 开启二进制日志功能,可以随便取,最好有含义

 log-bin=edu-mysql-bin

# 为每个 session 分配的内存,在事务过程中用来存储二进制日志的缓存

 binlog_cache_size=1M

# 主从复制的格式(mixed,statement,row,默认格式是 statement)

 binlog_format=mixed

# 二进制日志自动删除/过期的天数。默认值为 0,表示不自动删除。

 expire_logs_days=7

# 如果需要同步函数或者存储过程

 log_bin_trust_function_creators=true

三、启动/重启 Master 数据库服务,登录数据库,创建数据同步用户,并授予相应的权限

[root@localhost  ~]# service  mysql  restart

 Shutting  down  MySQL..[  OK  ] Starting  MySQL..[  OK  ]

[root@localhost  ~]# mysql  -uroot  -p

 Enter  password:

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1900

Server version: 5.6.45-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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>

#创建数据同步用户,并授予相应的权限

mysql> grant  replication  slave,  replication  client  on  *.*  to  'repl'@'192. 168.1.52'  identified  by  '123456';

Query  OK,  0  rows  affected  (0.00  sec)

# 刷新授权表信息

 mysql> flush  privileges;

 Query  OK,  0  rows  affected  (0.00  sec)

# 查看 MySQL 现在有哪些用户及对应的 IP 权限

 mysql> select  user,host  from  mysql.user;

 # 查看 position 号,记下 position 号(从机上需要用到这个 position 号和现 在的日志文件)

 mysql> show  master  status;

四、接下来处理 Slave(192.168.1.52),配置文件只需修改一项,其余配 置用命令来操作

[root@localhost  ~]# vi  /etc/my.cnf

## 在 [mysqld] 中增加以下配置项

# 设置 server_id,一般设置为 IP

 server_id=52

# 复制过滤:需要备份的数据库,输出 binlog

 binlog-do-db=test

#复制过滤:不需要备份的数据库,不输出(mysql 库一般不同步)

 binlog-ignore-db=mysql

# 开启二进制日志,以备 Slave 作为其它 Slave 的 Master 时使用

 log-bin=edu-mysql-slave1-bin

# 为每个 session 分配的内存,在事务过程中用来存储二进制日志的缓存

 binlog_cache_size  =  1M

# 主从复制的格式(mixed,statement,row,默认格式是 statement)

binlog_format=mixed

# 二进制日志自动删除/过期的天数。默认值为 0,表示不自动删除。

expire_logs_days=7

#  relay_log 配置中继日志

 relay_log=edu-mysql-relay-bin

#  log_slave_updates 表示 slave 将复制事件写进自己的二进制日志

 log_slave_updates=1

# 防止改变数据(除了特殊的线程)

 read_only=1

五、保存后重启 MySQL 服务,还原备份数据

[root@edu-mysql-02  ~]# service  mysql  restart

 Shutting  down  MySQL..[  OK  ] Starting  MySQL..[  OK  ]  

六、登录 Slave 数据库,添加相关参数

[root@localhost  ~]# mysql  -uroot  -p

 Enter  password:

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1900

Server version: 5.6.45-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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>

mysql> change  master  to  master_host='192.168.1.49',  master_user=' repl',  master_password='123456',  master_port=3306,  master_log_file=' mysql-bin.000005',  master_log_pos=259,  master_connect_retry=3 0;

Query  OK,  0  rows  affected,  2  warnings  (0.01  sec)

上面执行的命令的解释:

master_host='192.168.1.49'                        ##  Master 的 IP 地址

master_user='repl'    ## 用于同步数据的用户(在 Master 中授权的用户)

master_password='123456'                          ## 同步数据用户的密码

master_port=3306          ##  Master 数据库服务的端口

master_log_file='mysql-bin.000001'     ##指定 Slave 从哪个日志文 件开始读复制数据(可在 Master 上使用 show  master  status 查看到日志 文件名)

master_log_pos=259                                  ## 从哪个 POSITION 号开始读

master_connect_retry=30                          ##当重新建立主从连接时,如果连接 建立失败,间隔多久后重试。单位为秒,默认设置为 60 秒,同步延迟调优 参数。

 ## 查看主从同步状态 

mysql> show  slave  status\G;

主要看以下两个参数,这两个参数如果是 Yes 就表示主从同步正常

Slave_IO_Running:  Yes Slave_SQL_Running:  Yes

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值