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. 主从复制配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 172.16.12.128 | centos7/redhat7 mysql-5.7 | 有数据 |
从数据库 | 172.16.12.129 | centos7/redhat7 mysql-5.7 | 无数据 |
3.1 mysql安装
分别在主从两台服务器上安装mysql-5.7版本,此处略过安装步骤,若有疑问请参考《mysql基础》与《mysql进阶》两篇文章。
//安装依赖包
[root@yh ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
Installing:
cmake x86_64 2.8.12.2-2.el7 myrepo 7.0 M
mariadb-devel x86_64 1:5.5.56-2.el7 myrepo 752 k
ncurses-devel x86_64 5.9-13.20130511.el7 myrepo 713 k
Installing for dependencies:
libarchive x86_64 3.1.2-10.el7_2 myrepo 319 k
Transaction Summary
================================================================================================
Install 3 Packages (+1 Dependent package)
Total download size: 8.8 M
.....
Installed:
cmake.x86_64 0:2.8.12.2-2.el7 mariadb-devel.x86_64 1:5.5.56-2.el7
ncurses-devel.x86_64 0:5.9-13.20130511.el7
Dependency Installed:
libarchive.x86_64 0:3.1.2-10.el7_2
Complete!
//创建用户和组
[root@yh src]# groupadd -r -g 306 mysql
[root@yh src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
//下载二进制格式的mysql软件包
[root@yh ~]# cd /usr/src/
[root@yh src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
--2018-08-13 23:56:27-- https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
Connecting to downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz [following]
......
Saving to: ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’
100%[=====================================>] 643,790,848 2.46MB/s in 4m 20s
2018-08-14 00:00:50 (2.36 MB/s) - ‘mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz’saved [643790848/643790848]
//解压软件至/usr/local/
[root@yh src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@yh ~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
[root@yh ~]# cd /usr/local/
[root@yh local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@yh local]# ll
total 0
drwxr-xr-x. 2 root root 6 Mar 10 2016 bin
drwxr-xr-x. 2 root root 6 Mar 10 2016 etc
drwxr-xr-x. 2 root root 6 Mar 10 2016 games
drwxr-xr-x. 2 root root 6 Mar 10 2016 include
drwxr-xr-x. 2 root root 6 Mar 10 2016 lib
drwxr-xr-x. 2 root root 6 Mar 10 2016 lib64
drwxr-xr-x. 2 root root 6 Mar 10 2016 libexec
lrwxrwxrwx 1 root root 36 Aug 14 16:00 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x 9 root root 129 Aug 14 00:16 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Mar 10 2016 sbin
drwxr-xr-x. 5 root root 49 Jun 13 19:03 share
drwxr-xr-x. 2 root root 6 Mar 10 2016 src
//修改目录/usr/local/mysql的属主属组
[root@yh ~]# chown -R mysql.mysql /usr/local/mysql
[root@yh ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 Aug 14 16:00 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
//添加环境变量
[root@yh ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@yh ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@yh ~]# . /etc/profile.d/mysql.sh
[root@yh ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
[root@yh mysql]# mkdir /opt/data
[root@yh mysql]# chown -R mysql.mysql /opt/data/
[root@yh mysql]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Aug 14 16:54 data
//初始化数据库
[root@yh ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-04-29T04:05:32.038502Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-04-29T04:05:32.499955Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-04-29T04:05:32.613609Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-04-29T04:05:32.669108Z 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: 089a6629-6a34-11e9-b210-000c295c6114.
2019-04-29T04:05:32.672769Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-04-29T04:05:32.673879Z 1 [Note] A temporary password is generated for root@localhost: rFpVEXawu9#:
//请注意,这个命令的最后会生成一个临时密码,此处密码是 rFpVEXawu9
//再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到
//配置mysql
[root@yh ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@yh ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@yh ~]# ldconfig -v
ldconfig: Can't stat /libx32: No such file or directory
ldconfig: Path `/usr/lib' given more than once
ldconfig: Path `/usr/lib64' given more than once
ldconfig: Can't stat /usr/libx32: No such file or directory
/usr/lib64/mysql:
libmysqlclient.so.18 -> libmysqlclient_r.so
/usr/local/mysql/lib:
libmysqlclient.so.20 -> libmysqlclient.so.20.3.9
......
/lib/sse2: (hwcap: 0x0000000004000000)
/lib64/sse2: (hwcap: 0x0000000004000000)
/lib64/tls: (hwcap: 0x8000000000000000)
[root@yh ~]# ldconfig -p |grep mysql
libmysqlclient.so.20 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.20
libmysqlclient.so.18 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.18
libmysqlclient.so (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so
libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so
//生成配置文件
[root@yh ~]# cat > /etc/my.cnf <<EOF
[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
EOF
//配置服务启动脚本
[root@yh ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@yh ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@yh ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@yh ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@yh ~]# ps -ef|grep mysql
root 1521 1 0 01:58 pts/0 00:00:00 /bin/sh /usr/local/mysql/binmysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 1699 1521 0 01:58 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 1734 1301 0 01:59 pts/0 00:00:00 grep --color=auto mysql
[root@yh ~]# 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 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//修改密码
//使用临时密码登录
[root@yh ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22
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>
//设置新密码
mysql> set password = password('yh123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
3.2 mysql主从配置(2个新的mysql服务器)
在主数据库里创建一个同步账号授权给从数据库使用
mysql> create user 'yh'@'192.168.19.140' identified by 'yh';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'yh'@'192.168.19.140';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
配置主数据库
[root@yh ~]# 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
log-bin = mysql-bin
server-id = 1
log-error = /var/log/mysqld.log
重启mysql服务
[root@yh ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
查看主库的状态
r/local/mysql/bin/mysql -uroot -pyh123456 -e 'show master status;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
配置从数据库
[root@yh1 ~]# 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=2
relay-log=mysql-relay-bin
symbolic-links=0
log-error=/var/log/mysqld.log
[root@yh1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
配置并启动主从复制
[root@yh1 ~]# mysql -uroot -pyh123456
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 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> change master to
-> master_host='192.168.19.128',
-> master_user='yh',
-> master_password='yh',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
\\在主服务器上关闭防火墙 selinux
mysql> stop slave;
Query OK, 0 rows affected (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.19.128
Master_User: yh
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
测试验证
在主服务器创建aiwhs库和it表并插入数据:
mysql> create database yh1;
Query OK, 1 row affected (0.00 sec)
mysql> use yh1
Database changed
mysql> create table student1 (name VARCHAR(100) NOT NULL,age tinyint);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into student values ('tom',20),('jerry',23),('wangqing',25);
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student1;
+----------+------+
| name | age |
+----------+------+
| tom | 20 |
| jerry | 23 |
| wangqing | 25 |
+----------+------+
3 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
[root@yh1 ~]# /usr/local/mysql/bin/mysql -uroot -pyh123456 -e 'select * from yh1.student1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------+------+
| name | age |
+----------+------+
| tom | 20 |
| jerry | 23 |
| wangqing | 25 |
+----------+------+
3.3 mysql主从配置
确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
先查看主库有哪些库
[root@yh ~]# mysql -uroot -pyh123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| yh1 |
+--------------------+
再查看从库有哪些库
[root@yh1 ~]# mysql -uroot -pyh123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| yh1 |
+--------------------+
全备主库
全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
此锁表的终端必须在备份完成以后才能退出
备份主库并将备份文件传送到从库
[root@yh ~]# mysqldump -uroot -pyh123456 --all-databases >/opt/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@yh ~]# ls /opt/
all.sql my.cnf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz mysqld
解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@yh1 ~]# cd /opt/
[root@yh1 ~]# ll
total 630544
-rw-r--r--. 1 root root 792967 May 15 07:50 all-201905151548.sql
-rw-r--r--. 1 root root 155 May 15 06:20 my.cnf
-rw-r--r--. 1 7161 31415 644862820 Dec 21 11:23 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
-rwxr-xr-x. 1 root root 10601 May 15 06:20 mysqld
[root@yh1 ~]# mysql -uroot -pjbgsn123! < all-201905151548.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@yh1 ~]# mysql -uroot -pyh123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| yh1 |
| mysql |
| performance_schema |
| sys |
+--------------------+
在主数据库里创建一个同步账号授权给从数据库使用
mysql> create user 'yh'@'192.168.19.140' identified by 'yh';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'yh'@'192.168.19.140';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
配置主数据库
[root@yh ~]# /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-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) //发现,重启mysql后log-bin会变,position也会变,之前配的从服务器大约10秒后会同步状态
配置从数据库
[root@yh ~]# 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 = 3 //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log = mysql-relay-bin //启用中继日志relay-log
symbolic-links = 0
log-error = /var/log/mysqld.log
重启从库的mysql服务
[root@yh ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
配置并启动主从复制
mysql> change master to
-> master_host='192.168.19.128',
-> master_user='yh',
-> master_password='yh',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
\\在主服务器上关闭防火墙 selinux
mysql> stop slave;
Query OK, 0 rows affected (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.19.128
Master_User: yh
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
测试验证
在主服务器的yh1库的student1表中插入数据:
mysql> insert into student values ('tom',20),('jerry',23),('wangqing',25);
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student1;
+----------+------+
| name | age |
+----------+------+
| tom | 20 |
| jerry | 23 |
| wangqing | 25 |
+----------+------+
3 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
[root@yh1 ~]# /usr/local/mysql/bin/mysql -uroot -pyh123456 -e 'select * from yh1.student1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------+------+
| name | age |
+----------+------+
| tom | 20 |
| jerry | 23 |
| wangqing | 25 |
+----------+------+