理论+实操:mysql主从复制

文章目录


前言:

  • 案例概述
  • 案例前置知识点
  • 案例环境
  • 案例实施

一:案例概述

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

  • 单点故障————服务不可用
  • 无法处理大量的并发数据请求
  • 数据丢失————大灾难

在这里插入图片描述

1.2 改造办法

  • 增加mysql数据库服务器,对数据进行备份,形成主备
  • 确保主备mysql数据库服务器数据是一样的
  • 主服务器宕机了,备份服务器继续工作,数据有保障

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

在这里插入图片描述

选举出主服务器,其他的都是从服务器,从服务器向主服务器看齐

1.3 更高级解决方案

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

在这里插入图片描述

amoeba 变形虫,java语言写的,配置文件是xml

它主要负责对外的一个代理的ip,别人去访问这个ip,发送请求,若是写请求,就转给主服务器;读请求通过调度转发给从服务器,使用轮询算法,轮流给两台从服务器

amoeba可以视为调度器,

若是主服务器挂掉,会有MHA解决这个问题

要点:权限

1.主从同步账号,主服务器给账号同步的权限,从服务器可以使用这个账号去同步

2.所有的节点服务器,要开放被调度的权限的账号

3.amoeba代理账号,创建访问ameba的账号

二:案例前置知识点

2.1 mysql主从复制的类型

  • 基于语句的复制(默认)
    • 在主服务器上执行的语句,从服务器执行同样的语句
  • 基于行的复制
    • 把改变的内容复制到从服务器
  • 混合类型的复制
    • 一旦发现基于语句无法精确复制时,就会采用基于行的复制

2.2 主从复制的工作过程

在这里插入图片描述

relay log 中继日志

需要开启二进制日志功能

在这里插入图片描述

三:案例实施

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

3.2 建立时间同步环境

  • 在主服务器上安装配置NTP时间同步服务器

    • 使用yum安装ntp服务
    • 修改ntp.conf,设置主服务器为时间同步源
  • 在从服务器上进行时间同步

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

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

  • 编译安装mysql
  • 优化调整
  • 初始化数据库
  • 启动mysql服务并进行root用户密码设置

3.4 配置mysql master主服务器

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

在这里插入图片描述

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

在这里插入图片描述

3.5 配置两台从服务器

  • 修改/etc/my.cnf配置文件,增加服务器id,配置二进制日志选项
  • 登录mysql,配置主从同步

在这里插入图片描述

四:实操

4.1 实验环境

五台服务器

在这里插入图片描述

在这里插入图片描述

配置环境

[root@lamp ~]# hostnamectl set-hostname amoeba
[root@lamp ~]# su
[root@amoeba ~]# 
[root@nginx ~]# hostnamectl set-hostname master
[root@nginx ~]# su
[root@master ~]# 
[root@localhost ~]# hostnamectl set-hostname slave1
[root@localhost ~]# su
[root@slave1 ~]# 

[root@localhost ~]# hostnamectl set-hostname slave2
[root@localhost ~]# su
[root@slave2 ~]# 
[root@localhost ~]# hostnamectl set-hostname client
[root@localhost ~]# su
[root@client ~]# 

4.2 实验拓扑图

在这里插入图片描述

4.3 实验步骤

思路:先在主从服务器上做时间同步,然后再去装mysql

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

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

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

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

4.3.2 修改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

④时间同步完成,

4.3.3 接下来安装mysql

①挂载源码包

