利用keepalived构建高可用MySQL-HA

MySQL-VIP:192.168.174.100

MySQL-master1:192.168.174.135

MySQL-master2:192.168.174.139

OS版本:CentOS 6.5

MySQL版本:5.6.17

Keepalived版本:1.1.16
 最新版

第一部分 搭建主主复制

1.1、创建复制帐号

主服务器

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.*  TO [email protected]‘192.168.174.135‘ IDENTIFIED BY ‘rep‘;

另一个主服务器

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.*  TO [email protected]‘192.168.174.139‘ IDENTIFIED BY ‘rep‘;

1.2、配置master

[client]

port = 3306

#socket = /tmp/mysql.sock

[mysqld_safe]

#socket = /tmp/mysql.sock

#nice = 0

[mysqld]

sql_mode=‘ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION‘

user = mysql

#socket = /tmp/mysql.sock

port = 3306

basedir = /usr

datadir = /var/lib/mysql

log-error=/var/lib/mysql/centos3.err

pid-file=/var/lib/mysql/centos3.pid

# tmp dir settings ##

tmpdir = /tmp

tmp_table_size = 64M

max_heap_table_size=64M

slow_query_log = 1

long_query_time = 1

slow_query_log_file = /var/lib/mysql/slow.log

log-output = FILE

## character set ##

character-set-server = utf8

init_connect =‘set names utf8‘

skip-character-set-client-handshake = 1

lower_case_table_names=1

skip-log-warnings

skip-name-resolve

open_files_limit=65535

max_connections = 1000

max_connect_errors = 1000

wait_timeout = 864000

interactive_timeout=864000

connect_timeout = 5

max_allowed_packet = 16M

#table_cache = 256

thread_cache_size = 32

thread_concurrency = 8

#key_buffer_size = 16M #8M

join_buffer_size = 2M

sort_buffer_size = 2M

read_buffer_size = 2M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 64M

innodb_fast_shutdown = 1

innodb_buffer_pool_size =200M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 2

innodb_lock_wait_timeout = 10

#innodb_thread_concurrency = 24

#innodb_commit_concurrency = 24

#innodb_file_io_threads = 4

#innodb_read_io_threads = 8

#innodb_write_io_threads = 8

#innodb_log_file_size = 128M

#innodb_log_files_in_group = 3

#innodb_max_dirty_pages_pct = 90

# * Query Cache Configuration

query_cache_limit = 2M

query_cache_size = 16M

query_cache_type = 1

log-bin=centos3-bin

log-bin-index=centos3-bin.index

binlog_format=mixed

expire_logs_days = 15

binlog_cache_size = 1M

max_binlog_size = 128M

log_bin_trust_function_creators=1

#######################SERVER ID########################

server-id=1

log_slave_updates = 1

relay-log = centos3-relay-log.log

relay-log-index = centos3-relay-log.index

#######################MASTER########################

binlog-ignore-db=mysql

binlog-ignore-db=test

binlog-ignore-db=information_schema

binlog-ignore-db=performance_schema

replicate-ignore-db = mysql

replicate-ignore-db = test

replicate-ignore-db = information_schema

replicate-ignore-db = performance_schema

#max_relay_log_size = 50000000

[mysqldump]

quick

1.3 配置从主

[client]

port = 3306

#socket = /tmp/mysql.sock

[mysqld_safe]

#socket = /tmp/mysql.sock

#nice = 0

[mysqld]

sql_mode=‘ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION‘

user = mysql

#socket = /tmp/mysql.sock

port = 3306

basedir = /usr

datadir = /var/lib/mysql

log-error=/var/lib/mysql/centos4.err

pid-file=/var/lib/mysql/centos4.pid

# tmp dir settings ##

tmpdir = /tmp

tmp_table_size = 64M

max_heap_table_size=64M

slow_query_log = 1

long_query_time = 1

slow_query_log_file = /var/lib/mysql/slow.log

log-output = FILE

## character set ##

character-set-server = utf8

init_connect =‘set names utf8‘

skip-character-set-client-handshake = 1

lower_case_table_names=1

skip-log-warnings

skip-name-resolve

open_files_limit=65535

max_connections = 1000

max_connect_errors = 1000

wait_timeout = 864000

interactive_timeout=864000

connect_timeout = 5

max_allowed_packet = 16M

#table_cache = 256

thread_cache_size = 32

thread_concurrency = 8

#key_buffer_size = 16M #8M

join_buffer_size = 2M

sort_buffer_size = 2M

read_buffer_size = 2M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 64M

innodb_fast_shutdown = 1

innodb_buffer_pool_size =200M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 2

innodb_lock_wait_timeout = 10

#innodb_thread_concurrency = 24

#innodb_commit_concurrency = 24

