1. 主从简介
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?
1.1 主从作用
- 实时灾备:一台主数据库宕机了,启用从数据库,用于故障切换
- 读写分离:主服务器可以只用于写操作,从服务器只用于读取,用于查询服务
- 备份:这个问题可以很好的解决数据丢失的问题,避免影响业务
1.2主从形式
- 一主多从 表示只有一台主服务器,多台从服务器
- 主主复制 表示互为主服务器,同时也互为从服务器
- 一主多从 ----扩展系统读取的性能,因为读是在从库读取的
- 多主一从 ----5.7开始支持
- 联级复制
2.主从的复制原理
MySQL之间数据复制的基础是二进制日志文件(binary log file)。一台MySQL数据库一旦启用二进制日志后,其作为master,它的数据库中所有操作都会以“事件”的方式记录在二进制日志中,其他数据库作为slave通过一个I/O线程与主服务器保持通信,并监控master的二进制日志文件的变化,如果发现master二进制日志文件发生变化,则会把变化复制到自己的中继日志中,然后slave的一个SQL线程会把相关的“事件”执行到自己的数据库中,以此实现从数据库和主数据库的一致性,也就实现了主从复制。
3.主从复制配置
主从复制配置步骤:(必须按顺序)
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
环境信息:
master
- ip:192.168.20.111
- Mysql版本:5.6.45
- 服务器版本:CentOS7
- 有数据
slave
- ip:192.168.20.100
- Mysql版本:5.6.45
- 服务器版本:CentOS7
- 无数据
3.1数据库安装
3.2 mysql主从配置
3.2.1 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库的数据
[root@master ~]# mysql -uroot -p'zzl123' -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> show databases;
[root@slave ~]# mysql -uroot -p'zzl123' -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@slave ~]#
//主库先设置一个读锁,以防主库和从库数据不一致
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.35 sec)
[root@master bin]# mysqldump -uroot -p'zzl123' --all-databases > all.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master bin]# cd
[root@master ~]# ls
databash
//把全备的数据传到从库
[root@master ~]# scp root@192.168.20.100:/opt/
The authenticity of host '192.168.20.100 (192.168.20.100)' can't be established.
ECDSA key fingerprint is 2a:34:f4:86:0c:1b:c8:ed:fc:d5:3c:9a:04:13:c0:39.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.20.100' (ECDSA) to the list of known hosts.
root@192.168.20.100's password:
Permission denied, please try again.
root@192.168.20.100's password:
100% 0 0.0KB/s 00:00
[root@master ~]#
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@slave opt]# mysql -uroot -p'zzl123' <
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
3.2.2 在主数据库里创建一个同步账号授权给从数据库使用
mysql> create user 'zzl'@'192.168.20.111' identified by 'zzl123';
Query OK, 0 rows affected (0.31 sec)
mysql> grant replication slave on *.* to 'zzl'@'192.168.20.111';
Query OK, 0 rows affected (0.10 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.10 sec)
mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.2.3 配置主数据库
[root@master ~]# cat /etc/
[mysqld]
basedir = /usr/local/mysql
datadir = /mysqldata
socket = /tmp/
port = 3306
pid-file = /mysqldata/
user = mysql
skip-name-resolve
log-bin = mysql-bin //启用binlog日志
server-id = 10 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
[root@master ~]#
//重启mysql服务
[root@localhost databash]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL..................... SUCCESS!
[root@localhost databash]# mysql -uroot -p'zzl123'
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.30-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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_bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
3.2.4 配置从数据库
[root@localhost databash]# cat /etc/
[mysqld]
basedir = /usr/local/mysql
datadir = /mysqldata
socket = /tmp/
port = 3306
pid-file = /mysqldata/
user = mysql
skip-name-resolve
relay-log = mysql_relay_bin //开启中继日志
server-id = 20 //设置从库的唯一标识符,从库的server-id值必须大于主库的该值
//重启从库的mysql服务
[root@localhost databash]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
//配置并启动主从复制
mysql> change master to \
-> master_host='192.168.20.100',
-> master_user='zzl',
-> master_password='zzl123',
-> master_log_file='mysql_bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.51 sec)
mysql>
//查看从服务器状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.20.100
Master_User: zzl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql_relay_bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes //此处必须为yes
Slave_SQL_Running: Yes //此处必须为yes
Replicate_Do_DB:
Replicate_Ignore_DB:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
3.2.5 测试验证
在主服务器的student库的yun表中插入数据:
mysql> insert yun(id,name,age) values(1,'zhangshan',25),(2,'lisi',23),(3,'wangwu',18);
Query OK, 3 rows affected (1.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from yun;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 25 |
| 2 | lisi | 23 |
| 3 | wangwu | 18 |
+----+-----------+------+
3 rows in set (0.02 sec)
mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
在从数据库中查看数据是否同步:
mysql> select * from yun;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | zhangshan | 25 |
| 2 | lisi | 23 |
| 3 | wangwu | 18 |
+----+-----------+------+
3 rows in set (0.02 sec)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9