Mysql主从
Mysql主从
目录
1. 主从简介
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一些隐患:
- 用一台数据库服务器存放数据,如果服务器宕机则服务器内数据库的数据则会丢失
- 业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量
1.1 主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务(这里的备份并不是只全量,增量,差异等对数据库内数据备份,而是对服务器的一种备份)
1.2 主从形式
- 一主一从
一主一从:在主服务器上的写操作会被复制进从服务器
- 主主复制
主主复制:互为主备,相互复制写入的信息
- 一主多从---扩展系统读取的性能,因为读是在从库读取的
一主多从:当公司某业务读取量特别大时,可将数据库主备做成一主多从,将从服务器用作业务的读服务器,可以在流量大时保证服务质量
- 多主一从---5.7开始支持
多主一从:公司业务写入量大的时候,如春运抢票软件,双十一电商平台购物。可将服务器做成多主一从,保证流量大时写入业务的服务质量
- 联级复制
2. 主从复制原理
主从复制步骤:
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
3. 主从复制配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
3.1 主从复制两台新服务器无数据时的配置
注意: 请将防火请关闭或设置防火墙规则
因为是新的服务器所以两边的数据一定相同,所以跳过了第一步同步主从的数据
需求:搭建两台MySQL服务器,一台作为主服务器(master164),一台作为从服务器(slaver163),主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.112.164 | centos7/redhat7 mysql-5.7 |
无数据 |
从数据库 | 192.168.112.163 | centos7/redhat7 mysql-5.7 |
无数据 |
3.1.1 mysql安装
请使用二进制mysql包安装
3.1.2 在主数据库里创建一个同步账号授权给从数据库使用
[root@master164 ~]# mysql -uroot -pcwh123!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave on *.* to 'repl'@'192.168.112.163' identified by 'cwh123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.1.3 配置主数据库
[root@master164 ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
log-bin = mysql-master-bin
server-id = 1
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//重启mysql服务
[root@master164 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@master164 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 32 :::21 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//查看主库的状态
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-master-bin.000002 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
//注意其中的file下的文件是主服务器的log-bin日志
//position下的数字是后面从数据库需要用到的起始位置
3.1.4 配置从数据库
symbolic-links=0
[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
server-id = 6
relay-log = mysql-relay-bin
//重启从库的mysql服务
[root@slaver163 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@slaver163 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//配置并启动主从复制
mysql> change master to master_host='192.168.112.164',master_user='repl',master_password='cwh123!',master_log_file='mysql-master-bin.000002',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
//查看从服务器状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.112.164
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-master-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 327
Relay_Master_Log_File: mysql-master-bin.000002
Slave_IO_Running: Yes //slave的io线程yes 为成功启动
Slave_SQL_Running: Yes //slave的SQL线程yes为成功启动
3.1.5 测试验证
//在主服务器新建cwh库并新建aaa表向aaa表中插入数据:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database cwh;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cwh |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use cwh;
Database changed
mysql> create table aaa(name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.05 sec)
mysql> show tables;
+---------------+
| Tables_in_cwh |
+---------------+
| aaa |
+---------------+
1 row in set (0.00 sec)
mysql> insert into aaa values('tom',20),('jerry',30);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from aaa;
+-------+------+
| name | age |
+-------+------+
| tom | 20 |
| jerry | 30 |
+-------+------+
2 rows in set (0.00 sec)
//在从数据库中查看数据是否同步:
mysql> select * from cwh.aaa;
+-------+------+
| name | age |
+-------+------+
| tom | 20 |
| jerry | 30 |
+-------+------+
2 rows in set (0.00 sec)
//可以看出同步成功了
3.1.6 从服务器IO线程和SQL线程变化
//主服务器每次重启后log-bin日志文件的编号会发生改变
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-master-bin.000003 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
[root@master164 ~]# /etc/init.d/mysqld restart
Shutting down MySQL............ SUCCESS!
Starting MySQL. SUCCESS!
[root@master164 ~]# mysql -uroot -pcwh123!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-master-bin.000004 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
//可以看出已经改变变为00004
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 192.168.112.164
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-master-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000006
Relay_Log_Pos: 381
Relay_Master_Log_File: mysql-master-bin.000004
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
//可以看出IO线程状态从yes变为connecting
//等待一段时间后在查看从服务器
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.112.164
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-master-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000008
Relay_Log_Pos: 381
Relay_Master_Log_File: mysql-master-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
//可以看出IO线程变为yes,也就说明此时同步完成
3.2主从复制两台主服务器有数据时的配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.112.164 | centos7/redhat7 mysql-5.7 |
有数据 |
从数据库 | 192.168.112.165 | centos7/redhat7 mysql-5.7 |
无数据 |
3.2.1 mysql安装
安装MySQL二进制包
3.2.2 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cwh |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//再查看从库有哪些库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql>
//此时在原来终端上创建一个库试试读锁是否生效
//此锁表的终端必须在全备配置完成以后才能退出
//全备主库并将备份文件传送到从库
[root@master164 ~]# mysqldump -uroot -pcwh123! --all-databases > /opt/master_all_backup.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master164 ~]# ls /opt/
data master_all_backup.sql yumbak
[root@master164 ~]# scp /opt/master_all_backup.sql root@192.168.112.165:/opt/
root@192.168.112.165's password:
master_al 100% 203 199.6KB/s 00:00
//解除主库的锁表状态,直接退出交互式界面即可
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@slave165 ~]# mysql -uroot -pcwh123! < /opt/master_all_backup.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@slave165 ~]# mysql -uroot -pcwh123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| cwh |
| mysql |
| performance_schema |
| sys |
+--------------------+
3.2.3 在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication slave on *.* to 'repl'@'192.168.112.165' identified by 'cwh123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.2.4 配置主数据库
[root@master164 ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
log-bin = mysql-master-bin
server-id = 1
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//重启mysql服务
[root@master164 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
//查看主库的状态
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-master-bin.000007 | 603 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3.2.5 配置从数据库
[root@slave165 ~]# vim /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
server-id = 8
relay-log = mysql-relay-bin
//重启从库的mysql服务
[root@slave165 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
//配置并启动主从复制
mysql> change master to master_host='192.168.112.164',master_user='repl',master_password='cwh123!',master_log_file='mysql-master-bin.000007',master_log_pos=603;
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: 192.168.112.164
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-master-bin.000007
Read_Master_Log_Pos: 603
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 327
Relay_Master_Log_File: mysql-master-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3.2.6 测试验证
//在主服务器的创建库:
mysql> create database ddl;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cwh |
| ddl |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
//在从数据库中查看数据是否同步:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cwh |
| ddl |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
//可以看出数据同步成功