MySQL備份與恢復(fù)之冷備(1)
用一句話概括冷備,就是把數(shù)據(jù)庫(kù)服務(wù),比如MySQL,Oracle停下來(lái),然后使用拷貝、打包或者壓縮命令對(duì)數(shù)據(jù)目錄進(jìn)行備份。如果數(shù)據(jù)出現(xiàn)異常,則可以通過(guò)備份數(shù)據(jù)恢復(fù)。冷備一般需要定制計(jì)劃,比如什么時(shí)候做備份,每次對(duì)哪些數(shù)據(jù)進(jìn)行備份等等。但是由于這樣的備份占用過(guò)多的空間,對(duì)大數(shù)據(jù)量的環(huán)境下不一定適合,故生產(chǎn)環(huán)境很少使用。
冷備示意圖
冷備實(shí)驗(yàn)
第一步,創(chuàng)建測(cè)試數(shù)據(jù)庫(kù),插入測(cè)試數(shù)據(jù)
mysql> use larrydb; Database changed mysql> show tables; +-------------------+ | Tables_in_larrydb | +-------------------+ | access | +-------------------+ 1 row in set (0.00 sec) mysql> drop table access; Query OK, 0 rows affected (0.00 sec) mysql> clear mysql> show tables; Empty set (0.00 sec) mysql> mysql> create table class( -> cid int, -> cname varchar(30)); Query OK, 0 rows affected (0.01 sec) mysql> show create table class \G; *************************** 1. row *************************** Table: class Create Table: CREATE TABLE `class` ( `cid` int(11) DEFAULT NULL, `cname` varchar(30) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: No query specified mysql> create table stu( -> sid int, -> sname varchar(30), -> cid int) engine=myisam; Query OK, 0 rows affected (0.00 sec) mysql> show create table stu \G; *************************** 1. row *************************** Table: stu Create Table: CREATE TABLE `stu` ( `sid` int(11) DEFAULT NULL, `sname` varchar(30) DEFAULT NULL, `cid` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 1 row in set (0.00 sec) ERROR: No query specified mysql> insert into class values(1,'linux'),(2,'oracle'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> desc class; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | cid | int(11) | YES | | NULL | | | cname | varchar(30) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> desc stu; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | sid | int(11) | YES | | NULL | | | sname | varchar(30) | YES | | NULL | | | cid | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> insert into stu values(1,'larry01',1),(2,'larry02',2); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from stu; +------+---------+------+ | sid | sname | cid | +------+---------+------+ | 1 | larry01 | 1 | | 2 | larry02 | 2 | +------+---------+------+
第二步,停掉MySQL
[root@serv01 ~]# /etc/init.d/mysqld stop Shutting down MySQL... SUCCESS!
第三步,創(chuàng)建備份目錄,并修改擁有者和所屬組
[root@serv01 ~]# mkdir /databackup [root@serv01 ~]# chown mysql.mysql /databackup/ -R [root@serv01 ~]# ll /databackup/ -d drwxr-xr-x. 2 mysql mysql 4096 Sep 10 17:46 /databackup/ [root@serv01 ~]# cd /databackup/
第四步,冷備(使用tar命令)
[root@serv01 databackup]# tar -cvPzf mysql01.tar.gz
第五步,測(cè)試?yán)鋫涞臄?shù)據(jù)是否正常,我們刪除掉data下的所有數(shù)據(jù)
[root@serv01 databackup]# rm -rf /usr/local/mysql/data/*
第六步,刪除所有數(shù)據(jù)后數(shù)據(jù)庫(kù)不能啟動(dòng)
[root@serv01 databackup]# /etc/init.d/mysqld start Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/serv01.host.com.pid).
第七步,恢復(fù)數(shù)據(jù)(使用tar命令)
[root@serv01 databackup]# tar -xvPf mysql01.tar.gz
第八步,啟動(dòng)MySQL,然后登錄MySQL,查看數(shù)據(jù)是否丟失,如果數(shù)據(jù)正常代表冷備成功
[root@serv01 databackup]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS! [root@serv01 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.29-log Source distribution Copyright (c) 2000, 2012, 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> use larrydb; Database changed mysql> select * from class; +------+--------+ | cid | cname | +------+--------+ | 1 | linux | | 2 | oracle | +------+--------+ 2 rows in set (0.00 sec) mysql> select * from stu; +------+---------+------+ | sid | sname | cid | +------+---------+------+ | 1 | larry01 | 1 | | 2 | larry02 | 2 | +------+---------+------+ 2 rows in set (0.00 sec)
以上就是實(shí)現(xiàn)MySQL冷備的全部過(guò)程,大家對(duì)冷備有沒(méi)有了大概的了解,希望這篇文章可以對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
centos7下mysqldump定時(shí)備份數(shù)據(jù)庫(kù)的方法實(shí)現(xiàn)
MySQL Dump是MySQL提供的方便導(dǎo)出數(shù)據(jù)庫(kù)數(shù)據(jù)的工具,本文主要介紹了centos7下mysqldump定時(shí)備份數(shù)據(jù)庫(kù)的方法實(shí)現(xiàn),感興趣的可以了解一下2023-08-08
MySQL實(shí)現(xiàn)類似于connect_by_isleaf的功能MySQL方法或存儲(chǔ)過(guò)程
這篇文章主要介紹了MySQL實(shí)現(xiàn)類似于connect_by_isleaf的功能MySQL方法或存儲(chǔ)過(guò)程,需要的朋友可以參考下2017-02-02
Mysql中varchar長(zhǎng)度設(shè)置方法
這篇文章主要介紹了Mysql中varchar長(zhǎng)度設(shè)置方法的相關(guān)資料,本文還給大家?guī)?lái)了valar類型的變化及char()和varchar()的區(qū)別介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
mysql特殊語(yǔ)法insert?into?..?on?duplicate?key?update?..使用方
在我們的日常開(kāi)發(fā)中經(jīng)常會(huì)遇到過(guò)這樣的情景,查看某條記錄是否存在,不存在的話創(chuàng)建一條新記錄,存在的話更新某些字段,下面這篇文章主要給大家介紹了關(guān)于mysql特殊語(yǔ)法insert?into?..?on?duplicate?key?update?..使用方法的相關(guān)資料,需要的朋友可以參考下2023-04-04
mysql連接過(guò)多和死掉以及拒絕服務(wù)的解決方法
mysql連接過(guò)多和死掉以及拒絕服務(wù)的解決方法...2007-12-12
MySQL中between...and的使用對(duì)索引的影響說(shuō)明
這篇文章主要介紹了MySQL中between...and的使用對(duì)索引的影響說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

