1. 程式人生 > 其它 >linux啟動Redis客戶端時報錯 (error) NOAUTH Authentication required.

linux啟動Redis客戶端時報錯 (error) NOAUTH Authentication required.

目錄

資料備份的重要性

在生產環境中,資料的安全性至關重要

任何資料的丟失都可能產生嚴重的後果

造成資料丟失的原因

  • 程式錯誤
  • 人為操作錯誤
  • 運算錯誤
  • 磁碟故障
  • 災難(如火災、地震)和盜竊

資料庫備份的分類

Ⅰ、從物理與邏輯的角度,備份可分為

  1. 物理備份:對資料庫作業系統的物理檔案(如資料檔案、日誌檔案等)的備份
    物理備份方法
    ① 冷備份(離線備份)︰是在關閉資料庫的時候進行的
    ② 熱備份(聯機備份)∶資料庫處於執行狀態,依賴於資料庫的日誌檔案
    ③ 溫備份:資料庫鎖定表格(不可寫入但可讀)的狀態下進行備份操作

  2. 邏輯備份:對資料庫邏輯元件(如:表等資料車物件)的備份

Ⅱ、從資料庫的備份策略角度,備份可分為

  1. 完全備份:每次對資料庫進行完整的備份
  2. 差異備份:備份自從上次完全備份之後被修改過的檔案
  3. 增量備份:只有在上次完全備份或者增量備份後被修改的檔案才會被備份
    在這裡插入圖片描述

常見的備份方法

物理冷備

  1. 備份時資料庫處於關閉狀態,直接打包資料庫檔案
  2. 備份速度快,恢復時也是最簡單的

專用備份工具mydump或mysqlhotcopy

  1. mysqldump常用的邏輯備份工具
  2. mysqlhotcopy僅擁有備份MyISAM和ARCHIVE表

啟用二進位制日誌進行增量備份

  • 進行增量備份,需要重新整理二進位制日誌

第三方工具備份

  • 免費的MySQL熱備份軟體Percona XtraBackup

MySQL完全備份

定義

  1. 是對整個資料庫、資料庫結構和檔案結構的備份
  2. 儲存的是備份完成時刻的資料庫
  3. 是差異備份與增量備份的基礎

優點

  • 備份與恢復操作簡單方便

缺點

  • 資料存在大量的重複
  • 佔用大量的備份空間
  • 備份與恢復時間長

資料庫完全備份分類

物理冷備份與恢復

  • 關閉MySQL資料庫
  • 使用tar命令直接打包資料庫資料夾
  • 直接替換現有MySQL目錄即可

mysqldump備份與恢復

  • MySQL自帶的備份工具,可方便實現對MySQL的備份
  • 可以將指定的庫、表匯出為SQL指令碼
  • 使用命令mysql匯入備份的資料

專案一:物理冷備份及恢復

建立新庫及表

mysql> create database abc;
Query OK, 1 row affected (0.00 sec)

mysql> use abc;
Database changed

mysql> create table a(id int(1));
Query OK, 0 rows affected (0.00 sec)

mysql> desc a;
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id    | int(1) | YES  |     | NULL    |       |
+-------+--------+------+-----+---------+-------+
1 row in set (0.00 sec)

在這裡插入圖片描述

建立備份,並檢視備份檔案

[root@server1 ~]# systemctl stop mysqld.service 
[root@server1 ~]# tar zcvf /opt/mysql_all-$(date +%F).tar.gz /usr/local/mysql/data

[root@server1 ~]# cd /opt/
[root@server1 opt]# ls -lh
總用量 1.3M
-rw-r--r--. 1 root root 1.3M 12月 24 19:09 mysql_all-2020-12-24.tar.gz
drwxr-xr-x. 2 root root    6 3月  26 2015 rh

在這裡插入圖片描述

模擬毀壞abc表

[root@server1 ~]# systemctl start mysqld.service 
[root@server1 ~]# mysql -uroot -p123123

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| abc                |
| auth               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql> drop database abc;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| auth               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

abc資料表刪除成功

在這裡插入圖片描述

恢復備份

