A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

一、概述

CDH集群中,有很多地方会用到关系型数据库,例如hive元数据库、cloudera manager数据库、hue/oozie配置库等,因此,其高可用性非常重要。本文主要讨论MySQL的主备同步与故障切换问题,一般可满足生产环境,在发生故障时,手动切换为备库即可。若需要热备,则可参考一些主主互备的文章,当然,会相对比较复杂,可根据自己的需求制定方案。

二、主备同步配置步骤

1.关闭相关服务

关闭掉所有会操作MySQL的服务进程。

2.主备库配置同步账号

为了主备切换方便,主备服务器都需要建立同步账号

GRANT REPLICATION SLAVE ON *.* TO 'myuser'@'%' IDENTIFIED BY 'myuser';
FLUSH PRIVILEGES;
1
2
3.导出主库全部数据

# 设为只读
mysql> flush tables with read lock;
# 命令行中导出所有数据
mysqldump -uroot -p --events --all-database > all.dump
1
2
3
4
4.导入备库完整数据

将备份文件从主库复制到备库服务器后,执行导入操作

mysql -uroot -p < all.dump
1
5.更新主从mysql配置文件/etc/my.cn并重启主备mysql进程

主库关键配置

[mysqld]
...
expire_logs_days = 15 #binlog日志过期自动清理时间
back_log = 200 #操作系统在监听队列中所能保持的连接数
binlog_cache_size = 16M #binlog缓存大小,默认32K
binlog_format = mixed #binlog日志格式
##########主从设置#######
log_bin = mysql-bin_test
server-id = 111                           
###############################
1
2
3
4
5
6
7
8
9
10
从库关键配置

[mysqld]
...
expire_logs_days = 15 #binlog日志过期自动清理时间
back_log = 200 #操作系统在监听队列中所能保持的连接数
binlog_cache_size = 16M #binlog缓存大小
binlog_format = mixed #binlog日志格式
##########主从设置#######
log_bin = mysql-bin_test    //开启二进制日志,日志文件前缀
server-id = 222            //数据库服务的唯一标识,确保标识不重复,一般设置为服务器ip的末尾数               
read_only = 1              //备库一般设置为只读模式
###############################
1
2
3
4
5
6
7
8
9
10
11
6.主库重启后重新加锁

flush tables with read lock;
1
7.查看主库状态

查看主库状态,记录以下两个值

mysql> show master status;
+----------------------+----------+--------------+------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| mysql-bin_test.000002 | 11351 |              |                  |
+----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8.配置主从同步

配置同步

mysql>
change master to
master_host=[master ip],
master_user='myuser',
master_password='myuser',
master_log_file='mysql-bin_test.000002’,    //跟主库信息一致
master_log_pos=11351;                       //跟主库信息一致
1
2
3
4
5
6
7
启动同步

mysql> start slave;
1
查看同步状态,两个Running为Yes则表示主从复制构建成功

mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: [master ip]
                  Master_User: myuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin_test.000002
          Read_Master_Log_Pos: 1580835
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 1257897
        Relay_Master_Log_File: mysql-bin_test.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: 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: 1580835
              Relay_Log_Space: 1258053
              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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
9.解锁主库写操作

mysql> unlock tables;
1
10. 验证同步是否成功

主库执行create database test; 看备库是否有正确同步

11.启动各个服务进程

查看服务是否运行正常

三、故障切换步骤

1.关闭主库进程模拟进程或服务器挂掉状态

2.dump出从库完整备份文件并同步到其他服务器以防异常

3.从库取消同步与只读

mysql>
STOP SLAVE IO_THREAD;
SHOW PROCESSLIST;       //system user 线程确保状态为:has read all relay log 或者消失
STOP SLAVE;
RESET MASTER;
RESET SLAVE;
show master status;    //输出类似以下信息
+----------------------+----------+--------------+------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| mysql-bin_test.000002 |    52605 |              |                  |
+----------------------+----------+--------------+—————————+
1
2
3
4
5
6
7
8
9
10
11
12
修改/etc/my.cnf,注释掉read_only = 1,变为可写状态
重启从库mysql,切换为主库

4.启动相关服务进程

5.原主库恢复后的操作

原主库需要配置为备库,步骤与以上第二节操作类似,这里不再赘述。
---------------------
【转载】
作者:fish的饭票
原文:https://blog.csdn.net/lxbalex/article/details/83422738


3 个回复

倒序浏览

ヾ(◍°∇°◍)ノ゙
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马