MySQL数据库高可用快速实施方案

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL数据库高可用快速实施方案相关的知识,希望对你有一定的参考价值。

Note:以下为mysql+DRBD+HEARTBEAT快速实施文档,若要用于生产环境,请仔细阅读官方文档并结合实际业务调整参数,涉及数据部署请慎重!!!

(个人建议:在基于个人熟悉服务的情况下并通过测试环境才可在线上使用。)


数据库高可用

MySQL+DRBD+HEARTBEAT实施方案

环境:

mysql-5.5.49

heartbeat-3.0.4-2.el6.x86_64

drbd84-utils-8.9.8-1.el6.elrepo.x86_64

CentOS release 6.7 (Final)

2.6.32-573.el6.x86_64

x86_64

 

 

规划表:

名称

接口

IP

用途

Master

eth0

10.0.0.51

管理IP


eth1

172.16.1.51

心跳线

Backup

eth0

10.0.0.52

管理IP


eth1

172.16.1.52

心跳线

VIP

eth0

10.0.0.50

提供数据库服务IP

 

以下操作除特殊说明外,均为在两台服务器操作

1.     初始化

关闭selinux,iptables

配置主机名,/etc/hosts,/etc/sysconfig/network,配置网络配置

2.     分区

parted /dev/sdb mklabel gpt

parted /dev/sdb mkpart primary 0 1024

parted /dev/sdb mkpart primary 1025 2146

parted /dev/sdb p

3.     主MySQL格式化/dev/sdb1,从MySQL不需要格式化(特殊说明)

mkfs.ext4 /dev/sdb1

 

4.     安装DRBD

rpm -Uvh http://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm

yum install -y drbd84-utils kmod-drbd84

5.     初始化DRBD

modprobe drbd

6.     创建配置资源文件/etc/drbd.d/r0.res

resource r0 {

  net {

    protocol C;

    cram-hmac-alg "sha1";

    shared-secret "c4f9375f9834b4e7f0a528cc65c055702bf5f24a";

}

  device      /dev/drbd0;

  disk        /dev/sdb1;

  meta-disk   /dev/sdb2[0];

  on mysql {

    address     172.16.1.51:7788;

  }

  on mysql-s {

    address     172.16.1.52:7788;

  }

}

7.     初始化drbd设备

drbdadm create-md r0

drbdadm up r0

cat /proc/drbd

8.     将mysql提升为主,从mysql不需要操作(特殊说明)

drbdadm -- --overwrite-data-of-peer primary r0

9.     将mysql的drbd0格式化以供挂载使用,从mysql不需要操作(特殊说明)

mkfs.ext4 /dev/drbd0

10.   测试挂载

mkdir /data

mount /dev/drbd0 /data

 

至此,DRBD安装完毕,下面安装mysql,以多实例的方式安装

 

1.     上传mysql二进制包并解压

mkdir /home/oldboy/tools -p

cd /home/oldboy/tools/

rz

#mysql-5.5.49-linux2.6-x86_64.tar.gz

tar -xf mysql-5.5.49-linux2.6-x86_64.tar.gz

 

2.     安装mysql多实例

mkdir /application

mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49

useradd -s /sbin/nologin mysql -M

ln -s /application/mysql-5.5.49/ /application/mysql

cd /application/mysql

chown -R mysql.mysql /application/mysql/

echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile

source /etc/profile

 

3.     配置多实例配置和启动文件

mkdir /data/{3306,3307}/data -p

配置3306配置文件/data/3306/my.cnf

[client]

port            = 3306

socket          = /data/3306/mysql.sock

[mysql]

no-auto-rehash

[mysqld]

user    = mysql

port    = 3306

socket  = /data/3306/mysql.sock

basedir = /application/mysql

datadir = /data/3306/data

open_files_limit    = 1024

back_log = 600

max_connections = 800

max_connect_errors = 3000

table_cache = 614

external-locking = FALSE

max_allowed_packet =8M

sort_buffer_size = 1M

join_buffer_size = 1M

thread_cache_size = 100

thread_concurrency = 2

query_cache_size = 2M

query_cache_limit = 1M

query_cache_min_res_unit = 2k

#default_table_type = InnoDB

thread_stack = 192K

#transaction_isolation = READ-COMMITTED

tmp_table_size = 2M

max_heap_table_size = 2M

long_query_time = 1

#log_long_format

