MariaDB Galera集群部署--技术流ken

Galera集群介绍

MariaDB集群是MariaDB同步多主机集群。它仅支持XtraDB/ InnoDB存储引擎。

主要功能

  

  • 同步复制
  • 真正的multi-master,即所有节点可以同时读写数据库
  • 自动的节点成员控制,失效节点自动被清除
  • 新节点加入数据自动复制
  • 真正的并行复制,行级
  • 用户可以直接连接集群,使用感受上与MySQL完全一致

优势

  • 因为是多主,所以不存在Slavelag(延迟)
  • 不存在丢失事务的情况
  • 同时具有读和写的扩展能力
  • 更小的客户端延迟
  • 节点间数据是同步的,而Master/Slave模式是异步的,不同slave上的binlog可能是不同的

技术

Galera集群的复制功能基于Galeralibrary实现,为了让MySQL与Galera library通讯,特别针对MySQL开发了wsrep API。

Galera插件保证集群同步数据,保持数据的一致性,靠的就是可认证的复制,工作原理如下图:

当客户端发出一个commit的指令,在事务被提交之前,所有对数据库的更改都会被 write-set 收集起来,并且将 write-set 纪录的内容发送给其他节点。

write-set 将在每个节点进行认证测试,测试结果决定着节点是否应用write-set更改数据。

如果认证测试失败,节点将丢弃 write-set ;如果认证测试成功,则事务提交。

安装环境准备

准备三台服务器

操作系统版本: centos7

数据库版本:5.5.56-MariaDB

主节点:192.168.64.4

节点1:192.168.64.5

节点2:192.168.64.7

各个节点之间需要解析

[[email protected] ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.64.4 ken-node1
192.168.64.5 ken-node2
192.168.64.7 ken-node3

配置yum源

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3.5/centos74-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
enabled=0

安装配置

第一步:在各个节点下载下面的软件包

[[email protected] ~]# yum install -y MariaDB-server MariaDB-client galera -y

第二步: 初始化数据库

只需要在一个节点执行即可192.168.64.4

[[email protected] ~]# systemctl restart mariadb
[[email protected] ~]# 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
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
 ... 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
 ... 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] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 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!

第三步:关闭数据库,修改/etc/my.cnf.d/server.cnf

在主配置节点配置如下

[[email protected] ~]# systemctl stop mariadb
[[email protected] ~]# vim /etc/my.cnf.d/server.cnf
...
[galera]
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.64.4,192.168.64.5,192.168,64.7"  #节点IP地址
wsrep_node_name= ken-node1        #主节点主机名
wsrep_node_address=192.168.64.4   #主节点IP
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=120M
wsrep_sst_method=rsync
wsrep_causal_reads=ON

第四步:将此文件复制到mariadb-2、mariadb-3,注意要把 wsrep_node_name 和 wsrep_node_address 改成相应节点的 hostname 和 ip。

第五步:启动集群

主节点192.168.64.4启动命令如下:

[[email protected] ~]# galera_new_cluster 

各个节点监听的端口

集群服务监听了4567和3306端口(每个节点都监听了4567和3306端口)

[[email protected] ~]# ss -tnl
State      Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port
LISTEN     0      128                                     *:22                                                  *:*
LISTEN     0      128                                     *:4567                                                *:*
LISTEN     0      100                             127.0.0.1:25                                                  *:*
LISTEN     0      80                                     :::3306                                               :::*
LISTEN     0      128                                    :::80                                                 :::*
LISTEN     0      128                                    :::22                                                 :::*
LISTEN     0      100                                   ::1:25                                                 :::*     

其他节点启动命令如下:

[[email protected] ~]# systemctl restart mariadb

验证集群

在主节点执行如下操作

连接mariadb,查看是否启用galera插件

显示ON表示开启

MariaDB [(none)]> show status like "wsrep_ready";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_ready   | ON    |
+---------------+-------+
1 row in set (0.005 sec)

查看目前集群机器数

机器数为3个显示正确

MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.003 sec)

查看集群状态

