如何在Ubuntu 18.04上配置MySQL 8.0主从复制

时间:2020-02-23 14:38:21  来源:igfitidea点击:

本教程是在Ubuntu 18.04上配置MySQL 8.0主从复制。
MySQL是一个广泛采用的开放式关系数据库管理系统。
某些基础架构设置要求我们拥有只读数据库服务器,用于读取"选择"语句等操作。

MySQL复制过程允许我们维护多个MySQL数据副本。
主设备中的所有数据都在自动进程中同步到从服务器,如果我们有灾难,则可以轻松地将从站推广到Master以进行提交操作。
复制的主要作用是在多个服务器上传播读写工作负载,以便于可扩展性。

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

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

在CentOS 7/CentOS 6上安装MySQL Server

如果我们愿意安装从上游仓库中可用的版本,请运行:

$sudo apt-get install mysql-server mysql-client

完成MySQL Server的安装后,继续配置复制。

步骤1:配置主服务器

第一个配置更改为make是设置主数据库的服务器ID:

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

在[mysqld]部分下添加下面的行。
请注意,编号集需要是唯一的,它无法在群集中的任何节点上重新使用。

server-id = 1

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

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步:在主数据库服务器上创建Replication User

我们现在需要创建一个数据库用户,以便在连接时由SLAVES使用。
以root用户身份登录MySQL数据库并创建用户:

Hyman@theitroad:~# 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, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names Jan be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create user Hyman@theitroad identified by 'password';
Query OK, 0 rows affected (0.08 sec)

授予用户 REPLICATION SLAVE特权:

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

确认创建用户的授权:

mysql> show grants for [email protected];
+——————————————————————+
| Grants for [email protected]                            |
+——————————————————————+
| 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

使用安装完成后,通过编辑文件来配置从设备:

$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:这将从站设置为只读模式。
只有具有超级特权和Replication Slave线程的用户才能能够修改其上的数据。
这可确保没有可能意外地在从站而不是主机上修改数据的应用程序。

server-id = 2:这是一个唯一的服务器标识号。
如果未设置"主机",则默认为1.

log_bin = /var/log/mysql/mysql-bin.log:这允许二进制日志记录。
这是在复制配置中充当主设备所必需的。
如果我们需要从最新备份中恢复时间点,还需要二进制日志。

完成更改后重新启动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)

拍摄当前主日志文件和位置。
然后使用从主状态命令获得的详细信息配置从服务器:

Hyman@theitroad:~# 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, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names Jan 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