#log-error = /data/3306/error.log

#log-slow-queries = /data/3306/slow.log

pid-file = /data/3306/mysql.pid

log-bin = /data/3306/mysql-bin

relay-log = /data/3306/relay-bin

relay-log-info-file = /data/3306/relay-log.info

binlog_cache_size = 1M

max_binlog_cache_size = 1M

max_binlog_size = 2M

expire_logs_days = 7

key_buffer_size = 16M

read_buffer_size = 1M

read_rnd_buffer_size = 1M

bulk_insert_buffer_size = 1M

#myisam_sort_buffer_size = 1M

#myisam_max_sort_file_size = 10G

#myisam_max_extra_sort_file_size = 10G

#myisam_repair_threads = 1

#myisam_recover

lower_case_table_names = 1

skip-name-resolve

slave-skip-errors = 1032,1062

replicate-ignore-db=mysql

server-id = 6

innodb_additional_mem_pool_size = 4M

innodb_buffer_pool_size = 32M

innodb_data_file_path = ibdata1:128M:autoextend

innodb_file_io_threads = 4

innodb_thread_concurrency = 8

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 2M

innodb_log_file_size = 4M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_file_per_table = 0

[mysqldump]

quick

max_allowed_packet = 2M

[mysqld_safe]

log-error=/data/3306/mysql_3306.err

pid-file=/data/3306/mysqld.pid

 

配置3306启动文件/data/3306/mysql

#!/bin/sh

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

#this scripts is created by chentaicheng at 2016-06-09

# QQ:791582297

#site:http://www.unix-idc.com

#blog: http://blog.51cto.com/ilctc

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

 

#init

port=3306

mysql_user="root"

mysql_pwd="unix-idc-MySQL"

CmdPath="/application/mysql/bin"

mysql_sock="/data/${port}/mysql.sock"

#startup function

function_start_mysql()

{

    if [ ! -e "$mysql_sock" ];then

      printf "Starting MySQL...\n"

      /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

    else

      printf "MySQL is running...\n"

      exit

    fi

}

 

#stop function

function_stop_mysql()

{

    if [ ! -e "$mysql_sock" ];then

       printf "MySQL is stopped...\n"

       exit

    else

       printf "Stoping MySQL...\n"

       ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown

   fi

}

 

#restart function

function_restart_mysql()

{

    printf "Restarting MySQL...\n"

    function_stop_mysql

    sleep 2

    function_start_mysql

}

 

case $1 in

start)

    function_start_mysql

;;

stop)

    function_stop_mysql

;;

restart)

    function_restart_mysql

;;

*)

    printf "Usage: /data/${port}/mysql {start|stop|restart}\n"

esac

 

配置3307配置文件/data/3307/my.cnf

[client]

port            = 3307

socket          = /data/3307/mysql.sock

[mysql]

no-auto-rehash

[mysqld]

user    = mysql

port    = 3307

socket  = /data/3307/mysql.sock

basedir = /application/mysql

datadir = /data/3307/data

open_files_limit    = 1024

back_log = 600

max_connections = 800

max_connect_errors = 3000

table_cache = 614

external-locking = FALSE

max_allowed_packet =8M

sort_buffer_size = 1M

join_buffer_size = 1M

thread_cache_size = 100

thread_concurrency = 2

query_cache_size = 2M

query_cache_limit = 1M

query_cache_min_res_unit = 2k

#default_table_type = InnoDB

thread_stack = 192K

#transaction_isolation = READ-COMMITTED

tmp_table_size = 2M

max_heap_table_size = 2M

long_query_time = 1

#log_long_format

#log-error = /data/3307/error.log

#log-slow-queries = /data/3307/slow.log

pid-file = /data/3307/mysql.pid

log-bin = /data/3307/mysql-bin

relay-log = /data/3307/relay-bin

relay-log-info-file = /data/3307/relay-log.info

binlog_cache_size = 1M

max_binlog_cache_size = 1M

max_binlog_size = 2M

expire_logs_days = 7

key_buffer_size = 16M

read_buffer_size = 1M

read_rnd_buffer_size = 1M

bulk_insert_buffer_size = 1M

#myisam_sort_buffer_size = 1M

#myisam_max_sort_file_size = 10G

#myisam_max_extra_sort_file_size = 10G

#myisam_repair_threads = 1

#myisam_recover

lower_case_table_names = 1

