MySQL 8.4一主多从数据库集群搭建

243 阅读3分钟

一、关键步骤:

  1. linux机器三台,一主两从,安装MySQL
  2. 修改MySQL配置文件。主从节点都需要修改
  3. 在master节点上创建 用于复制的用户
  4. 配置主从复制,查看复制状态,排除故障
  5. 建库建表测试
节点IPOS角色
master192.168.0.70ubuntu 22.04主节点
nodeA192.168.0.71ubuntu 22.04从节点
nodeB192.168.0.72ubuntu 22.04从节点

二、配置修改

主节点配置修改

 vim /etc/mysql/mysql.conf.d/mysqld.cnf
 server-id       = 70   #集群中唯一
 log_bin         = mysql-bin
 binlog_format   = ROW
 bind-address    = 0.0.0.0
 binlog_ignore_db=mysql  #忽略的表
 binlog_ignore_db=information_schema  #忽略的表
 binlog_ignore_db=performance_schema  #忽略的表
 mysql_native_password=ON   #用户名密码认证

改完 需要重启 MySQL

从节点修改配置

 server_id       =71       # 节点ID 保证集群内 不重复
 mysql_native_password=ON  # 

两个从节点都需要改

更改完后 重新MySQL

 systemctl restart mysql

三、建立复制用户 并测试

主节点建立复制用户并授权

 CREATE USER 'repl'@'%' IDENTIFIED BY 'password'; # 改成自己设定的密码
 ​
 GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';  
 FLUSH PRIVILEGES;

建立用户后,需要在从节点上测试 测试复制用户是否正常登录

 mysql -u repl -p -P 3306 -h 192.168.0.70

四、开启复制

4.1 查看主节点 Binlog,在主节点执行 ,为了 确定复制的起始点,由binlog 和 position确定起始点

 SHOW BINARY LOG STATUS;
 +------------------+----------+--------------+---------------------------------------------+-------------------+
 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                            | Executed_Gtid_Set |
 +------------------+----------+--------------+---------------------------------------------+-------------------+
 | mysql-bin.000008 |     1458 |              | mysql,information_schema,performance_schema |                   |
 +------------------+----------+--------------+---------------------------------------------+-------------------+
 1 row in set (0.00 sec)

4.2 从节点创建复制源信息,需要填写主节点对应的 binlog文件和position

 CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.0.70', SOURCE_LOG_FILE='mysql-bin.000003', SOURCE_LOG_POS=208, SOURCE_PORT=3306, SOURCE_USER='repl', SOURCE_PASSWORD='password';
 START REPLICA;  # 开启复制

两个从节点都需要执行 上述操作

4.3 查看复制状态

 SHOW REPLICA STATUS\G;
 *************************** 1. row ***************************
              Replica_IO_State: Waiting for source to send event
                   Source_Host: 192.168.0.70
                   Source_User: repl
                   Source_Port: 3306
                 Connect_Retry: 60
               Source_Log_File: mysql-bin.000008
           Read_Source_Log_Pos: 1458
                Relay_Log_File: ubuntu-server-relay-bin.000002
                 Relay_Log_Pos: 1179
         Relay_Source_Log_File: mysql-bin.000008
            Replica_IO_Running: Yes
           Replica_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_Source_Log_Pos: 1458
               Relay_Log_Space: 1398

Replica_IO_Running 和 Replica_SQL_Running 都为Yes 说明正常,如果出现Connecting 状态,需要检查端口是否能连通,账户配置是否正常

五、排除故障常用

测试端口是否通

 nc -zv 127.0.0.1 3306   

查看错误日志

 tail /var/log/mysql/error.log -n 20   

修改用户认证方式

 ALTER USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY 'xxpassword';
 FLUSH PRIVILEGES;

查看认证相关插件

 SHOW PLUGINS;
  Name                             | Status   | Type               | Library | License |
 +----------------------------------+----------+--------------------+---------+---------+
 | binlog                           | ACTIVE   | STORAGE ENGINE     | NULL    | GPL     |
 | sha256_password                  | ACTIVE   | AUTHENTICATION     | NULL    | GPL     |
 | caching_sha2_password            | ACTIVE   | AUTHENTICATION     | NULL    | GPL     |
 xxx
 mysql_native_password            | ACTIVE   | AUTHENTICATION     | NULL    | GPL    

如果mysql_native_password插件必须要开启,否则复制过程 会出现错误

 Last_IO_Errno: 2061
 Last_IO_Error: Error reconnecting to source 'repl@xx:3306'. This was attempt 10/10, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

原因是 MySQL 默认的caching_sha2_password用户认证方式,和当前复制用户的认证方式不匹配

附录:主从复制相关的SQL操作命令

开启复制

 START REPLICA;

查看复制状态

 SHOW REPLICA STATUS\G;

关闭复制

 STOP REPLICA;

重置复制信息,清理复制的元信息

 RESET REPLICA ALL;