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

相关文章

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Redis分片集群的实现

《Redis分片集群的实现》Redis分片集群是一种将Redis数据库分散到多个节点上的方式,以提供更高的性能和可伸缩性,本文主要介绍了Redis分片集群的实现,具有一定的参考价值,感兴趣的可以了解一... 目录1. Redis Cluster的核心概念哈希槽(Hash Slots)主从复制与故障转移2.

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Python3.6连接MySQL的详细步骤

《Python3.6连接MySQL的详细步骤》在现代Web开发和数据处理中,Python与数据库的交互是必不可少的一部分,MySQL作为最流行的开源关系型数据库管理系统之一,与Python的结合可以实... 目录环境准备安装python 3.6安装mysql安装pymysql库连接到MySQL建立连接执行S

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同