Mysql主从

时间:2020-05-08 10:23:41   收藏:0   阅读:59
MySQL主从复制虽好,能完美解决数据库单点问题吗?

一、单个数据库服务器的缺点

二、如何解决单点问题

三、MySQL主从复制架构
1、主库将变更写入到主库的binlog中

2、从库的IO线程在指定位置读取主库binlog内容存储到本地的中继日志(Relay Log)中

四. 主从复制的一些缺点

虽然主从复制增加了一个数据库副本,但从数据库和主数据库的数据最终会是一致的。之所以说是最终一致,因为MySQL复制是异步的,正常情况下主从复制数据之间会有一个微小的延迟。
通过这个数据库副本看似解决了数据库单点问题,但并不完美:因为这种架构下,如果主服务器宕机,需要手动切换从服务器,业务中断不能忍受,不能满足应用高可用的要求。

1.主从简介

在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:

1.1 主从作用

2. 主从复制原理

主从复制步骤:

3. 主从复制配置

主从复制配置步骤:

需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:Red Hat Enterprise Linux 8.0 (Ootpa)
技术分享图片

3.1mysql安装

分别在主从两台机上二进制安装mysql-5.7版本,此处略过安装步骤

3.2 mysql主从配置(2个新的mysql服务器)

这里的都是二进制安装的Mysql

3.2.1 确保从数据库与主数据库里的数据一样

为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中

//查看数据库
主库的数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wlw                |
| www                |
+--------------------+
6 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)
//备份主库并将备份文件传送到从库
[root@zhu ~]# mysqldump -uroot -pw --all-databases > /opt/all-202005071900.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@zhu ~]# ls /opt/
1  all-202005071900.sql  data
[root@zhu ~]# scp /opt/all-202005071900.sql root@192.168.66.128:/opt
The authenticity of host ‘192.168.66.128 (192.168.66.128)‘ can‘t be established.
ECDSA key fingerprint is SHA256:lpKqeNs7QFkySWsYsJ1IMnidcmZotljVvyB/y1YuWW4.
ECDSA key fingerprint is MD5:ad:d8:3d:e7:7e:aa:fd:07:79:3c:7f:ca:82:0c:42:2f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.66.128‘ (ECDSA) to the list of known hosts.
root@192.168.66.128‘s password: 
all-202005071900.sql                                                            100%  774KB  47.5MB/s   00:00    

//在从库上恢复主库的备份并查看,与主库保持一致
[root@cong ~]# ls /opt/
all-202005071900.sql  data
[root@cong ~]# mysql < /opt/all-202005071900.sql 
[root@cong ~]# mysql -e ‘show databases;‘
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wlw                |
| www                |
+--------------------+

3.2.2在主数据库里创建一个同步账号授权给从数据库使用

//在主库里面创建一个同步账号授权给从数据库
mysql> create user ‘wlw‘@‘192.168.66.128‘ identified by ‘w‘ ;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to ‘wlw‘@‘192.168.66.128‘;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.2.3配置主数据库

//配置主库
[root@zhu ~]# vim /etc/my.cnf
[mysql]
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  //启用binlog日志
server-id=1     //数据库服务器唯一的标识符,主库的值一定要大于从库
relay-log=mysql-relay-bin
symbolic-links=0

log-error=/var/log/mysqld.log

//重启mysql服务
[root@zhu ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
Failed to restart mysqld.service: Unit not found.
[root@zhu ~]# 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@zhu ~]# systemctl stop firewalld
[root@zhu ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@zhu ~]# vim /etc/selinux/config 
[root@zhu ~]# setenforce 0

3.2.4配置从数据库

//配置从库
[root@cong ~]# vim /etc/my.cnf
[mysql]
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  //启用binlog日志
server-id=2     //数据库服务器唯一的标识符,主库的值一定要大于从库
relay-log=mysql-relay-bin
symbolic-links=0

log-error=/var/log/mysqld.log

//重启mysql服务
[root@cong ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
[root@cong ~]# ss -antl
State      Recv-Q Send-Q            Local Address:Port                           Peer Address:Port              
LISTEN     0      128                           *:22                                        *:* 
[root@cong ~]# systemctl stop firewalld
[root@cong ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@cong ~]# vim /etc/selinux/config 
[root@cong ~]# setenforce 0

LISTEN     0      100                   127.0.0.1:25                                        *:*                  
LISTEN     0      128                          :::22                                       :::*                  
LISTEN     0      100                         ::1:25                                       :::*                  
LISTEN     0      80                           :::3306                                     :::*
//配置并启动主从复制
mysql> CHANGE MASTER TO
 -> MASTER_HOST=‘192.168.66.128‘,
 -> MASTER_USER=‘wlw‘,
 -> MASTER_PASSWORD=‘w‘,
 -> MASTER_LOG_FILE=‘mysql-bin.000001‘,
 -> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.33 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.66128
 Master_User: wlw
 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 //?????Yes
 Slave_SQL_Running: Yes //?????Yes
 Replicate_Do_DB:
 Replicate_Ignore_DB: 

3.2.5测试验证

//验证
 在主库表中创建内容
 mysql> use wlw;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from w;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | q    |   20 |
|  2 | w    |   23 |
|  3 | e    |   25 |
|  4 | r    |   28 |
+----+------+------+
4 rows in set (0.00 sec)

mysql>  insert into w values (5,‘sean‘,20),(6,‘tom‘,23),(7,‘jerry‘,30);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from w;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | q     |   20 |
|  2 | w     |   23 |
|  3 | e     |   25 |
|  4 | r     |   28 |
|  5 | sean  |   20 |
|  6 | tom   |   23 |
|  7 | jerry |   30 |
+----+-------+------+
7 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

在从库查看是否同步
mysql> select * from w;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | q     |   20 |
|  2 | w     |   23 |
|  3 | e     |   25 |
|  4 | r     |   28 |
|  5 | sean  |   20 |
|  6 | tom   |   23 |
|  7 | jerry |   30 |
+----+-------+------+
7 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

原文:https://blog.51cto.com/14736606/2493318

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!