mysql備份

1、mysql單庫備份html

一、備份數據庫mysql

mysqldump -uroot -pwc20080512 heruiguo>/opt/mysql_bak/mysql.sql;sql

二、刪除數據庫的表表,便於驗證數據庫

mysql -uroot -pwc20080512 -e "use heruiguo;drop table user;"bash

[root@localhost mysql_bak]# mysql -uroot -pwc20080512 -e "use heruiguo;drop table user;"
[root@localhost mysql_bak]# 
[root@localhost mysql_bak]# mysql -uroot -pwc20080512
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.1.20-MariaDB MariaDB Server

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

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

MariaDB [(none)]> use  heruiguo;
Database changed
MariaDB [heruiguo]> show  tables;
Empty set (0.00 sec)

三、用備份的數據還原;ui

mysql -uroot -pwc20080512  heruiguo </opt/mysql_bak/mysql.sql ;spa

四、驗證還原的數據;code

[root@localhost mysql_bak]# mysql -uroot -pwc20080512  -e "use  heruiguo;select * from user";
+----+----------+-----------+
| id | name     | dizhi     |
+----+----------+-----------+
|  1 | heruiguo | chongqing |
|  2 | zhangsan | chengdu   |
|  3 | lisi     | beijing   |
|  4 | wangwu   | shanghai  |
+----+----------+-----------+

注意:咱們在備份的時候建議加上-B,好處是在備份數據的時候會建立庫,選中庫orm

備份:mysqldump -uroot -pwc20080512 -B heruiguo >/opt/mysql_bak/mysql_B.sql;視頻

 還原:mysqldump -uroot -pwc20080512 </opt/mysql_bak/mysql_B.sql;

五、壓縮備份,減少文件的大小

mysqldump -uroot -pwc20080512 -B heruiguo | gzip >/opt/mysql_bak/mysql_B.sql.gz;

2、mysql多庫備份

一、建立庫

create dababases  wanghaixue;

二、建立表

create  table school(

id varchar(10),

name varchar(20),

dizhi  varchar(30)

);

三、插入數據

insert  into school (id,name,dizhi) values (2,'daxue','nanjing'),(3,'xiaoxue','sichuan');

四、查詢數據:

MariaDB [wanghaixue]> select * from  school;
+------+----------+---------+
| id   | name     | dizhi   |
+------+----------+---------+
| 1    | zhongxue | beijing |
| 2    | daxue    | nanjing |
| 2    | daxue    | nanjing |
| 3    | xiaoxue  | sichuan |
+------+----------+---------+
4 rows in set (0.00 sec)

 

五、備份多個數據庫

 mysqldump -uroot -pwc20080512  -B heruiguo  wanghaixue|gzip >/opt/mysql_bak/mysql_duogeku.sql.gz;

heruiguo和wanghaixue是庫名

 

 3、多庫批量分庫備份

一、過濾掉多餘的庫

[root@localhost mysql_bak]# mysql -uroot -pwc20080512 -e "show databases;"
+--------------------+
| Database |
+--------------------+
| heruiguo |
| information_schema |
| mysql |
| performance_schema |
| wanghaixue |
+--------------------+

[root@localhost mysql_bak]# mysql -uroot -pwc20080512 -e "show databases;" |grep -Evi "info|per|data"|sed 's#^#mysqldump -uroot -pwc20080512 -B #g'

mysqldump -uroot -pwc20080512 -B heruiguo mysqldump -uroot -pwc20080512 -B mysql mysqldump -uroot -pwc20080512 -B wanghaixue

 

二、拼接備份語句

[root@localhost mysql_bak]# mysql -uroot -pwc20080512  -e "show databases;" |grep -Evi "info|per|data"|sed -r 's#^([a-z].*$)#mysqldump -uroot -pwc20080512 -B \1|gzip >/opt/mysql_bak/\1.sql.gz#g'
mysqldump -uroot -pwc20080512 -B heruiguo|gzip >/opt/mysql_bak/heruiguo.sql.gz
mysqldump -uroot -pwc20080512 -B mysql|gzip >/opt/mysql_bak/mysql.sql.gz
mysqldump -uroot -pwc20080512 -B wanghaixue|gzip >/opt/mysql_bak/wanghaixue.sql.gz

 

 

三、拼接可執行備份語句

[root@localhost mysql_bak]# mysql -uroot -pwc20080512  -e "show databases;" |grep -Evi "info|per|data"|sed -r 's#^([a-z].*$)#mysqldump -uroot -pwc20080512 -B \1|gzip >/opt/mysql_bak/\1.sql.gz#g'|bash

 

四、驗證備份的結果

[root@localhost mysql_bak]# ll
總用量 144
-rw-r--r--. 1 root root    814 4月  29 11:33 heruiguo.sql.gz
-rw-r--r--. 1 root root 136444 4月  29 11:33 mysql.sql.gz
-rw-r--r--. 1 root root    783 4月  29 11:33 wanghaixue.sql.gz

 

4、備份單個表

mysqldump -uroot -pwc20080512   heruiguo user >/opt/mysql_bak/user.sql;

 

分庫分表視頻:

http://edu.51cto.com/course/808.html

備份表結構(-d)

mysqldump -uroot -pwc20080512 -d  heruiguo user

 

備份數據(-t)

mysqldump -uroot -pwc20080512 -t  heruiguo user
相關文章
相關標籤/搜索