#innodb_file_io_threads = 4

#innodb_read_io_threads = 8

#innodb_write_io_threads = 8

#innodb_log_file_size = 128M

#innodb_log_files_in_group = 3

#innodb_max_dirty_pages_pct = 90

# * Query Cache Configuration

query_cache_limit = 2M

query_cache_size = 16M

query_cache_type = 1

log-bin=centos4-bin

log-bin-index=centos4-bin.index

binlog_format=mixed

expire_logs_days = 15

binlog_cache_size = 1M

max_binlog_size = 128M

log_bin_trust_function_creators=1

#######################SERVER ID########################

server-id=2

log_slave_updates = 1

relay-log = centos4-relay-log.log

relay-log-index = centos4-relay-log.index

#######################MASTER########################

binlog-ignore-db=mysql

binlog-ignore-db=test

binlog-ignore-db=information_schema

binlog-ignore-db=performance_schema

replicate-ignore-db = mysql

replicate-ignore-db = test

replicate-ignore-db = information_schema

replicate-ignore-db = performance_schema

#max_relay_log_size = 50000000

[mysqldump]

quick

1.4 重启主主 及 从主mysql服务

service mysqld restart

注意:从主要先备份mysql db 的5个innodb table,后面恢复热备数据库时用到,不然虽然启动成功,但错误日志会报错

脚本如下:

[[email protected] mysql]#
cat /dba_script/get_mysql_innodb.sh

#!/bin/bash

TABLELIST="innodb_index_stats"

TABLELIST="${TABLELIST} innodb_table_stats"

TABLELIST="${TABLELIST} slave_master_info"

TABLELIST="${TABLELIST} slave_relay_log_info"

TABLELIST="${TABLELIST} slave_worker_info"

mysqldump -uroot -p mysql ${TABLELIST} > /dba_script/mysql_innodb_tables.sql

执行:

./dba_script/get_mysql_innodb.sh

1.5在主主热备数据库到从主

innobackupex --defaults-file=/etc/my.cnf  --user=root --password=admin --port=3306   --stream=tar /data/backup/full |gzip
1>/data/backup/full/masteralldb.tar.gz

-rw-r--r-- 1 root root 24304564 Apr 21 06:47 masteralldb.tar.gz

1.6 从主恢复masteralldb.tar.gz,并同步bin日志

[[email protected] full]# scp masteralldb.tar.gz [email protected]:/data/backup/full

tar -izxvf /data/backup/full/masteralldb.tar.gz -C /data/backup/full/

[[email protected] full]# ll

total 101592

-rw-r--r--. 1 root root 295 Apr 21 06:47 backup-my.cnf

-rw-rw----. 1 root root 79691776 Apr 21 06:29 ibdata1

-rw-r--r--. 1 root root 24304564 Apr 21 06:49 masteralldb.tar.gz

drwxr-xr-x. 2 root root 4096 Apr 21 06:51 mysql

drwxr-xr-x. 2 root root 4096 Apr 21 06:51 testdb

drwxr-xr-x. 2 root root 4096 Apr 21 06:51 testdb2

-rw-r--r--. 1 root root 23 Apr 21 06:47 xtrabackup_binlog_info

-rw-rw----. 1 root root 95 Apr 21 06:47 xtrabackup_checkpoints

-rw-r--r--. 1 root root 617 Apr 21 06:47 xtrabackup_info

-rw-rw----. 1 root root 2560 Apr 21 06:47 xtrabackup_logfile

[[email protected] full]# cat xtrabackup_binlog_info

centos3-bin.000007 120

停止从主mysql服务

service mysql stop

备份mysql 数据目录,只拷贝需要恢复的DB和共享表空间,testdb testdb2 ibdata1

[[email protected] full]# cp -R -p /var/lib/mysql /var/lib/mysql_bak

[[email protected] full]# cp -p -R testdb testdb2 ibdata1 /var/lib/mysql/

[[email protected] full]#
chown -R mysql.mysql /var/lib/mysql

[[email protected] mysql]# service mysql start

启动成功,但是在错误日志中看到如下错误警报:

2015-04-22 06:37:56 11330 [Warning] ‘user‘ entry ‘[email protected]‘
ignored in --skip-name-resolve mode.

2015-04-22 06:37:56 11330 [Warning] ‘proxies_priv‘ entry ‘@ [email protected]‘
ignored in --skip-name-resolve mode.

2015-04-22 06:37:56 11330 [Warning] InnoDB: Cannot open table mysql/slave_master_info from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for
how you can resolve the problem.

2015-04-22 06:37:56 11330 [Warning] Info table is not ready to be used. Table ‘mysql.slave_master_info‘ cannot be opened.

