MySQL + MHA + keepalive + VIP.docx
- 文档编号:10404435
- 上传时间:2023-02-11
- 格式:DOCX
- 页数:14
- 大小:18.84KB
MySQL + MHA + keepalive + VIP.docx
《MySQL + MHA + keepalive + VIP.docx》由会员分享,可在线阅读,更多相关《MySQL + MHA + keepalive + VIP.docx(14页珍藏版)》请在冰豆网上搜索。
MySQL+MHA+keepalive+VIP
一、总概:
本文介绍了MySQL高可用性的实现方案MHA,MHA由Node和Manager组成,Node运行在每一台MySQL服务器上,不管是MySQL主服务器,还是MySQL从服务器,都要安装Node。
主从安装keepalived,实现虚拟ip漂移,程序不用改IP自动切换。
二、环境
1.软件版本
操作系统版本:
CentOSrelease6.4
MySQL数据库版本:
5.6.14
MHA版本:
mha4mysql-node-0.54-0.el6.noarch.rpm、mha4mysql-manager-0.55-0.el6.noarch.rpm
keepalived版本:
1.2.7
2.主机部署
MHA管理机、客户端模拟:
192.168.1.100
master:
192.168.1.1
slave:
192.168.1.2
三、实验步骤
.安装MySQL
.MySQL配置主重复制
.建立ssh无密码登录环境
.安装MHAmha4mysql-node,mha4mysql-manager软件包
.管理机manager上配置MHA文件
.masterha_check_ssh工具验证ssh信任登录是否成功
.masterha_check_repl工具验证mysql复制是否成功
.启动MHAmanager,并监控日志文件
.测试master宕机后,是否会自动切换
.测试使用MHA的master_ip_failover,实现故障转移
.主从安装keepalived,实现虚拟ip漂移
1.安装MySQL
master的f
more/etc/f
[plain] viewplaincopy
1.[mysqld]
2.basedir = /usr/local/mysql
3.datadir = /usr/local/mysql/data
4.sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_UNSIGNED_SUBTRACTION
5.port = 6603
6.server_id = 1
7.lower_case_table_names = 1
8.character_set_server = utf8
9.autocommit = off
10.innodb_flush_log_at_trx_commit = 1
11.skip_name_resolve = ON
12.innodb_strict_mode = ON
13.log_bin = mysql-bin
14.innodb_data_home_dir = /usr/local/mysql/data
15.innodb_log_group_home_dir = /usr/local/mysql/data
16.binlog_format=mixed
slave的f
more/etc/f
[plain] viewplaincopy
1.[mysqld]
2.basedir = /usr/local/mysql
3.datadir = /usr/local/mysql/data
4.sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_UNSIGNED_SUBTRACTION
5.port = 6603
6.server_id = 2
7.lower_case_table_names = 1
8.character_set_server = utf8
9.autocommit = off
10.innodb_flush_log_at_trx_commit = 1
11.skip_name_resolve = ON
12.innodb_strict_mode = ON
13.log_bin = mysql-bin
14.innodb_data_home_dir = /usr/local/mysql/data
15.innodb_log_group_home_dir = /usr/local/mysql/data
16.binlog_format=mixed
17.read_only = ON
MySQL安装步骤略
2.MySQL配置主重复制(略)
3.建立ssh无密码登录环境
在192.168.1.100、192.168.1.1、192.168.1.2上修改hostname:
vi/etc/hosts
[plain] viewplaincopy
1.192.168.1.100 manage
2.192.168.1.1 master
3.192.168.1.2 slave
[plain] viewplaincopy
1.# 在192.168.1.100:
2.ssh-keygen -t rsa
3.ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.1
4.ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.2
5.
6.# 在192.168.1.1:
7.ssh-keygen -t rsa
8.ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.100
9.ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.2
10.
11.# 在192.168.1.2:
12.ssh-keygen -t rsa
13.ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.100
14.ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.1
测试:
[plain] viewplaincopy
1.# 在192.168.1.100:
2.ssh 192.168.1.1
3.ssh 192.168.1.2
4.
5.# 在192.168.1.1:
6.ssh 192.168.1.100
7.ssh 192.168.1.2
8.
9.# 在192.168.1.2:
10.ssh 192.168.1.100
11.ssh 192.168.1.1
4.安装MHAmha4mysql-node,mha4mysql-manager软件包
[plain] viewplaincopy
1.# 在192.168.1.100:
2.rpm -ivh rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm
3.yum install perl-DBD-MySQL
4.yum install perl-Config-Tiny
5.yum install perl-Log-Dispatch
6.yum install perl-Parallel-ForkManager
7.rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
8.rpm -ivh mha4mysql-manager-0.55-0.el6.noarch.rpm
9.
10.# 在192.168.1.1:
11.rpm -ivh rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm
12.yum install perl-DBD-MySQL
13.yum install perl-Config-Tiny
14.yum install perl-Log-Dispatch
15.yum install perl-Parallel-ForkManager
16.rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
17.
18.# 在192.168.1.2:
19.rpm -ivh rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm
20.yum install perl-DBD-MySQL
21.yum install perl-Config-Tiny
22.yum install perl-Log-Dispatch
23.yum install perl-Parallel-ForkManager
24.rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
5.管理机manager上配置MHA文件
在192.168.1.100:
mkdir-p/masterha/app1
mkdir/etc/masterha
vi/etc/masterha/f
[plain] viewplaincopy
1.[server default]
2.user=root
3.password=12345
4.manager_workdir=/masterha/app1
5.manager_log=/masterha/app1/manager.log
6.remote_workdir=/masterha/app1
7.ssh_user=root
8.repl_user=repl
9.repl_password=12345
10.ping_interval=1
11.[server1]
12.hostname=192.168.1.1
13.port=6603
14.master_binlog_dir=/usr/local/mysql/data
15.candidate_master=1
16.[server2]
17.hostname=192.168.1.2
18.port=6603
19.master_binlog_dir=/usr/local/mysql/data
20.candidate_master=1
6.masterha_check_ssh工具验证ssh信任登录是否成功
在192.168.1.100:
masterha_check_ssh--conf=/etc/masterha/f
7.masterha_check_repl工具验证mysql复制是否成功
在192.168.1.100:
masterha_check_repl--conf=/etc/masterha/f
8.启动MHAmanager,并监控日志文件
在192.168.1.100:
nohupmasterha_manager--conf=/etc/masterha/f>/tmp/mha_manager.log2>&1
#单开一个终端监控日志
tail-ftail-f/masterha/app1/manager.log
9.测试master宕机后,是否会自动切换
在192.168.1.1:
servicemysqlstop
在192.168.1.2:
mysql>showslavestatus\G
mysql>showvariableslike'read_only';
10.测试使用MHA的master_ip_failover,实现故障转移
恢复主从配置(略)
在192.168.1.100:
在文件/etc/masterha/f的[serverdefault]段添加下面一行
master_ip_failover_script=/tmp/master_ip_failover
然后在/tmp/master_ip_failover写入如下代码:
[php] viewplaincopy
1.#!
/usr/bin/env php
2.
php
3.$longopts = array(
4. 'command:
',
5. 'ssh_user:
',
6. 'orig_master_host:
',
7. 'orig_master_ip:
',
8. 'orig_master_port:
',
9. 'new_master_host:
:
',
10. 'new_master_ip:
:
',
11. 'new_master_port:
:
', );
12.$options = getopt(null, $longopts);
13.if ($options['command'] == 'start') {
14. $params = array(
15. 'ip' => $options['new_master_ip'],
16. 'port' => $options['new_master_port'],
17. );
18. $string = '
php return ' . var_export($params, true) . '; ?
>';
19. file_put_contents('config.php', $string, LOCK_EX); }
20.exit(0);
21.?
>
赋给这个文件可执行权限:
chmod+x/tmp/master_ip_failover
安装PHP:
yuminstallphp
运行MHA进程:
nohupmasterha_manager--conf=/etc/masterha/f>/tmp/mha_manager.log2>&1
#单开一个终端监控日志
tail-ftail-f/masterha/app1/manager.log
测试切换:
停止master上的mysql
在192.168.1.1:
servicemysqlstop
在192.168.1.2:
mysql>showslavestatus\G
mysql>showvariableslike'read_only';
11.主从安装keepalived,实现虚拟ip漂移
恢复主从配置(略)
vip:
192.168.1.3/192.168.1.4
master:
192.168.1.1
slave:
192.168.1.2
在主从两个机器上安装keepalived(略)
配置keepalived
在192.168.1.1:
vim/etc/keepalived/keepalived.conf
[plain] viewplaincopy
1.global_defs {
2. router_id MySQL-ha
3.}
4.
5.vrrp_instance VI_1 {
6. state BACKUP
7. interface eth1
8. virtual_router_id 90
9. priority 100
10. advert_int 1
11. nopreempt
12.
13. authentication {
14. auth_type PASS
15. auth_pass 1111
16. }
17.virtual_ipaddress {
18. 192.168.1.3
19. 192.168.1.4
20. }
21.}
22.
23.virtual_server 192.168.1.3 6603 {
24. delay_loop 6
25. lb_algo wrr
26. lb_kind DR
27. nat_mask 255.255.255.0
28. persistence_timeout 50
29. protocol TCP
30. real_server 192.168.1.1 6603 {
31. weight 1
32. notify_down /usr/local/mysql/bin/mysql.sh
33. TCP_CHECK {
34. connect_timeout 10
35. nb_get_retry 3
36. connect_port 6603
37. }
38. }
39.}
vim/usr/local/mysql/bin/mysql.sh
[sql] viewplaincopy
1.#!
/bin/bash
2.pkill keepalived
chmod+x/usr/local/mysql/bin/mysql.sh
#先启动mysql,再启动keepalived
servicemysqlstart
servicekeepalivedstart
在192.168.1.2:
vim/etc/keepalived/keepalived.conf
[plain] viewplaincopy
1.global_defs {
2. router_id MySQL-ha
3.}
4.
5.vrrp_instance VI_1 {
6. state BACKUP
7. interface eth1
8. virtual_router_id 90
9. priority 80
10. advert_int 1
11. authentication {
12. auth_type PASS
13. auth_pass 1111
14. }
15.virtual_ipaddress {
16. 192.168.1.3
17. 192.168.1.4
18. }
19.}
20.
21.virtual_server 192.168.1.3 6603 {
22. delay_loop 6
23. lb_algo wrr
24. lb_kind DR
25. nat_mask 255.255.255.0
26. persistence_timeout 50
27. protocol TCP
28. real_server 192.168.1.2 6603 {
29. weight 1
30. notify_down /usr/local/mysql/bin/mysql.sh
31. TCP_CHECK {
32. connect_timeout 10
33. nb_get_retry 3
34. connect_port 6603
35. }
36. }
37.}
vim/usr/local/mysql/bin/mysql.sh
[plain] viewplaincopy
1.#!
/bin/bash
2.pkill keepalived
chmod+x/usr/local/mysql/bin/mysql.sh
#先启动mysql,再启动keepalived
servicemysqlstart
servicekeepalivedstart
在192.168.1.100:
运行MHA进程:
nohupmasterha_manager--conf=/etc/masterha/f>/tmp/mha_manager.log2>&1
#单开一个终端监控日志
tail-ftail-f/masterha/app1/manager.log
测试:
在192.168.1.1:
ipa
在192.168.1.2:
ipa
mysql>showslavestatus\G
在192.168.1.100:
mysql-uu1-p12345-P6603-h192.168.1.3-e"showvariableslike'server_id'"
mysql-uu1-p12345-P6603-h192.168.1.3-e"showvariableslike'read_only'"
停master的mysql,再查看
在192.168.1.1:
servicemysqlstop
ipa
在192.168.1.2:
ipa
mysql>showslavestatus\G
在192.168.1.100:
mysql-uu1-p12345-P6603-h192.168.1.3-e"showvariableslike'server_id'"
mysql-uu1-p12345-P6603-h192.168.1.3-e"showvariableslike'read_only'"
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- MySQL MHA keepalive VIP