手把手!MySQL数据库——主从复制

MySQL主从同步复制概述

为什么要进行主从复制

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

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

解决办法办法

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

MySQL主从复制的类型

  1. 基于语句的复制(默认):在主服务器上执行的语句,从服务器执行同样的语句

  2. 基于行的复制:把改变的内容复制到从服务器

  3. 混合类型的复制:—旦发现基于语句无法精确复制时,就会采用基于行的复制

MySQL主从复制的工作过程

使用Mysql的replication机制实现主从同步时,其是由三个线程实现了,主库一个I/O线程,从库一个I/O线程和一个SQL线程

在这里插入图片描述

实验

实验设计

1.配置一台主MySQL服务器
2. 配置两台从MySQL服务器
3. 搭建主从备份
4. 在主服务器写入数据测试

一、实验前提步骤

建立时间同步环境:

  1. 在主服务器上安装配置NTP时间同步服务器
  2. 使用yum安装ntp服务
  3. 修改ntp.conf,设置主服务器为时间同步源
  4. 在从服务器上进行时间同步,使用yum安装ntpdate并进行时间同步

1.主服务器(master)

[root@localhost ~]# yum -y install ntp
[root@localhost ~]# vi /etc/ntp.conf
……省略部分
server 127.127.1.0  //末尾插入这两行配置
fudge 127.127.1.0 stratum 8

2.从服务器(slave1)

[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.12   //一定要和主服务器时间同步
15 Sep 00:57:54 ntpdate[10127]: step time server 20.0.0.12 offset -0.960713 sec
[root@localhost ~]# crontab -e   //设置计划任务
*/2 * * * * /usr/sbin/ntpdate 20.0.0.12 >>/var/log/ntpdate.log
[root@localhost ~]# service crond start
Redirecting to /bin/systemctl start crond.service
[root@localhost ~]# crontab -l
*/2 * * * * /usr/sbin/ntpdate 20.0.0.12 >>/var/log/ntpdate.log

3.从服务器(slave2)

[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 20.0.0.12   //一定要和主服务器时间同步
15 Sep 00:57:54 ntpdate[10127]: step time server 20.0.0.12 offset -0.960713 sec
[root@localhost ~]# crontab -e   //设置计划任务
*/2 * * * * /usr/sbin/ntpdate 20.0.0.12 >>/var/log/ntpdate.log
[root@localhost ~]# service crond start
Redirecting to /bin/systemctl start crond.service
[root@localhost ~]# crontab -l
*/2 * * * * /usr/sbin/ntpdate 20.0.0.12 >>/var/log/ntpdate.log

二、配置主从备份

1.主服务器(master)

[root@localhost ~]# vi /etc/my.cnf
[mysqld]
……
server-id = 11   //配置server-id
log-bin = master-bin  //指定二进制日志文件,默认在安装目录下的、data/目录中
log-slave-updates = true  //允许从库从主库复制数据时可以写入到binlog日志
[root@localhost ~]# systemctl restart mysqld  //重启生效配置
[root@localhost data]# mysql -uroot -p  登入数据库
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by 'abc123';  //授予账户监视Replication(复制)状况的权力,授予slave服务器以该账户连接master后可以执行replicate操作的权利
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;  //重新加载权限
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show master status;   //查看master状态表,记住 File 和 Position这两个字段的参数,到从服务器有用
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000002 |      447 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

2.从服务器(slave1)


[root@localhost ~]# vi /etc/my.cnf
[mysqld]
……省略部分
server-id = 22
relay-log = relay-log-bin  //从服务器二进制日志文件路径
relay-log-index = slave-relay-bin.index
[root@localhost ~]# systemctl restart mysqld  //重启生效
[root@localhost data]# mysql -uroot -p  //登入MySQL
mysql> change master to master_host='20.0.0.12',   //master_host对应主服务器的IP地址。
    -> master_user='myslave',
    -> master_password='abc123',
    -> master_log_file='master-bin.000001',  //master_log_file对应show master status显示的File列:master-bin.000001。
    -> master_port=3306,    //master_port对应主服务器的端口。
    -> master_log_pos=447;   //master_log_pos对应show master status显示的Position列:447。
    Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 20.0.0.12
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 447
               Relay_Log_File: relay-log-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: yes   
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 

3.从服务器(slave2)

[root@localhost ~]# vi /etc/my.cnf
[mysqld]
……省略部分
server-id = 33
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@localhost ~]# systemctl restart mysqld
[root@localhost data]# mysql -uroot -p
mysql> change master to master_host='20.0.0.12',   //指定主服务器地址
    -> master_user='myslave',    //主服务器管理账号
    -> master_password='abc123',   //账号密码
    -> master_log_file='master-bin.000001',   //注意,这个文件一定要去master再次查看,保证一致
    -> master_log_pos=447;   //二进制文件位置,可在主服务器show master status查看
Query OK, 0 rows affected, 2 warnings (0.00 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: 20.0.0.12
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 447
               Relay_Log_File: relay-log-bin.000001
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-bin.000002
             Slave_IO_Running: Yes  // I/O进程已经运行   
            Slave_SQL_Running: Yes  //SQL进程已经运行
              Replicate_Do_DB: 

测试验证

(1)去主服务器写入数据

[root@localhost data]# mysql -uroot -p  //登入
mysql> create database test;   //创建库
Query OK, 1 row affected (0.01 sec)
mysql> use test;
Database changed
mysql> create table text (a int,b int,c int);   //创建表
Query OK, 0 rows affected (0.02 sec)
mysql> insert into text values (1,2,3);   //写入数据
Query OK, 1 row affected (0.01 sec)
mysql> select * from text;
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    2 |    3 |
+------+------+------+
1 row in set (0.00 sec)

(2)去slave1查看验证

[root@localhost data]# mysql -uroot -p
mysql> show databases;   //存在新建的库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from test.text;   //数据也正常,同步成功
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    2 |    3 |
+------+------+------+
1 row in set (0.00 sec)

(3)去slave2查看验证

[root@localhost data]# mysql -uroot -p
mysql> show databases;   //存在新建的库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> select * from test.text;  //数据也正常,同步成功
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    2 |    3 |
+------+------+------+
1 row in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/CN_LiTianpeng/article/details/108589610