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. 主从复制配置
主从复制配置步骤:
1.确保从数据库与主数据库里的数据一样
2.在主数据库里创建一个同步账号授权给从数据库使用
3.配置主数据库(修改配置文件)
4.配置从数据库(修改配置文件)
需求:
搭建两台MySQL
服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.220.134 | RHEL8/ mysql-5.7 | 有数据 |
从数据库 | 192.168.220.135 | RHEL8/ mysql-5.7 | 无数据 |
3.1 mysql安装
下载到/usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# ls
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
解压到/usr/local
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql-5.7.34-linux-glibc2.12-x86_64 sbin share src
创建用户和组创建用户和组
[root@localhost ~]# groupadd -r mysql
useradd -M -s /sbin/nologin -g mysql mysql
修改属主属组
[root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64 mysql
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
.....
drwxr-xr-x. 9 mysql mysql 129 Aug 30 04:53 mysql
添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql
建立数据存放目录
[root@localhost ~]# mkdir /opt/data -p
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 30 05:00 data
初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data
2021-08-30T09:04:19.992958Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-30T09:04:20.122553Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-30T09:04:20.144643Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-30T09:04:20.148207Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 42fd1efa-0971-11ec-b90a-000c29bb4cb2.
2021-08-30T09:04:20.149165Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-30T09:04:21.004032Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-30T09:04:21.339873Z 1 [Note] A temporary password is generated for root@localhost: =*tbdjhekly>
[root@localhost ~]# echo '*tbdjhekly>' > pass
[root@localhost ~]# ls pass
pass
查看是否缺少依赖包
[root@localhost ~]# ldd /usr/local/mysql/bin/mysql
linux-vdso.so.1 (0x00007ffe9fd3c000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f9d26825000)
librt.so.1 => /lib64/librt.so.1 (0x00007f9d2661c000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f9d26418000)
libncurses.so.5 => not found)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f9d26083000)
libm.so.6 => /lib64/libm.so.6 (0x00007f9d25d01000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f9d25ae9000)
libc.so.6 => /lib64/libc.so.6 (0x00007f9d25727000)
/lib64/ld-linux-x86-64.so.2 (0x00007f9d26a45000)
libtinfo.so.5 => not found
[root@localhost ~]# yum whatprovides libtinfo.so.5
Failed to set locale, defaulting to C.UTF-8
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 6 days, 20:05:31 ago on Mon Aug 23 09:02:10 2021.
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
Repo : baseos
Matched from:
Provide : libtinfo.so.5
[root@localhost ~]# yum -y install ncurses-compat-libs
生成配置文件
[root@localhost ~]# vi /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
skip-name-resolve
修改配置文件
[root@localhost support-files]# pwd
/usr/local/mysql/support-files
[root@localhost support-files]# vim mysql.server
# overwritten by settings in the MySQL configuration files.
basedir=/usr/local/mysql # 安装mysql的位置
datadir=/opt/data # 数据存放的位置
启动服务
[root@localhost ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
登录数据库修改密码
[root@localhost ~]# mysql -uroot -p'*tbdjhekly>'
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.34
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> set password=password('syb123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
[root@localhost ~]# mysql -uroot -psyb123
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.34 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
第二台虚拟机二进制安装MySQL步骤是一样的
3.2 MySQL主从配置(数据一致时)
查看两边数据库数据是否一致
主库
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@localhost ~]# mysql -uroot -syb123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
从库
[root@localhost ~]# hostnamectl set-hostname salve
[root@localhost ~]# bash
[root@salve ~]# mysql -uroot -psyb123 -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 'rhel'@'192.168.220.135' identified by 'rhel';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;(刷新)
Query OK, 0 rows affected (0.00 sec)
配置主数据库
[root@master ~]# vim /etc/my.cnf
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
skip-name-resolve
# replacation slave config
log-bin = mysql_bin //启用binlog日志
server-id = 10 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
//重启服务
[root@master ~]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.Logging to '/opt/data/master.err'.
SUCCESS!
//查看主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置从数据库
[root@salve ~]# cat /etc/my.cnf
[mysqld]
datadir = /opt/data
basedir = /usr/local/mysql
pid-file = /opt/data/mysql.pid
socket = /tmp/mysql.sock
port = 3306
skip-name-resolve
# replacation slave config
relay-log = mysql_relay //启用中继日志relay-log
server-id = 20 //设置从库的唯一标识符,从库的server-id值必须大于主库的值
//重启数据库
[root@salve ~]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.Logging to '/opt/data/salve.err'.
SUCCESS!
//配置并启动主从复制
mysql> change master to
-> master_host='192.168.220.134', (IP是主库的IP)
-> master_user='rhel', (用户是主库创建的同步账户)
-> master_password='rhel', (密码是主库创建的同步账户密码)
-> master_log_file='mysql_bin.000001', (log_file是主库状态里的文件)
-> master_log_pos=154; (log_pos是主库状态里的Position)
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: Waiting for master to send event
Master_Host: 192.168.220.134
Master_User: rhel
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql_relay.000004
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes //必须是yes
Slave_SQL_Running: Yes //必须是yes
验证
在主库创建库,从库查看
//主库创建school库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database student;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//从库查看是否同步
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
+--------------------+
5 rows in set (0.00 sec)
MySQL主从配置(数据不一致时)
主从数据库数据不一致时,将主库的数据备份传到从库,再进行配置
查看主库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use student
Database changed
mysql> select * from student;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | zs | 21 |
+----+------+------+
| 2 | ls | 25 |
+----+------+------+
| 3 | ww | 24 |
+----+------+------+
3 row 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)
//此锁表的终端必须在备份完成以后才能退出
//备份主库并将备份文件传送到从库
[root@master ~]# mysqldump -uroot -psyb123 --all-databases > all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# ls
all.sql anaconda-ks.cfg
//传输到从库
[root@master ~]# scp /root/all.sql root@192.168.220.135:/root/
The authenticity of host '192.168.220.135 (192.168.220.135)' can't be established.
ECDSA key fingerprint is SHA256:tMfq4r/I6NXr8WxEv9mh+SJI75Dkju8az0Rx6NMuBBY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.220.135' (ECDSA) to the list of known hosts.
root@192.168.220.135's password:
Permission denied, please try again.
root@192.168.220.135's password:
all.sql 100% 853KB 75.5MB/s 00:00
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
在从库上恢复主库的文件达到数据一致
[root@salve ~]# ls
all.sql anaconda-ks.cfg
[root@salve ~]# mysql -uroot -psyb123 < all.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@salve ~]# mysql -uroot -syb123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
+--------------------+
[root@salve ~]# mysql -uroot -psyb123 -e 'select * from school.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | zs | 21 |
+----+------+------+
| 2 | ls | 25 |
+----+------+------+
| 3 | ww | 24 |
+----+------+------+
在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication slave on *.* to 'rhel'@'192.168.220.135' identified by 'rhel';
Query OK, 0 rows affected, 1 warning (0.00 sec)
// 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
配置主数据库
[root@master ~]# vim /etc/my.cnf
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
skip-name-resolve
# replacation slave config
log-bin = mysql_bin //启用binlog日志
server-id = 10 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
//重启服务
[root@master ~]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.Logging to '/opt/data/master.err'.
SUCCESS!
//查看主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置从数据库
[root@salve ~]# vim /etc/my.cnf
[mysqld]
datadir = /opt/data
basedir = /usr/local/mysql
pid-file = /opt/data/mysql.pid
socket = /tmp/mysql.sock
port = 3306
skip-name-resolve
# replacation slave config
relay-log = mysql_relay //启用中继日志relay-log
server-id = 20 //设置从库的唯一标识符,从库的server-id值必须大于主库的值
//重启数据库
[root@salve ~]# /usr/local/mysql/support-files/mysql.server restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.Logging to '/opt/data/salve.err'.
SUCCESS!
//配置并启动主从复制
mysql> change master to
-> master_host='192.168.220.134', (IP是主库的IP)
-> master_user='rhel', (用户是主库创建的同步账户)
-> master_password='rhel', (密码是主库创建的同步账户密码)
-> master_log_file='mysql_bin.000001', (log_file是主库状态里的文件)
-> master_log_pos=154; (log_pos是主库状态里的Position)
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: Waiting for master to send event
Master_Host: 192.168.220.134
Master_User: rhel
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql_relay.000004
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes //必须是yesseLinux,重启mysql服务
Slave_SQL_Running: Yes //必须是yes
验证
在主库创建库,从库查看
//主库创建syb库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database syb;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| syb |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//从库查看是否同步
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| syb |
| sys |
+--------------------+
5 rows in set (0.00 sec)