2015-04-22 06:37:56 11330 [Warning] InnoDB: Cannot open table mysql/slave_worker_info from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for
how you can resolve the problem.

2015-04-22 06:37:56 11330 [Warning] InnoDB: Cannot open table mysql/slave_relay_log_info from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for
how you can resolve the problem.

2015-04-22 06:37:56 11330 [Warning] Info table is not ready to be used. Table ‘mysql.slave_relay_log_info‘ cannot be opened.

2015-04-22 06:37:56 11330 [Note] Event Scheduler: Loaded 0 events

2015-04-22 06:37:56 11330 [Note] /usr/sbin/mysqld: ready for connections.

Version: ‘5.6.17-log‘ socket: ‘/var/lib/mysql/mysql.sock‘ port: 3306 MySQL Community Server (GPL)

2015-04-22 06:39:07 7f32ac141700 InnoDB: Error: Table "mysql"."innodb_table_stats" not found.

2015-04-22 06:39:07 7f32ac141700 InnoDB: Error: Fetch of persistent statistics requested for table "testdb"."student" but the required system tables mysql.innodb_table_stats and mysql.innodb_index_stats are not present or have
unexpected structure. Using transient stats instead.

2015-04-22 06:39:23 7f32ac141700 InnoDB: Error: Table "mysql"."innodb_table_stats" not found.

2015-04-22 06:39:23 7f32ac141700 InnoDB: Error: Fetch of persistent statistics requested for table "testdb2"."student2" but the required system tables mysql.innodb_table_stats and mysql.innodb_index_stats are not present or have
unexpected structure. Using transient stats instead.

当写入数据库还会报如下错:

2015-04-22 06:52:24 7f32695a5700
InnoDB: Recalculation of persistent statistics requested for table "testdb2"."student2" but the required persistent statistics storage is not present or is corrupted. Using transient stats instead.

2015-04-22 06:52:35 7f32695a5700
InnoDB: Error: Table "mysql"."innodb_table_stats" not found.

2015-04-22 06:52:35 7f32695a5700
InnoDB: Recalculation of persistent statistics requested for table "testdb2"."student2" but the required persistent statistics storage is not present or is corrupted. Using transient stats instead.

在从主上查询mysql db 自己的5个innodb table,因为之前更换了共享表空间,所以下面的innodb table出现了问题

[[email protected] mysql]# ll /var/lib/mysql/mysql/*.ibd

-rw-------. 1 mysql mysql 98304 Apr 21 06:39 /var/lib/mysql/mysql/innodb_index_stats.ibd

-rw-------. 1 mysql mysql 98304 Apr 21 06:39 /var/lib/mysql/mysql/innodb_table_stats.ibd

-rw-------. 1 mysql mysql 98304 Mar 20 22:38 /var/lib/mysql/mysql/slave_master_info.ibd

-rw-------. 1 mysql mysql 98304 Mar 20 22:38 /var/lib/mysql/mysql/slave_relay_log_info.ibd

-rw-------. 1 mysql mysql 98304 Mar 20 22:38 /var/lib/mysql/mysql/slave_worker_info.ibd

现删除

[[email protected] mysql]# rm -rf /var/lib/mysql/mysql/*.ibd

用之前的脚本重建,如果不删除直接重建会报错

[[email protected] dba_script]# mysql -uroot -p mysql < /dba_script/mysql_innodb_tables.sql

Enter password:

ERROR 1813 (HY000) at line 25: Tablespace for table ‘`mysql`.`innodb_index_stats`‘ exists. Please DISCARD the tablespace before IMPORT.

1.7 搭建互为slave

从主服务器

mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.174.139‘, MASTER_USER=‘rep‘,MASTER_PASSWORD=‘rep‘, MASTER_LOG_FILE=‘centos3-bin.000008‘, MASTER_LOG_POS=120;

主主服务器

mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.174.135‘, MASTER_USER=‘rep‘,MASTER_PASSWORD=‘rep‘, MASTER_LOG_FILE=‘centos4-bin.000006‘, MASTER_LOG_POS=3329;

1.8主从都开启slave

mysql>start slave;

mysql>show slave status \G;

主主配置成功!

时间: 2024-08-26 19:43:46

利用keepalived构建高可用MySQL-HA的相关文章

利用keepalived构建高可用MySQL High Application

关于MySQL-HA,目前有多种解决方案,比如heartbeat.drbd.mmm.共享存储,但是它们各有优缺点.heartbeat. drbd配置较为复杂,需要自己写脚本才能实现MySQL自动切换:对于mmm,生产环境中很少有人 用,且mmm 管理端需要单独运行一台服务器上,要是想实现高可用,就得对mmm管理端做HA,这样无疑又增加了硬件开支:对于共享存储,个人觉得MySQL数据还是放 在本地较为安全,存储设备毕竟存在单点隐患. 使用MySQL双master+keepalived是一种非常好的

