如何在Ubuntu 18.04/Debian 9上配置MariaDB复制

时间:2020-02-23 14:40:46  来源:igfitidea点击:

在本教程中,我们将介绍如何配置MariaDB Master-Slave
在Ubuntu 18.04和Debian 9服务器上进行复制。 MariaDB是一个
社区开发的MySQL关系数据库管理分支
系统,其开发,安全性和
改进。

MariaDB复制过程使我们可以维护多个
MySQL数据的副本。主服务器中的所有数据均已同步到从服务器
在自动化的过程中,如果我们遇到灾难,我们可以轻松地
将Slave提升为Master进行提交操作。主要作用
复制是为了将读写工作负载分散到多个
服务器,可轻松扩展。

在Ubuntu 18.04/Debian 9上安装MariaDB

我有两个节点将用于设置MariaDB
主从复制。第一个节点将充当主节点,而
第二个是奴隶。

Node 1: 192.168.18.40
Node 2: 192.168.18.41

本演示中使用的MariaDB版本是10.3. 我们可以将10.3替换为要安装的MariaDB版本。

在Ubuntu 18.04上安装MariaDB 10.3

使用以下命令在Ubuntu 18.04服务器上安装MariaDB 10.3.

sudo apt update
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64] http://mirror.zol.co.zw/mariadb/repo/10.3/ubuntu bionic main'
sudo apt update
sudo apt install mariadb-server mariadb-client

在Debian 9上安装MariaDB 10.3

添加MariaDB 10.3存储库并安装mariadb-server软件包。

sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mirror.zol.co.zw/mariadb/repo/10.3/debian stretch main'
sudo apt-get update && sudo apt-get install mariadb-server

出现提示时设置root密码

While not mandatory, it is highly recommended that you set a password for 
the MariaDB administrative "root" user.

If this field is left blank, the password will not be changed.

New password for the MariaDB "root" user:
Repeat password for the MariaDB "root" user:

配置MariaDB主服务器

在两台服务器上都安装了MariaDB之后,通过ssh登录到节点1(主节点),并将侦听地址更改为服务器的实际IP地址。编辑文件" /etc/mysql/my.cnf",并在" mysqld"部分下添加以下行。

#bind-address            = 127.0.0.1
bind-address             = 192.168.18.40

设置服务器ID,它将是主服务器的唯一标识符。

server-id = 100

创建数据库复制用户

$mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 50
Server version: 10.3.9-MariaDB-1:10.3.9+maria~bionic-log mariadb.org binary distribution

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> grant replication slave on *.* to theitroad@localhost'%' identified by 'StrongPassword';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges; 
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> exit
Bye

重新启动MariaDB服务器,以使更改生效。

sudo systemctl restart mysql

使用ss或者netstat命令检查状态。

# ss -tunelp | grep 3306
tcp   LISTEN  0       70               192.168.18.40:3306         0.0.0.0:*      users:(("mysqld",pid=16877,fd=22)) uid:111 ino:48116 sk:4 <->

如果我们正在运行防火墙,请打开port3306

sudo ufw allow 3306

配置MariaDB从服务器

登录到一个或者多个从属服务器并配置MariaDB:

$sudo vim /etc/mysql/my.cnf

mysqld部分下设置以下值。

[mysqld]
bind-address = 192.168.18.41
server-id = 101
log_bin = /var/log/mysql/mariadb-bin
read_only = 1
report-host = mariadb-slave1
expire-logs-days = 7

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

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

log_bin =/var/log/mysql/mariadb-bin:启用
二进制日志记录。这是在复制中充当MASTER所必需的
组态。如果我们需要以下功能,则还需要二进制日志:
从最新备份中进行时间点恢复。

更改后重新启动" mariadb"。

sudo systemctl restart mysql

初始化复制过程

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

MariaDB [(none)]> show master status\G
** **** **** **** **** **** ***** 1. row ** **** **** **** **** **** *****
            File: mariadb-bin.000003
        Position: 344
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.000 sec)

记下currentMaster日志文件和位置。然后配置
从服务器,其详细信息是从master status命令获得的。

以root用户身份登录MariaDB Slave服务器并配置与主服务器的连接

$mysql -u root -p

CHANGE MASTER TO MASTER_HOST='192.168.18.40',
MASTER_USER='mysql_replica',
MASTER_PASSWORD='StrongPassword',
MASTER_LOG_FILE='mariadb-bin.000003',
MASTER_LOG_POS=344;

然后在从属服务器上开始复制:

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

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

MariaDB [(none)]> show slave status\G
** **** **** **** **** **** ***** 1. row ** **** **** **** **** **** *****
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.18.40
                   Master_User: mysql_replica
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mariadb-bin.000003
           Read_Master_Log_Pos: 344
                Relay_Log_File: mysqld-relay-bin.000002
                 Relay_Log_Pos: 557
         Relay_Master_Log_File: mariadb-bin.000003
              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: 344
               Relay_Log_Space: 867
               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: 100
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
1 row in set (0.001 sec)

从站IO和SQL应指示运行状态:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

主服务器上的进程列表检查还应显示来自从服务器的连接。

MariaDB [(none)]> select ID,user,host,db,command,time,state from information_schema.processlist order by time desc limit 5;
+----+---------------+---------------------+------+-------------+------+------------------------------------------------------------------+
| ID | user          | host                | db   | command     | time | state                                                            |
+----+---------------+---------------------+------+-------------+------+------------------------------------------------------------------+
| 38 | mysql_replica | 192.168.18.41:51522 | NULL | Binlog Dump |  988 | Master has sent all binlog to slave; waiting for binlog to be up |
|  2 | system user   |                     | NULL | Daemon      |    0 | InnoDB purge worker                                              |
|  5 | system user   |                     | NULL | Daemon      |    0 | InnoDB shutdown handler                                          |
|  1 | system user   |                     | NULL | Daemon      |    0 | InnoDB purge coordinator                                         |
|  4 | system user   |                     | NULL | Daemon      |    0 | InnoDB purge worker                                              |
+----+---------------+---------------------+------+-------------+------+------------------------------------------------------------------+
5 rows in set (0.000 sec)

如果我们是MySQL用户,请查看如何在Ubuntu 18.04上配置MySQL 8.0主从复制。