MariaDB [(none)]> show status like "wsrep%";
+------------------------------+-------------------------------------------------------+
| Variable_name                | Value                                                 |
+------------------------------+-------------------------------------------------------+
| wsrep_apply_oooe             | 0.000000                                              |
| wsrep_apply_oool             | 0.000000                                              |
| wsrep_apply_window           | 0.000000                                              |
| wsrep_causal_reads           | 2                                                     |
| wsrep_cert_deps_distance     | 0.000000                                              |
| wsrep_cert_index_size        | 0                                                     |
| wsrep_cert_interval          | 0.000000                                              |
| wsrep_cluster_conf_id        | 14                                                    |
| wsrep_cluster_size           | 3                                                     |
| wsrep_cluster_state_uuid     | 541248ce-56d0-11e9-b41b-3a9775d73ca7                  |
| wsrep_cluster_status         | Primary         #主服务器                                      |
| wsrep_commit_oooe            | 0.000000                                              |
| wsrep_commit_oool            | 0.000000                                              |
| wsrep_commit_window          | 0.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             | 540ee869-56d0-11e9-955e-9b7f30e437a6                  |
| wsrep_incoming_addresses     | 192.168.64.5:3306,192.168.64.4:3306,192.168.64.7:3306 | #连接中的数据库
| wsrep_last_committed         | 0                                                     |
| wsrep_local_bf_aborts        | 0                                                     |
| wsrep_local_cached_downto    | 18446744073709551615                                  |
| wsrep_local_cert_failures    | 0                                                     |
| wsrep_local_commits          | 0                                                     |
| wsrep_local_index            | 1                                                     |
| 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       | 541248ce-56d0-11e9-b41b-3a9775d73ca7                  |
| wsrep_protocol_version       | 8                                                     |
| wsrep_provider_name          | Galera                                                |
| wsrep_provider_vendor        | Codership Oy <[email protected]>                     |
| wsrep_provider_version       | 25.3.23(r3789)                                        |
| wsrep_ready                  | ON                                                    |
| wsrep_received               | 20                                                    |
| wsrep_received_bytes         | 3651                                                  |
| wsrep_repl_data_bytes        | 0                                                     |
| wsrep_repl_keys              | 0                                                     |
| wsrep_repl_keys_bytes        | 0                                                     |
| wsrep_repl_other_bytes       | 0                                                     |
| wsrep_replicated             | 0                                                     |
| wsrep_replicated_bytes       | 0                                                     |
| wsrep_thread_count           | 2                                                     |
+------------------------------+-------------------------------------------------------+
58 rows in set (0.002 sec)

查看连接的主机

MariaDB [(none)]> show status like "wsrep_incoming_addresses";
+--------------------------+-------------------------------------------------------+
| Variable_name            | Value                                                 |
+--------------------------+-------------------------------------------------------+
| wsrep_incoming_addresses | 192.168.64.5:3306,192.168.64.4:3306,192.168.64.7:3306 |
+--------------------------+-------------------------------------------------------+
1 row in set (0.001 sec)

测试集群是否同步

在各个节点创建数据库,可以发现每个节点都可以同步

我在192.168.64.4创建ken数据库

[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.3.5-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)]> create database ken;
Query OK, 1 row affected (0.041 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ken                |
| ken1               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.001 sec)

在192.168.64.5节点上查看数据库

[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.5-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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ken                |   #ken数据库已经同步过来
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.002 sec)

192.168.64.7节点上查看数据库

