mysql备份

[root@localhost  init.d]# mysqldump   -S /data/3308/mysql.sock -B tt >~/bak.sql #mysqldump -uroot -padmin -B  数据库名 >~/bak.sql逻辑备份若B 换A为备份所有的库
[root@localhost  init.d]# rm bak.sql
rm:是否删除普通文件 "bak.sql"?y
[root@localhost  init.d]# cd ~/
[root@localhost  ~]# ls
anaconda-ks.cfg  Desktop      install.log.syslog  公共的  视频  文档  音乐
bak.sql          install.log  soft                模板    图片  下载  桌面
insert优化 批量插入
insert into test values (1,'tt01'),(2,'tt02'),(3,'tt03');
2不用* ,查询的列列出来
select * from test
select * from test  limit 2;
使用explain查看执行计划

mysql> explain select * from student where id=1;
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                               |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql5.1 参考手册5,6,7,8,10,11,13,14,15

[root@localhost  ~]# mysqldump   -S /data/3308/mysql.sock tt <~/bak.sql
[root@localhost ~]# mysqldump   -S /data/3308/mysql.sock tt <~/bak.sql
-- MySQL dump 10.13  Distrib 5.5.32, for Linux (x86_64)
--
-- Host: localhost    Database: tt
-- ------------------------------------------------------
-- Server version    5.5.32-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2015-12-28  4:18:01

利用bin_log恢复【更改语句】
[root@localhost data]# mysqlbinlog mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#151227  2:04:34 server id 4  end_log_pos 107     Start: binlog v 4, server v 5.5.32-log created 151227  2:04:34 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
srd/Vg8EAAAAZwAAAGsAAAABAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACyt39WEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==
'/*!*/;
# at 107
#151227 22:58:59 server id 4  end_log_pos 271     Query    thread_id=2    exec_time=0    error_code=0
SET TIMESTAMP=1451285939/*!*/;

【】
mysqladmin  -uroot -padmin flush_log对日志切割
mysql -uroot -padmin -e “select* from tt.student”
mysqlbinlog -d tt[数据库] mysql-bin.000001 >~/bak.sql
mysql -uroot -padmin -U 防止误删
truncate table test【物理,清空文件】;和delete【逻辑,一行一行】的区别是前者更快

增删改字段
alter table test  add sex char(4)
rename table 原表 新表
alter table test1 rename to test

全量备份和增量备份
备份所有库
mysqldump -uroot -padmin -F -A -B >TEST.sql
增量备份
从上次全量备份后,更新的数据
周备和天备
周备:恢复时间短,恢复费用低,缺点:占用空间多,占用资源多,经常锁表
单台数据库:用rsync(inotify)若没有主从同步
rsync -avz /data/3306/mysql-bin* rsync_backup@ip::backup
--password-file=/etc/rsync.password
mysqldump备份:
1.迁移或者升级数据库时
2增加从库的时候
3,因为硬件或特殊异常情况,主库或者从库宕机,主从可以互相切换
4,人为的sql语句主从库没有办都要法, 所有库会执行,此时要被
5跨机房灾备,需要备份拷贝走
增量恢复应用场景(人为)应为所有从库都执行了误操作
增量恢复的条件
1开启binlog
存在全备+以及到出现的增量恢复
innodb
mysqldump -uroot -padmin --single-transaction -A -B|gzip >~/tt.sql.gzip
mysqldump -uroot -padmin -A -B --lock-all-tables|gzip >~/tt.sql.gz
增量恢复全过程
锁表或开启防火墙
grep -i "change" 全备的log
发现binlog从14开始
mysqlbinlog -d tt >恢复
show variables like '%log_bin%'
sql_log_bin不
mysql <导入

你可能感兴趣的:(mysql备份)