CentOS6安装MySQL HA架构部署(MySQL + Heartbeat + DRBD)

Heartbeat + DRBD安装配置参考上一篇文章:CentOS6安装NFS HA架构部署(NFS + Heartbeat + DRBD)
这里只说下mysql的安装方法


Mysql的编译安装(默认都是双台配置)

这里选取的是mysql-5.6.30进行源码编译安装。
yum -y install make gcc-c++ cmake bison bison-devel  ncurses-devel libaio

groupadd mysql
useradd -r -g mysql mysql
mkdir -p /drbd_data/mysql/data
mkdir -p /drbd_data/mysql/mysql-bin

wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30.tar.gz
tar zxvf mysql-5.6.30.tar.gz
cd mysql-5.6.30
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/drbd_data/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1

/drbd_data/mysql/data是drbd切换的目录

make
make install

chown -R mysql.mysql /usr/local/mysql
cd /usr/local/mysql/scripts
./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/drbd_data/mysql/data

cd /usr/local/mysql/support-files
cp mysql.server /etc/rc.d/init.d/mysqld
修改/etc/my.cnf,更新datadir取值。

修改环境变量:
#vi /etc/profile
PATH=/usr/local/mysql/bin:$PATH
export PATH

#source /etc/profile

设置用户访问mysql权限,允许root从任意一台访问,可以针对这个虚拟IP设置权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;

haresources的配置:
vi /etc/ha.d/haresources
testserver2.bj IPaddr::10.111.32.177/24/eth0:1 drbddisk::r0 Filesystem::/dev/drbd0::/drbd_data::ext4 mysqld

如果有多个服务切换最后空格加上就好,例如:
testserver2.bj IPaddr::10.111.32.177/24/eth0:1 drbddisk::r0 Filesystem::/dev/drbd0::/drbd_data::ext4 mysqld killnfsd

只再drbd主的节点启动mysqld服务,drbd切换的时候会自动启动mysqld服务(单台机器操作)
service mysqld start
mysql_secure_installation #设置密码

删除drbd从节点的/hadata/mysql/中文件,因为切换后会将数据漂移过来。

提示:
配置好heartbeat之后,需要将mysql从自启动服务器中去掉,因为主heartbeat启动的时候会挂载drdb文件系统以及启动mysql,切换的时候会将主上的mysql停止并卸载文件系统,从上会挂载文件系统,并启动mysql。因此需要做如下操作(两台服务器):
chkconfig mysqld off  #不开启开机启动

mysql配置文件参考:
# more my.cnf
[client]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
default-character-set=utf8
[mysqld]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
character-set-server = utf8
init-connect='SET NAMES utf8'
basedir         = /usr/local/mysql
datadir         = /drbd_data/mysql/data
skip-external-locking
skip-name-resolve
default-storage-engine = InnoDB
back_log = 2048
max_connections = 1024
max_connect_errors = 256
max_allowed_packet = 32M
table_open_cache = 2048
sort_buffer_size = 8M
read_buffer_size = 8M
read_rnd_buffer_size = 64M
join_buffer_size = 512K
max_heap_table_size = 256M
tmp_table_size = 256M
tmpdir = /dev/shm
#query_cache_type = 0
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8
bulk_insert_buffer_size = 64M
thread_cache_size = 16384
thread_concurrency = 8
thread_stack = 512K
key_buffer_size = 4096M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 15G
myisam_repair_threads = 1
myisam_recover_options = DEFAULT
server-id = 1
log-bin=/hadata/mysql/mysql-bin
binlog_format=mixed
binlog_cache_size = 4M
max_binlog_size = 1024M
expire-logs-days = 7
slow_query_log = 1
long_query_time = 10
slow_query_log_file=/hadata/mysql/slow_query.log
log-error = /var/log/mysqld.log
innodb_data_file_path = ibdata1:12M:autoextend
innodb_buffer_pool_size = 15G
innodb_buffer_pool_instances = 8
innodb_additional_mem_pool_size = 64M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_log_buffer_size = 64M
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 75
innodb_support_xa = 1
innodb_thread_concurrency = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_io_capacity = 1600
innodb_flush_method = O_DIRECT
innodb_file_per_table = 1
innodb_file_format = Barracuda
innodb_strict_mode = 1
innodb_purge_threads = 1
innodb_change_buffering = all
transaction_isolation = REPEATABLE-READ
explicit_defaults_for_timestamp=true
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no-auto-rehash
default-character-set = utf8
[myisamchk]
key_buffer_size = 1024M
sort_buffer_size = 1024M
read_buffer = 32M
write_buffer = 32M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 65535
default-character-set = utf8


正常情况/etc/init.d/heartbeat stop会进行切换drbd和mysql的动作,但是mysql本身异常停止,并不能切换drbd和mysql,会导致无法提供服务。

对于mysqld服务挂掉的情况无法实现自动切换,所以需要一个脚本来帮助我们完成,我这里有个简单的脚本,能实现当mysqld服务不可用时进行自动切换,当进行切换时发送邮件等。该脚本放在主服务器执行,也就是运行mysqld服务的服务器上执行。

安装mutt email程序
# yum install -y mutt
后台监控脚本
# cat mysqlmon.sh

#!/bin/bash
trap 'echo  PROGRAM INTERRUPTED; exit 1'  INT
username=root
password=123456
n=0
log='/var/log/mysqlmon.log'
while true
do
    if /usr/bin/mysql -u${username} -p${password} -e "use mysql"   >&/dev/null
    then
        echo `date +"%Y-%m-%d  %H:%M:%S"`  mysqld is alive!  >> ${log}
        n=0
    else
        echo  "`date +"%Y-%m-%d  %H:%M:%S"`  mysqld  cannot be  connected!"  >> ${log}
        n=$[n + 1]
        if [ $n -eq 3 ]
        then
            /etc/init.d/heartbeat stop
            echo  "`date +"%Y-%m-%d  %H:%M:%S"`  mysqld switched to backup!" >> ${log}
            echo "`date +"%Y-%m-%d  %H:%M:%S"`  mysqld switched to backup" | mutt -s "mysqld switched to backup" [email protected]
            break
        fi
    fi
    sleep 10
done


后台执行:
# chmod 755 mysqlmon.sh
# nohup ./mysqlmon.sh &

停止mysqld服务,看是否进行切换以及发送邮件:
# /etc/init.d/mysqld stop
Shutting down MySQL.                                       [  OK  ]

查看下邮件是否收到,这个脚本只在主mysql运行。

参考:http://www.cnblogs.com/gomysql/p/3674030.html

你可能感兴趣的:(系统运维)