[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.5-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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ken                | #ken数据库已经同步            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.002 sec)

原文地址:https://www.cnblogs.com/kenken2018/p/10651435.html

时间: 2024-10-10 20:12:41

MariaDB Galera集群部署--技术流ken的相关文章

mariadb galera集群配置

最近在看一些关于数据库的资料,从最开始的mysql的主从复制到mysql的双主+heartbeat实现mysql的高可用再到mysql+drbd+heartbeat实现底层数据同步的双主高可用再到mysql_mmm+amoeba实现双主多从的高可用和负载均衡以及读写分离,再到后来发现mysql自从被Oracle收购后已经越来越走向了封闭,更新也不如以前频繁,并且新版的mysql已经不支持GPL协议了...感觉mysql已经在Oracle手中渐渐没落了...后来发现了一个更好的替代方案那就是mar

MariaDB GALERA 集群双节点部署

节点1:10.2.2.41 节点2:10.2.2.42 软件: mariadb-galera-10.0.22-linux-x86_64.tar.gz #galera相关参数:(两个节点配置文件类似) wsrep_on=onbinlog_format=ROWdefault-storage-engine=innodbinnodb_autoinc_lock_mode=2bind-address=10.2.2.41wsrep_provider=/usr/local/mariadb-galera-10.0

网站集群架构实战(LVS负载均衡、Nginx代理缓存、Nginx动静分离、Rsync+Inotify全网备份、Zabbix自动注册全网监控)--技术流ken

前言 最近做了一个不大不小的项目,现就删繁就简单独拿出来web集群这一块写一篇博客.数据库集群请参考<MySQL集群架构篇:MHA+MySQL-PROXY+LVS实现MySQL集群架构高可用/高性能-技术流ken>下面是项目的一些简单介绍. WEB集群项目简介 随着网站访问量的激增,势必会导致网站的负载增加,现需求搭载一套高性能,高负载,高可用的网站集群架构以保障网站的持续.高效.安全.稳定的运行. 针对以上需求,我们采用了如下的技术: 使用负载均衡技术来实现网站请求的调度分发,减小后端服务器

高可用集群之keepalived+lvs实战-技术流ken

1.keepalived简介 lvs在我之前的博客<高负载集群实战之lvs负载均衡-技术流ken>中已经进行了详细的介绍和应用,在这里就不再赘述.这篇博文将把lvs与keepalived相结合使用,在实际工作中搭建高可用,高负载,高性能的服务器集群. "Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将

Jenkins+Git+Gitlab+Ansible实现持续集成自动化部署动态网站(二)--技术流ken

项目前言 在上一篇博客<Jenkins+Git+Gitlab+Ansible实现持续化集成一键部署静态网站(一)--技术流ken>中已经详细讲解了如何使用这四个工具来持续集成自动化部署一个静态的网站. 如果大家可以熟练掌握以上内容,势必会在工作中减轻不小的工作量. 本篇博客将再次使用这四个工具结合freestyle和pipeline来完成动态网站的部署. 为了拓宽知识点,本篇博客将使用jenkins的两种常用方法来进行部署,如果你对pipeline还不熟悉,请参考我之前的博客<Jenki

线上测试高可用集群部署文档【我的技术我做主】

线上测试高可用集群部署文档 目录: 目录:1 项目需求:2 实现方式:2 拓扑图:3 系统及软件版本:3 安装步骤:4 IP分配:4 LVS和keepalived的安装和配置:4 LVS主配置:4 LVS2备 配置:7 web服务器配置9 Mysql-MHA高可用:13 Mysql主配置:13 manager管理端操作:15 VIP切换:16 测试:26 下面是centos5.6的系统环境,如果是centos6版本,只需改动少许地方即可,步骤一致 . ---- by 金戈铁马行飞燕 项目需求:

Jenkins+Git+Gitlab+Ansible实现持续集成自动化部署静态网站(一)--技术流ken

前言 在之前已经写了关于Git,Gitlab以及Ansible的两篇博客<Git+Gitlab+Ansible剧本实现一键部署Nginx--技术流ken>,<Git+Gitlab+Ansible剧本实现一键部署动态网站(二)--技术流ken>,以及关于jenkins的简单使用<Jenkins持续集成介绍及插件安装版本更新演示(一)--技术流ken>.相信大家也已经完全掌握了这三项工具的使用,也可以使用这几项工具可以部署静态以及动态网站了. 以前的博客可以实现一键部署网站

大数据技术之_10_Kafka学习_Kafka概述+Kafka集群部署+Kafka工作流程分析+Kafka API实战+Kafka Producer拦截器+Kafka Streams

第1章 Kafka概述1.1 消息队列1.2 为什么需要消息队列1.3 什么是Kafka1.4 Kafka架构第2章 Kafka集群部署2.1 环境准备2.1.1 集群规划2.1.2 jar包下载2.2 Kafka集群部署2.3 Kafka命令行操作第3章 Kafka工作流程分析3.1 Kafka 生产过程分析3.1.1 写入方式3.1.2 分区(Partition)3.1.3 副本(Replication)3.1.4 写入流程3.2 Broker 保存消息3.2.1 存储方式3.2.2 存储策

HAProxy高可用负载均衡集群部署

HAProxy高可用负载均衡集群部署 基本信息: 系统平台:VMware WorkStation 系统版本: CentOS Linux release 7.2.1511 (Core) 内核版本: 3.10.0-327.el7.x86_64 集群架构: 前端:HAProxy 1.虚拟FQDN:www.simpletime.net 2.VIP:192.168.39.1:DIP:172.16.39.50 3.调度服务器:Varnish1.Varnish2 4.调度算法:URL_Hash_Consist