MariaDB Cluster搭建

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 休息了一段时间后,技术忘记的差不多了,现在公司搞了一个MariaDB集群用docker跑的,我自己用三台docker容器尝试了一下搭建过程。docker和MariaDB这里不作太多的介绍,想了解的朋友可以去官网查看。
休息了一段时间后,技术忘记的差不多了,现在公司搞了一个MariaDB集群用docker跑的,我自己用三台docker容器尝试了一下搭建过程。docker和MariaDB这里不作太多的介绍,想了解的朋友可以去官网查看。
环境信息如下:
IP地址            主机名             系统版本
172.17.0.18    test-node1        CentOS Linux release 7.2.1511 (Core)
172.17.0.19    test-node2        CentOS Linux release 7.2.1511 (Core)
172.17.0.21    test-node3        CentOS Linux release 7.2.1511 (Core)
关闭三台机器的selinux和防火墙哈~~~
1、三台机器都添加mariadb的源,内容如下:
# MariaDB 10.1 CentOS repository list - created 2015-11-30 13:13 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
2、三台机器都安装mariadb相关的软件包
yum -y install MariaDB-server MariaDB-client galera           ===>最后一个是集群需要的软件
3、安装集群之间同步的软件,这里我们推荐使用percona公司的xtrabackup工具,因为官方推荐也是这种,当然默认是rsync工具。
yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
yum -y percona-xtrabackup-24 which socat               ===>最后两个软件包是同步时候需要使用的命令,不然第二和第三个数据库会启动失败。
相关解释文档如下:

4、初始化第一个数据库及修改属主属组
mysql_install_db
chown -R mysql.mysql /var/lib/mysql
5、修改第一个启动数据库的server.cnf文件,默认配置文件如下:
 more /etc/my.cnf.d/server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

#
# * Galera-related settings
#
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.1 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.1]
修改为如下内容:
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]
# GENERAL #
user                           = mysql

# DATA STORAGE #
datadir                        = /var/lib/mysql

#
# * Galera-related settings
#
[galera]
wsrep_on                       = on
wsrep_sst_auth                 = test-cluster:test-password
wsrep_sst_method               = xtrabackup-v2
wsrep_cluster_name             = test-cluster
wsrep_cluster_address          = gcomm://                           ===>注意这里一个都不写
wsrep_node_name                = test-node1
wsrep_node_address             = 172.17.0.18:4567
wsrep_provider                 = /usr/lib64/galera/libgalera_smm.so
#wsrep_slave_threads            = 8
#innodb-flush-log-at-trx-commit = 2
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0

# BINARY LOGGING #
binlog-format                  = row

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.1 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.1]

#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
6、启动第一个数据库,启动方式如下:
nohup mysqld --wsrep-new-cluster &
7、登录第一个数据库授权同步用户和密码,注意用户和密码和上面配置文件里面定义的相同
grant all on *.* to 'test-cluster'@'localhost' identified by 'test-password';
8、分别修改第二个和第三个数据库的server.cnf文件,内容主要修改如下:
wsrep_on                       = on
wsrep_sst_auth                 = test-cluster:test-password
wsrep_sst_method               = xtrabackup-v2
wsrep_cluster_name             = test-cluster
wsrep_cluster_address          = gcomm://172.17.0.18:4567,172.17.0.19:4567               ===>注意这里写第一台数据库和本身
wsrep_node_name                = test-node2
wsrep_node_address             = 172.17.0.19:4567
wsrep_provider                 = /usr/lib64/galera/libgalera_smm.so
启动第二台数据库
/etc/init.d/mysql start                  ===>记得观察输出日志
===================下面是第三台数据库server.cnf的内容======================
wsrep_on                       = on
wsrep_sst_auth                 = test-cluster:test-password
wsrep_sst_method               = xtrabackup-v2
wsrep_cluster_name             = test-cluster
wsrep_cluster_address          = gcomm://172.17.0.18:4567,172.17.0.19:4567,172.17.0.21:4567              ===>写集群中所有的
wsrep_node_name                = test-node3
wsrep_node_address             = 172.17.0.21:4567
wsrep_provider                 = /usr/lib64/galera/libgalera_smm.so
同样启动第三台数据库
/etc/init.d/mysql start                  ===>记得观察输出日志
如果正常按照我的步骤操作的话应该启动成功,剩下的就是测试了。

登录第二台数据库查看

登录第三台数据库查看