mysql> exit
Bye
[root@server1 ~]# systemctl stop mysqld.service 
[root@server1 ~]# cd /opt/
[root@server1 opt]# tar zxvf mysql_all-2020-12-24.tar.gz 
[root@server1 opt]# ls -lh
總用量 1.3M
-rw-r--r--. 1 root root 1.3M 12月 24 19:09 mysql_all-2020-12-24.tar.gz
drwxr-xr-x. 2 root root    6 3月  26 2015 rh
drwxr-xr-x. 3 root root   19 12月 24 19:18 usr

在這裡插入圖片描述

[root@server1 opt]# mkdir mysql_old
[root@server1 opt]# mv /usr/local/mysql/data/ mysql_old/
[root@server1 opt]# mv usr/local/mysql/data/ /usr/local/mysql/
[root@server1 opt]# cd /usr/local/mysql/
[root@server1 mysql]# ls -lh

[root@server1 data]# date
2020年 12月 24日 星期四 19:21:30 CST
[root@server1 data]# systemctl start mysqld.service 

[root@server1 data]# mysql -uroot -p123123

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| abc                |
| auth               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql> use abc;
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> dasc a;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dasc a' at line 1
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| a             |
+---------------+
1 row in set (0.00 sec)

mysql> desc a;
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| id    | int(1) | YES  |     | NULL    |       |
+-------+--------+------+-----+---------+-------+
1 row in set (0.00 sec)

恢復備份成功

在這裡插入圖片描述

專案二:物理熱備份及恢復

建立新表

