mysql 主从 三台_MySQL主从复制

本文详细介绍了如何在三台MySQL服务器上配置主从复制,解决单点故障和并发请求问题。通过时间同步、主服务器授权、从服务器配置,实现数据实时备份和故障切换,提高数据库系统的可用性和并发处理能力。
摘要由CSDN通过智能技术生成

MySQL主从复制

案例概述

在企业网站中,后端MySQL数据库只有一台时,会有以下问题:

单点故障服务不可用

无法处理大量的并发数据请求

数据丢失

c75f96114d50bc9bfec0951d0e322a5e.png

改造办法

增加MySQL数据库服务器,对数据进行备份,形成主备

确保主备MySQL数据库服务器数据是一样的

主服务器宕机了,备份服务器继续工作,数据有保障

MySQL的主从复制与读写分离是密切相关的

fb0ca562234b17b41ed255afadcf2dd5.png

更高级的解决方案

通过主从复制的方式来同步数据,在通过读写分离提升数据库的并发能力

c8cc1939f481a305404f17ac84a2376e.png

Amoeba 变形虫

案例实施

1.所有服务器关闭firewalld或者进行规则设置

2.建立时间同步环境

在主服务器上安装ntp时间同步服务器

使用yum安装ntp服务

修改ntp.conf,设置主服务器为时间同步源

在从服务器上进行时间同步

使用yum安装ntpdate并进行时间同步

在三台数据库服务器上安装mysql

编译安装mysql

优化调整

初始化数据库

启动mysql服务并进行root用户密码设置

配置mysql master主服务器

修改/etc/my.cnf配置文件,增加服务器id,配置二进制日志选项

f1431ca4d6f6ae2e7cea24754e162803.png

登录mysql服务,授权所有的从服务器复制二进制日志的权限

1111b0be0077174f8d4fbcd45252405a.png

配置两台从服务器

修改/etc/my.cnf配置文件,增加服务器id,配置二进制日志选项

登录mysql,配置主从同步

b1ef51e3f73651f419ba33a4653e0bc9.png

实操

实验环境:准备5台centos 7系统的虚拟机

一台主服务器、两台从服务器、一台amoeba、一台客户机

实验拓扑图:

5868365a8460b92705dfb76f6f43f75a.png

三台mysql服务器先都安装ntp、ntpdate

用于哦配置时间同步

[root@master ~]# yum install ntp -y

[root@slave1 ~]# yum install ntp ntpdate -y

[root@slave2 ~]# yum install ntp ntpdate -y

修改ntp配置文件

在主服务器下设置ntp配置文件,然后开启ntpd,关闭防火墙

[root@master ~]# vim /etc/ntp.conf

server 0.centos.pool.ntp.org iburst

server 1.centos.pool.ntp.org iburst

server 2.centos.pool.ntp.org iburst

server 3.centos.pool.ntp.org iburst

‘server 127.127.247.0 //设置本地是时钟源,这里的127.127指自己的192.168

’fudge 127.127.247.0 stratum 8 //设置时间环的时间层级(时间环)为8

[root@master ~]# systemctl start ntpd

[root@master ~]# systemctl stop firewalld

[root@master ~]# setenforce 0

[root@master ~]# systemctl enable ntpd

Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

配置从服务器slave1,直接开启ntpd服务,进行时间同步,去匹配主服务器(ip地址)时间

[root@slave1 ~]# systemctl start ntpd

[root@slave1 ~]# systemctl stop firewalld

[root@slave1 ~]# setenforce 0

[root@slave1 ~]# systemctl enable ntpd

Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

[root@slave1 ~]# /usr/sbin/ntpdate 192.168.247.160

8 Jan 18:39:26 ntpdate[114393]: the NTP socket is in use, exiting

slave1同步完成,接下来同步slave2

[root@slave2 ~]# systemctl start ntpd

[root@slave2 ~]# systemctl enable ntpd

Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

[root@slave2 ~]# systemctl stop firewalld

[root@slave2 ~]# setenforce 0

[root@slave2 ~]# /usr/sbin/ntpdate 192.168.247.160

8 Jan 18:40:38 ntpdate[82655]: the NTP socket is in use, exiting

接下来就是安装MySQL和优化的过程

由于过程冗杂,详细过程请查看本人之前博客:

https://blog.51cto.com/14557905/2458283

安装mysql完毕,并且做完一系列优化

开始做主从同步

1.修改主服务器配置文件

[root@master mysql-5.6.26]# vim /etc/my.cnf

//写在mysqld下

log-bin=master-bin

//上面是开启二进制文件

log-slave-update=true

//开启从服务器更新

server-id = 11

//服务器id为11(id不可以重复)

重启服务

[root@master mysql-5.6.26]# service mysqld restart

Shutting down MySQL.. SUCCESS!

Starting MySQL.. SUCCESS!

主服务器登陆mysql,给从服务器创建用户并且允许复制所有数据(ON .)

[root@master mysql-5.6.26]# mysql -uroot -p

Enter password:

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

mysql> GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'192.168.247.%' IDENTIFIED BY 'abc123';

//允许 复制,从服务器以myslave用户的身份,从192.168.247.0的网段,使用abc123的密码,去复制所有的数据库以及下面的表(*.*)

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

//刷新数据库

Query OK, 0 rows affected (0.00 sec)

mysql> show master status;

//查看主服务器的位置点,从服务器的同步位置点就是下面的412

+-------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+-------------------+----------+--------------+------------------+-------------------+

| master-bin.000001 | 412 | | | |

+-------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

配置从服务器slave1,服务器id不能一致,开启中继日志,索引中继日志

[root@slave1 mysql-5.6.26]# vim /etc/my.cnf

[mysqld]

//写在mysqld下

log-bin=mysql-bin

server-id = 22

//另一台slave2 id 为23

relay-log=relay-log-bin

relay-log-index=slave-relay-bin.index

[root@slave1 mysql-5.6.26]# service mysqld restart

Shutting down MySQL.. SUCCESS!

Starting MySQL.. SUCCESS!

从服务器登陆mysql,添加主服务器(ip地址,使用主服务器的账户mysalve,输入主服务器账户的密码,确定同步的二进制文件,同步的位置点)

[root@slave1 mysql-5.6.26]# mysql -uroot -p

Enter password:

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

mysql> change master to master_host='192.168.247.160',master_user='myslave',master_password='abc123',master_log_file='master-bin.000001',master_log_pos=412;

//添加主服务器

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

开启从服务器功能

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

查看从服务器状态

mysql> show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.247.160

Master_User: myslave

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: master-bin.000001

Read_Master_Log_Pos: 412

Relay_Log_File: relay-log-bin.000002

Relay_Log_Pos: 284

Relay_Master_Log_File: master-bin.000001

‘ Slave_IO_Running: Yes //显示slave功能已开启

‘ Slave_SQL_Running: Yes //显示slave功能已开启

Exec_Master_Log_Pos: 412

Relay_Log_Space: 455

Master_Server_Id: 11

Master_UUID: e9a82741-3223-11ea-af25-000c29524d89

Master_Info_File: /home/mysql/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

Master_Retry_Count: 86400

1 row in set (0.00 sec)

同步配置完成,接下来测试

主服务器,创建一个school数据库

mysql> create database school;

Query OK, 1 row affected (0.00 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| school |

| test |

+--------------------+

5 rows in set (0.00 sec)

从服务器直接查看

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| school |

| test |

+--------------------+

5 rows in set (0.00 sec)

MySQL服务器的主从同步就做完成了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值