MySQL中的數(shù)據(jù)備份與SQL語句
1.mysql數(shù)據(jù)庫備份與恢復(fù)
1.1 數(shù)據(jù)庫常用備份方案
數(shù)據(jù)庫備份方案:
- 全量備份
- 增量備份
- 差異備份
備份方案 | 特點 |
---|---|
全量備份 | 全量備份就是指對某一個時間點上的所有數(shù)據(jù)或應(yīng)用進行的一個完全拷貝。數(shù)據(jù)恢復(fù)快。 備份時間長 |
增量備份 | 增量備份是指在一次全備份或上一次增量備份后,以后每次的備份只需備份與前一次相比增加和者被修改的文件。這就意味著,第一次增量備份的對象是進行全備后所產(chǎn)生的增加和修改的文件;第二次增量備份的對象是進行第一次增量備份后所產(chǎn)生的增加和修改的文件,如此類推。 沒有重復(fù)的備份數(shù)據(jù) 備份時間短 恢復(fù)數(shù)據(jù)時必須按一定的順序進行 |
差異備份 | 備份上一次的完全備份后發(fā)生變化的所有文件。 差異備份是指在一次全備份后到進行差異備份的這段時間內(nèi) 對那些增加或者修改文件的備份。在進行恢復(fù)時,我們只需對第一次全量備份和最后一次差異備份進行恢復(fù)。 |
1.2 mysql備份工具mysqldump
語法
mysqldump [OPTIONS] database [tables ...] ? ? mysqldump [OPTIONS] --all-databases [OPTIONS] ? ? mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
常用的OPTIONS
-uUSERNAME ? ? ?//指定數(shù)據(jù)庫用戶名 ? ? -hHOST ? ? ? ? ?//指定服務(wù)器主機,請使用ip地址 ? ? -pPASSWORD ? ? ?//指定數(shù)據(jù)庫用戶的密碼 ? ? -P# ? ? ? ? ? ? //指定數(shù)據(jù)庫監(jiān)聽的端口,這里的#需用實際的端口號代替,如-P3307
全量備份
[root@localhost ~]# mysql -uroot -p Enter password:? Welcome to the MariaDB monitor. ?Commands end with ; or \g. Your MariaDB connection id is 20 Server version: 10.3.17-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)]> show databases; ?//查看庫,然后進入庫 +--------------------+ | Database ? ? ? ? ? | +--------------------+ | information_schema | | mysql ? ? ? ? ? ? ?| | performance_schema | | shaoyaobang ? ? ? ? ? ?| +--------------------+ 4 rows in set (0.000 sec) MariaDB [(none)]> use shaoyaobang; //庫中表里面有下面這些內(nèi)容 MariaDB [shaoyaobang]> select * from student; ? +----+-------------+------+ | id | name ? ? ? ?| age ?| +----+-------------+------+ | ?1 | tom ? ? ? ? | ? 20 | | ?2 | jerry ? ? ? | ? 23 | | ?3 | wangqing ? ?| ? 25 | | ?4 | sean ? ? ? ?| ? 28 | | ?5 | zhangsan ? ?| ? 26 | | ?6 | zhangsan ? ?| ? 20 | | ?7 | lisi ? ? ? ?| ? 50 | | ?8 | chenshuo ? ?| ? 10 | | ?9 | wangwu ? ? ?| ? ?3 | | 10 | qiuyi ? ? ? | ? 15 | | 11 | qiuxiaotian | ? 20 | +----+-------------+------+ 11 rows in set (0.000 sec) [root@localhost ~]# mkdir mysqldump [root@localhost ~]# cd mysqldump/ [root@localhost mysqldump]# ls [root@localhost mysqldump]# mysqldump -u root -p1 --all-databases > all-20210826.sql ? //全量備份完成 [root@localhost mysqldump]# ls all-20210826.sql //上面已經(jīng)全量備份,刪除庫中的shaoyaobang MariaDB [(none)]> show databases; ? +--------------------+ | Database ? ? ? ? ? | +--------------------+ | information_schema | | mysql ? ? ? ? ? ? ?| | performance_schema | | shaoyaobang ? ? ? ? ? ?| +--------------------+ 4 rows in set (0.000 sec) //刪除成功 MariaDB [(none)]> drop database shaoyaobang; ?? Query OK, 1 row affected (0.004 sec) MariaDB [(none)]> show databases; +--------------------+ | Database ? ? ? ? ? | +--------------------+ | information_schema | | mysql ? ? ? ? ? ? ?| | performance_schema | +--------------------+ 3 rows in set (0.000 sec)
還原全量備份
[root@localhost mysqldump]# mysql -u root -p1 < all-20210826.sql? [root@localhost mysqldump]#? MariaDB [(none)]> show databases; ?//登錄數(shù)據(jù)庫已經(jīng)還原 +--------------------+ | Database ? ? ? ? ? | +--------------------+ | information_schema | | mysql ? ? ? ? ? ? ?| | performance_schema | | shaoyaobang ? ? ? ? ? ?| +--------------------+ 4 rows in set (0.000 sec) //進入庫中查看內(nèi)容還原成功 MariaDB [(none)]> use shaoyaobang; 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 MariaDB [student]> select * from student; +----+-------------+------+ | id | name ? ? ? ?| age ?| +----+-------------+------+ | ?1 | tom ? ? ? ? | ? 20 | | ?2 | jerry ? ? ? | ? 23 | | ?3 | wangqing ? ?| ? 25 | | ?4 | sean ? ? ? ?| ? 28 | | ?5 | zhangsan ? ?| ? 26 | | ?6 | zhangsan ? ?| ? 20 | | ?7 | lisi ? ? ? ?| ? 50 | | ?8 | chenshuo ? ?| ? 10 | | ?9 | wangwu ? ? ?| ? ?3 | | 10 | qiuyi ? ? ? | ? 15 | | 11 | qiuxiaotian | ? 20 | +----+-------------+------+ 11 rows in set (0.000 sec)
2. 差異備份與恢復(fù)
mysql差異備份
開啟MySQL服務(wù)器的二進制日志功能
[mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /tmp/mysql.pid skip-name-resolve server-id=1 ? ? ? ? //設(shè)置服務(wù)器標(biāo)識符 log-bin=mysql_bin ? ?//開啟二進制日志功能 [root@localhost ~]# service mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
對數(shù)據(jù)庫進行完全備份
[root@localhost ~]# mysql -uroot -p Enter password:? Welcome to the MariaDB monitor. ?Commands end with ; or \g. Your MariaDB connection id is 20 Server version: 10.3.17-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)]> show databases; +--------------------+ | Database ? ? ? ? ? | +--------------------+ | information_schema | | mysql ? ? ? ? ? ? ?| | performance_schema | | shaoyaobang ? ? ? ? ? ? ? ?| +--------------------+ 4 rows in set (0.00 sec MariaDB [(none)]> show tables from shaoyaobang; +--------------------+ | Tables_in_shaoyaobang | +-----------------------+ | student ? ? ? ? ? ? ? | +-----------------------+ 1 rows in set (0.01 sec) MariaDB [(none)]> select * from shaoyaobang.student; +----+-------------+------+ | id | name ? ? ? ?| age ?| +----+-------------+------+ | ?1 | tom ? ? ? ? | ? 20 | | ?2 | jerry ? ? ? | ? 23 | | ?3 | wangqing ? ?| ? 25 | | ?4 | sean ? ? ? ?| ? 28 | | ?5 | zhangsan ? ?| ? 26 | | ?6 | zhangsan ? ?| ? 20 | | ?7 | lisi ? ? ? ?| ? 50 | | ?8 | chenshuo ? ?| ? 10 | | ?9 | wangwu ? ? ?| ? ?3 | | 10 | qiuyi ? ? ? | ? 15 | | 11 | qiuxiaotian | ? 20 | +----+-------------+------+ 11 rows in set (0.000 sec) //完全備份 [root@localhost ~]# mysqldump -uroot -syb123 --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-22010826.sql mysqldump: [Warning] Using a password on the command line interface can be insecure [root@localhost ~]# ls all-22010826.sql ? all-20210826.sql 模擬誤刪數(shù)據(jù) MariaDB [(none)]> drop database shaoyaobang; Query OK, 1 row affected (0.004 sec MariaDB [(none)]> show databases; +--------------------+ | Database ? ? ? ? ? | +--------------------+ | information_schema | | mysql ? ? ? ? ? ? ?| | performance_schema | +--------------------+
mysql差異備份恢復(fù)
//刷新創(chuàng)建新的二進制日志 [root@localhost ~]# mysqladmin -uroot -psyb123 flush-logs mysqladmin: [Warning] Using a password on the command line interface can be insecure. [root@localhost ~]# ll /opt/data/ 總用量 188552 -rw-r-----. 1 mysql mysql ? ? ? 56 8月 ?20 10:11 auto.cnf -rw-r-----. 1 mysql mysql ? ? ?996 8月 ?21 14:54 ib_buffer_pool -rw-r-----. 1 mysql mysql 79531776 8月 ?21 16:17 ibdata1 -rw-r-----. 1 mysql mysql 30331268 8月 ?21 16:18 ib_logfile0 -rw-r-----. 1 mysql mysql 803389648 8月 21 ?10:11 ib_logfile1 -rw-r-----. 1 mysql mysql 24582512 8月 ?21 14:54 ibtmp1 -rw-r-----. 1 mysql mysql ? ? 8234 8月 ?21 08:45 linux-node1.com.err -rw-r-----. 1 mysql mysql ? ?75650 8月 ?21 14:54 localhost.err drwxr-x---. 2 mysql mysql ? ? 4086 8月 ?21 16:16 mysql -rw-r-----. 1 mysql mysql ? ? ?915 8月 ?21 16:18 mysql_bin.000001 -rw-r-----. 1 mysql mysql ? ? ?256 8月 ?21 16:18 mysql_bin.000002 -rw-r-----. 1 mysql mysql ? ? ? 45 8月 ?21 16:18 mysql_bin.index drwxr-x---. 2 mysql mysql ? ? 789 8月 ?21 10:11 performance_schema
恢復(fù)完全備份
[root@localhost ~]# mysql -uroot -psyb123 < all-20210826.sql mysql: [Warning] Using a password on the command line interface can be insecure. MariaDB [(none)]> show databases; +--------------------+ | Database ? ? ? ? ? | +--------------------+ | information_schema | | mysql ? ? ? ? ? ? ?| | performance_schema | | shaoyaobang ? ? ? ?| +--------------------+ 4 rows in set (0.000 sec) MariaDB [(none)]> use shaoyaobang; 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 MariaDB [shaoyaobang]> show tables; +-----------------------+ | Tables_in_shaoyaobang | +-----------------------+ | student ? ? ? ? ? ? ? | +-----------------------+ 1 row in set (0.000 sec) MariaDB [(shaoyaobang)]> select * from student; +----+-------------+------+ | id | name ? ? ? ?| age ?| +----+-------------+------+ | ?1 | tom ? ? ? ? | ? 20 | | ?2 | jerry ? ? ? | ? 23 | | ?3 | wangqing ? ?| ? 25 | | ?4 | sean ? ? ? ?| ? 28 | | ?5 | zhangsan ? ?| ? 26 | | ?6 | zhangsan ? ?| ? 20 | | ?7 | lisi ? ? ? ?| ? 50 | | ?8 | chenshuo ? ?| ? 10 | | ?9 | wangwu ? ? ?| ? ?3 | | 10 | qiuyi ? ? ? | ? 15 | | 11 | qiuxiaotian | ? 20 | +----+-------------+------+ 11 rows in set (0.000 sec)
3. group by
group by 的簡單說明:
group by 一般和聚合函數(shù)一起使用才有意義,比如 count sum avg等,使用group by的兩個要素:
(1) 出現(xiàn)在select后面的字段 要么是是聚合函數(shù)中的,要么就是group by 中的.
(2) 要篩選結(jié)果 可以先使用where 再用group by 或者先用group by 再用having
//查看表的內(nèi)容 MariaDB [shaoyaobang]> select * from student; ?? +----+-------------+------+ | id | name ? ? ? ?| age ?| +----+-------------+------+ | ?1 | tom ? ? ? ? | ? 20 | | ?2 | jerry ? ? ? | ? 23 | | ?3 | wangqing ? ?| ? 25 | | ?4 | sean ? ? ? ?| ? 28 | | ?5 | zhangsan ? ?| ? 26 | | ?6 | zhangsan ? ?| ? 20 | | ?7 | lisi ? ? ? ?| ? 50 | | ?8 | chenshuo ? ?| ? 10 | | ?9 | wangwu ? ? ?| ? ?3 | | 10 | qiuyi ? ? ? | ? 15 | | 11 | qiuxiaotian | ? 20 | +----+-------------+------+ 11 rows in set (0.001 sec) //對表進去求平均值 MariaDB [shaoyaobang]> select avg(age) from student; ? +----------+ | avg(age) | +----------+ | ?21.8182 | +----------+ 1 row in set (0.000 sec) //也可以進行求和 MariaDB [shaoyaobang]> select sum(age) from student; ? +----------+ | sum(age) | +----------+ | ? ? ?240 | +----------+ 1 row in set (0.000 sec) ?//也可以進行計數(shù) MariaDB [shaoyaobang]> select count(age) from student;? +------------+ | count(age) | +------------+ | ? ? ? ? 11 | +------------+ 1 row in set (0.000 sec)
4. 內(nèi)連接 INNER JOIN
內(nèi)連接(INNER JOIN)有兩種,顯式的和隱式的,返回連接表中符合連接條件和查詢條件的數(shù)據(jù)行。(所謂的鏈接表就是數(shù)據(jù)庫在做查詢形成的中間表)。
內(nèi)連接使用比較運算符(包括=、>、<、<>、>=、<=、!> 和!<)進行表間的比較操作,查詢與連接條件相匹配的數(shù)據(jù)。
根據(jù)所使用的比較方式不同,內(nèi)連接分為等值連接、自然連接和自連接三種。
語句1:隱式的內(nèi)連接,沒有INNER JOIN,形成的中間表為兩個表的笛卡爾積。
SELECT O.ID,O.ORDER_NUMBER,C.ID,C.NAME FROM CUSTOMERS C,ORDERS O WHERE C.ID=O.CUSTOMER_ID;
語句2:顯示的內(nèi)連接,一般稱為內(nèi)連接,有INNER JOIN,形成的中間表為兩個表經(jīng)過ON條件過濾后的笛卡爾積。
SELECT O.ID,O.ORDER_NUMBER,C.ID,C.NAME FROM CUSTOMERS C INNER JOIN ORDERS O ON C.ID=O.CUSTOMER_ID;
語句3和語句4的查詢結(jié)果。
5. inner join、left join和right join
內(nèi)連接只返回滿足連接條件的數(shù)據(jù)行,外連接不只列出與連接條件相匹配的行,而是列出左表(左外連接時)、右表(右外連接時)或兩個表(全外連接時)中所有符合搜索條件的數(shù)據(jù)行。
外連接分為左外連接、右外鏈接、全外連接三種。
1)LEFT JOIN或LEFT OUTER JOIN
左向外聯(lián)接的結(jié)果集包括 LEFT OUTER子句中指定的左表的所有行,而不僅僅是聯(lián)接列所匹配的行。如果左表的某行在右表中沒有匹配行,則在相關(guān)聯(lián)的結(jié)果集行中右表的所有選擇列表列均為空值。
2)RIGHT JOIN 或 RIGHT OUTER JOIN
右向外聯(lián)接是左向外聯(lián)接的反向聯(lián)接。將返回右表的所有行。如果右表的某行在左表中沒有匹配行,則將為左表返回空值。
查看庫中兩張表的內(nèi)容
MariaDB [shaoyaobang]> select * from student1; +----+------+------+ | id | name | age ?| +----+------+------+ | ?1 | shao | ? 10 | | ?2 | yao ?| ? 20 | | ?3 | bang | ? 30 | +----+------+------+ 3 rows in set (0.000 sec) MariaDB [shaoyaobang]> select * from student; ?? +----+-------------+------+ | id | name ? ? ? ?| age ?| +----+-------------+------+ | ?1 | tom ? ? ? ? | ? 20 | | ?2 | jerry ? ? ? | ? 23 | | ?3 | wangqing ? ?| ? 25 | | ?4 | sean ? ? ? ?| ? 28 | | ?5 | zhangsan ? ?| ? 26 | | ?6 | zhangsan ? ?| ? 20 | | ?7 | lisi ? ? ? ?| ? 50 | | ?8 | chenshuo ? ?| ? 10 | | ?9 | wangwu ? ? ?| ? ?3 | | 10 | qiuyi ? ? ? | ? 15 | | 11 | qiuxiaotian | ? 20 | +----+-------------+------+ 11 rows in set (0.001 sec)
內(nèi)連(inner join)
MariaDB [shaoyaobang]> select student1.*,student.* from student1 inner join student on student1.age=student.age; +----+------+------+----+-------+------+ | id | name | age ?| id | name ?| age ?| +----+------+------+----+-------+------+ | ?3 | bang | ?30 ?| ?2 | jerry | ? 23 | +----+------+------+----+-------+------+ 1 row in set (0.000 sec)
左連(left join)
MariaDB [shaoyaobang]> select student1.*,student.* from student1 left join student on student1.age=student.age; +----+------+------+------+-------+------+ | id | name | age ?| id ? | name ?| age ?| +----+------+------+------+-------+------+ | ?3 | bang | ?30 ?| ? 2 ?| jerry | 23 ? |? | ?1 | shao | ?10 ?| NULL | NULL ?| NULL | | ?2 | yao ?| ?20 ?| NULL | NULL ?| NULL | +----+------+------+------+-------+------+ 3 rows in set (0.001 sec)
右連(right join)
MariaDB [shaoyaobang]> select student1.*,student.* from student1 right join student on shenlongfei1.age=student.age; +------+------+------+----+-------------+------+ | id ? | name | age ?| id | name ? ? ? ?| age ?| +------+------+------+----+-------------+------+ | ? ?3 | bang | ? 30 | ?2 | jerry ? ? ? | ? 23 | | NULL | NULL | NULL | ?1 | tom ? ? ? ? | ? 20 | | NULL | NULL | NULL | ?3 | wangqing ? ?| ? 25 | | NULL | NULL | NULL | ?4 | sean ? ? ? ?| ? 28 | | NULL | NULL | NULL | ?5 | zhangsan ? ?| ? 26 | | NULL | NULL | NULL | ?6 | zhangsan ? ?| ? 20 | | NULL | NULL | NULL | ?7 | lisi ? ? ? ?| ? 50 | | NULL | NULL | NULL | ?8 | chenshuo ? ?| ? 10 | | NULL | NULL | NULL | ?9 | wangwu ? ? ?| ? ?3 | | NULL | NULL | NULL | 10 | qiuyi ? ? ? | ? 15 | | NULL | NULL | NULL | 11 | qiuxiaotian | ? 20 | +------+------+------+----+-------------+------+ 11 rows in set (0.000 sec)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mysql關(guān)于數(shù)據(jù)庫是否應(yīng)該使用外鍵約束詳解說明
MySQL 外鍵約束(FOREIGN KEY)是表的一個特殊字段,經(jīng)常與主鍵約束一起使用。對于兩個具有關(guān)聯(lián)關(guān)系的表而言,相關(guān)聯(lián)字段中主鍵所在的表就是主表,外鍵所在的表就是從表。外鍵用來建立主表與從表的關(guān)聯(lián)關(guān)系,為兩個表的數(shù)據(jù)建立連接,約束兩個表中數(shù)據(jù)的一致性和完整性2021-10-10mysql 實現(xiàn)添加時間自動添加更新時間自動更新操作
這篇文章主要介紹了mysql 實現(xiàn)添加時間自動添加更新時間自動更新操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01mysql數(shù)據(jù)庫索引損壞及修復(fù)經(jīng)驗分享
這篇文章主要介紹了mysql數(shù)據(jù)庫索引損壞及修復(fù)經(jīng)驗分享,需要的朋友可以參考下2015-06-06