1、MySQL 主从复制主要用途:
(1) 读写分离
在开发工作中,有时候会遇见某个sql 语句需要锁表,导致暂时不能使用读的服务,这样就会影响现有业务,使用主从复制,让主库负责写,从库负责读,这样,即使主库出现了锁表的情景,通过读从库也可以保证业务的正常运作。
(2) 数据实时备份
当系统中某个节点发生故障时,可以方便的故障切换
(3) 高可用HA
随着系统中业务访问量的增大,如果是单机部署数据库,就会导致I/O访问频率过高。有了主从复制,增加多个数据存储节点,将负载分布在多个从节点上,降低单机磁盘I/O访问的频率,提高单个机器的I/O性能。
2、复制原理
(1)Slave上面的IO线程连接上Master,并请求从指定Binary log文件的指定位置(或者从最开始的日志)之后的日志内容;
(2)Master接收到来自Slave的IO线程的请求后,通过负责复制的IO线程根据请求信息读取指定日志指定位置之后的日志信息,返回给Slave端的IO线程。返回信息中除了日志所包含的信息之外,还包括本次返回的信息在Master端Binary log文件的名称以及在Binary log中的位置;
(3)Slave的IO线程收到信息后,将接收到的日志内容依次写入到Slave端的RelayLog文件(mysql-relay-lin.xxxxx)的最末端,并将读取到的Master端的bin-log的文件名和位置记录到master-info文件中,以便在下一次读取的时候能够清楚的告诉master“我需要从某个bin-log的哪个位置开始往后的日志内容,请发给我”
(4)Slave的SQL线程检测到Relay Log中新增加了内容后,会马上解析该Log文件中的内容成为在Master端真实执行时候的那些可执行的查询或操作语句,并在自身执行那些查询或操作语句,这样,实际上就是在master端和Slave端执行了同样的查询或操作语句,所以两端的数据是完全一样的。
3、MySQL复制过程分成三步:
- master将改变记录到二进制日志(binary log)。这些记录过程叫做二进制日志事件,binary log events;
- slave将master的binary log events拷贝到它的中继日志(relay log);
- slave重做中继日志中的事件,将改变应用到自己的数据库中。
环境准备
两台机器一主一从。
主库(MySQL Master):[ip为192.168.91.139 port为3306]
从库(MySQL Slave ):[ip为192.168.91.132 port为3306]
===================== 在两个数据不一致的环境下=====================
一、配置master主服务器
1.在/etc/my.cnf文件的 [mysql] 中添加两行代码,然后重启mysql
①设置server-id值并开启binlog参数
[root@localhost ~]# vi /etc/my.cnf
log_bin=mysql
server-id=101 #必须是集群中的唯一id
[root@localhost ~]#
②重启mysql数据库
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]#
2.建立同步账号
[root@localhost ~]# mysql -uroot -pAdmin@123
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 12
Server version: 5.7.14-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>
2.1创建复制用户,让其具有replication slave权限
mysql> grant replication slave on *.* to 'root'@'192.168.91.139' identified by 'Admin@123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
3.锁表设置只读
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)
mysql>
4.查看主库状态,得到binlog日志文件名和偏移量
mysql> show master status;
+--------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------+----------+--------------+------------------+-------------------+
| mysql.000001 | 740 | | | |
+--------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
5.备份数据库数据
[root@localhost ~]# mkdir /date
[root@localhost ~]# touch /date/mysql_alldb_$(date +%F).sql.gz
[root@localhost ~]# mysqldump -uroot -pAdmin@123 -A -B |gzip > /date/mysql_alldb_$(date +%F).sql.gz
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]#
6.解锁
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql>
7.主库备份数据传到从库
[root@localhost ~]# scp /date/mysql_alldb_2019-04-19.sql.gz 192.168.91.132:/date/mysql
root@192.168.91.132's password:
mysql_alldb_2019-04-19.sql.gz 100% 202KB 40.1MB/s 00:00
[root@localhost ~]#
二、配置slave从服务器
1.设置server-id值并关闭binlog参数,并重启数据库
[root@localhost ~]# vi /etc/my.cnf
#log_bin = /data/mysql/data/mysql-bin
server_id = 130
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]#
2.还原从主库备份数据
[root@localhost ~]# cd /date/mysql/
[root@localhost mysql]# ls
mysql_alldb_2019-04-19.sql.gz
[root@localhost mysql]# gzip -d mysql_alldb_2019-04-19.sql.gz
[root@localhost mysql]# ls
mysql_alldb_2019-04-19.sql
[root@localhost mysql]# mysql -uroot -pAdmin@123 <mysql_alldb_2019-04-19.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost mysql]#
检查还原
[root@localhost mysql]# mysql -uroot -pAdmin@123 -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 mysql]#
3.设定从主库同步
mysql> change master to
-> master_host='192.168.91.139',
-> master_port=3306,
-> master_user='root',
-> master_password='Admin@123',
-> master_log_file='mysql.000001',
-> master_log_pos=740;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql>
4.启动从库同步开关
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql>
检查状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.91.139
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql.000001
Read_Master_Log_Pos: 740
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 740
Relay_Log_Space: 154
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1045
Last_IO_Error: error connecting to master 'root@192.168.91.139:3306' - retry-time: 60 retries: 4
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 190419 13:15:17
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql>
三、测试主从是否同步
1.主服务器上创建一个新表
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql>
2.检查从服务器
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
+--------------------+
5 rows in set (0.00 sec)
mysql>
即主从实现同步