目 录CONTENT

文章目录

mysql主从复制

ZiChen D
2021-08-31 / 0 评论 / 0 点赞 / 377 阅读 / 9,273 字 / 正在检测是否收录...

主从复制概述

MySQL作为世界上使用最为广泛的数据库之一,免费是其原因之一;但不可忽略的是它本身的功能的确很强大。随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求。此时数据库集群就很好的解决了这个问题了。采用MySQL分布式集群,能够搭建一个高并发、负载均衡的集群服务器。在此之前我们必须要保证每台MySQL服务器里的数据同步。

主从作用

  • 实时灾备,用于故障切换
  • 读写分离,提供查询服务
  • 备份,避免影响业务

主从形式

  • 一主一从
  • 主主复制
  • 一主多从---扩展系统读取的性能,因为读是在从库读取的
  • 多主一从---5.7开始支持
  • 联级复制

主从复制原理

主从复制步骤:

  • 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
  • 从库生成两个线程,一个I/O线程,一个SQL线程
    • I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
    • SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的

主从复制配置

主从复制配置步骤:

  1. 确保从数据库与主数据库里的数据一样
  2. 在主数据库里创建一个同步账号授权给从数据库使用
  3. 配置主数据库(修改配置文件)
  4. 配置从数据库(修改配置文件)

环境说明:

数据库角色IP应用与系统版本有无数据
主数据库192.168.159.130centos7/redhat7 mysql-5.7有数据
从数据库192.168.159.140centos7/redhat7 mysql-5.7无数据

mysql主从配置

确保从数据库与主数据库里的数据一样

为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中

//先查看主库有哪些库
[root@master ~]# mysql -uroot -p1 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

//再查看从库有哪些库
[root@master ~]# mysql -uroot -p1 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

//创建并授权账号
mysql> grant replication slave on *.* to 'repl'@'192.168.159.140' identified by 'dzc123!';
Query OK, 0 rows affected, 1 warning (0.05 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

//配置主从数据库
[root@master ~]# cat /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
skip-name-resolve

log-bin = mysql_bin  //启用binlog日志
server-id = 10   //数据库服务器唯一标识符,主库的server-id值必须比从库的小

//重启服务
[root@master ~]# systemctl mysqld restart

//查看主库
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.03 sec)

//从库配置
[root@slave ~]# cat /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
skip-name-resolve

server-id = 20      //设置从库的唯一标识符,从库的server-id值必须大于主库的该值
relay-log = mysql_relay      //启用中继日志relay-log

//重启从库
[root@slave ~]# systemctl mysqld restart

//配置并启动主从复制
mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.159.130',
    -> MASTER_USER='repl',
    -> MASTER_PASSWORD='dzc123!',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.33 sec)

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

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.159.130
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_relay.000003
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql_bin.000004
             Slave_IO_Running: Yes  //此处必须为Yes
            Slave_SQL_Running: Yes  //此处必须为Yes

//测试验证
在主服务器的student库的bj2表中插入数据:
mysql> create database dzc;
Query OK, 1 row affected (0.04 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| dzc                |
+--------------------+
5 rows in set (0.02 sec)

在从数据库中查看数据是否同步:
mysql> create database dzc;
Query OK, 1 row affected (0.04 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| dzc                |
+--------------------+
5 rows in set (0.02 sec)

主数据库原先有数据

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| dzc                |
+--------------------+
5 rows in set (0.02 sec)

mysql> select * from student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | akali |   18 |
|  2 | jie   |   19 |
|  3 | shen  |   20 |
|  4 | gailun|   21 |
+----+-------+------+
4 rows in set (0.03 sec)

//全备主从数据库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.14 sec)

//备份主库并将备份文件传送到从库
[root@master ~]# mysqldump -uroot -pdzc123! --all-databases > all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

[root@master ~]# ls /opt/
all-2021-08-30.sql 
[root@master ~]# scp /opt/all-2021-08-30.sql root@192.168.159.140:/opt/
root@192.168.159.140's password: 
all-2021-08-30.sql                       100%    0     0.0KB/s   00:00    

[root@slave ~]# mysql -uroot -pdzc123! -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| dzc                |
+--------------------+

//在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication slave on *.* to 'repl'@'192.168.159.140' identified by 'dzc123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

//配置主数据库
[root@master ~]# cat /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
log-bin = mysql_bin
server-id = 10

//查看主数据库状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000003 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.02 sec)

//配置从数据库
[root@slave ~]# vim /etc/my.cnf
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysqld.pid
skip-name-resolve

server-id=20    //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log=mysql-relay-bin       //启用中继日志relay-log

//配置并启动主从复制
mysql> change master to
    -> master_host='192.168.47.133',
    -> master_user='repl',
    -> master_password='repl123!',
    -> master_log_file='mysql_bin.000001',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

//查看主从复制状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.159.130
                  Master_User: wxy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql_bin.000003
             Slave_IO_Running: YES
            Slave_SQL_Running: YES

//测试
mysql> select * from student;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | akali    |   18 |
|  2 | jie      |   19 |
|  3 | shen     |   20 |
|  4 | gailun   |   21 |
+----+----------+------+
4 rows in set (0.01 sec)

mysql> insert student(name,age) value('lakesi',22);
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | akali    |   18 |
|  2 | jie      |   19 |
|  3 | shen     |   20 |
|  4 | gailun   |   21 |
|  5 | lakesi   |   22 |
+----+----------+------+
5 rows in set (0.00 sec)

mysql> use dzc;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | akali    |   18 |
|  2 | jie      |   19 |
|  3 | shen     |   20 |
|  4 | gailun   |   21 |
|  5 | lakesi   |   22 |
+----+----------+------+
5 rows in set (0.00 sec)
0

评论区