mysql> insert into abc.a values(1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into abc.a values(2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from abc.a;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

在這裡插入圖片描述

恢復資料庫2-1

  • 使用mysqldump匯出的指令碼,可使用匯入的方法
    ① source命令
    ② mysql命令

  • 使用source恢復資料庫的步驟
    ① 登入到MySQL資料庫
    ② 執行source備份sql指令碼的路徑

  • source恢復的示例

MySQL [(none)]>source /backup/all-data.sql

:需先建立同名的空資料庫,use這個資料庫再恢復

  • 使用mysql命令恢復資料
mysql -u使用者名稱-p[密碼]<庫備份指令碼的路徑

mysql命令恢復的示例

mysql -u root -p </backupl/all-data.sql

進行熱備份(完全備份)

[root@server1 opt]# cd /usr/local/mysql/
[root@server1 mysql]# cd data/

[root@server1 data]# mysqldump -uroot -p123123 aaa > /backup-$(date +%F).sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.   //提示密碼不安全

[root@server1 data]# mysqldump -u root -p --all-databases > /backup-all_databases-$(date +%F).sql
Enter password:                //非明文顯示,密碼輸入123123

[root@server1 ~]# cd /
[root@server1 /]# ls -lh
總用量 802K
drwxr-xr-x.   2 root root    6 12月 24 16:44 backup
-rw-r--r--.   1 root root 3.6K 12月 24 19:30 backup-2020-12-24.sql
-rw-r--r--.   1 root root 768K 12月 24 19:35 backup-all_databases-2020-12-24.sql

在這裡插入圖片描述

[root@server1 /]# vi backup-2020-12-24.sql    //檢視備份內容

在這裡插入圖片描述

[root@server1 /]# vi backup-all_databases-2020-12-24.sql 

在這裡插入圖片描述

刪除aaa資料庫

[root@server1 ~]# mysql -uroot -p123123

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| abc                |
| auth               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql> drop database aaa;
Query OK, 3 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |
| auth               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

在這裡插入圖片描述

還原備份

mysql> create database aaa;    //建立同名庫
Query OK, 1 row affected (0.00 sec)

mysql> use aaa;      //進入庫中還原
Database changed
mysql> source /backup-2020-12-24.sql

在這裡插入圖片描述

在這裡插入圖片描述

mysql> show tables;
+---------------+
| Tables_in_aaa |
+---------------+
| cj            |
| cj2           |
| t1            |
+---------------+
3 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| abc                |
| auth               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

在這裡插入圖片描述

恢復成功

MySQL增量備份

1、使用mysqldump進行完全備份存在的問題

  • 備份資料中有重複資料
  • 備份時間與恢復時間過長

2、是自上一次備份後增加/變化的檔案或者內容

3、特點

  • 沒有重複資料備份量不大時間短
  • 恢復需要上次完全備份及完全備份之後所有的增量備份才能恢復,而且要對所有增量備份進行逐個反推恢復

4、MySQL沒有提供直接的增量備份方法

5、可通過MySQL提供的二進位制日誌間接實現增量備份

6、MySQL二進位制日誌對備份的意義

  • 二進位制日誌儲存了所有更新或者可能更新資料庫的操作
  • 二進位制日誌在啟動MySQL伺服器後開始記錄,並在檔案達到max_binlog_size所設定的大小或者接收到flush
    logs令後重新建立新的日誌檔案
  • 只需定時執行flush logs方法重新建立新的日誌,生成二進位制檔案序列,並及時把這些日誌儲存到安全的地方就完成了一個時間段的增量備份

MySQL資料庫增量恢復

一般恢復

  • 將所有備份的二進位制日誌內容全部恢復

基於位置恢復

  • 資料庫在某一時間點可能既有錯誤的操作也有正確的操作
  • 可以基於精準的位置跳過錯誤的操作

基於時間點恢復

  • 跳過某個發生錯誤的時間點實現資料恢復

專案三:基於時間點恢復

建立新表

mysql> create table IT_salary(崗位類別 CHAR(16) NOT NULL, 姓名 CHAR(16) NOT NULL, 年齡 INT(10) NOT NULL, 員工ID INT(10) NOT NULL, 學歷 CHAR(16) NOT NULL, 年限 INT(10) NOT NULL, 薪資 INT(10) NOT NULL, PRIMARY KEY (姓名));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into IT_salary values('網路工程師','張三','27','016','本科','3','6000');
Query OK, 1 row affected (0.00 sec)

mysql> select * from IT_salary;
+-----------------+--------+--------+----------+--------+--------+--------+
| 崗位類別        | 姓名   | 年齡   | 員工ID   | 學歷   | 年限   | 薪資   |
+-----------------+--------+--------+----------+--------+--------+--------+
| 網路工程師      | 張三   |     27 |       16 | 本科   |      3 |   6000 |
+-----------------+--------+--------+----------+--------+--------+--------+
1 row in set (0.00 sec)

在這裡插入圖片描述

增加表ttt及新資料

mysql> create table ttt(name char(16), scores int (20));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into ttt values('sam','80');
Query OK, 1 row affected (0.00 sec)

mysql> insert into ttt values('jack','70');
Query OK, 1 row affected (0.00 sec)

mysql> select * from ttt;
+------+--------+
| name | scores |
+------+--------+
| sam  |     80 |
| jack |     70 |
+------+--------+
2 rows in set (0.00 sec)

在這裡插入圖片描述

開啟增量備份

[root@server1 ~]# cd /opt/
[root@server1 opt]# mkdir bak_sql
[root@server1 opt]# mysqldump -uroot -p aaa > /opt/bak_sql/cloud-$(date +%F).sql
Enter password:              //非明文,輸入密碼123123

[root@server1 opt]# vi /etc/my.cnf
log_bin=/usr/local/mysql/data/mysql_bin   //新增位置如下

在這裡插入圖片描述

重啟服務

[root@server1 opt]# cd
[root@server1 ~]# systemctl restart mysqld.service 

開始操作增量備份

[root@server1 ~]# mysqladmin -uroot -p flush-logs
Enter password:              //非明文,輸入密碼123123

[root@server1 ~]# cd /usr/local/mysql/data/
[root@server1 data]# ls -lh

在這裡插入圖片描述

查詢該二進位制日誌內容是否正確

[root@server1 ~]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /usr/local/mysql/data/mysql_bin.000002

在這裡插入圖片描述

再次操作,再次增量備份

[root@server1 ~]# mysql -uroot -p123123
mysql> show tables;
+---------------+
| Tables_in_aaa |
+---------------+
| IT_salary     |
| cj            |
| cj2           |
| t1            |
| ttt           |
+---------------+
5 rows in set (0.00 sec)

mysql> insert into ttt values('sisi','90');
Query OK, 1 row affected (0.01 sec)   //正確操作

mysql> delete from ttt where name='jack';
Query OK, 1 row affected (0.00 sec)    //誤操作

mysql> insert into ttt values('haha','65');
Query OK, 1 row affected (0.01 sec)    //正確操作

在這裡插入圖片描述

開始操作增量備份

mysql> exit

[root@server1 ~]# mysqladmin -uroot -p flush-logs
Enter password:             //非明文,輸入123123
[root@server1 ~]# 

[root@server1 ~]# cd /usr/local/mysql/data/
[root@server1 data]# ls -lh

在這裡插入圖片描述

查詢該二進位制日誌內容

[root@server1 data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /usr/local/mysql/data/mysql_bin.000003

**加粗樣式**

刪除原先壞掉的那張表

[root@server1 ~]# mysql -uroot -p123123
mysql> use aaa;
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> drop table ttt;
Query OK, 0 rows affected (0.00 sec)

在這裡插入圖片描述

還原完全備份的那個資料庫

檢視備份檔案

[root@server1 ~]# cd /opt/bak_sql/
[root@server1 bak_sql]# ls -lh
總用量 8.0K
-rw-r--r--. 1 root root 5.0K 12月 24 20:08 cloud-2020-12-24.sql

在這裡插入圖片描述

開始還原

[root@server1 ~]# mysql  -u root -p aaa < /opt/bak_sql/cloud-2020-12-24.sql
Enter password:        //非明文,輸入123123

再次檢視備份日誌

確認停止錯誤的時間和開始正確的時間

mysqlbinlog --no-defaults --base64-output=decode-rows -v /usr/local/mysql/data/mysql_bin.000003

停止錯誤的時間
在這裡插入圖片描述
開始正確的時間
在這裡插入圖片描述

[root@server1 ~]# mysqlbinlog --no-defaults --stop-datetime='2020-12-24 20:48:38' /usr/local/mysql/data/mysql_bin.000003 | mysql -uroot -p
Enter password:          //非明文,輸入123123,停止錯誤的時間
[root@server1 ~]# mysqlbinlog --no-defaults --start-datetime='2020-12-24 20:58:02' /usr/local/mysql/data/mysql_bin.000003 | mysql -uroot -p
Enter password:         //非明文,輸入123123,開始正確的時間

在這裡插入圖片描述

驗證

登入資料庫,檢視刪除的ttt表是否恢復

[root@server1 ~]# mysql -uroot -p123123
mysql> use aaa;
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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| abc                |
| auth               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql> show tables;
+---------------+
| Tables_in_aaa |
+---------------+
| IT_salary     |
| cj            |
| cj2           |
| t1            |
| ttt           |
+---------------+
5 rows in set (0.00 sec)

mysql> select * from ttt;
+------+--------+
| name | scores |
+------+--------+
| sam  |     80 |
| jack |     70 |
| sisi |     90 |
| haha |     65 |
+------+--------+
4 rows in set (0.00 sec)

在這裡插入圖片描述
在這裡插入圖片描述

專案四:基於位置點恢復

刪除原先壞掉的那張表;

mysql> use aaa;

mysql> delete from ttt where name='sisi';
Query OK, 1 row affected (0.00 sec)

mysql> delete from ttt where name='haha';
Query OK, 1 row affected (0.00 sec)

在這裡插入圖片描述

查詢該二進位制日誌內容

[root@server1 ~]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /usr/local/mysql/data/mysql_bin.000003

上一次正確操作的位置點停止

[root@server1 ~]# mysqlbinlog --no-defaults --stop-position='13492' /usr/local/mysql/data/mysql_bin.000003 | mysql -uroot -p

在這裡插入圖片描述

下一次正確操作的位置點開始

[root@server1 ~]# mysqlbinlog --no-defaults --start-position='13523' /usr/local/mysql/data/mysql_bin.000003 | mysql -uroot -p

驗證

mysql> select * from ttt;
+------+--------+
| name | scores |
+------+--------+
| sam  |     80 |
| jack |     70 |
| sisi |     90 |
| haha |     65 |
+------+--------+
4 rows in set (0.00 sec)

在這裡插入圖片描述
恢復成功成功