mysql备份
一、mysql单库备份
1、备份数据库
mysqldump -uroot -pwc20080512 heruiguo>/opt/mysql_bak/mysql.sql;
2、删除数据库的表表,便于验证
mysql -uroot -pwc20080512 -e "use heruiguo;drop table user;"
[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)
3、用备份的数据还原;
mysql -uroot -pwc20080512 heruiguo </opt/mysql_bak/mysql.sql ;
4、验证还原的数据;
[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,好处是在备份数据的时候会创建库,选中库
备份:mysqldump -uroot -pwc20080512 -B heruiguo >/opt/mysql_bak/mysql_B.sql;
还原:mysqldump -uroot -pwc20080512 </opt/mysql_bak/mysql_B.sql;
5、压缩备份,减小文件的大小
mysqldump -uroot -pwc20080512 -B heruiguo | gzip >/opt/mysql_bak/mysql_B.sql.gz;
二、mysql多库备份
1、创建库
create dababases wanghaixue;
2、创建表
create table school(
id varchar(10),
name varchar(20),
dizhi varchar(30)
);
3、插入数据
insert into school (id,name,dizhi) values (2,'daxue','nanjing'),(3,'xiaoxue','sichuan');
4、查询数据:
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)
5、备份多个数据库
mysqldump -uroot -pwc20080512 -B heruiguo wanghaixue|gzip >/opt/mysql_bak/mysql_duogeku.sql.gz;
heruiguo和wanghaixue是库名
三、多库批量分库备份
1、过滤掉多余的库
[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
2、拼接备份语句
[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
3、拼接可执行备份语句
[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
4、验证备份的结果
[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
四、备份单个表
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