实现MySQL8.0.25集群主从复制
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现MySQL8.0.25集群主从复制相关的知识,希望对你有一定的参考价值。
mysql集群
- 避免单点故障
- 负载均衡读写分离
- 主从复制依赖于二进制日志
- 主从节点版本应该一直,如不一致,从节点应用高版本
主从复制原理
- 主开启一个线程监听并传递修改
- 从节点开启两个线程,接受修改,完成修改
配置实现MySQL主从复制
主节点配置
- 确认启用二进制日志,把二进制文件放在独立的目录中
- 分配ID号,默认是1,修改配置文件/etc/my.cnf
建议使用ip地址的主机位最后一位作为server-ID 避免ID冲突 - 确定二进制日志文件位置和二进制日志中所在位置
- 创建有复制权限的账号
MySQL8.0分成两步实现
从节点配置
- 确认启用二进制日志
- 从节点设置和主节点不同的ID号
- 从数据库开启只读模式
配置实现
- 主节点配置服务器配置文件my.cnf
[root@C8-183 ~]# cat /etc/my.cnf
[mysqld]
server-id=183 ##确保唯一的ID号
#log-bin
log-bin=/data/mysql/mysql-bin ## 独立存放的二进制日志
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
- 在mysel内创建专门用于复制的用户并赋权
mysql> create user 'cper'@'10.0.0.%' identified by 'bbbbbbbb';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'cper'@'10.0.0.%';
Query OK, 0 rows affected (0.01 sec)
- 查看当前二进制日志所记录的条目
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 | 179 | No |
| mysql-bin.000002 | 949 | No |
+------------------+-----------+-----------+
2 rows in set (0.00 sec)
- 从节点配置服务器配置文件my.cnf
[root@C8-185 ~]# cat /etc/my.cnf
[mysqld]
server-id=185 ##确保唯一的ID号
#read-only ##开启只读模式
read_only=ON #设置数据库只读,针对supper user无效
relay_log=relay-log #relay log的文件路径,默认值hostname-relay-bin
relay_log_index=relay-log.index #默认值hostname-relay-bin.index
#log-bin
log-bin=/data/mysql/mysql-bin ## 独立存放的二进制日志
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
- 进入从节点MySQL服务器
- 使用有复制权限的用户账号连接至主服务
mysql> change master to master_host='10.0.0.183', ##连接至主服务器IP
-> master_user='cper',## 主服务器创建的专门复制用的账号
-> master_password='aaaaaaaa',## 专用复制账号的密码
-> master_log_file='mysql-bin.000002', ## 二进制日志文件名
-> master_log_pos=949; ## 记录条目
Query OK, 0 rows affected, 8 warnings (0.01 sec)
- 启动复制线程
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
查看从节点监听状态
mysql> show slave status\\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.183
Master_User: cper
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 949
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 324
Relay_Master_Log_File: mysql-bin.000002
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: 949
Relay_Log_Space: 527
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: 183
Master_UUID: 811da04d-b7bd-11eb-b225-000c29d2606b
Master_Info_File: mysql.slave_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:
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: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
测试集群主从是否同步
初始状态查看
主节点导入数据库
mysql> system wget 10.0.0.88/mysql/hellodb_innodb.sql
--2021-05-18 18:32:26-- http://10.0.0.88/mysql/hellodb_innodb.sql
Connecting to 10.0.0.88:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7786 (7.6K) [application/sql]
Saving to: ‘hellodb_innodb.sql’
hellodb_innodb.sql 100%[======================================>]
2021-05-18 18:32:26 (1.08 GB/s) - ‘hellodb_innodb.sql’ saved [7786/7786]
mysql> system ls;
anaconda-ks.cfg hellodb_innodb.sql original-ks.cfg
mysql> source /root/hellodb_innodb.sql
Query OK, 0 rows affected (0.01 sec
从节点查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
同步成功
![主从复制成功](https://img-blog.csdnimg.cn/20210518183443546.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RpbW9uaXVt,size_16,color_FFFFFF,t_70)
以上是关于实现MySQL8.0.25集群主从复制的主要内容,如果未能解决你的问题,请参考以下文章