Ubuntu16.04搭建三台主机的mysql galera cluster集群(实践版)

本文主要是介绍Ubuntu16.04搭建三台主机的mysql galera cluster集群(实践版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

直接复制粘贴可能会提示错误,复制代码检查代码间的空格。

 

一  搭建环境:

1 Ubuntu16.04版本

  1. 主 10.1.6.241 用户user admin@123  root admin@134
  2. 从 10.1.6.48  用户user admin@124  root admin@135
  3. 从 10.1.6.129 用户 user admin@125  root admin@136

2 网络

无论是物理机还是虚拟机都需在一个子网范围内,且ping通彼此。侧重说下虚拟机,虚拟机选择网络方式为桥接方式,最为便捷。直接加入实体内网。网络联网以及访问最为直接。其他网络方式可自行探讨。

 

二 安装环境:

首先,不论是在云服务器、物理机还是虚拟机上,至少需要3台主机(我们将这三台主机称为“Nodes”),对于每一个”Node”,硬件最低配置要求为: 

 

- 1GHz单核CPU 
- 512M的内存 
- 100Mbps的网络连接 
- 三台主机IP为: 
- Node1: 10.1.6.241(主节点)
- Node2: 10.1.6.48(从节点1)
- Node3: 10.1.6.129(从节点2)

 

其次要防火墙配置禁用SElinux、、禁用AppArmor,

这些在Ubuntu14.04下都是不用配置的,所以就不详细说明配置步骤。但在16.04环境下需严格配置:

  1. 如果搭建的平台仅用于测试,可直接关闭防火墙

Sudo ufw disable

16.04默认情况下,是关闭防火墙的。因此此项防火墙设置不需要操作。

对于正式平台,则需要配置防火墙,以提高平台安全接入

防火墙配置:我们需要在每个节点上设置其防火墙规则,使得各个节点能与集群通信。在每个节点上均执行如下命令:

 

  iptables --append INPUT --protocol tcp \

      --source 10.1.6.241 --jump ACCEPT

 iptables --apend INPUT --protocol tcp \

      --source 10.1.6.48 --jump ACCEPT

 iptables --append INPUT --protocol tcp \

      --source 10.1.6.129 --jump ACCEPT

然后保存防火墙规则:

iptables-save

 

2.禁用SElinux

默认16014 selinux是禁用的

因此也不需要执行 semanage permissive -a mysqld_t

 

3.禁用AppArmor

禁用Ubuntu的apparmor:apparmor主要是Ubuntu系统中对mysql访问权限的控制,我们必须禁用AppArmor,以便配置mysql的mysqld能够运行外部程序。

执行

sudo ln -s /etc/apparmor.d/usr /etc/apparmor.d/disable/.sbin.mysqld

重启apparmor:

sudo service apparmor restart

 

三 开始安装
(1)安装对应依赖

apt-get install python-software-properties  

apt-get install software-properties-common

 

(2)添加GunPG key

apt-key adv --keyserver keyserver.ubuntu.com --recv BC19DDBA

 

(3)配置Ubuntu下galera源

 

创建galera.list文件:

cd /etc/apt/sources.list.d

touch galera.list 

nano -w galera.list

 

在galera.list中添加内容:

deb [arch=amd64] http://releases.galeracluster.com/mysql-wsrep-5.7/ubuntu xenial main  

deb [arch=amd64] http://releases.galeracluster.com/galera-3/ubuntu xenial main  

 

(4)继续配置源

 cd /etc/apt/preferences.d

touch  galera.pref 

nano -w /etc/apt/preferences.d/galera.pref

 

在galera.pref文件中添加内容:

 

Package: *

Pin: origin releases.galeracluster.com

Pin-Priority: 1001

 

(5)接下来更新一下:apt-get update

 

(6) 一旦在所有三个服务器上更新了存储库,我们就可以在三台服务器上运行以下命令安装MySQL5.7的修补程序与Galera

 

apt-get install galera-3 galera-arbitrator-3 mysql-wsrep-5.7

 

程序大概占用500M

 

下载执行完后,弹出输入root密码2次确认红框

输入密码执行

 

(7)安装完成之后,在每一个节点上启动Mysql:

service mysql start

 

  1. 分别在三个节点上修改mysql的配置文件/etc/mysql/my.cnf

vi /etc/mysql/my.cnf

主节点241上

绿色为系统内容  红色为需要添加修改内容

# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation; version 2 of the License.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#

# The MySQL database server configuration file.

#

# You can copy this to one of:

# - "/etc/mysql/my.cnf" to set global options,

# - "~/.my.cnf" to set user-specific options.

 

#

# One can use all long options that the program supports.

# Run program with --help to get a list of available options and with

# --print-defaults to see which it would actually understand and use.

#

# For explanations see

# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#

# * IMPORTANT: Additional settings that can override those from this file!

#   The files must end with '.cnf', otherwise they'll be ignored.

#

!includedir /etc/mysql/conf.d/

!includedir /etc/mysql/mysql.conf.d/

[mysqld]

binlog_format=ROW

default-storage-engine=innodb

innodb_autoinc_lock_mode=2

wsrep_provider=/usr/lib/libgalera_smm.so

wsrep_provider_options="gcache.size=300M; gcache.page_size=500M"  

//此处size可根据实体机或虚拟机的实际情况配置。此处我配置500M

wsrep_cluster_name="galeracluster1"

wsrep_cluster_address="gcomm://"

wsrep_sst_method=rsync

wsrep_sst_auth=wsrep:199077

wsrep_node_address="10.1.6.241"      //此IP为主IP

wsrep_node_name=node135

bind-address=0.0.0.0

 

 

 

从节点48上:

# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation; version 2 of the License.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#

# The MySQL database server configuration file.

#

# You can copy this to one of:

# - "/etc/mysql/my.cnf" to set global options,

# - "~/.my.cnf" to set user-specific options.

#

# One can use all long options that the program supports.

# Run program with --help to get a list of available options and with

# --print-defaults to see which it would actually understand and use.

#

# For explanations see

# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#

# * IMPORTANT: Additional settings that can override those from this file!

#   The files must end with '.cnf', otherwise they'll be ignored.

 

#

!includedir /etc/mysql/conf.d/

!includedir /etc/mysql/mysql.conf.d/

[mysqld]

binlog_format=ROW

default-storage-engine=innodb

innodb_autoinc_lock_mode=2

wsrep_provider=/usr/lib/libgalera_smm.so

wsrep_provider_options="gcache.size=300M; gcache.page_size=500M"

//同样此处500M

wsrep_cluster_name="galeracluster1"

wsrep_cluster_address="gcomm://10.1.6.241"

//此处填写主的IP地址

wsrep_sst_method=rsync

wsrep_sst_auth=wsrep:199077

wsrep_node_address="10.1.6.48"

//此处是从的本机地址

wsrep_node_name=node132

bind-address=0.0.0.0

 

 

 

从节点129上:

 

# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation; version 2 of the License.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#

# The MySQL database server configuration file.

#

# You can copy this to one of:

 

# - "/etc/mysql/my.cnf" to set global options,

# - "~/.my.cnf" to set user-specific options.

#

# One can use all long options that the program supports.

# Run program with --help to get a list of available options and with

# --print-defaults to see which it would actually understand and use.

#

# For explanations see

#

# * IMPORTANT: Additional settings that can override those from this file!

#   The files must end with '.cnf', otherwise they'll be ignored.

#

!includedir /etc/mysql/conf.d/

!includedir /etc/mysql/mysql.conf.d/

 

[mysqld]

binlog_format=ROW

default-storage-engine=innodb

innodb_autoinc_lock_mode=2

wsrep_provider=/usr/lib/libgalera_smm.so

wsrep_provider_options="gcache.size=300M; gcache.page_size=500M"

//同样此处500M

wsrep_cluster_name="galeracluster1"

wsrep_cluster_address="gcomm://10.1.6.129"

//此处填写主的IP地址

wsrep_sst_method=rsync

wsrep_sst_auth=wsrep:199077

wsrep_node_address="10.1.6.241"

//此处填本机IP地址

wsrep_node_name=node134

bind-address=0.0.0.0

9

在每个节点上重启mysql:

service mysql restart

注意:此处最好是按照主次依次启动

(10) 试着在任意一个节点上登录其余两个节点,如果都能彼此登录,则表明三台服务器上的mysql彼此监听成功。

如在241上登录48,129

mysql -h 10.1.6.48 -u wsrep -p   不成功 deny

mysql -h 10.1.6.129 -u wsrep -p  不成功 deny

 

如果彼此登录不成功,则需要在每个节点上创建用户以实现mysql的远程登录。

。创建用户时,需要对应//每个节点(主节点即可)的mysql的配置文件/etc/mysql/my.cnf中的“wsrep_sst_auth=wsrep:199077”项,该项用来设置服务器之间mysql彼此访问的用户名和密码,其中wsrep为用户名,199077为密码。

 

具体创建命令如下:

 

每个节点数据库远程访问的创建

create user 'wsrep'@'%' identified by '199077';

create user 'root'@'%' identified by '199077';#这一句不是很清楚是否可以去掉

GRANT ALL PRIVILEGES ON *.* TO 'wsrep'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;

创建完后重启各个节点的mysql,

service mysql restart

如在241上登录48,129

mysql -h 10.1.6.48 -u wsrep -p   成功

mysql -h 10.1.6.129 -u wsrep -p  成功

 

  • 启动集群

主节点上将mysql服务先停掉然后重启,注意一定要在前面提到的wsrep_cluster_address="gcomm://"的节点上执行最开始的启动程序:

service mysql stop 

service mysql start --wsrep-new-cluster

在其他节点上执行:service mysql restart

 

此时,你的主机可能会报错

Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

 

Galera 集群中非常重要的一个文件

即grastate.dat,

分别登陆三个节点查看grastate.dat文件

cat /var/lib/mysql/grastate.date

 

从48

 

从129

 

从上图我们可以看到,从129的safe_to-boostrap这个数值为1

三个节点的seqno均为-1

seqno为-1则说明数据库正在运行,或者非正常关闭

我们可以重新启动他,Galera集群会自动恢复并同步数据

6.241 的safe_to-boostrap 为0 所以无法启动,因此修改主数据库的safe_to-boostrap为1

修改后执行

service mysql stop 

service mysql start --wsrep-new-cluster

 

Mysql集群启动,相应的从主机也启动成功

 

 验证是否成功

查看数据表

登录主节点241:

mysql -u root -p 

Enter password:

mysql> SHOW STATUS LIKE 'wsrep%';

重点关注的是下面这几行:

wsrep_ready     |     ON

如果状态是 ON,那么就要看:

wsrep_cluster_size    |   3

如果是3,那么是成功的,否则是不成功的!

 

 数据库复制测试

在主节点241上创建一个表:

CREATE database test;

立刻到节点48129上使用 show databases; 查看是否也有了test这个数据库,如果有则成功!

 

 

 

至此,集群测试完成。

这篇关于Ubuntu16.04搭建三台主机的mysql galera cluster集群(实践版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/930136

相关文章

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired