mysql主从
1.主从介绍
MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的
1.1 主从的作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
1.2主从的形式
1.一主一从
2. 主主复制
3. 一主多从—扩展系统读取的性能,因为读是在从库读取的
4. 多主一从—5.7开始支持
5. 联级复制
2.主从复制原理
主从复制过程:
主上面把所有的写操作记录到binlog
日志生成logdump
线程,从上面有两个线程,I/O
线程和SQL
线程,I/O
线程去请求主上面的binlog
日志,而主上面通过logdump
线程发送binlog
日志到到从服务器上,从服务器收到了请求的binlog
日志之后,写到本地的relaylog
日志(中继日志)上面,sql
线程去读取relaylog
日志,重放relaylog
日志的内容,并解析成具体操作重新执行一遍,实现从上面的数据和主上面的数据完全一样。
3. 主从复制配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.100.100 | centos7/redhat7 mysql-5.7 | 有数据 |
从数据库 | 192.168.100.96 | centos7/redhat7 mysql-5.7 | 无数据 |
mysql安装
# 关闭防火墙和selinux
[root@96 ~]# systemctl stop firewalld
[root@96 ~]# systemctl disable firewalld
[root@96 ~]# getenforce
Disabled
# 创建用户和组
[root@96 src]# groupadd -r -g 306 mysql
[root@96 src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
# 下载二进制格式的mysql软件包
[root@96 ~]# cd /usr/src/
[root@96 src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
# 解压软件至/usr/local/
[root@96 src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
[root@96 src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@96 ~]# ls /usr/local/
bin games lib libexec sbin src
etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
[root@96 ~]# cd /usr/local/
[root@96 local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
# 修改目录/usr/local/mysql的属主属组
[root@96 ~]# chown -R mysql.mysql /usr/local/mysql
# 添加环境变量
[root@96 ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@96 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@96 ~]# . /etc/profile.d/mysql.sh
[root@96 ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# 建立数据存放目录
[root@96 mysql]# mkdir /opt/data
[root@96 mysql]# chown -R mysql.mysql /opt/data/
[root@96 mysql]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Aug 14 16:54 data
# 初始化数据库
[root@96 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-15T07:57:46.168380Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-15T07:57:50.542516Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-15T07:57:50.927286Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-15T07:57:51.071260Z 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: e8600890-a060-11e8-b1a2-000c294c50b4.
2018-08-15T07:57:51.074566Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-15T07:57:51.078089Z 1 [Note] A temporary password is generatedfor root@localhost: hfGdwnViq6,,
# 请注意,这个命令的最后会生成一个临时密码,此处密码是hfGdwnViq6,,
# 配置mysql
[root@96 ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@96 ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@96 ~]# ldconfig -v
# 生成配置文件
[root@96 ~]# 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@96 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@96 ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@96 ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
# 启动mysql
[root@96 ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@96 ~]# 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@96 ~]# mysql -uroot -p‘hfGdwnViq6,,’
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('shicailun123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
# 使用新密码登录
[root@96 ~]# mysql -uroot -p‘shicailun123!’
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| scl |
| sys |
+--------------------+
# 查看从库中有哪些库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
# 主库比从库多了一个scl库,查看主库中scl库有哪些表
mysql> use scl
Database changed
mysql> show tables;
+---------------+
| Tables_in_scl |
+---------------+
| student |
+---------------+
# 查看scl表中的内容
mysql> select * from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | lisi | 21 |
| 3 | wangwu | 22 |
| 4 | zhaoliu | 23 |
+----+-----------+------+
# 全备主库
# 全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致,加锁命令如下
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql>
# 此锁住的终端必须在另一终端备份完成后才能退出
# 备份主库并将备份文件传送到从库
[root@master ~]# mysqldump -uroot -pscl666 --all-databases > all-201908202014
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# ls
all-201908202014
[root@master ~]# scp all-201908202014 root@192.168.100.96:/root/
# 解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
# 在从库上恢复主库的备份并查看内容,确保跟主库一致
[root@slave ~]# mysql -uroot -pscl666 < all-201908202014
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@slave ~]# mysql -uroot -pscl666 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| scl |
| sys |
+--------------------+
[root@slave ~]# mysql -uroot -pscl666 -e 'select *from scl.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | lisi | 21 |
| 3 | wangwu | 22 |
| 4 | zhaoliu | 23 |
+----+-----------+------+
# 可以看到目前内容跟主库完全一致
在主数据库里创建一个同步账号授权给从数据库使用
mysql> create user 'repl'@'192.168.100.96' identified by 'repl666';
mysql> grant replication slave on *.* to 'repl'@'192.168.100.96';
mysql> flush privileges;
配置主数据库
# 编辑配置文件/etc/my.cnf 在[mysqld]这段后面加上如下内容
[root@master ~]# vim /etc/my.cnf
...
log-bin = mysql_bin # 启用binlog日志
server-id = 1 # 数据库服务器唯一标识符,主库的server-id值必须比从库的小
# 重启mysql服务
[root@master ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
# 查看主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000004 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
配置从数据库
# 编辑配置文件/etc/my.cnf 在[mysqld]这段后面加上如下内容
[root@slave ~]# vim /etc/my.cnf
...
relay-log = mysql_relay_bin # 启用relay(中继日志)日志
server-id = 2 # 数据库服务器唯一标识符,从库的server-id值必须比主库的大
# 重启从库的mysql服务
[root@slave ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
# 配置并启动主从复制
[root@slave ~]# mysql -uroot -pscl666
mysql> change master to
-> master_host='192.168.100.100',
-> master_user='repl',
-> master_password='repl666',
-> master_log_file='mysql_bin.000004',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.08 sec)
mysql>
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.100.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: mysql_relay_bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql_bin.000004
Slave_IO_Running: yes # 此处必须为yes
Slave_SQL_Running: yes # 此处必须为yes
# 若配置没问题依然显示no请重启服务后重试
验证:
在主服务器的scl库的student表中插入数据:
mysql> select *from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | lisi | 21 |
| 3 | wangwu | 22 |
| 4 | zhaoliu | 23 |
+----+-----------+------+
mysql> insert into student(name,age) values('chenxi',30),('chengsong',35);
mysql> select *from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | lisi | 21 |
| 3 | wangwu | 22 |
| 4 | zhaoliu | 23 |
| 5 | chenxi | 30 |
| 6 | chengsong | 35 |
+----+-----------+------+
在从数据库中查看数据是否同步:
mysql> select *from scl.student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 20 |
| 2 | lisi | 21 |
| 3 | wangwu | 22 |
| 4 | zhaoliu | 23 |
| 5 | chenxi | 30 |
| 6 | chengsong | 35 |
+----+-----------+------+
# 此处跟主库完全一致说明同步成功