0029-如何实现CDH元数据库MySQL的主备
时间: 2018-11-20来源:OSCHINA
前景提要
【围观】麒麟芯片遭打压成绝版,华为亿元投入又砸向了哪里?>>>
1.文档编写目的
MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的扩展。多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能。本文档讲述如何实现MySQL主从复制。注:本文档实现的MySQL主备模式为Active-Passive而不是Active-Active,如果使用双活的方式,建议企业内部配备MySQL的DBA来维护MySQL。CDH集群在运行过程中,MySQL的负载并不会太高,推荐的方式是Active-Passive模式,以降低维护成本和维护难度。 内容概述
1.Master和Slave配置
2.构建主从复制
3.主从复制验证 测试环境
1.两台Linux服务器(172.31.10.118(主)/172.31.5.190),操作系统为CentOS6.5
2.MySQL5.1.73
3.采用root用户操作 前置条件
1.两个MySQL版本必须一致
2.两个MySQL已安装好,且没有任何数据
3.主MySQL必须开启bin-log日志
2.MySQL主从复制
2.1Master和Slave配置
配置文件说明:
log-bin:开启二进制日志,日志文件前缀
server-id:数据库服务的唯一标识确保标识不重复,一般设置为服务器ip的末尾数
binlog-format:设置Mysql binlog记录日志的格式(格式含:Statement、MIXED、ROW),MySQL默认使用的是Statement,推荐使用MIXED。 Master主服务配置(172.31.10.118)
修改/etc/my.conf文件,增加如下配置 [root@ip-172-31-10-118 cloudera-scm-server]# vim /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-bin=mysql-bin server-id=118 binlog_format=MIXED [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
Slave服务配置(172.31.5.190)
修改/etc/my.conf文件,增加如下配置 [root@ip-172-31-5-190 ~]# vim /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-bin=mysql-bin server-id=190 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
修改配置后重启Master和Slave的MySQL服务。 [root@ip-172-31-5-190 ~]# service mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ] [root@ip-172-31-5-190 ~]#

2.1构建主从复制 在Master(172.31.10.118) 主MySQL上创建一个mysnc用户
用户名:mysync 密码:mysync GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'172.31.%' IDENTIFIED BY 'mysync'; FLUSH PRIVILEGES;
mysync用户必须具有REPLICATION SLAVE权限。说明一下172.31.%,这个配置是指明mysync用户所在服务器,这里%是通配符,表示IP以172.31开头的Server都可以使用mysync用户登陆Master主服务器。也可以指定固定IP。
命令行操作 [root@ip-172-31-10-118 ~]# mysql -uroot -p Enter password: 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 input statement. mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'172.31.%' IDENTIFIED BY 'mysync'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)

2.查看Master(172.31.10.118) MySQL二进制日志File与Position mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 1121 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql>

3.在Slave从MySQL上执行如下SQL change master to master_host='172.31.10.118', master_user='mysync', master_password='mysync', master_log_file='mysql-bin.000003', master_log_pos=1121;
命令行执行 [root@ip-172-31-5-190 ~]# mysql -uroot -p ... Server version: 5.1.73-log Source distribution ... mysql> change master to -> master_host='172.31.10.118', -> master_user='mysync', -> master_password='mysync', -> master_log_file='mysql-bin.000003', -> master_log_pos=1121; Query OK, 0 rows affected (0.02 sec) mysql>

4.在Slave从MySQL上执行命令,启动同步 mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql>

5.在Slave MySQL上查看Slave状态 mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.31.10.118 Master_User: mysync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 1121 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000003 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: 1121 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) mysql>

注意:上图标注部分显示为“Yes”则表示主从复制构建成功。
2.3主从复制验证 登录Master主MySQL上执行SQL

2.登录Slave从MySQL上执行SQL

3.在Master主MySQL上执行SQL mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> use test; Database changed mysql> create table table1(id int, name varchar(32)); Query OK, 0 rows affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | table1 | +----------------+ 1 row in set (0.00 sec) mysql>

4.在Slave从MySQL上执行SQL查看 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | table1 | +----------------+ 1 row in set (0.00 sec) mysql>

通过上述测试,Master主MySQL创建的库和表都正常的同步到Slave从MySQL。
3.备注 如何停止并删除主从同步,在Slave从MySQL上执行如下SQL mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> reset slave; Query OK, 0 rows affected (0.00 sec) mysql>

注意:执行上述操作后,需要重启MySQL服务。 MySQL配置参数说明
参考文档: http://blog.csdn.net/wlzx120/article/details/52301383
推荐关注Hadoop实操,第一时间,分享更多Hadoop干货,欢迎转发和分享。

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行