在Ubuntu 20.04|18.04上配置MySQL 8.0主从复制在Ubuntu 20.04|18.04上配置MySQL 8.0主从复制在Ubuntu 20.04|18.04上配置MySQL 8.0主从复制在Ubuntu 20.04|18.04上配置MySQL 8.0主从复制
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

在Ubuntu 20.04|18.04上配置MySQL 8.0主从复制

发表 admin at 2025年2月28日
类别
  • 未分类
标签

本指南旨在帮助您在 Ubuntu 20.04|18.04 上配置 MySQL 8.0 主从复制。 MySQL是一个被广泛采用的开源关系数据库管理系统。某些基础设施设置要求您有一个只读数据库服务器来执行 SELECT 语句等读取操作。

MySQL 复制过程允许您维护 MySQL 数据的多个副本。主服务器中的所有数据都会自动同步到从服务器,如果发生灾难,您可以轻松地将从服务器升级为主服务器以进行提交操作。复制的主要作用是将读写工作负载分散到多个服务器上,以实现轻松的可扩展性。

此设置将使用以下服务器详细信息:

Master MySQL Server: 10.131.74.92
Slave MySQL Server:  10.131.35.167

设置先决条件:

您需要在所有服务器上安装 MySQL Server 才能继续,请参阅以下 MySQL Server 安装指南:

  • 如何在 Ubuntu 18.04/16.04 上安装 MySQL 8.0
  • 如何在 Ubuntu 20.04 上安装 MySQL 8.0

MySQL 服务器安装完成后,继续配置复制。

第 1 步:在 Ubuntu 上配置主服务器

要进行的第一个配置更改是设置主数据库的服务器 ID:

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

在[mysqld]部分下添加以下行。请注意,编号集需要是唯一的,不能在集群中的任何节点上重复使用。

server-id = 1

设置 log_bin 位置,这是所有复制信息所在的位置。在 master 上所做的所有更改都会写入此文件。所有从站都会从中复制数据。

log-bin = /var/log/mysql/mysql-bin.log
tmpdir = /tmp
binlog_format = ROW
max_binlog_size = 500M
sync_binlog = 1
expire-logs-days = 7
slow_query_log

一个完整的简单配置如下所示:

[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
server-id = 1
log-bin = /var/log/mysql/mysql-bin.log
tmpdir = /tmp
binlog_format = ROW
max_binlog_size = 500M
sync_binlog = 1
expire-logs-days = 7
slow_query_log

重新启动 mysql 服务以使更改生效:

sudo systemctl restart mysql

步骤2:在主数据库服务器上创建复制用户

我们现在需要创建一个数据库用户,供从站连接时使用。以root用户登录MySQL数据库并创建用户:

root@node-01:~# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.11 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE USER [email  IDENTIFIED BY 'StrongPassw0rd';
Query OK, 0 rows affected (0.08 sec)

授予用户REPLICATION SLAVE权限:

mysql> grant replication slave on *.* to [email ;
Query OK, 0 rows affected (0.09 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

确认已创建用户的授权:

mysql> show grants for [email ;
+------------------------------------------------------------------+
| Grants for [email                             |
+------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `replica_user`@`10.131.35.167` |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

步骤3:安装并配置从服务器

在从服务器上安装 MySQL Server 8.0 的过程与主服务器类似。使用之前共享的链接进行安装。

如何在 Ubuntu 18.04/16.04 上安装 MySQL 8.0

如何在 Ubuntu 20.04 上安装 MySQL 8.0

安装完成后,通过编辑文件来配置从站:

$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
log_bin = /var/log/mysql/mysql-bin.log
server-id = 2
read_only = 1
tmpdir = /tmp
binlog_format = ROW
max_binlog_size = 500M
sync_binlog = 1
expire-logs-days = 7
slow_query_log   = 1

read_only=1:这会将从属设备设置为只读模式。只有具有 SUPER 权限的用户和复制从线程才能修改其上的数据。这确保了没有应用程序可以意外修改从站而不是主站上的数据。

server-id=2:这是唯一的服务器标识号。如果未设置“master-host”,则默认为1。

log_bin=/var/log/mysql/mysql-bin.log:这会启用二进制日志记录。这是在复制配置中充当 MASTER 所必需的。如果您需要能够从最新备份进行时间点恢复,则还需要二进制日志。

完成更改后重新启动 mysql 服务器:

sudo systemctl restart mysql

第4步:初始化复制过程

我们应该准备好在从服务器上启动复制过程。首先检查主服务器上的状态:

mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 155
Binlog_Do_DB: 
Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

记下当前的主日志文件和位置。然后使用从 master status 命令获取的详细信息配置从服务器:

root@node-02:~# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CHANGE MASTER TO MASTER_HOST='10.131.74.92',
-> MASTER_USER='rpl_user',
-> MASTER_PASSWORD='password',
-> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=155;
Query OK, 0 rows affected, 2 warnings (0.11 sec)

然后在从机上开始复制:

mysql> start slave;
Query OK, 0 rows affected (0.06 sec)

要检查从站状态,请使用:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.131.74.92
                  Master_User: rpl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 550
               Relay_Log_File: node-02-relay-bin.000002
                Relay_Log_Pos: 717
        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: 550
              Relay_Log_Space: 927
              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: d62cd5d2-784a-11e8-9768-eacea5a1be5e
             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
1 row in set (0.00 sec)

Slave IO和SQL应该指示运行状态:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

这证明我们的复制设置正在按预期工作。

要阅读的数据库书籍:

  • 学习 MySQL/MariaDB 数据库的最佳书籍
©2015-2025 Norria support@alaica.com