MySql:利用keepalived构建高可用MySQL-HA

关于MySQL-HA,目前有多种解决方案,比如heartbeat.drbd.mmm.共享存储,但是它们各有优缺点.heartbeat.drbd配置较为复杂,需要自己写脚本才能实现MySQL自动切换,对于不会脚本语言的人来说,这无疑是一种脑裂问题:对于mmm,生产环境中很少有人用,且mmm 管理端需要单独运行一台服务器上,要是想实现高可用,就得对mmm管理端做HA,这样无疑又增加了硬件开支:对于共享存储,个人觉得MySQL数据还是放在本地较为安全,存储设备毕竟存在单点隐患.使用MySQL双mast

用HAProxy和KeepAlived构建高可用的反向代理

用HAProxy和KeepAlived构建高可用的反向代理 前言对于访问量较大的网站来说,随着流量的增加单台服务器已经无法处理所有的请求,这时候需要多台服务器对大量的请求进行分流处理,即负载均衡.而如果实 现负载均衡,必须在网站的入口部署服务器(不只是一台)对这些请求进行分发,这台服务器即反向代理.由于反向代理服务器是网站的入口,其负载压力大且易遭 到攻击,存在单点故障的风险,所以我们需要一个高可用的方案来实现当一台反向代理服务器宕机的时候,另一台服务器会自动接管服务.基于以上要求,我们使用 H

基于Keepalived构建高可用集群配置实例(HA Cluster)

什么是集群 简单的讲集群(cluster)就是一组计算机,它们作为一个整体向用户提供一组网络资源.这些单个的计算机系统就是集群的节点(node).一个理想的集群是,用户从来不会意识到集群系统底层的节点,在他/她们看来,集群是一个系统,而非多个计算机系统.并且集群系统的管理员可以随意增加和删改集群系统的节点. 关于更详细的高可用集群我们在后面再做详解,先来说说Keepalived Keepalived是什么 Keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbea

drbd+corosync+pacemaker构建高可用MySQL集群

一.drbd简介 drbd全称Distributed Replicated Block Device,为分布式复制块设备,基于软件实现的,不共享任何东西的,通过复制的方式构建镜像模式工作的磁盘,类似于raid1,但不同于raid的是,drbd实现了跨主机镜像块数据.drbd工作原理:由工作于内核层次的drbd,将要写入本地磁盘的数据镜像一份发往本地网卡,由本地网卡发往另一台drbd主机的本地磁盘存储.因此,drbd的两个主机的,磁盘存储一模一样,从而实现分布式复制块设备的实现.drbd进程对磁盘

架构设计:负载均衡层设计方案(6)——Nginx + Keepalived构建高可用的负载层

1.概述 前两遍文章中,我们一直在说后文要介绍Nginx + Keepalived的搭建方式.这篇文章开始,我们就来兑现前文的承诺,后续的两篇文章我们将介绍Nginx + Keepalived和 LVS + Keepalived搭建高可用的负载层系统.如果你还不了解Nginx和LVS的相关知识,请参见我之前的两篇文章<架构设计:负载均衡层设计方案(2)--Nginx安装>(http://blog.csdn.net/yinwenjie/article/details/46620711).<

CentOS7+MySQL/MariaDB+Galera+HAProxy+Keepalived构建高可用数据库集群

方案优势: Galera能够实现MySQL/MariaDB数据库的主主复制和多主复制等模式,这些复制模式都是同步进行的,同步时间非常短 每一个节点都可以同时写入和读取,当某一节点发生故障时,可自动从集群中自动剔除 HAProxy能提供负载均衡和故障判断等功能解决服务器系统存在的单点故障 Keepalived能提供客户端连接数据库时使用的虚拟IP地址(VIP) 关于HAProxy的负载均衡算法 轮询方式(roundrobin):不适合用于backend为web服务器的情况,因为session.co

LVS+Keepalived构建高可用群集

今天以DR模式的LVS群集为基础,增加了一台从负载调度器,使用keepalived来实现主.备调度服务器的热备,从而构架具有负载均衡.高可用两种能力的LVS网站群集平台. 网站拓扑架构图 系统环境 主机 操作系统 IP地址 DR1主调度服务器: CentOS 7.0 x86_64 192.168.100.25 DR2备调度服务器: CentOS 7.0 x86_64 192.168.100.26 web1节点服务器: CentOS 7.0 x86_64 192.168.100.27 web2节点

使用Keepalived构建高可用热备

简介 Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器. 工作原理 keepalived的工作原理是VRRP(Virtual Router Redundancy Protocol)虚拟路由冗余协议. 在VRRP中有两组重要的概念