原理: 数据库在进行DDL和DML语句操作时,会被记录到binlog的日志文件里,而读取这里面的日志就可以知道数据库进行过哪些DDL和DML操作,这是主数据库的日志,从数据库经过相关配置可以实时获取到这份日志,通过IO线程把这份日志里需要同步的内容写入到从数据库的中继日志里,注意这里是写入中继日志,并不是直接写入数据库。然后SQL线程再从中继日志文件里获取日志信息,读取到相关的DDL和DML操作然后再执行同步操作。
进行主从复制操作需要先开启数据库端口(MySQL端口默认是3306)或者关闭防火墙。
如果是学习,可以选择关闭防火墙,操作如下
# 关闭防火墙服务
sudo systemctl stop firewalld
# 禁止防火墙服务开机自启动
sudo systemctl disable firewalld
如果是开发环境,建议只开启需要开启的端口,以3306为例,操作如下
# 开放3306端口的TCP协议
sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重新加载防火墙配置
sudo firewall-cmd --reload
在主库服务器进行下列配置
# 先安装纯净的mysql,配置需要设定为主库的/etc/my.cnf文件
vim /etc/my.cnf# 在文件中加上两行配置,如下,server-id是一个唯一的服务id,read-only为0代表允许读写,为1代表只允许读
server-id=1
read-only=0# 重启数据库服务
systemctl restart mysqld# 登录数据库,创建slave的user
create user 'rep'@'%' identified with mysql_native_password by 'Root@123456';# 授予用户权限
grant replication slave on *.* to 'itcast'@'%';# 查看二进制日志的文件状态
show master status;# 这时候你可能看到这样的数据
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 156 | | | |
+---------------+----------+--------------+------------------+-------------------+# 上面的记录表示当前的二进制文件记录到binlog.000001文件里面,位置是1476
# 记录上面的信息
从库进行以下配置
# 配置从库的/etc/my.cnf文件,加入下面两行配置
server-id=2
read-only=1# 重启数据库服务
systemctl restart mysqld# 登录数据库,执行下面语句,我的MySQL版本是8.0.26,据说8.0.23之前的版本命令会有些许不一致,这里的source_host换成自己的主数据库服务器ip,其他配置看情况更改
change replication source to source_host='192.168.179.136',source_user='rep',source_password='Root@123456',source_log_file='binlog.000002',source_log_pos=156;# 在mysql窗口开启主从复制
start slave;# 可以使用下面语句查看同步状态
show slave status\G;# 得到的信息如下
Slave_IO_State: Waiting for source to send eventMaster_Host: 192.168.179.136Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: binlog.000002Read_Master_Log_Pos: 156Relay_Log_File: localhost-relay-bin.000002Relay_Log_Pos: 321Relay_Master_Log_File: binlog.000002Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 156Relay_Log_Space: 534Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: f432d09b-058f-11f0-9f06-000c29058b59Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Replica has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0Network_Namespace: # 关于上面的配置,讲一些比较关键的,
# Master_Host是主库的ip
# Master_User是用户
# Master_Port是端口
# Master_Log_File是主库binlog文件名
# Read_Master_Log_Pos是主库binlog文件写入到的位置
# Relay_Log_File是中继日志文件的名称
# Relay_Log_Pos是从库中继日志写入到的位置
# Slave_IO_Running是IO线程运行的状态,主要是从主库中获取主库的binlog,写到从库的中继日志里,也就是Relay_Log_File
# Slave_SQL_Running是SQL线程运行的状态,主要是把Relay_Log_File日志里的文件给写入从库,实现数据的同步
# 如果Slave_IO_Running和Slave_SQL_Running其中之一出现了问题,那主从复制架构就已经没法正常同步数据了,需要检查并解决错误再重新开启同步
如果上述步骤都正常了,接下来就可以对主库进行建库建表和增删改数据的操作了,验证一下从库是否能成功同步主库的数据。
注意:此时的从库是无法增删改数据的,但有个例外,如果是超级管理员的权限,则依然可以进行增删改,需要在从库的/etc/my.cnf文件里加上配置【super-read-only=true】。