配置galera+nginx 实现 mariadb、mysql数据库多主模式

需求 、解决问题点:

1:现有的mysql数据主从模式,数据同步延迟;

2:mysql主从进程经常崩溃,数据找平困难;

3:数据库主从或者主主模式数据查询、写入慢;

4:mariadb或者mysql数据库 节点性能需要指数级提升;

5:mariadb或者mysql数据库对数据安全性有较高的要求;

6:项目要求数据库有横向扩展的能力;

7 :等等。。。。。。

一、MariaDB Galera Cluster概要:

1.简述:

MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的系统架构,业务层面无需做读写分离工作,数据库读写压力都能按照既定的规则分发到各个节点上去。在数据方面完全兼容 MariaDB 和 MySQL。

2.特性:

(1).同步复制 Synchronous replication

(2).Active-active multi-master 拓扑逻辑

(3).可对集群中任一节点进行数据读写

(4).自动成员控制,故障节点自动从集群中移除

(5).自动节点加入

(6).真正并行的复制,基于行级

(7).直接客户端连接,原生的 MySQL 接口

(8).每个节点都包含完整的数据副本

(9).多台数据库中数据同步由 wsrep 接口实现

3.局限性

(1).目前的复制仅仅支持InnoDB存储引擎,任何写入其他引擎的表,包括mysql.*表将不会复制,但是DDL语句会被复制的,因此创建用户将会被复制,但是insert into mysql.user…将不会被复制的.

(2).DELETE操作不支持没有主键的表,没有主键的表在不同的节点顺序将不同,如果执行SELECT…LIMIT… 将出现不同的结果集.

(3).在多主环境下LOCK/UNLOCK TABLES不支持,以及锁函数GET_LOCK(), RELEASE_LOCK()…

(4).查询日志不能保存在表中。如果开启查询日志,只能保存到文件中。

(5).允许最大的事务大小由wsrep_max_ws_rows和wsrep_max_ws_size定义。任何大型操作将被拒绝。如大型的LOAD DATA操作。

(6).由于集群是乐观的并发控制,事务commit可能在该阶段中止。如果有两个事务向在集群中不同的节点向同一行写入并提交,失败的节点将中止。对 于集群级别的中止,集群返回死锁错误代码(Error: 1213 SQLSTATE: 40001 (ER_LOCK_DEADLOCK)).

(7).XA事务不支持,由于在提交上可能回滚。

(8).整个集群的写入吞吐量是由最弱的节点限制,如果有一个节点变得缓慢,那么整个集群将是缓慢的。为了稳定的高性能要求,所有的节点应使用统一的硬件。

(9).集群节点建议最少3个。

(10).如果DDL语句有问题将破坏集群。

二:安装、配置

1:安装系统、内核版本

[[email protected] ~]#

more /etc/redhat-release

CentOS Linux release 7.2.1511 (Core)

[[email protected] ~]#

uname -a

Linux galeradbnode-6-11 3.10.0-327.18.2.el7.x86_64 #1 SMP Thu May 12 11:03:55 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

mysqld 10.1.14-MariaDB

2:节点信息如下:

安装集群至少需要3个节点

galeradbnode-6-11

galeradbnode-6-12

galeradbnode-6-13

3:各节点配置yum源:

cat >/etc/yum.repos.d/mariadb.repo<<-EOF

[mariadb]

name = MariaDB

baseurl = http://yum.mariadb.org/10.1/centos7-amd64

gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB

gpgcheck=1

EOF

安装注意事项:

清理

/usr/bin/mysql*

/usr/sbin/mysql*

# 确认本机防火墙上开放了所需TCP 3306和TCP 4567的端口

iptables -A INPUT -i eth0 -p tcp --dport 3306 -j ACCEPT

iptables -A INPUT -i eth0 -p tcp --dport 4567 -j ACCEPT

简单配置:

galeradbnode-6-12:172.16.6.12