上面显示正常同步了,注意一般生产环境不要轻易重启第一个数据库,因为重启第一个数据库后它的数据会和别的节点的数据不同步,这样就发生了脑裂。虽然是多主模式,但是还是建议只往一个数据库上写,从其余所有的数据库来读,记得重启了第一个数据库,其它数据库也要重启,而且是一台重启数据同步完成后再重启下一台,这样很费时间和IO。另外生产环境注意MariaDB集群都接入千兆交换机下面。不然性能上不去。
我上面server.cnf配置文件基本都是最简单的写法,下面附一个生产环境的写法。
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]
# GENERAL #
user                           = mysql
default-storage-engine         = InnoDB
socket                         = /var/lib/mysql/mysql.sock
pid-file                       = /var/lib/mysql/mysql.pid
bind-address                   = 0.0.0.0

# CHARACTER SET #
collation-server               = utf8_unicode_ci
init-connect                   = 'SET NAMES utf8'
character-set-server           = utf8
             

# MyISAM #
key-buffer-size                = 32M
myisam-recover-options         = FORCE,BACKUP

# SAFETY #
skip-host-cache
skip-name-resolve
max-allowed-packet             = 16M
max-connect-errors             = 1000000
#sql-mode                       = STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE
sysdate-is-now                 = 1
innodb                         = FORCE
innodb-strict-mode             = 1
innodb-autoinc-lock-mode       = 2
innodb-doublewrite             = 1
innodb_flush_log_at_trx_commit = 0
innodb_file_per_table          = 1

# DATA STORAGE #
datadir                        = /var/lib/mysql

# BINARY LOGGING #
log-bin                        = /var/lib/mysql/mysql-bin
expire-logs-days               = 2
sync-binlog                    = 1
binlog-format                  = row

# CACHES AND LIMITS #
tmp-table-size                 = 32M
max-heap-table-size            = 32M
query-cache-type               = 0
query-cache-size               = 0
max-connections                = 500
thread-cache-size              = 50
open-files-limit               = 65535
table-definition-cache         = 4096
table-open-cache               = 4096

# INNODB #
innodb-flush-method            = O_DIRECT
innodb-log-files-in-group      = 2
innodb-log-file-size           = 128M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table          = 1
innodb-buffer-pool-size        = 128M

# LOGGING #
log-error                      = /dev/stdout
slow-query-log-file            = /var/lib/mysql
log-queries-not-using-indexes  = 1
slow-query-log                 = 1

#
# * Galera-related settings
#
[galera]
wsrep_provider                 = /usr/lib64/galera/libgalera_smm.so
wsrep_slave_threads            = 8
innodb-flush-log-at-trx-commit = 2

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.1 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.1]

#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
[mysql]
port                           = 3306
socket                         = /var/lib/mysql/mysql.sock
上述里没有集群配置信息,我们可以在/etc/my.cnf.d/galera.cnf文件里面写入集群相关信息。
[galera]
wsrep_on                       = on
wsrep_sst_auth                 = test-cluster:test-password
wsrep_sst_method               = xtrabackup-v2
wsrep_cluster_name             = test-node
wsrep_cluster_address          = gcomm://xxxx:4567,xxxx:4567,xxxx:4567
wsrep_node_name                = test-nodex
wsrep_node_address             = 本机IP地址:4567





相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
关系型数据库 网络安全 数据库
|
存储 关系型数据库 MySQL
|
关系型数据库 MySQL 网络安全
|
网络协议 关系型数据库 MySQL
|
MySQL 关系型数据库 测试技术
|
NoSQL 关系型数据库 MySQL
阿里云RDS关系型数据库大全_MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等
阿里云RDS关系型数据库如MySQL版、PolarDB、PostgreSQL、SQL Server和MariaDB等,NoSQL数据库如Redis、Tair、Lindorm和MongoDB
339 0
|
4月前
|
关系型数据库 Java MySQL
Linux安装JDK1.8 & tomcat & MariaDB(MySQL删减版)
本教程提供了在Linux环境下安装JDK1.8、Tomcat和MariaDB的详细步骤。这三个组件的组合为Java Web开发和部署提供了一个强大的基础。通过遵循这些简单的指导步骤,您可以轻松建立起一个稳定、高效的开发和部署环境。希望这个指导对您的开发工作有所帮助。
238 8
|
5月前
|
缓存 关系型数据库 MySQL
error: Failed dependencies: mariadb-connector-c-config is obsoleted by mysql-community-server-8.0.36-1.el7.x86_64 问题解决
error: Failed dependencies: mariadb-connector-c-config is obsoleted by mysql-community-server-8.0.36-1.el7.x86_64 问题解决
285 19