mysql主从复制
1: 主从复制的作用
数据库主从复制是一种常见的数据库复制技术,在MySQL等数据库管理系统中得到广泛应用。它的原理是将一个数据库主服务器的数据,复制到一个或多个其他数据库服务器。其他的数据库服务器可以用于读取查询、备份、灾难恢复等目的。
2: 数据库主从复制原理
主从复制的形式有5种
(1):一主一从: 一台主服务器将数据复制给一台从服务器
(2):主主复制: 两台主服务器将数据互相复制
(3):一主多从: 一台主服务器将数据复制给多台从服务器
(4):多主一从: 多台主服务器将数据复制给一台从服务器
(5):联级复制:一台主服务器复制给一台从服务器,再由这台从服务器将数据复制给其他从服务器。
主从复制是基于异步复制的模式,其中主服务器将写操作记录在二进制日志(binary log)中,二进制日志中记录了所有的数据库写操作,然后将这些日志通过log dump线程传输给从服务器的i/O进程。从服务器接收到日志以后,生成两个线程,一个i/O线程(负责接受和请求主服务器日志信息)、一个SQL线程(负责将请求的二进制日志解析成具体操作进行执行),按照顺序来执行这些操作,将数据一条一条写入数据库,以保持与主服务器的数据一致。
3:主从复制的步骤
-
主服务器产生二进制日志(binary log):【在主服务器上进行的所有写操作都会被记录在二进制日志中,包括数据修改、插入和删除操作。】
-
从服务器连接到主服务器:【从服务器通过配置连接信息,和主服务器建立连接。】
-
从服务器请求并获取二进制日志:【从服务器发送一个请求,要求获取主服务器上的二进制日志。】
-
主服务器将二进制日志发送给从服务器:【主服务器收到从服务器的请求后,将二进制日志传送给从服务器。】
-
从服务器将二进制日志应用到本地数据库:【从服务器接收到二进制日志后,将其应用到本地的数据库中,重放日志中所记录的操作,以保持与主服务器的数据一致性。】
-
从服务器定期与主服务器同步:【从服务器和主服务器之间会建立一个心跳机制,定期进行检查和同步,以确保数据的一致性。】
4:主从复制的配置
搭建两台数据库服务器,一台为主服务器,另一台为从服务器
数据库角色 | IP地址 | 应用系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.62.130 | redhat8/mariadb | 有 |
从数据库 | 192.168.62.134 | centos8/mariadb | 无 |
1:关闭防火墙和selinux
#: 主服务器
[root@redhat8 ~]# systemctl disable -- now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@redhat8 ~]# setenforce 0
#:从服务器
[root@centos8 ~]# systemctl disable -- now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@centos8 ~]# setenforce 0
2:使用yum 安装mariadb 和mariadb-server
主服务器:
[root@redhat8 ~]# yum -y install mariadb mariadb-server
从服务器:
[root@centos8 ~]# yum -y install mariadb mariadb-server
3:mysql主从配置具体操作
#: 1: 查看两台服务其中有哪些库
#: 主服务器
[root@redhat8 ~]# mysql -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
#: 从服务器
[root@centos8 ~]# mysql -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
#: 2: 全备主库,并将备份文件传送到从库
#: 重开一个新的终端,将数据库加上读锁,防止备份期间数据写入,锁表的终端在数据备份完成之前不能退出数据库,解除锁表状态退出数据库即可。
[root@redhat8 ~]# mysql
MariaDB [(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> exit
Bye
[root@redhat8 ~]# mysqldump --all-databases > /opt/all-04-54.sql
[root@redhat8 ~]# ls /opt
all-04-54.sql
[root@redhat8 ~]# scp /opt/all-04-54.sql root@192.168.62.134:/opt/
The authenticity of host '192.168.62.134 (192.168.62.134)' can't be established.
ECDSA key fingerprint is SHA256:aF17Do7+YuKwngd3tIH1HwjCA6FElSgzKGVw/Kzsx84.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes 【是否接受秘钥】
Warning: Permanently added '192.168.62.134' (ECDSA) to the list of known hosts.
root@192.168.62.134's password: 【对面主机的密码】
all-04-54.sql 100% 468KB 51.3MB/s 00:00
#: 在从库上查看是否上传成功,
[root@centos8 ~]# ls /opt
all-04-54.sql
#: 恢复主库的备份并并确认与主库一致
[root@centos8 ~]# mysql < /opt/all-04-54.sql
[root@centos8 ~]# mysql -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
#: 在主数据库上创建同步账户供从库使用
[root@redhat8 ~]# mysql
MariaDB [(none)]> create user 'liu'@'192.168.62.134' identified by 'liu123';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> grant replication slave on *.* to 'liu'@'192.168.62.134';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
#: 编辑配置文件,
#: 主服务器:
[root@redhat8 ~]# vi /etc/my.cnf.d/mariadb-server.cnf
--------省略----------
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
log-bin = mysql-bin
server-id = 10
-----省略---------
#: 从服务器:
[root@centos8 ~]# vi /etc/my.cnf.d/mariadb-server.cnf
--------省略----------
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
relay-log = mysql-relay-bin
server-id = 20
symbolic-links=0
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
------省略------------
#: 将两台数据库服务器服务重启
[root@redhat8 ~]# systemctl restart mariadb
[root@centos8 ~]# systemctl restart mariadb
#: 查看主库状态
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 968 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
#: 在从服务器上启动主从复制
MariaDB [(none)]> change master to
master_host='192.168.62.130',
master_user='liu',
master_password='liu123',
master_log_file='mysql-bin.000001',
master_log_pos=968;
Query OK, 0 rows affected (0.004 sec)
#: 查看从服务器状态
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.62.130
Master_User: liu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 968
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: yes
Slave_SQL_Running: yes
Replicate_Do_DB:
Replicate_Ignore_DB:
测试验证:
#: 主服务器
MariaDB [tast]> show tables;
+----------------+
| Tables_in_tast |
+----------------+
| student |
+----------------+
1 row in set (0.000 sec)
MariaDB [tast]> insert into student (name,age) values ('tom',20),('jerry',23),('wangqing',25),('sean',28),('zhangshan',26),('zhengshen',20),('lisi',null),('chenshuo',10),('wangwu',3),('qiuyi',15),('qiuxiaotian',20);
Query OK, 11 rows affected (0.001 sec)
Records: 11 Duplicates: 0 Warnings: 0
MariaDB [tast]> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhengshen | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.000 sec)
#: 从服务器
MariaDB [(none)]> use tast
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
MariaDB [tast]> show tables;
+----------------+
| Tables_in_tast |
+----------------+
| student |
+----------------+
1 row in set (0.000 sec)
MariaDB [tast]> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhengshen | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.000 sec)