1. 程式人生 > >0029-如何實現CDH元資料庫MySQL的主備

0029-如何實現CDH元資料庫MySQL的主備

溫馨提示:要看高清無碼套圖,請使用手機開啟並單擊圖片放大檢視。

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檔案,增加如下配置

[[email protected] 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

0029-如何實現CDH元資料庫MySQL的主備

  • Slave服務配置(172.31.5.190)

修改/etc/my.conf檔案,增加如下配置

 [[email protected] ~]# 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

0029-如何實現CDH元資料庫MySQL的主備

  • 修改配置後重啟Master和Slave的MySQL服務。
[[email protected] ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[[email protected] ~]# 

0029-如何實現CDH元資料庫MySQL的主備

2.1構建主從複製

  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。

命令列操作

[[email protected] ~]# 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)

0029-如何實現CDH元資料庫MySQL的主備

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> 

0029-如何實現CDH元資料庫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;

命令列執行

[[email protected] ~]# 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> 

0029-如何實現CDH元資料庫MySQL的主備

4.在Slave從MySQL上執行命令,啟動同步

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> 

0029-如何實現CDH元資料庫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> 

0029-如何實現CDH元資料庫MySQL的主備

注意:上圖標註部分顯示為“Yes”則表示主從複製構建成功。

2.3主從複製驗證


  1. 登入Master主MySQL上執行SQL

0029-如何實現CDH元資料庫MySQL的主備

2.登入Slave從MySQL上執行SQL

0029-如何實現CDH元資料庫MySQL的主備

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> 

0029-如何實現CDH元資料庫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> 

0029-如何實現CDH元資料庫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> 

0029-如何實現CDH元資料庫MySQL的主備

注意:執行上述操作後,需要重啟MySQL服務。

  • MySQL配置引數說明

參考文件:http://blog.csdn.net/wlzx120/article/details/52301383

推薦關注Hadoop實操,第一時間,分享更多Hadoop乾貨,歡迎轉發和分享。

0029-如何實現CDH元資料庫MySQL的主備
原創文章,歡迎轉載,轉載請註明:轉載自微信公眾號Hadoop實操