摘要:本文主要向大家介绍了MySQL数据库之Heartbeat+Drbd+Mysql主从高可用实现 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。
本文主要向大家介绍了MySQL数据库之Heartbeat+Drbd+Mysql主从高可用实现 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。
在上一篇中已经实现了MySQL服务的高可用,MySQL的数据目录放在drbd的共享目录中,并且只有获取到heartbeat资源的VIP才能挂载共享目录,从而启动MySQL服务,但是两端的数据使用drbd同步,保证发生故障时,服务和资源能够从一个节点切换到另外一个节点,下面是一个简略的架构图:
对于MySQL服务,一般在生产环境中都要做主从结构,从而保证数据的完整性,所以这次要在这个架构的前提下,在两个heartbeat节点下再部署一台MySQL从库,而主库是heartbeat集群中的一台(主库的IP设置为VIP地址),从而实现发生故障时,从库可以自动切换到另一台主库下面,主从同步的状态保持稳定。
当heartbeat01获取VIP时,MySQL主库跑在heartbeat01上,MySQL从库从heartbeat01同步数据,heartbeat02相当于备份服务器,通过drbd的块设备同步heartbeat01上drbd目录的内容。
当heartbeat02获取VIP时,MySQL主库跑在heartbeat02上,MySQL从库从heartbeat02同步数据,heartbeat01相当于备份服务器,通过drbd的块设备同步heartbeat02上drbd目录的内容。
注意:本文中出现的节点1/节点2都是指heartbeat的节点,而Master节点/Slave节点都是指MySQL节点。
一、环境准备
这里需要添加一台主机,主要信息如下:
主机名 角色 IP地址
server136.contoso.com MySQL从库(Slave) 192.168.49.136
以下都在上一篇的基础上完成《Heartbeat+Drbd+MySQL高可用》
1)两个节点上依次启动heartbeat服务:
[root@heartbeat01 ~]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
Done.
[root@heartbeat02 ~]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
Done.
2)检查两个节点的状态
节点1heartbeat01:
[root@heartbeat01 ~]# ip a |grep 49.100
inet 192.168.49.100/24 scope global eth1
[root@heartbeat01 ~]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:216 nr:0 dw:216 dr:3929 al:4 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@heartbeat01 ~]# ll /data
total 16
drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 lib
drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:21 lock
drwxr-xr-x 2 mysql mysql 4096 Sep 27 01:18 log
drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 run
[root@heartbeat01 ~]# /etc/init.d/mysqld status
mysqld (pid 2414) is running...
[root@heartbeat01 ~]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 2414 mysql 18u IPv4 12859 0t0 TCP *:mysql (LISTEN)
节点2 heartbeat02:
[root@heartbeat02 ~]# ip a |grep 49.100
[root@heartbeat02 ~]# cat /proc/drbd
version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37
0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:0 nr:784 dw:784 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@heartbeat02 ~]# ll /data
total 0
[root@heartbeat02 ~]# /etc/init.d/mysqld status
mysqld is stopped
二、MySQL主从复制
1、配置heartbeat01
因为节点1(heartbeat01)目前正在运行数据库,所以需要先在heartbeat01上进行配置,当然如果MySQL和heartbeat的资源正在运行在heartbeat02上,也可以先对heartbeat02进行配置,配置的目的是保证两个节点上MySQL Master节点的配置完成,且能正常运行。
1)配置/etc/my.cnf,添加Master配置
[root@heartbeat01 ~]# cp /etc/my.cnf /etc/my.cnf.bak$(date +%F)
[root@heartbeat01 ~]# vi /etc/my.cnf
[root@heartbeat01 ~]# diff /etc/my.cnf.bak2016-09-27 /etc/my.cnf
6a7,10
> ######################################
> #add master settings
> log-bin=mysql-bin
> server-id=1
# 主从配置就不多讲了,网上的教程有很多,这里也只是开启了binlog并添加serverID,没有做详细的设置。
2)导出Master节点的数据
[root@heartbeat01 ~]# mysqldump -uroot -p123456 --database tb01 > /root/tb01.sql
# 因为之前我创建了一个tb01的数据库,所以这里先将该数据库备份
[root@heartbeat01 ~]# ll tb01.sql
-rw-r--r-- 1 root root 2008 Sep 27 22:14 tb01.sql
[root@heartbeat01 ~]# scp tb01.sql 192.168.49.136:/tmp/
root@192.168.49.136's password:
tb01.sql 100% 2008 2.0KB/s 00:00
# 然后通过scp拷贝到MySQL从库(server136)上
3)创建备份账号
[root@heartbeat01 ~]# mysql -uroot -p123456
# 登录MySQL
mysql> grant replication slave on *.* to 'replication'@'192.168.49.136' identified by 'rep@123';
Query OK, 0 rows affected (0.00 sec)
# 创建主从同步账号并授权
mysql> select Host,User,Password from mysql.user;
+-------------------------+-------------+-------------------------------------------+
| Host | User | Password |
+-------------------------+-------------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| heartbeat01.contoso.com | root | |
| 127.0.0.1 | root | |
| localhost | | |
| heartbeat01.contoso.com | | |
| 192.168.49.136 | replication | *6E0E8E398CC10050A0AF2E9B4B27FBF3181D220A |
+-------------------------+-------------+-------------------------------------------+
6 rows in set (0.00 sec)
# 检查一下创建的结果
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
4)重启MySQL,查询Master状态
[root@heartbeat01 ~]# /etc/init.d/mysqld restart
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 344 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> quit
Bye
2、配置heartbeat02
因为heartbeat02暂时没有获取VIP,也未运行MySQL服务,所以只需要修改/etc/my.cnf文件,将Master的配置加入进去即可,这里采用scp的方式从节点1上复制过来。
[root@heartbeat02 ~]# mv /etc/my.cnf /etc/my.cnf.bak$(date +%F)
[root@heartbeat02 ~]# scp heartbeat01:/etc/my.cnf /etc/
Warning: Permanently added the RSA host key for IP address '172.16.49.133' to the list of known hosts.
root@heartbeat01's password:
my.cnf 100% 345 0.3KB/s 00:00
[root@heartbeat02 ~]# diff /etc/my.cnf.bak2016-09-27 /etc/my.cnf
6a7,10
> ######################################
> #add master settings
> log-bin=mysql-bin
> server-id=1
3、配置Slave节点(server136)
1)安装MySQL
[root@server136 ~]# yum -y install mysql-devel mysql-server
[root@server136 ~]# /etc/init.d/mysqld start
[root@server136 ~]# mysqladmin -uroot password '123456'
2)修改配置文件
[root@server136 ~]# cp /etc/my.cnf /etc/my.cnf.bak$(date +%F)
[root@server136 ~]# vi /etc/my.cnf
[root@server136 ~]# diff /etc/my.cnf.bak2016-09-28 /etc/my.cnf
6a7,9
> ####################################
> #add slave settings
> server-id=2
# 只需要在/etc/my.cnf中添加一行server-id=值
3)导入Master上导出的数据
[root@server136 ~]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@server136 ~]# mysql -uroot -p123456 < /tmp/tb01.sql
下面进入MySQL数据库查看一下导入的数据:
[root@server136 ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select * from tb01.staff;
+----+-------+
| id | name |
+----+-------+
| 1 | Tom |
| 2 | Jerry |
+----+-------+
2 rows in set (0.01 sec)
mysql> quit
Bye
4)启动slave节点
mysql> change master to
-> master_host = '192.168.49.100',
-> master_user = 'replication',
-> master_password = 'rep@123',
-> master_log_file = 'mysql-bin.000001',
-> master_log_pos = 344;
Query OK, 0 rows affected (0.10 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.49.100
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 344
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes #这两项为Yes说明主从复制成功
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 344
Relay_Log_Space: 407
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
5)主从复制测试
现在在Master上创建一张表,并插入数据:
[root@heartbeat01 ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.73-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current in
本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号