Mysql 数据库集群DRBD
<Alvin-zeng:孤独0-1>
目录
二、Mysql安装2
1、安装MYSQL和innob引擎2
3、建立Mysql用户及目录3
4、初始化数据库,注意请mysql 用户执行3
5、建立 My.cnf 配置文件3
6、启动mysql 服务5
二、DRBD安装6
1、编译DRBD-主备操作6
2、配置DRBD文件-主备操作6
3、创建DRBD逻辑设备-主备操作7
4、格式化DRBD逻辑设备-主操作7
5、主备切换-主操作7
6、主备切换-从操作7
7、DRBD常用故障处理7
三、Heatbeat安装8
1、编译glue,主备操作8
2、编译Cluster agent主备操作8
3、编译Heartbeat 主备操作9
4、配置心跳文件,主操作9
5、编写MYSQL启动脚本-主操作9
6、拷贝一些常用脚本-主操作11
7、将主的配置文件拷贝到从来,备操作11
官方下载
http://www.innodb.com/download/innodb_plugin/innodb_plugin-1.0.6.tar.gz
#:tar �Cxvf /mnt/mysql-5.1.51.tar.gz�CC /opt/
#: tar �Cxvf /mnt//innodb_plugin-1.0.6.tar.gz�CC /opt/mysql-5.1.51/storage
#: cd /opt/mysql-5.1.51/storage
#:rm �Crf innobase
#: mv innodb_plugin-1.0.6innobase
#:cd /opt/mysql-5.1.51/
/usr/local/webserver/mysql 路径必须跟这个一样,否则启动时候,脚本会出错,
./configure --prefix=/usr/local/webserver/mysql
--enable-local-infile
--with-charset=gbk
--with-extra-charsets=complex
--with-server-suffix=max
--with-libwrap=/usr
--with-low-memory
--with-mysqlmanager
--with-big-tables
--with-ndb-test
--with-ndb-docs
--with-debug
--with-plugins=partition,innobase,myisammrg
--enable-assembler
--with-client-ldflags=-all-static
--with-mysqld-ldflags=-all-static
--with-embedded-server
--with-ssl
--with-readline
--enable-thread-safe-client
./configure --prefix=/usr/local/webserver/mysql --enable-local-infile --with-charset=gbk --with-extra-charsets=complex --with-server-suffix=max --with-libwrap=/usr --with-low-memory --with-mysqlmanager --with-big-tables --with-ndb-test --with-ndb-docs --with-debug --with-plugins=partition,innobase,myisammrg --enable-assembler --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --with-embedded-server --with-ssl --with-readline --enable-thread-safe-client
Make && make install
#:groupadd mysql
#:useradd �Cg mysql mysql
#:mkdir /opt/data && chown -R mysql.mysql /opt/data
#:mkdir /var/run/mysqld && chown -R mysql:mysql /var/run/mysqld
#:mkdir /var/log/mysqld && chown -R mysql:mysql /var/log/mysqld
#:cp /usr/local/webserver/mysql/share/mysql/mysql.server /etc/rc.d/init.d/mysqld
#:chmod 755 /etc/rc.d/init.d/mysqld
#:cp /usr/local/webserver/mysql/bin/mysql /usr/bin/
#: su mysql
$:/usr/local/webserver/mysql/bin/mysql_install_db--basedir=/usr/local/webserver/mysql --datadir=/opt/data --user=mysql
$:exit
#:vi /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/webserver/mysql
datadir = /opt/data
log-error = /var/log/mysqld/mysql_error.log
pid-file = /var/run/mysqld/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
#thread_concurrency = 8
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
log-bin = /var/log/mysqld/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /var/log/mysqld/relaylog
relay-log-info-file = /var/log/mysqld/relaylog
relay-log = /var/log/mysqld/relaylog
expire_logs_days = 30
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
#master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
#master-host = 192.168.1.2
#master-user = username
#master-password = password
#master-port = 3306
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
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 = 32M
# /etc/init.d/mysql5 start
Starting MySQL..... [ OK ]
创建一个具有root权限的用户(admin)和密码(12345678):、
#:mysql
Mysql-> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678';
Mysql-> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678';
下载http://oss.linbit.com/drbd/drbd-8.3.8.1.tar.gz
#: tar -xvf drbd-8.3.8.1.tar.gz -C /opt/
#: cd drbd-8.3.8.1
#:./configure --prefix=/ --with-km
#: make && make install
#: insmod drbd/drbd.ko
#: modprobe drbd 这两个命令,任选一个,放到/etc/rc.d/rc.local
#:cat /proc/drbd
#: vi /etc/drbd.d/dbdata.res
resource dbdata {
on SQL-1 { #主机名
device /dev/drbd1; #:drbd逻辑设备块
disk /dev/hdb1; #:物理设备块
address 192.168.1.236:7789; #:IP地址与端口
meta-disk internal;
}
on SQL-2 { #主机名
device /dev/drbd1; #:drbd逻辑设备块
disk /dev/hdb1; #:物理设备块
address 192.168.1.237:7789; #:IP地址与端口
meta-disk internal;
}
}
#: drbdadm create-md all
#: /etc/init.d/drbd restart
#: ls �Cl /dev/drbd1
#: cat /proc/drbd
#:drbdadm -- --overwrite-data-of-peer primary all
#:mkfs.ext3 /dev/drbd1
#:mount /dev/drbd1 /opt/data
#:cd /opt/data
#: touch 1.txt
#:umount /opt/data
#:drbdadm secondary all 设置为从服务器
#:mount /dev/drbd1 /opt/data
#:drbdadm primary all 设置为主服务器
#: cd /opt/data && ls
节点连接:
#:drbdadm connect|disconnect all
DRBD脑裂后的处理,
两个节点间数据不同步,主从关系失效,需要按下面的步骤修复
A:在从节点如下操作:
#drbdadm secondary dbdata
#drbdadm -- --discard-my-data connect dbdata
B: 在主节点上如下操作
通过cat /proc/drbd查看状态,如果不是WFConnection状态,需要再手动连接:
#drbdadm connect dbdata
官方网站下载
http://linux-ha.org/w/index.php?title=Download&setlang=zh
#:tar �Cxvf Reusable-Cluster-Components-glue-1.0.6.tar.bz2�CC /opt/
#:cd /opt/ Reusable-Cluster-Components-glue-1.0.6
#: ./autogen.sh
打开lib/stonith/main.c,将version相关的注释掉,否则编译不过,有点诡异。
1、找到其64行,将其注释掉。
2、找到其76到81行全部注释掉。
3、找到其390行,将其注释
#: ./configure
#: groupadd haclient
#: useradd -g haclient hacluster
#: make && make install
官方网站下载
http://linux-ha.org/w/index.php?title=Download&setlang=zh
#:tar �Cxvf Cluster-Resource-Agents-agents-1.0.3.tar.bz2�CC /opt/
#:cd Cluster-Resource-Agents-agents-1.0.3
#./autogen.sh
#./configure
#make && make install
官方网站下载
http://linux-ha.org/w/index.php?title=Download&setlang=zh
#./bootstrap
#./ConfigureMe configure
#make && make install
# vi /etc/ha.d/ha.cf #:此文件默认是没有的,现在新编辑一个
logfacility local0
keepalive 500ms
deadtime 10
warntime 5
initdead 60
mcast eth0 225.0.0.1 694 1 0
auto_failback off
node SQL-1
node SQL-2
#:vi /etc/ha.d/haresources #:此文件默认是没有的,现在新编辑一个
SQL-1 drbddisk Filesystem::/dev/drbd1::/data/dbdata::ext3 IPaddr::192.168.1.238/24/eth0 mysql
生成/etc/ha.d/authkeys ,:此文件默认是没有的,现在新生成一个
#( echo -ne "auth 1\n1 sha1 "; \
dd if=/dev/urandom bs=512 count=1 | openssl md5 ) \
> /etc/ha.d/authkeys
#:chmod 600 /etc/ha.d/authkeys
#:vi /etc/ha.d/resource.d/mysql
#!/bin/bash
#Totle:mysql
#Description:start mysql service
#system:Use Linux
#Company:Fashion's friend
#Author:ZengYong
#Version:1.0
#DateTime:2010-10-10
########################################
. /etc/ha.d/shellfuncs
#======================================
#:Function-->Mysql_Start
#=======================================
Mysql_Start()
{
case "$1" in
start)
SQLDIR=`/etc/init.d/mysqld start`
RET=$?
ha_log $SQLDIR
exit $RET
;;
stop)
SQLDIR=`/etc/init.d/mysqld stop`
RET=$?
ha_log $SQLDIR
exit $RET
;;
status)
if [[ `ps -ef | grep '[m]ysqld'` > 1 ]] ; then
tput setf 2
echo "running"
tput sgr0
else
tput setf 4
echo "stopped"
tput sgr0
fi
;;
*)
echo "Usage: mysql {start|stop|status}"
return 1
;;
esac
}
#======================================
#:Function-->Main
#=======================================
Main()
{
Mysql_Start $1
if [ $? -eq 1 ]
then
exit 1
fi
}
#:主函数
Main $1
#:cd /opt/Heartbeat-3-0-STABLE-3.0.3
#:cp heartbeat/lib/hb_standbyhb_takeover/usr/lib/heartbeat
#: cp /usr/etc/ha.d/shellfuncs /etc/ha.d/
#: cp -rf /etc/ha.d/* /usr/etc/ha.d/
#: scp �Cr 192.168.1.236:/etc/ha.d/ /mnt/
#:rm �Crf /etc/ha.d/
#:cp �Cr /mnt/ha.d/ /etc/
#:cp �Crf /etc/ha.d/* /usr/etc/
1. 设置为自启动:
chkconfig mysqld off
chkconfig --add heartbeat
chkconfig heartbeat on
2. 手动切换两个节点、主从增加host 解析
vi /etc/hosts 主
192.168.1.237 SQL-2
vi /etc/hosts
192.168.1.236 SQL-1
/etc/init.d/heartbeat start
/usr/lib/heartbeat/hb_takeover 将当前节点设为主节点
/usr/lib/heartbeat/hb_standby 将当前节点设为从节点