MySQL/MariaDB數據庫的冷備份和還原

     MySQL/MariaDB數據庫的冷備份和還原html

                            做者:尹正傑 node

版權聲明:原創做品,謝絕轉載!不然將追究法律責任。mysql

 

 

 

一.MySQL/MariaDB數據庫的備份和還原概述linux

1>.爲何要備份sql

  爲了預防災難恢復:硬件故障、軟件故障、天然災害、黑客攻擊、誤操做測試等數據丟失場景。

2>.備份注意要點數據庫

  能容忍最多丟失多少數據

  恢復數據須要在多長時間內完成
  須要恢復哪些數據

3>.還原要點vim

  作還原測試,用於測試備份的可用性

  還原演練(備份和備份成功是兩碼事!必定要進行還原演練!!!最好有相關的操做文檔,遇到問題你也不慌了直接按照你本身寫的文檔執行就好!)

4>.備份類型centos

徹底備份:
  整個數據集 部分備份:
  只備份數據子集,如部分庫或表
徹底備份、增量備份、差別備份   增量備份:僅備份最近一次徹底備份或增量備份(若是存在增量)以來變化的數據,備份較快,還原複雜   差別備份:僅備份最近一次徹底備份以來變化的數據,備份較慢,還原簡單
舒適提示:
  二進制日誌文件不該該與數據文件放在同一磁盤

5>.冷、溫、熱備份服務器

冷備:
  讀寫操做均不可進行 溫備:
  讀操做可執行;但寫操做不可執行 熱備:
  讀寫操做都可執行
存儲引擎支持的備份類型:
  MyISAM:支持冷備和溫備,不支持熱備   InnoDB:都支持

6>.物理和邏輯備份app

物理備份:
  直接複製數據文件進行備份,與存儲引擎有關,佔用較多的空間,速度快
邏輯備份:
  從數據庫中「導出」數據另存而進行的備份,與存儲引擎無關,佔用空間少,速度慢,可能丟失精度

7>.備份時須要考慮的因素

  溫備的持鎖多久
  備份產生的負載
  備份過程的時長
  恢復過程的時長

8>.備份什麼

  數據
  二進制日誌、InnoDB的事務日誌
  程序代碼(存儲過程、函數、觸發器、事件調度器)
  服務器的配置文件

9>.備份工具

cp, tar等複製歸檔工具:
  物理備份工具,適用全部存儲引擎;只支持冷備;徹底和部分備份
LVM的快照:
  先加鎖,作快照後解鎖,幾乎熱備;藉助文件系統工具進行備份
mysqldump:
  邏輯備份工具,適用全部存儲引擎,溫備;支持徹底或部分備份;對InnoDB存儲引擎支持熱備,結合binlog的增量備份
xtrabackup:
  由Percona提供支持對InnoDB作熱備(物理備份)的工具,支持徹底備份、增量備份
MariaDB Backup:
  從MariaDB
10.1.26開始集成,基於Percona XtraBackup 2.3.8實現
mysqlbackup:
  熱備份, MySQL Enterprise Edition組件
mysqlhotcopy:
  PERL 語言實現,幾乎冷備,僅適用於MyISAM存儲引擎,使用LOCK TABLES、FLUSH TABLES和cp或scp來快速備份數據庫

 

二.冷備份實戰案例(徹底備份) 

爲了試驗方便,本案例採用多實例來模擬數據庫的備份,關於MySQL的多實例安裝方式可參考我以前的筆記。

博主推薦閱讀:
    https://www.cnblogs.com/yinzhengjie/p/11741690.html

1>.查看兩個實例的數據庫信息