skip-name-resolve

slave-skip-errors = 1032,1062

replicate-ignore-db=mysql

server-id = 6

innodb_additional_mem_pool_size = 4M

innodb_buffer_pool_size = 32M

innodb_data_file_path = ibdata1:128M:autoextend

innodb_file_io_threads = 4

innodb_thread_concurrency = 8

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 2M

innodb_log_file_size = 4M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_file_per_table = 0

[mysqldump]

quick

max_allowed_packet = 2M

[mysqld_safe]

log-error=/data/3307/mysql_3307.err

pid-file=/data/3307/mysqld.pid

 

配置3307启动文件/data/3307/mysql

#!/bin/sh

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

#this scripts is created by chentaicheng at 2016-06-09

# QQ:791582297

#site:http://www.unix-idc.com

#blog: http://blog.51cto.com/ilctc

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

 

#init

port=3307

mysql_user="root"

mysql_pwd="unix-idc-MySQL"

CmdPath="/application/mysql/bin"

mysql_sock="/data/${port}/mysql.sock"

#startup function

function_start_mysql()

{

    if [ ! -e "$mysql_sock" ];then

      printf "Starting MySQL...\n"

      /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &

    else

      printf "MySQL is running...\n"

      exit

    fi

}

 

#stop function

function_stop_mysql()

{

    if [ ! -e "$mysql_sock" ];then

       printf "MySQL is stopped...\n"

       exit

    else

       printf "Stoping MySQL...\n"

       ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown

   fi

}

 

#restart function

function_restart_mysql()

{

    printf "Restarting MySQL...\n"

    function_stop_mysql

    sleep 2

    function_start_mysql

}

 

case $1 in

start)

    function_start_mysql

;;

stop)

    function_stop_mysql

;;

restart)

    function_restart_mysql

;;

*)

    printf "Usage: /data/${port}/mysql {start|stop|restart}\n"

esac

 

初始化3306数据库数据

sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data/ --user=mysql

 

初始化3307数据库数据

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data/ --user=mysql

 

授权mysql用户为/data目录的权限所有者

chown -R mysql.mysql /data

 

启动数据库

chmod +x /data/3306/mysql

chmod +x /data/3307/mysql

/data/3306/mysql start

/data/3307/mysql start

 

设置数据库密码为:unix-idc-MySQL

mysqladmin -u root -S /data/3306/mysql.sock password 'unix-idc-MySQL'

mysqladmin -u root -S /data/3307/mysql.sock password 'unix-idc-MySQL'

 

启动3306和3307实例数据库

mysql -uroot -punix-idc-MySQL -S /data/3306/mysql.sock

mysql -uroot -punix-idc-MySQL -S /data/3306/mysql.sock

 

至此,数据库安装完毕,下面安装heartbeat!

 

 

1.     安装heartbeat

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

yum repolist

yum install -y heartbeat

2.     将默认的配置文件拷贝至/etc/ha.d/并作更改

修改第一个配置文件:/etc/ha.d/ha.cf

cat >/etc/ha.d/ha.cf<<EOF

logfile /var/log/ha-log

logfacility  local0

keepalive 2

deadtime 30

warntime 10

initdead 60

mcast eth0 225.0.0.51 694 1 0

auto_failback on

node mysql

node mysql-s

EOF

修改第二个配置文件:/etc/ha.d/authkeys

cat >/etc/ha.d/authkeys <<EOF

auth 1

1 sha1 c4f9375f9834b4e7f0a528cc65c055702bf5f24a

EOF

chmod 600 /etc/ha.d/authkeys

修改第三个配置文件:/etc/ha.d/haresources

cat >/etc/ha.d/haresources<<EOF

mysql 10.0.0.50 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext4 mysql

EOF

3.     修改mysql启动脚本到heartbeat资源里

cp /data/3306/mysql /etc/ha.d/resource.d/

4.     启动heartbeat

/etc/init.d/heartbeat start

 

至此,MySQL+DRBD+HEARTBEAT高可用存储搭建完毕!

 

 

 


以上是关于MySQL数据库高可用快速实施方案的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL数据库高可用方案探究

mysql高可用集群方案

Oracle Compute云快速搭建MySQL Keepalived高可用架构

基于半同步复制的MHA高可用MySql集群架构搭建实战

五大常见的MySQL高可用方案(最全)

新书推荐 - 《MySQL高可用解决方案》