温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL的主从复制是什么?怎么实现MySQL服务器的主从同步?

发布时间:2020-06-08 11:25:52 来源:亿速云 阅读:439 作者:Leah 栏目:MySQL数据库

MySQL的主从复制是什么?怎么实现MySQL服务器的主从同步?这些问题可能是我们日常工作会见到的。通过这些问题,希望你能收获更多。下面是揭开这些问题的详细内容。

MySQL主从复制
案例概述
在企业网站中,后端MySQL数据库只有一台时,会有以下问题:

  • 单点故障服务不可用
  • 无法处理大量的并发数据请求
  • 数据丢失
    MySQL的主从复制是什么?怎么实现MySQL服务器的主从同步?
    改造办法
  • 增加MySQL数据库服务器,对数据进行备份,形成主备
  • 确保主备MySQL数据库服务器数据是一样的
  • 主服务器宕机了,备份服务器继续工作,数据有保障
    MySQL的主从复制与读写分离是密切相关的
    MySQL的主从复制是什么?怎么实现MySQL服务器的主从同步?
    更高级的解决方案
    通过主从复制的方式来同步数据,在通过读写分离提升数据库的并发能力
    MySQL的主从复制是什么?怎么实现MySQL服务器的主从同步?
    Amoeba 变形虫
    案例实施
    1.所有服务器关闭firewalld或者进行规则设置
    2.建立时间同步环境
  • 在主服务器上安装ntp时间同步服务器
    • 使用yum安装ntp服务
    • 修改ntp.conf,设置主服务器为时间同步源
  • 在从服务器上进行时间同步
    • 使用yum安装ntpdate并进行时间同步
      在三台数据库服务器上安装mysql
  • 编译安装mysql
  • 优化调整
  • 初始化数据库
  • 启动mysql服务并进行root用户密码设置
    配置mysql master主服务器
  • 修改/etc/my.cnf配置文件,增加服务器id,配置二进制日志选项
    MySQL的主从复制是什么?怎么实现MySQL服务器的主从同步?
  • 登录mysql服务,授权所有的从服务器复制二进制日志的权限
    MySQL的主从复制是什么?怎么实现MySQL服务器的主从同步?
    配置两台从服务器
  • 修改/etc/my.cnf配置文件,增加服务器id,配置二进制日志选项
  • 登录mysql,配置主从同步
    MySQL的主从复制是什么?怎么实现MySQL服务器的主从同步?
    实操
    实验环境:准备5台centos 7系统的虚拟机
    一台主服务器、两台从服务器、一台amoeba、一台客户机
    实验拓扑图:
    MySQL的主从复制是什么?怎么实现MySQL服务器的主从同步?
    三台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的主从复制介绍以及MySQL服务器实现主从同步的方法,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注亿速云行业资讯!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI