数据库高可用
# MariaDB Galera Cluster
# 掌握要点
(1)熟悉 Gelera Cluster 数据库集群的工作原理与方式。
(2)能够搭建 MariaDB Gelera Cluster 数据库集群。
(3)学习 Gelera Cluster 集群的使用。
# MariaDB Galera Cluster
MariaDB Galera Cluster 是一套在 MySQL InnoDB 存储引擎上面实现 multi-master 及数据实时同步的系统架构,业务层面无需做读写分离工作,数据库读写压力都能按照既定的规则分发到各个节点上去。在数据方面完全兼容 MariaDB 和 MySQL。
⚫主要功能
① 同步复制。
② 真正的 multi-master,即所有节点可以同时读写数据库。
③ 自动的节点成员控制,失效节点自动被清除。
④ 新节点加入数据自动复制。
⑤ 真正的行级别,并行复制。
⑥ 用户可以直接连接集群,使用感受上与 MySQL 完全一致。
⚫局限性
① 目前复制仅仅支持 InnoDB 存储引擎,任何写入其他引擎的表,包括 mysql.*表将不会复制,但是 DDL 语句会被复制的,因此创建用户将会被复制, 但是插入用户将不会被复制的。
② DELETE 操作不支持没有主键的表,没有主键的表在不同的节点顺序将不同,如果执行 SELECT…LIMIT…,将出现不同的结果集。
③ 在多主环境下,不支持 LOCK/UNLOCK TABLES(锁定表/解锁表),以及 GET_LOCK(),RELEASE_LOCK()…(锁函数)
④ 查询日志不能保存在表中。如果开启查询日志,只能保存到文件中。
⑤ 允许最大的事务大小由wsrep_max_ws_rows和wsrep_max_ws_size定义,任何大型操作将被拒绝,如大型的 LOAD DATA 操作。
⑥ 由于集群是乐观的并发控制,事务 commit 可能在该阶段中止。如果有两个事务向在集群中不同的节点向同一行写入并提交,失败的节点将中止。对于 集群级别的中止,集群返回死锁错误代码为 Error: 1213 SQLSTATE: 40001(ER_LOCK_DEADLOCK)。
⑦ XA 事务不支持,因为在提交上可能回滚。
⑧ 整个集群的写入吞吐量是由最弱的节点限制,如果有一个节点变得缓慢,那么整个集群将变得缓慢。为了稳定的高性能要求,所有的节点应使用统一 的硬件。
⑨ 集群节点建议最少 3 个。
⑩ 如果 DDL 语句有问题将破坏集群。
# 规划节点
主机名 | 系统 | IP 地址 | 节点 |
---|---|---|---|
node1 | CentOS7.5-1708 | 192.168.1.18 | 高可用集群中一节点 |
node2 | CentOS7.5-1708 | 192.168.1.5 | 高可用集群中一节点 |
node3 | CentOS7.5-1708 | 192.168.1.13 | 高可用集群中一节点 |
# 集群部署
# 1.关闭防火墙
⚫将所有的节点关闭防火墙以及seLinux,实际生产环境使用firewalld-cmd.
[root@node1 ~]# systemctl stop firewalld
[root@node1 ~]# setenforce 0
# 2.基础环境配置
⚫以下操作在所有机器上进行
⚫修改并添加主机hosts的映射。
⚫配置mariadb-ha的yum源。 (但是得先安装unzip)
⚫安装MariaDB Galera。
[root@node1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.18 node1
192.168.1.5 node2
192.168.1.13 node3
[root@node1 ~]# unzip mariadb-ha.zip && mv mariadb-ha /opt/
[root@node1 ~]# cat /etc/yum.repos.d/zyh.repo
[centos]
name=centos
gpgcheck=0
baseurl=http://192.168.1.112/file/repo/centos/
[ha]
name=ha
baseurl=file:///opt/mariadb-ha
gpgcheck=0
[root@node1 ~]# for i in node1 node2 node3
> do
> ssh $i "yum install -y MariaDB-server MariaDB-client galera"
> done
# 3.初始规数据库
⚫在每台机器
上初始化数据库设置密码
⚫将不允许根用户远程登录设置为no
[root@node1 ~]# systemctl start mariadb
[root@node1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR
ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.
Enter current password for root (enter for none): 默认按回车 OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
Set root password? [Y/n] y #按 y 设置数据库密码
New password: #输入数据库密码
Re-enter new password: #重复数据库密码
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? [Y/n] y #按 y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n #按 n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? [Y/n] y #按 y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? [Y/n] y #按 y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
# 4.配置数据库文件
⚫以下操作在每个节点上进行
⚫在配置数据库文件的前提将数据库授权root所有访问权限,并赋予 root 用户远程权限。
⚫配置数据库的高可用,需要将配置文件不同的节点更改为不同的节点名称以及IP地址。
[root@node1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.3.22-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '000000';
Query OK, 0 rows affected (0.020 sec)
node1节点的配置文件如下:
⚫wsrep_node_name=node1 ⚫wsrep_node_address=192.168.1.18 ⚫wsrep_sst_method=rsync ⚫wsrep_causal_reads=ON ⚫innodb_buffer_pool_size=120M
[root@node1 ~]# cat /etc/my.cnf.d/server.cnf
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.1.18,192.168.1.5,192.168.1.13 #添加集群的主机IP
wsrep_node_name=node1 #修改当前的主机名称
wsrep_node_address=192.168.1.18 #修改当前的主机IP
wsrep_sst_method=rsync
wsrep_causal_reads=ON
innodb_buffer_pool_size=120M
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
bind-address=192.168.1.18 #修改成当前的主机IP
#
# Optional setting
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=0
node2节点的配置文件如下:
[root@node1 ~]# cat /etc/my.cnf.d/server.cnf
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.1.18,192.168.1.5,192.168.1.13 #添加集群的主机IP
wsrep_node_name=node2 #修改当前的主机名称
wsrep_node_address=192.168.1.5 #修改当前的主机IP
wsrep_sst_method=rsync
wsrep_causal_reads=ON
innodb_buffer_pool_size=120M
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
bind-address=192.168.1.5 #修改成当前的主机IP
#
# Optional setting
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=0
node3节点的配置文件如下:
[root@node1 ~]# cat /etc/my.cnf.d/server.cnf
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.1.18,192.168.1.5,192.168.1.13 #添加集群的主机IP
wsrep_node_name=node3 #修改当前的主机名称
wsrep_node_address=192.168.1.13 #修改当前的主机IP
wsrep_sst_method=rsync
wsrep_causal_reads=ON
innodb_buffer_pool_size=120M
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
bind-address=192.168.1.13 #修改成当前的主机IP
#
# Optional setting
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=0
在修改完配置文件并确认无误后,将 3 个节点的数据库服务关闭。
[root@node1 ~]# systemctl stop mariadb
[root@node2 ~]# systemctl stop mariadb
[root@node3 ~]# systemctl stop mariadb
# 5.启动数据库集群
⚫启动数据库集群,在不同的节点需要使用不同的命令,在 node1 节点单独启动 MariaDB Galera Cluster 服务。
⚫node2 和 node3 节点启动数据库服务。
[root@node1 ~]# galera_new_cluster
[root@node2 ~]# systemctl start mariadb
[root@node3 ~]# systemctl start mariadb
使用命令查看端口,若看到 3306 和 4567 端口,则 MariaDB Galera Cluster 集群配置成功。
[root@node1 ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.1.18:9000 0.0.0.0:* LISTEN 12378/haproxy
tcp 0 0 192.168.1.15:3306 0.0.0.0:* LISTEN 12378/haproxy
tcp 0 0 192.168.1.18:3306 0.0.0.0:* LISTEN 1813/mysqld
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 502/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1173/sshd
tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 1813/mysqld
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 978/master
tcp6 0 0 :::111 :::* LISTEN 502/rpcbind
tcp6 0 0 :::22 :::* LISTEN 1173/sshd
tcp6 0 0 ::1:25 :::* LISTEN 978/master
# 6.验证集群功能
⚫登录 node1 节点的数据库,并查看 Galera 插件是否启动。
[root@node1 ~]# mysql -uroot -p000000
MariaDB [(none)]> show status like "wsrep_ready";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_ready | ON |
+---------------+-------+
1 row in set (0.002 sec)
- 查看到 wsrep_ready 是“ON”状态,说明 wsrep 同步已开启,Galera 集群的同步方式不同于其他类型的数据库集群使用的是 bin-log 同步,Galera 集群使用的是更底层的 wsrep 方式同步。在使用的过程中,3 个节点都可以是主节点,用户可以直接连接集群,使用感受上与单节点完全一致。继续查看集群成员数量。
MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.001 sec)
- 查看 Galera 集群状态。
MariaDB [(none)]> show status like 'wsrep%';
+-------------------------------+------------------------------------------------------+
| Variable_name | Value |
+-------------------------------+------------------------------------------------------+
| wsrep_applier_thread_count | 1 |
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| wsrep_causal_reads | 8 |
| wsrep_cert_deps_distance | 1.000000 |
| wsrep_cert_index_size | 1 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 3 |
| wsrep_cluster_size | 3 |
| wsrep_cluster_state_uuid | 92088228-8e00-11ec-9433-12f0862a42d5 |
| wsrep_cluster_status | Primary |
| wsrep_cluster_weight | 3 |
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 1.000000 |
| wsrep_connected | ON |
| wsrep_desync_count | 0 |
| wsrep_evs_delayed | |
| wsrep_evs_evict_list | |
| wsrep_evs_repl_latency | 0/0/0/0/0 |
| wsrep_evs_state | OPERATIONAL |
| wsrep_flow_control_paused | 0.000000 |
| wsrep_flow_control_paused_ns | 0 |
| wsrep_flow_control_recv | 0 |
| wsrep_flow_control_sent | 0 |
| wsrep_gcomm_uuid | 9207531a-8e00-11ec-99bc-66bde1755ac8 |
| wsrep_incoming_addresses | 192.168.1.18:3306,192.168.1.5:3306,192.168.1.13:3306 |
| wsrep_last_committed | 1 |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_cached_downto | 1 |
| wsrep_local_cert_failures | 0 |
| wsrep_local_commits | 0 |
| wsrep_local_index | 0 |
| wsrep_local_recv_queue | 0 |
| wsrep_local_recv_queue_avg | 0.100000 |
| wsrep_local_recv_queue_max | 2 |
| wsrep_local_recv_queue_min | 0 |
| wsrep_local_replays | 0 |
| wsrep_local_send_queue | 0 |
| wsrep_local_send_queue_avg | 0.000000 |
| wsrep_local_send_queue_max | 1 |
| wsrep_local_send_queue_min | 0 |
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
| wsrep_local_state_uuid | 92088228-8e00-11ec-9433-12f0862a42d5 |
| wsrep_open_connections | 0 |
| wsrep_open_transactions | 0 |
| wsrep_protocol_version | 9 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy <info@codership.com> |
| wsrep_provider_version | 25.3.28(r3875) |
| wsrep_ready | ON |
| wsrep_received | 10 |
| wsrep_received_bytes | 739 |
| wsrep_repl_data_bytes | 417 |
| wsrep_repl_keys | 1 |
| wsrep_repl_keys_bytes | 32 |
| wsrep_repl_other_bytes | 0 |
| wsrep_replicated | 1 |
| wsrep_replicated_bytes | 520 |
| wsrep_rollbacker_thread_count | 1 |
| wsrep_thread_count | 2 |
+-------------------------------+------------------------------------------------------+
63 rows in set (0.001 sec)
- 查看连接的主机。
MariaDB [(none)]> show status like 'wsrep_incoming_addresses';
+--------------------------+------------------------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------------------------+
| wsrep_incoming_addresses | 192.168.1.18:3306,192.168.1.5:3306,192.168.1.13:3306 |
+--------------------------+------------------------------------------------------+
1 row in set (0.000 sec)