First, using the system comes with tools to achieve a full backup: mysqldump
Data backup:
物理备份:直接拷贝库或表对应的文件。限于myisam,跨平台性差
逻辑备份:执行备份时,根据已有的数据,生成对应的sql命令,把sql保存到指定的文件里
1. Back up all databases:
mysqldump -hlocalhost -uroot -p12345678 --all-databases > /opt/$(date +%F).sql
//--all-databases--所有数据库 /opt/$(date +%F).sql 重定向到以日期命名的文件
2, back up the specified database:
mysqldump -hlocalhost -uroot -p12345678 db1 db2 > /opt/$(date +%F).sql
3, fully restored: first of all to ensure that the inventory, not create
mysql -hlocalhost -uroot -p12345678 db88 < /opt/123.sql
4, full recovery can also be carried out in the mysql command line: source /opt/2018-01-22.sql
5, with regular full backups can be scheduled task
if [ ! -d /data ];then //首先判断该目录是否存在,不存在则创建
mkdir /data
fi
mysqldump -uroot -p12345678 test > /data/$(date +%F)-test.sql
Second, to enable real-time incremental backup binlog log
Differential backup: backup since the full backup of all data generated
Incremental backup: backup since the last backup, all newly generated data
1, the binary log, recording sql query commands except
由于mysql默认没有启用binlog日志,故需在配置文件里面修改
日志文件,不能大于500M,mysqlbinlog server51-bin.000001 //查看日志命令
server_id=51 //服务器编号,唯一
log-bin=/mysqllog/server51 //启用logbin日志文件,指定其路径和文件名,必须要给mysql帐号权限
binlog-format="mixed" //指定日志文件格式,有三种格式
2, the log file records sql command mode
时间: 180127 23:23:29 server id 51
偏移量: at 313
3, recovery data
A, offset Recovery:
mysqlbinlog --start-position=378 --stop-position=534 /mysqllog/server51.000001 | mysql -uroot -p12345678
起始偏移量 终止偏移量 binlog日志文件 登陆的帐号和密码
B、时间恢复
mysqlbinlog --start-datetime="2018-1-27 23:22:22" --stop-datetime="2018-1-27 23:23:29" //指定起始时间和终止时间
/mysqllog/server51.000001 | mysql -uroot -p12345678 //指定日志文件和登陆的用户名和密码
4, manually generate a new log file binlog
刷新日志:flush logs;
登陆时创建:mysql -uroot -p123456 -e "flush logs"
导出备份数据库时:mysqldump -uroot -p123456 --flush-logs db4 > /root/db4.sql
重启服务器时生成新的日志文件
5, how to delete the log
mysql> reset master; // delete all the logs, generate a new log file
mysql> purge master logs to "log file name"; // delete a single log file
mysql> purge master logs to "plj.000006"; // delete all log files prior to the specified log file