安装mariadb:

yum -y install mariadb-server

##########如果需要修改数据目录执行下面操作#############

修改启动文件中数据存储目录

#vim /etc/init.d/mysql

basedir=/usr

datadir=/data/3306/data

创建数据目录

mkdir -p /data/3306/data

mkdir -p  /var/lib/mysql/

修改权限

chown -R mysql.mysql /data/3306/data

chown -R mysql.mysql  /var/lib/mysql/

#旧环境需要先删除旧文件

rm -rf /var/lib/mysql/*

#初始化mysql 服务

mysql_install_db --user=mysql

默认安装不用此选项

--datadir=/data/3306/data/

##############################################

安全配置、创建root密码,删除test库等:

mysql_secure_installation

创建集群连接账户密码

MariaDB [(none)]> grant all on *.* to [email protected]‘172.16.6.%‘ identified by ‘galerapassword‘;

关闭数据库:

/etc/init.d/mysql stop mysql

修改配置文件

############################以下为配置文件####################################

[[email protected] ~]# more /etc/my.cnf.d/server.cnf

#

[client]

user            = root

password        = dbpassword

port            = 13306

# 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]

user    = mysql

port    = 13306

#

# * Galera-related settings

#

[galera]

wsrep_on=ON

wsrep_provider=/usr/lib64/galera/libgalera_smm.so

wsrep_cluster_address="gcomm://172.16.6.12,172.16.6.13"

wsrep_cluster_name=PTDBCluster

wsrep_node_name = galeradbnode-6-12

wsrep_node_address=172.16.6.12

binlog_format=row

default_storage_engine=InnoDB

innodb_autoinc_lock_mode=2

wsrep_slave_threads=10

innodb_flush_log_at_trx_commit=2

innodb_buffer_pool_size=122M

wsrep_sst_method=rsync

wsrep_sst_auth  = dbclusteruser:galerapassword

[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]

############################以上为配置文件####################################

启动节点1,初始化集群:galeradbnode-6-12

/etc/init.d/mysql start --wsrep-new-cluster

验证集群数量

MariaDB [(none)]> SHOW STATUS LIKE ‘wsrep_cluster_size‘;

+--------------------+-------+

| Variable_name      | Value |

+--------------------+-------+

| wsrep_cluster_size | 1     |

+--------------------+-------+

1 row in set (0.00 sec)

其它节点安装部署

1:初始化mariadb 数据库;

2:copy galeradbnode-6-12 配置文件;

[galera]

wsrep_on=ON

wsrep_provider=/usr/lib64/galera/libgalera_smm.so

wsrep_cluster_address="gcomm://172.16.6.12,172.16.6.13"

wsrep_cluster_name=PTDBCluster

wsrep_node_name = galeradbnode-6-12         《==修改为本机主机名

wsrep_node_address=172.16.6.12 《==修改为本机IP地址

binlog_format=row

default_storage_engine=InnoDB

innodb_autoinc_lock_mode=2

wsrep_slave_threads=10

innodb_flush_log_at_trx_commit=2

innodb_buffer_pool_size=122M

wsrep_sst_method=rsync

wsrep_sst_auth  = dbclusteruser:ptmind890iop

3:启动数据库

#/etc/init.d/mysql start

4:验证集群数量,每加入一台机器,cluster 数量增加1个;

MariaDB [(none)]> SHOW STATUS LIKE ‘wsrep_cluster_size‘;

+--------------------+-------+

| Variable_name      | Value |

+--------------------+-------+

| wsrep_cluster_size | 2     |

+--------------------+-------+

查询集群详细配置信息命令如下:

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           | 0                                    |

| wsrep_cert_deps_distance     | 0.000000                             |

| wsrep_cert_index_size        | 0                                    |

| wsrep_cert_interval          | 0.000000                             |

| wsrep_cluster_conf_id        | 2                                    |

| wsrep_cluster_size           | 2                                    |

| wsrep_cluster_state_uuid     | 32725a2c-1be7-11e6-b6f0-87e13f10c1dd |

| wsrep_cluster_status         | Primary                              |

| wsrep_commit_oooe            | 0.000000                             |

| wsrep_commit_oool            | 0.000000                             |

| wsrep_commit_window          | 0.000000                             |

| wsrep_connected              | ON                                   |

| 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             | a38ee701-1be9-11e6-a73b-3357c886f499 |

| wsrep_incoming_addresses     | 172.16.6.22:3306,172.16.6.21:3306    |

| wsrep_last_committed         | 4                                    |

| 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.000000                             |

| wsrep_local_recv_queue_max   | 1                                    |

| 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       | 32725a2c-1be7-11e6-b6f0-87e13f10c1dd |

| wsrep_protocol_version       | 7                                    |

| wsrep_provider_name          | Galera                               |

| wsrep_provider_vendor        | Codership Oy <[email protected]>    |

| wsrep_provider_version       | 25.3.15(r3578)                       |

| wsrep_ready                  | ON                                   |

| wsrep_received               | 6                                    |

| wsrep_received_bytes         | 415                                  |

| 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           | 5                                    |

+------------------------------+--------------------------------------+

wsrep_connected = on 链接已开启

wsrep_cluster_size =3 集群中节点的数量

wsrep_incoming_addresses = 172.16.6.12:13306,172.16.6.13:13306 集群中节点的访问地址

监控状态参数说明:

集群完整性检查:

wsrep_cluster_state_uuid:在集群所有节点的值应该是相同的,有不同值的节点,说明其没有连接入集群.

wsrep_cluster_conf_id:正常情况下所有节点上该值是一样的.如果值不同,说明该节点被临时"分区"了.当节点之间网络连接恢复的时候应该会恢复一样的值.

wsrep_cluster_size:如果这个值跟预期的节点数一致,则所有的集群节点已经连接.

wsrep_cluster_status:集群组成的状态.如果不为"Primary",说明出现"分区"或是"split-brain"状况.

节点状态检查:

wsrep_ready: 该值为ON,则说明可以接受SQL负载.如果为Off,则需要检查wsrep_connected.

wsrep_connected: 如果该值为Off,且wsrep_ready的值也为Off,则说明该节点没有连接到集群.(可能是wsrep_cluster_address或wsrep_cluster_name等配置错造成的.具体错误需要查看错误日志)

wsrep_local_state_comment:如果wsrep_connected为On,但wsrep_ready为OFF,则可以从该项查看原因.

复制健康检查:

wsrep_flow_control_paused:表示复制停止了多长时间.即表明集群因为Slave延迟而慢的程度.值为0~1,越靠近0越好,值为1表示复制完全停止.可优化wsrep_slave_threads的值来改善.

wsrep_cert_deps_distance:有多少事务可以并行应用处理.wsrep_slave_threads设置的值不应该高出该值太多.

wsrep_flow_control_sent:表示该节点已经停止复制了多少次.

wsrep_local_recv_queue_avg:表示slave事务队列的平均长度.slave瓶颈的预兆.

最慢的节点的wsrep_flow_control_sent和wsrep_local_recv_queue_avg这两个值最高.这两个值较低的话,相对更好.

检测慢网络问题:

wsrep_local_send_queue_avg:网络瓶颈的预兆.如果这个值比较高的话,可能存在网络瓶

冲突或死锁的数目:

wsrep_last_committed:最后提交的事务数目

wsrep_local_cert_failures和wsrep_local_bf_aborts:回滚,检测到的冲突数目

MySQL Galera监控

查看MySQL版本:   mysql>

SHOW GLOBAL VARIABLES LIKE ‘version‘;

查看wsrep版本: mysql>

SHOW GLOBAL STATUS LIKE ‘wsrep_provider_version‘;

查看wsrep有关的所有变量: mysql>

HOW VARIABLES LIKE ‘wsrep%‘ \G

查看Galera集群状态: mysql>

show status like ‘wsrep%‘;

其他相关

1.Galera Cluster部署的前置检查

在要转成Galera Cluster的数据库上执行如下SQL语句:

SELECT DISTINCT

CONCAT(t.table_schema,‘.‘,t.table_name) as tbl,

t.engine,

IF(ISNULL(c.constraint_name),‘NOPK‘,‘‘) AS nopk,

IF(s.index_type = ‘FULLTEXT‘,‘FULLTEXT‘,‘‘) as ftidx,

IF(s.index_type = ‘SPATIAL‘,‘SPATIAL‘,‘‘) as gisidx

FROM information_schema.tables AS t

LEFT JOIN information_schema.key_column_usage AS c

ON (t.table_schema = c.constraint_schema AND t.table_name = c.table_name

AND c.constraint_name = ‘PRIMARY‘)

LEFT JOIN information_schema.statistics AS s

ON (t.table_schema = s.table_schema AND t.table_name = s.table_name

AND s.index_type IN (‘FULLTEXT‘,‘SPATIAL‘))

WHERE t.table_schema NOT IN (‘information_schema‘,‘performance_schema‘,‘mysql‘)

AND t.table_type = ‘BASE TABLE‘

AND (t.engine <> ‘InnoDB‘ OR c.constraint_name IS NULL OR s.index_type IN (‘FULLTEXT‘,‘SPATIAL‘))

ORDER BY t.table_schema,t.table_name;

上述SQL检索数据库,输出不符合使用Galera的表的信息,对应的5个字段顺序为:表,表引擎,是否无主键,是否有全文索引,是否有空间索引。

找到不符合的原因,对应修改即可。

配置高可用转发,集群升级到mmmnnn 模式

nginx配置如下:

upstream galera {

hash $remote_addr consistent;

server 172.16.6.11:13306;  #galera集群节点服务器

server 172.16.6.12:13306;

server 172.16.6.13:13306;

}

server {

listen 23306;                 #nginx 监听端口

proxy_pass galera;

}

登陆数据库:

mysql -h nginx转发服务器IP  -u dbuser -pdbpassword-P23306

参考文档:

http://www.linuxidc.com/Linux/2016-01/127256.htm

http://blog.sina.com.cn/s/blog_704836f40101lixp.html

http://www.open-open.com/lib/view/open1435888783513.html

http://www.open-open.com/lib/view/open1418521886917.html

时间: 2024-08-10 00:04:44

配置galera+nginx 实现 mariadb、mysql数据库多主模式的相关文章

CentOS 7 下安装 LEMP 服务(nginx、MariaDB/MySQL 和 php)

原文 CentOS 7 下安装 LEMP 服务(nginx.MariaDB/MySQL 和 php) LEMP 组合包是一款日益流行的网站服务组合软件包,在许多生产环境中的核心网站服务上起着强有力的作用.正如其名称所暗示的, LEMP 包是由 Linux.nginx.MariaDB/MySQL 和 PHP 组成的.在传统的 LAMP 包中使用的 Apache HTTP 协议服务器性能低下而且难于大规模集群,相比来说 nginx 的高性能及轻量级等特性,正是其的替代方案. MariaDB 是一款社

CentOS 下安装 LEMP 服务(nginx、MariaDB/MySQL 和 php)

转载自:https://linux.cn/article-4314-1.html 编译自:http://xmodulo.com/install-lemp-stack-centos.html 作者: Dan Nanni 原创:LCTT https://linux.cn/article-4314-1.html 译者: runningwater 本文地址:https://linux.cn/article-4314-1.html LEMP 组合包是一款日益流行的网站服务组合软件包,在许多生产环境中的核心

[转载]CentOS 下安装LEMP服务(Nginx、MariaDB/MySQL和PHP)

LEMP 组合包是一款日益流行的网站服务组合软件包,在许多生产环境中的核心网站服务上起着强有力的作用.正如其名称所暗示的, LEMP 包是由 Linux.nginx.MariaDB/MySQL 和 PHP 组成的.在传统的 LAMP 包中使用的 Apache HTTP 协议服务器性能低下而且难于大规模集群,相比来说 nginx 的高性能及轻量级等特性,正是其的替代方案. MariaDB 是一款社区支持驱动的 MySQL 数据库的分支,其功能更多性能更佳.PHP,服务端编程语言,具体是由 PHP

CentOS 下安装 LEMP 服务(Nginx、MariaDB/MySQL 和PHP)

本文环境基于 CentOS 7 1)安装Nginx # yum install nginx # systemctl enable nginx.service # systemctl start nginx.service 2)安装mariadb # yum install mariadb-server # systemctl enable mariadb # systemctl start mariadb 安装完成后,首次启动应先运行脚本: # mysql_secure_installation

CentOS环境利用mariadb(mysql)数据库使用golang实现分布式系统的Leader选举

一.准备工作 1.下载安装vmware,步骤省略. 2.下载CentOS系统ios包:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-1611.iso 3.下载安装Xshell5,步骤省略. 4.下载安装git,步骤省略. 5.mariadb用于golang的api:https://github.com/go-sql-driver/mysql 6.vmware中依次点击"创建新的虚拟机&q

MySql数据库双主(双向)同步实现数据库双主热备

MySql数据库双主(双向)同步实现数据库双主热备配置步骤有一点复杂,大家一定要看清楚每一步小细节哦,希望文章对各位会带来帮助呀. 之前写过一篇 mysql Master Slave主从同步(复制)配置,属于数据库备份级别的.现在的需求是,两台服务器上都装有数据库,为了防止某一服务器出现问题而影响业务的运行,需要准备两台服务器分别运行mysql,且需要两台服务器的数据是保持同步的.也就是现在要说的mysql双向同步,实现数据库主备模式. 基础环境 操作服务器系统:Ubuntu 12.04 64-

【DNS服务补录】+【mariadb,mysql数据库】

DNS服务补录,1)关于:副DNS服务器的配置/etc/resolv.conf 中的servername指向自己.slavesvim /etc/named.rfc1912.zones[添加更改为]zone "HXL.com" IN {    type slave;    masters { 172.25.254.206 ;};    file"slaves/HXL.com.zone";    allow-update { none; };};在主DNS服务器的vim

RaspberryPi(树莓派)如何安装 MariaDB / MySQL 数据库

安装的过程比较简单. 但是这里有一个地方需要注意,如果你希望是能够通过网络访问你安装的数据库的话. 在你设置好用户名和密码,以及访问权限后,你可能发现你还是访问不了. 这是因为你的安装服务器只绑定了能够本地访问,你需要修改配置,让你的服务器能够支持远程访问. 具体的方法,请参考 RaspberryPi(树莓派)安装 MariaDB 数据库没有办法远程访问 页面中的文章. 更新系统 通过运行命令: sudo apt-get update 来对你需要安装的系统进行更新,根据网络的情况可能需要的时间也

binlog+审计日志 定位mariadb(mysql)数据库特定sql

最近线上数据莫名的丢数据,故进行sql定位,定位线上数据为何丢失,最后定位到具体某开发程序. 审计日志:记录数据库所有信息,故会有巨大的日志,而且参数设置审计日志大小,会进行审计日志轮换分割. 1.基于审计日志这个特点,业务提出问题后,要及时发现问题解决问题. 线上业务日志为512M一个日志,共10个.大约能记录8个小时左右的数据库访问信息. 2.binlog:binlog是记录mysql数据库变化的信息,记录增删改等信息. 结合以上两点:可以binlog定位问题sql,通过审计日志定位操作DB