[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld start
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      80                                          :::3306                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.19-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| yinzhengjie        |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> USE yinzhengjie
Database changed
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW TABLES;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| classes               |
| coc                   |
| courses               |
| scores                |
| students              |
| teachers              |
| toc                   |
+-----------------------+
7 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> QUIT
Bye
[root@node105.yinzhengjie.org.cn ~]# 
查看3306實例的數據庫信息
[root@node105.yinzhengjie.org.cn ~]# /mysql/3307/mysqld start
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      80                                          :::3306                                                    :::*                  
LISTEN      0      80                                          :::3307                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3307/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> QUIT
Bye
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
查看3307實例數據庫信息

2>.關閉兩個實例並將主節點的數據拷貝到從節點並修改相應的權限

[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      80                                          :::3306                                                    :::*                  
LISTEN      0      80                                          :::3307                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3307/mysqld stop
Stoping MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld stop
Stoping MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
關閉3306和3307兩個數據庫實例
[root@node105.yinzhengjie.org.cn ~]# ll /mysql/3306/data/
total 176204
-rw-rw---- 1 mysql mysql    16384 Nov  5 07:18 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov  5 07:18 aria_log_control
-rw-rw---- 1 mysql mysql     2926 Nov  5 07:18 ib_buffer_pool
-rw-rw---- 1 mysql mysql 79691776 Nov  5 07:18 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:18 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Oct 26 22:17 ib_logfile1
-rw-rw---- 1 mysql mysql        0 Oct 26 23:26 multi-master.info
drwx------ 2 mysql mysql     4096 Oct 26 22:17 mysql
-rw-rw---- 1 mysql mysql    29032 Oct 26 22:17 mysql-bin.000001
-rw-rw---- 1 mysql mysql       19 Oct 26 22:17 mysql-bin.index
-rw-rw---- 1 mysql mysql        7 Oct 26 22:17 mysql-bin.state
-rw-rw---- 1 mysql mysql     3880 Nov  4 18:48 node105.log
-rw-rw---- 1 mysql mysql      948 Nov  4 17:41 node105-slow.log
drwx------ 2 mysql mysql       20 Oct 26 22:17 performance_schema
drwx------ 2 mysql mysql      272 Nov  4 22:03 yinzhengjie
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /mysql/3307/data/
total 110660
-rw-rw---- 1 mysql mysql    16384 Nov  5 07:18 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov  5 07:18 aria_log_control
-rw-rw---- 1 mysql mysql      942 Nov  5 07:18 ib_buffer_pool
-rw-rw---- 1 mysql mysql 12582912 Nov  5 07:18 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:18 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Oct 26 22:38 ib_logfile1
-rw-rw---- 1 mysql mysql        0 Oct 26 23:27 multi-master.info
drwx------ 2 mysql mysql     4096 Oct 26 22:38 mysql
-rw-rw---- 1 mysql mysql    29032 Oct 26 22:38 mysql-bin.000001
-rw-rw---- 1 mysql mysql       19 Oct 26 22:38 mysql-bin.index
-rw-rw---- 1 mysql mysql        7 Oct 26 22:38 mysql-bin.state
drwx------ 2 mysql mysql       20 Oct 26 22:38 performance_schema
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# scp -r  /mysql/3306/data/* /mysql/3307/data/
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /mysql/3307/data/
total 262028
-rw-rw---- 1 mysql mysql    16384 Nov  5 07:21 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov  5 07:21 aria_log_control
-rw-rw---- 1 mysql mysql     2926 Nov  5 07:21 ib_buffer_pool
-rw-rw---- 1 mysql mysql 79691776 Nov  5 07:21 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:21 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:21 ib_logfile1
-rw-rw---- 1 mysql mysql        0 Nov  5 07:21 multi-master.info
drwx------ 2 mysql mysql     4096 Oct 26 22:38 mysql
-rw-rw---- 1 mysql mysql    29032 Nov  5 07:21 mysql-bin.000001
-rw-rw---- 1 mysql mysql       19 Nov  5 07:21 mysql-bin.index
-rw-rw---- 1 mysql mysql        7 Nov  5 07:21 mysql-bin.state
-rw-r----- 1 root  root      3880 Nov  5 07:21 node105.log
-rw-r----- 1 root  root       948 Nov  5 07:21 node105-slow.log
drwx------ 2 mysql mysql       20 Oct 26 22:38 performance_schema
drwx------ 2 root  root       272 Nov  5 07:21 yinzhengjie
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# scp -r /mysql/3306/data/* /mysql/3307/data/  #生產環境可用使用tar壓縮再拷貝
[root@node105.yinzhengjie.org.cn ~]# ll /mysql/3306/data/
total 176204
-rw-rw---- 1 mysql mysql    16384 Nov  5 07:18 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov  5 07:18 aria_log_control
-rw-rw---- 1 mysql mysql     2926 Nov  5 07:18 ib_buffer_pool
-rw-rw---- 1 mysql mysql 79691776 Nov  5 07:18 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:18 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Oct 26 22:17 ib_logfile1
-rw-rw---- 1 mysql mysql        0 Oct 26 23:26 multi-master.info
drwx------ 2 mysql mysql     4096 Oct 26 22:17 mysql
-rw-rw---- 1 mysql mysql    29032 Oct 26 22:17 mysql-bin.000001
-rw-rw---- 1 mysql mysql       19 Oct 26 22:17 mysql-bin.index
-rw-rw---- 1 mysql mysql        7 Oct 26 22:17 mysql-bin.state
-rw-rw---- 1 mysql mysql     3880 Nov  4 18:48 node105.log
-rw-rw---- 1 mysql mysql      948 Nov  4 17:41 node105-slow.log
drwx------ 2 mysql mysql       20 Oct 26 22:17 performance_schema
drwx------ 2 mysql mysql      272 Nov  4 22:03 yinzhengjie
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /mysql/3307/data/
total 262028
-rw-rw---- 1 mysql mysql    16384 Nov  5 07:21 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov  5 07:21 aria_log_control
-rw-rw---- 1 mysql mysql     2926 Nov  5 07:21 ib_buffer_pool
-rw-rw---- 1 mysql mysql 79691776 Nov  5 07:21 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:21 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:21 ib_logfile1
-rw-rw---- 1 mysql mysql        0 Nov  5 07:21 multi-master.info
drwx------ 2 mysql mysql     4096 Oct 26 22:38 mysql
-rw-rw---- 1 mysql mysql    29032 Nov  5 07:21 mysql-bin.000001
-rw-rw---- 1 mysql mysql       19 Nov  5 07:21 mysql-bin.index
-rw-rw---- 1 mysql mysql        7 Nov  5 07:21 mysql-bin.state
-rw-r----- 1 root  root      3880 Nov  5 07:21 node105.log
-rw-r----- 1 root  root       948 Nov  5 07:21 node105-slow.log
drwx------ 2 mysql mysql       20 Oct 26 22:38 performance_schema
drwx------ 2 root  root       272 Nov  5 07:21 yinzhengjie
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# chown -R  mysql:mysql /mysql/3307/data/
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /mysql/3307/data/
total 262028
-rw-rw---- 1 mysql mysql    16384 Nov  5 07:21 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov  5 07:21 aria_log_control
-rw-rw---- 1 mysql mysql     2926 Nov  5 07:21 ib_buffer_pool
-rw-rw---- 1 mysql mysql 79691776 Nov  5 07:21 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:21 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:21 ib_logfile1
-rw-rw---- 1 mysql mysql        0 Nov  5 07:21 multi-master.info
drwx------ 2 mysql mysql     4096 Oct 26 22:38 mysql
-rw-rw---- 1 mysql mysql    29032 Nov  5 07:21 mysql-bin.000001
-rw-rw---- 1 mysql mysql       19 Nov  5 07:21 mysql-bin.index
-rw-rw---- 1 mysql mysql        7 Nov  5 07:21 mysql-bin.state
-rw-r----- 1 mysql mysql     3880 Nov  5 07:21 node105.log
-rw-r----- 1 mysql mysql      948 Nov  5 07:21 node105-slow.log
drwx------ 2 mysql mysql       20 Oct 26 22:38 performance_schema
drwx------ 2 mysql mysql      272 Nov  5 07:21 yinzhengjie
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# chown -R mysql:mysql /mysql/3307/data/

3>.啓動從節點實例驗證備份是否成功

[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3307/mysqld start
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      80                                          :::3307                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3307/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| yinzhengjie        |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> USE yinzhengjie
Database changed
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW TABLES;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| classes               |
| coc                   |
| courses               |
| scores                |
| students              |
| teachers              |
| toc                   |
+-----------------------+
7 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
詳細驗證過程請戳我

 

三.基於LVM的備份

1>.請求鎖定全部表

 

2>.記錄二進制日誌文件及事件位置

 

3>.建立快照

 

4>.釋放鎖

 

5>.掛載快照卷,執行數據備份(注意:用戶可進行讀寫操做,但該過程會下降服務器性能)

[root@node105.yinzhengjie.org.cn ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0 1000G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  999G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  947G  0 lvm  /home
sdb               8:16   0   50G  0 disk 
sr0              11:0    1 1024M  0 rom  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x0ba7f903.

Command (m for help): p

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0ba7f903

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-104857599, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +5G     
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0ba7f903

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
對新掛載磁盤進行分區
[root@node105.yinzhengjie.org.cn ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0 1000G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  999G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  947G  0 lvm  /home
sdb               8:16   0   50G  0 disk 
└─sdb1            8:17   0    5G  0 part 
sr0              11:0    1 1024M  0 rom  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# partprobe             #同步分區信息
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0 1000G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  999G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  947G  0 lvm  /home
sdb               8:16   0   50G  0 disk 
└─sdb1            8:17   0    5G  0 part 
sr0              11:0    1 1024M  0 rom  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# partprobe             #同步分區信息
[root@node105.yinzhengjie.org.cn ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0 1000G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  999G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  947G  0 lvm  /home
sdb               8:16   0   50G  0 disk 
└─sdb1            8:17   0    5G  0 part 
sr0              11:0    1 1024M  0 rom  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# pvcreate /dev/sdb1         #建立物理卷
  Physical volume "/dev/sdb1" successfully created.
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# vgcreate vg0 /dev/sdb1       #建立卷組
  Volume group "vg0" successfully created
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# lvcreate -n mysql -L 1G vg0    #建立名爲mysql的邏輯卷
  Logical volume "mysql" created.
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# lvcreate -n binlog -L 1G vg0    #建立名爲binlog的邏輯卷
  Logical volume "binlog" created.
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# lvs
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home   centos -wi-ao---- 946.99g                                                    
  root   centos -wi-ao----  50.00g                                                    
  swap   centos -wi-ao----   2.00g                                                    
  binlog vg0    -wi-a-----   1.00g                                                    
  mysql  vg0    -wi-a-----   1.00g                                                    
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0 1000G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  999G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  947G  0 lvm  /home
sdb               8:16   0   50G  0 disk 
└─sdb1            8:17   0    5G  0 part 
  ├─vg0-mysql   253:3    0    1G  0 lvm  
  └─vg0-binlog  253:4    0    1G  0 lvm  
sr0              11:0    1 1024M  0 rom  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
建立邏輯卷
[root@node105.yinzhengjie.org.cn ~]# mkfs.xfs /dev/vg0/mysql 
meta-data=/dev/vg0/mysql         isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mkfs.xfs /dev/vg0/binlog 
meta-data=/dev/vg0/binlog        isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# blkid 
/dev/sda1: UUID="8464dcda-1c53-46f4-9af1-c5e0b0321eed" TYPE="xfs" 
/dev/sda2: UUID="dvckLL-l1Y6-BQtf-VTx5-NHL7-2Gf0-n2ZzXp" TYPE="LVM2_member" 
/dev/sdb1: UUID="19dBzc-s160-ZgAl-ssGZ-7Mzf-52f3-tAb9jm" TYPE="LVM2_member" 
/dev/mapper/centos-root: UUID="9a7895fd-f750-43db-a863-052a35542278" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="d5f2e7d3-4917-431b-8679-bcedee595ff9" TYPE="swap" 
/dev/mapper/centos-home: UUID="20688187-61a7-4cae-b9a3-f215ea37a0e8" TYPE="xfs" 
/dev/mapper/vg0-mysql: UUID="34772274-02e8-4943-a2d2-9dad20aa7e8e" TYPE="xfs" 
/dev/mapper/vg0-binlog: UUID="30f93a13-98d4-49bd-896c-98f0b3b6014e" TYPE="xfs" 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mkdir -pv /mydata/{mysql,binlog} 
mkdir: created directory ‘/mydata’
mkdir: created directory ‘/mydata/mysql’
mkdir: created directory ‘/mydata/binlog’
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mount /dev/vg0/mysql /mydata/mysql/      #這是臨時掛載,生產環境應該寫入"/etc/fstab"
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mount /dev/vg0/binlog /mydata/binlog/
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# df
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  52403200 3592648  48810552   7% /
devtmpfs                   929020       0    929020   0% /dev
tmpfs                      941148       0    941148   0% /dev/shm
tmpfs                      941148    8776    932372   1% /run
tmpfs                      941148       0    941148   0% /sys/fs/cgroup
/dev/sda1                 1038336  148576    889760  15% /boot
/dev/mapper/centos-home 992508420  426364 992082056   1% /home
tmpfs                      188232       0    188232   0% /run/user/0
/dev/mapper/vg0-mysql     1038336   32992   1005344   4% /mydata/mysql
/dev/mapper/vg0-binlog    1038336   32992   1005344   4% /mydata/binlog
[root@node105.yinzhengjie.org.cn ~]# 
格式化邏輯卷並掛載到指定路徑
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf
[mysqld]
log_bin              = /data/logbin/mysql-bin
binlog_format          = STATEMENT
character-set-server      = utf8mb4
default_storage_engine    = InnoDB
port              = 3306
datadir              = /mysql/3306/data
socket              = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf      #我們也能夠規劃更詳細的目錄 
[mysqld]
log_bin              = /mydata/binlog/mysql-bin
binlog_format          = STATEMENT
character-set-server      = utf8mb4
default_storage_engine    = InnoDB
port              = 3306
datadir              = /mydata/mysql
socket              = /mydata/mysql/mysql.sock


[mysqld_safe]
log-error        = /mydata/mysql/mariadb.log
pid-file        = /mydata/mysql/mariadb.pid
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cp -a /data/logbin/*  /mydata/binlog/ 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /mydata/binlog/
total 12
-rw-rw---- 1 mysql mysql 351 Nov  5 07:14 mysql-bin.000003
-rw-rw---- 1 mysql mysql 351 Nov  5 07:18 mysql-bin.000004
-rw-rw---- 1 mysql mysql  60 Nov  5 07:14 mysql-bin.index
-rw-rw---- 1 mysql mysql   0 Nov  5 07:18 mysql-bin.state
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cp -a /mysql/3306/data/*  /mydata/mysql/
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /mydata/mysql/
total 176204
-rw-rw---- 1 mysql mysql    16384 Nov  5 07:18 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov  5 07:18 aria_log_control
-rw-rw---- 1 mysql mysql     2926 Nov  5 07:18 ib_buffer_pool
-rw-rw---- 1 mysql mysql 79691776 Nov  5 07:18 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Nov  5 07:18 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Oct 26 22:17 ib_logfile1
-rw-rw---- 1 mysql mysql        0 Oct 26 23:26 multi-master.info
drwx------ 2 mysql mysql     4096 Oct 26 22:17 mysql
-rw-rw---- 1 mysql mysql    29032 Oct 26 22:17 mysql-bin.000001
-rw-rw---- 1 mysql mysql       19 Oct 26 22:17 mysql-bin.index
-rw-rw---- 1 mysql mysql        7 Oct 26 22:17 mysql-bin.state
-rw-rw---- 1 mysql mysql     3880 Nov  4 18:48 node105.log
-rw-rw---- 1 mysql mysql      948 Nov  4 17:41 node105-slow.log
drwx------ 2 mysql mysql       20 Oct 26 22:17 performance_schema
drwx------ 2 mysql mysql      272 Nov  4 22:03 yinzhengjie
[root@node105.yinzhengjie.org.cn ~]# 
修改MySQL實例的配置文件並將數據文件和二進制文件拷貝到邏輯卷路徑中
[root@node105.yinzhengjie.org.cn ~]# ll /mydata/
total 0
drwxr-xr-x 2 root root 100 Nov  5 08:08 binlog
drwxr-xr-x 5 root root 326 Nov  5 08:06 mysql
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# chown mysql:mysql -R /mydata/
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /mydata/
total 0
drwxr-xr-x 2 mysql mysql 100 Nov  5 08:08 binlog
drwxr-xr-x 5 mysql mysql 326 Nov  5 08:06 mysql
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld start
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      80                                          :::3306                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mydata/mysql/mysql.sock
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.19-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000003 |       351 |
| mysql-bin.000004 |       351 |
| mysql-bin.000005 |       328 |
+------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SYSTEM ls /mydata/binlog
mysql-bin.000003  mysql-bin.000004  mysql-bin.000005  mysql-bin.index
MariaDB [(none)]> 
MariaDB [(none)]> 
修改相應的權限並啓動數據庫

 

 

6>.備份完成後,刪除快照卷

 

7>.制定好策略,經過原卷備份二進制日誌

相關文章
相關標籤/搜索