[root@master ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[root@master ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@master ~]# cd /abc
[root@master abc]# ls
12.17                             error.png                  LNMP-C7
8.tar.gz                          extundelete-0.2.4.tar.bz2  mysql
amoeba-mysql-binary-2.2.0.tar.gz  fiddler (1).exe            qq.jpg
apache-tomcat-9.0.16.tar.gz       httpd2.4.2版本             server2016-key.txt
awstats-7.6.tar.gz                jb                         shells-ceshi
centos7 samba匿名.txt             jdk-6u14-linux-x64.bin     shells.rar
cronolog-1.6.2-14.el7.x86_64.rpm  jdk-8u201-linux-x64.rpm    tomcat压测
doc                               john-1.8.0.tar.gz          学习资料
epel-release-latest-7.noarch.rpm  LAMP-C7
error.jpg                         LAMP-C7.rar
[root@master abc]# cd LAMP-C7/
[root@master LAMP-C7]# ls
apr-1.6.2.tar.gz       Discuz_X2.5_SC_UTF8.zip  LAMP-php5.6.txt      php-5.6.11.tar.bz2
apr-util-1.6.0.tar.gz  httpd-2.4.29.tar.bz2     mysql-5.6.26.tar.gz

②解压源码包

[root@master LAMP-C7]# tar xzvf mysql-5.6.26.tar.gz -C /opt
[root@master LAMP-C7]# cd /opt
[root@master opt]# ls
data  mysql-5.6.26  nginx-1.12.2  rh

③yum安装编译软件

[root@master opt]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
ncurses-devel \
autoconf \
cmake

④cmake

[root@master opt]# cd mysql-5.6.26/
[root@master mysql-5.6.26]# ls
BUILD           config.h.cmake       extra               libmysqld    packaging  sql-bench      unittest
BUILD-CMAKE     configure.cmake      include             libservices  plugin     sql-common     VERSION
client          COPYING              INSTALL-SOURCE      man          README     storage        vio
cmake           dbug                 INSTALL-WIN-SOURCE  mysql-test   regex      strings        win
CMakeLists.txt  Docs                 libevent            mysys        scripts    support-files  zlib
cmd-line-utils  Doxyfile-perfschema  libmysql            mysys_ssl    sql        tests
[root@master mysql-5.6.26]# cmake  \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DSYSCONFIDIR=/etc \
-DMYSQL_DATADIR=/home/mysql/ \
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock

⑤ make && make install

[root@master mysql-5.6.26]# make && make install

⑥ 安装mysql完毕

4.3.4 优化mysql

① 将解压包中的默认配置文件复制到/etc/下,覆盖原配置文件

[root@master ~]# cd /opt/mysql-5.6.26/
[root@master mysql-5.6.26]# cp support-files/my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y

②将解压包内的默认启动脚本复制到/etc/init.d中,给其755,添加到service中,便于被service管理

[root@master mysql-5.6.26]# cp support-files/mysql.server /etc/init.d/mysqld
[root@master mysql-5.6.26]# chmod 755 /etc/init.d/mysqld
[root@master mysql-5.6.26]# chkconfig --add /etc/init.d/mysqld
[root@master mysql-5.6.26]# chkconfig  mysqld --level 235 on

③ 优化环境变量,方便使用命令

[root@master mysql-5.6.26]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@master mysql-5.6.26]# source /etc/profile
[root@master mysql-5.6.26]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

④ 创建mysql用户,/sbin/nologin

[root@master mysql-5.6.26]# useradd -s /sbin/nologin mysql

⑤ 将/usr/local/mysql目录 -R 的权限设为 mysql

[root@master mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/

⑥ 初始化数据库

[root@master mysql-5.6.26]# /usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
//此处初始化数据库
Installing MySQL system tables...2020-01-08 22:32:53 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-01-08 22:32:53 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.26) starting as process 84380 ...

New default config file was created as /usr/local/mysql/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

⑦ 创建/var/lib/mysql/mysql.sock文件软连接到/home/mysql下

[root@master mysql-5.6.26]# ln -s /var/lib/mysql/mysql.sock  /home/mysql/mysql.sock

⑧在启动脚本/etc/init.d/mysqld中添加路径

[root@master mysql-5.6.26]# vim  /etc/init.d/mysqld
basedir=/usr/local/mysql     
datadir=/home/mysql

⑨ 开启服务,查看端口

[root@master mysql-5.6.26]# service mysqld start
Starting MySQL. SUCCESS! 
[root@master mysql-5.6.26]# netstat -anpt | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      85877/mysqld     

⑩ 修改数据库密码

[root@master mysql-5.6.26]# mysqladmin -u root -p password "abc123"
Enter password: 
Warning: Using a password on the command line interface can be insecure.

4.3.5 做主从同步

① 主服务器修改配置文件

[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 * . *)

REPLICATION 复制的意思

[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)

mysql> 

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

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

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> 

②从服务器直接查看

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

③同步成功

databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
±-------------------+
5 rows in set (0.00 sec)

mysql>


#### ②从服务器直接查看

mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
±-------------------+
5 rows in set (0.00 sec)

mysql>


#### ③同步成功
————接下来下篇博客进行mysql读写分离
发布了87 篇原创文章 · 获赞 26 · 访问量 4519

猜你喜欢

转载自blog.csdn.net/Lfwthotpt/article/details/103904968
今日推荐