1. mysql 主从复制原理
- 在从库上执行change master to;会将主库的信息保存到从库中的master.info文件中
- 在从库执行start slave;开启io_thread, sql_thread线程;
- io_thread工作;io_thread通过master.info文件中主库的连接信息去连接主库;连接成功后主库就会开启dump_thread;
- dump_thread读取主库新产生的二进制日志;然后投递给io_thread;
- io_thread接收dump_thread投递的新的二进制日志,将日志写入到relay log(中继日志);
- io_thread就会等待主库dump_thread主动把新产生的二进制日志投递;
- sql_thread会将relay log新产生的日志恢复到数据库,写到磁盘
2. 环境规划
IP | hostname | 说明 |
192.168.50.80 | db01 | 主库 |
192.168.50.81 | db02 | 从库 |
2.1. 修改SELINUX
echo "SELINUX=disabled" > /etc/selinux/config
echo "#SELINUXTYPE=targeted " >> /etc/selinux/config
cat /etc/selinux/config
setenforce 0
2.2. 关闭防火墙
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
2.3. 关闭透明大页
echo never > /sys/kernel/mm/transparent_hugepage/enabled
2.4. 内核参数配置
vi /etc/sysctl.conf
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_max_tw_buckets = 8000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 10
vm.swappiness=0
执行sysctl -p 命令是配置生效
2.5. 修改用户资源限制
/etc/security/limits.conf
mysql soft nofile 65535
mysql hard nofile 65535
mysql soft nproc 65535
mysql hard nproc 65535
3. 安装MySQL数据库
3.1. 下载mysql8,
下载rpm bundle包上传到centos系统上。官网 MySQL :: Download MySQL Community Server (Archived Versions)
或者在云盘下载:
通过百度网盘分享的文件:mysql-8.0.33-1.el7.x86_64.rpm-bundl...
链接:百度网盘 请输入提取码
3.2. 解压bundle包
tar xvf mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar
3.3. 卸载mariadb
rpm -qa | grep mariadb
rpm -e --nodeps mariadb-libs-*
3.4. 安装 MYSQL
rpm -ivh mysql-community-common-8.0.33-1.el7.x86_64.rpm mysql-community-client-plugins-8.0.33-1.el7.x86_64.rpm mysql-community-libs-8.0.33-1.el7.x86_64.rpm mysql-community-client-8.0.33-1.el7.x86_64.rpm mysql-community-icu-data-files-8.0.33-1.el7.x86_64.rpm mysql-community-server-8.0.33-1.el7.x86_64.rpm
3.5. 初始化MySQL并启动
#初始化
mysqld --initialize --console
#授权mysql用户
chown -R mysql:mysql /var/lib/mysql/
启动服务
systemctl start mysqld
systemctl enable mysqld
#查看初始化密码
cat /var/log/mysqld.log | grep localhost
第二台主机上面执行
3.6. 登录MySQL和配置用户
mysql -u root -p #登录,输入密码
3.7. 修改密码,开启远程
#修改登录密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'my@123';
#切换到mysql数据库
use mysql;
#开启数据库远程管理
update user set Host='%' where User='root';
flush privileges;
4. 主从配置
4.1. 确保你已经安装了MySQL 8.0并且两个服务器(主服务器和从服务器)都已经正常运行。
4.2. 在主服务器(db01)上修改配置文件(my.cnf或my.ini)新增下面参数:
cat >> /etc/my.cnf <<EOF
#服务器 id,随意,但要唯一
server-id = 1
#二进制文件存放路径
log-bin = mysql-bin
#参数用于排除自带的数据库。
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
#二进制日志格式,建议使用ROW格式以获得更好的兼容性和可靠性。
binlog-format = ROW
EOF
重启从服务以使配置更改生效。
systemctl restart mysqld
4.3. 在主服务器的数据库中创建用于复制的账户并授予相应的权限。
#登录MySQL
mysql -uroot -pmy@123
use mysql;
#创建用户
CREATE USER 'db_sync'@'%' IDENTIFIED BY 'my@123';
#授权账号复制权限
GRANT REPLICATION SLAVE ON *.* TO 'db_sync'@'%';
#刷新配置
FLUSH PRIVILEGES;
4.4. 在主服务器上执行以下命令获取当前二进制日志文件的名称和位置:
SHOW MASTER STATUS;
4.5. 在从服务器(db02)上修改配置文件(my.cnf或my.ini),找到并编辑以下参数:
cat >>/etc/my.cnf <<EOF
server-id = 2
#中继日志文件的名称,用于从主服务器接收二进制日志事件。
relay-log = mysql-relay-bin
#从服务器的二进制日志文件的名称。
log_bin = mysql-bin
#不同步相关的库
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
EOF
4.6. 配置主从
登录从服务器(db02)MySQL上更据主服务器建立的账号和show master status;显示的内容,修改以下命令配置主从复制:(根据第2.4上的内容修改)
mysql -u root -pmy@123
STOP SLAVE;
CHANGE MASTER TO
MASTER_HOST = '192.168.50.81',
MASTER_USER = 'db_sync',
MASTER_PASSWORD = 'my@123',
#主服务器数据库上的file值(不能有空格)
MASTER_LOG_FILE = 'mysql-bin.000001',
#主服务器数据库的Position值
MASTER_LOG_POS = 157,
get_master_public_key=1;
4.7. 在从服务器启动同步并查看状态
start slave;
4.8. 查看同步状态
SHOW SLAVE STATUS\G;
5. 测试数据插入
[root@db01 opt]# mysql -uroot -pmy@123
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> create database testhf;
Query OK, 1 row affected (0.00 sec)
mysql> use testhf
Database changed
mysql> CREATE TABLE testhf (
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(100) NOT NULL,
-> salary DECIMAL(10, 2),
-> date DATETIME
-> );
mysql> INSERT INTO testhf (name, salary,date) VALUES ('Alice', 5000.00,sysdate());
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO testhf (name, salary,date) VALUES ('Alice', 5000.00,sysdate());
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO testhf (name, salary,date) VALUES ('Alice', 5000.00,sysdate());
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO testhf (name, salary,date) VALUES ('Alice', 5000.00,sysdate());
Query OK, 1 row affected (0.01 sec)
mysql> select * from testhf.testhf;
+----+-------+---------+---------------------+
| id | name | salary | date |
+----+-------+---------+---------------------+
| 1 | Alice | 5000.00 | 2025-01-03 17:24:04 |
| 2 | Alice | 5000.00 | 2025-01-03 17:24:12 |
| 3 | Alice | 5000.00 | 2025-01-03 17:24:13 |
| 4 | Alice | 5000.00 | 2025-01-03 17:24:14 |
| 5 | Alice | 5000.00 | 2025-01-03 17:24:16 |
+----+-------+---------+---------------------+
6. 从库状态
mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.50.81
Master_User: db_sync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 2239
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 2408
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema,performance_schema
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: 2239
Relay_Log_Space: 2618
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: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 50a17fe2-c9af-11ef-b491-000c297836df
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
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:
Master_public_key_path:
Get_master_public_key: 1
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
-- show slave status \G; 查看从库状态
Master_Host: 192.168.50.81 主库ip
Master_User: db_sync 主库用户
Master_Port: 3306 主库端口号
Connect_Retry:60 失败连接的次数
Master_Log_File: mysql-bin.000001 当前从库io_thread正在读取主库的二进制日志文件
Read_Master_Log_Pos: 2239 当前从库io_thread正在读取主库二进制日志文件的位置 --【主库:show master status;】
Relay_Log_File: mysql-relay-bin.000002 当前从库sql_thread正在读取从库的中继日志文件
Relay_Log_Pos: 2199 当前sql_thread正在读取从库中继日志文件的位置
Relay_Master_Log_File: mysql-bin.000001 当前从库sql_thread从relay log中读取的正在进行的sql语句,对应主库的sql语句是在哪个binlog中;
Exec_Master_Log_Pos: 1986 从库sql_thread当前执行的事件,对应主库的binlog中的position
Seconds_Behind_Master: 0 主从复制延迟的时间;如果是0表示主从无延迟
Last_IO_Error 显示io线程错误信息
Last_SQL_Error 显示sql线程错误信息
SQL_Delay: 0 延迟复制
Retrieved_Gtid_Set: 94fc1fbe-b7a0-11eb-b0a0-000c2969aba1:17 当前io_thread已经接受到的binlog
Executed_Gtid_Set: 92fbca79-b219-11eb-9222-000c29a1a06e:1-2, 94fc1fbe-b7a0-11eb-b0a0-000c2969aba1:1-17 当前从库sql_thread执行的二进制日志位置
-- 过滤复制条件,从库上面配置的
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema,performance_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
7. 常用命令:
- start slave; 开启io_thread和sql_thread
- start slave io_thread;
- start slave sql_thread;
- stop slave; 关闭io_thread和sql_thread
- stop slave io_thread;
- stop slave sql_thread;
- show processlist;
- reset slave; --【从库执行】
- 删除master.info,relay-log.info数据;删除所有relay log;将延迟选项master_delay设为0;
- Reset slave all 相对于RESET SLAVE,RESET SLAVE ALL还会删除内存中的连接信息,这个时候,执行start slave会报错。
- reset master;删除所有的二进制日志文件 --【主库操作前要备份好日志】