mysql -u root -p
create database school;
use school;
create table class(
id int(10) not null,
name varchar(20) not null,
sex char(2) not null,
cardid varchar(20) not null,
phone varchar(11),
address varchar(50));
desc class;
insert into class values ('1','zhangsan','男','123456','111111','苏州');
insert into class values ('2','lisi','女','123123','222222','苏州');
insert into class values ('3','wangchao','男','123412','333333','扬州');
insert into class values ('4','zhanglong','男','112233','444444','南京');
insert into class values ('5','zhaohu','男','111222','555555','苏州');
select * from class;
MySQL 的日志默认保存位置为/usr/local/mysql/data
vim /etc/my.cnf
[mysqld]
##错误日志,用来记录当MySQL启动、停止或运行时发生的错误信息,默认已开启
#指定错误日志的保存位置和文件名
log-error=/usr/local/mysql/data/mysql_error.log
##通用查询日志,用来记录MySQL的所有连接和语句,默认是关闭的
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log
##二进制日志(binlog),用来记录所有更新了数据或者已经潜在更新了数据的语句,记录了数据的更改,可用于数据恢复,默认已开启
log-bin=mysql-bin
#也可以 log_bin=mysql-bin
##慢查询日志,用来记录所有执行时间超过long_query_time秒的语句,可以找到哪些查询语句执行时间长,以便于优化,默认是关闭的
slow_query_log=ON
slow_query_log_file=/usr/local/mysql/data/mysql_slow_query.log
long_query_time=5 #设置超过5秒执行的语句被记录,缺省时为10秒
systemctl restart mysqld.service
mysql -u root -p
show variables like 'general%';
show variables like 'log_bin%';
show variables like '%slow%';
show variables like 'long_query_time';
set global slow_query_log=ON;
systemctl stop mysqld
yum -y install xz #xz是一个压缩工具
#压缩备份
tar Jcvf /opt/mysql_all_$(date +%F).tar.xz /usr/local/mysql/data/
#解压恢复
tar Jxvf /opt/mysql_all_2021-02-05.tar.xz -C /usr/local/mysql/data
systemctl start mysqld
#导出的备份文件就是数据库脚本文件
mysqldump -u root -p[密码] --databases 库名1 [库名2] … > /备份路径/备份文件名.sql
例:
mysqldump -u root -p --databases school > /opt/school.sql
mysqldump -u root -p --databases mysql school > /opt/mysql-school.sql
mysqldump -u root -p[密码] --all-databases > /备份路径/备份文件名.sql
例:
mysqldump -u root -p --all-databases > /opt/all.sql
mysqldump -u root -p[密码] [-d] 库名 [表名1] [表名2] … > /备份路径/备份文件名.sql
#使用“ -d ”选项,说明只保存数据库的表结构
#不使用“ -d ”选项,说明表数据也进行备份
例:
mysqldump -u root -p school class > /opt/school_class.sql
cat /opt/备份的文件 |grep -v "^--" | grep -v "^/" | grep -v "^$"
例:
cat /opt/school_class.sql |grep -v "^--" | grep -v "^/" | grep -v "^$"
#“-e”选项,用于指定连接 MySQL 后执行的命令,命令执行完后自动退出
mysql -u root -p -e 'drop database school;'
mysql -u root -p -e 'show databases;'
mysql -u root -p < /opt/school.sql
mysql -u root -p -e 'show databases;'
mysql -u root -p -e 'drop table school.class;'
mysql -u root -p -e 'show tables from school;'
mysql -u root -p school < /opt/school_class.sql
mysql -u root -p -e 'show tables from school;'
vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id = 1
binlog_format = MIXED #指定二进制日志(binlog)的记录格式为 MIXED
#二进制日志(binlog)有3种不同的记录格式:STATEMENT(基于SQL语句)、ROW(基于行)、MIXED(混合模式),默认格式是STATEMENT
systemctl restart mysqld.service
ls -l /usr/local/mysql/data/mysql-bin.*
#手动执行备份
mysqldump -u root -p school class > /opt/school_class_$(date +%F).sql
mysqldump -u root -p --all-databases > /opt/allmysql_$(date +%F).sql
#使用crontab -e 计划性任务来执行;每周1凌晨2点对表class和所有的库进行备份
0 2 * * 1 mysqldump -u root -p school class > /opt/school_class_$(date +%F).sql
0 2 * * 1 mysqldump -u root -p --all-databases > /opt/allmysql_$(date +%F).sql
mysqladmin -u root -p flush-logs
mysql -u root -p
use school;
insert into class values ('6','zzz','男','897656','666666','南京');
insert into class values ('7','aaa','女','098765','777777','苏州');
mysqladmin -u root -p flush-logs
#之前的步骤4的数据库操作会保存到mysql-bin.000002文件中,之后数据库数据再发生变化则保存在mysql-bin.000003文件中
cp /usr/local/mysql/data/mysql-bin.000002 /opt/
mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
#--base64-output=decode-rows:使用64位编码机制去解码并按行读取
#-v:显示详细内容
mysql -u root -p
use school;
delete from class where id=6;
delete from class where id=7;
select * from class;
quit
mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -u root -p
mysql -u root -p -e "select * from school.class;"
mysql -u root -p
use school;
drop table class;
show tables;
quit
mysql -uroot -p school < /opt/school_class_2021-02-06.sql
mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -uroot -p
mysql -u root -p -e "select * from school.class;"
mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
#部分二进制文件的内容
......
BEGIN
/*!*/;
##-------------解释:at xxx 表示位置点------------------------------------------------
# at 302
##--------------解释:开头210206 15:45:53表示时间,其他的现在用不到-----------------------------------
#210206 15:45:53 server id 1 end_log_pos 449 CRC32 0xe972def7 Query thread_id=6 exec_time=0 error_code=0
##--------------解释:这里是执行的操作语句---------------------
use `school`/*!*/; <-------------use school;使用数据库
SET TIMESTAMP=1612597553/*!*/; <------------建立时间戳
insert into class values ('6','zzz','男','897656','666666','南京') <-------向表中插入数据
/*!*/;
##---------------------------------------------------------------
# at 449
#210206 15:45:53 server id 1 end_log_pos 480 CRC32 0x5efde826 Xid = 446
COMMIT/*!*/;
# at 480
#210206 15:45:54 server id 1 end_log_pos 545 CRC32 0x11768895 Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 545
#210206 15:45:54 server id 1 end_log_pos 628 CRC32 0x778ea5fa Query thread_id=6 exec_time=0 error_code=0
SET TIMESTAMP=1612597554/*!*/;
##-------------------------------插入第二个数据--------------------------
BEGIN
/*!*/;
# at 628
#210206 15:45:54 server id 1 end_log_pos 775 CRC32 0x66e3bb53 Query thread_id=6 exec_time=0 error_code=0
SET TIMESTAMP=1612597554/*!*/;
insert into class values ('7','aaa','女','098765','777777','苏州')
/*!*/;
# at 775
#210206 15:45:54 server id 1 end_log_pos 806 CRC32 0x7b972395 Xid = 447
COMMIT/*!*/;
# at 806
#210206 15:48:52 server id 1 end_log_pos 853 CRC32 0x0d77c456 Rotate to mysql-bin.000003 pos: 4
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
.......
#模拟数据丢失
mysql -uroot -p123456 school < /opt/school_class_2021-02-06.sql
mysql -uroot -p123456 -e "select * from school.class;"
#到位置点628停止恢复数据
mysqlbinlog --no-defaults --stop-position='628' /opt/mysql-bin.000002 | mysql -uroot -p123456
#查看class表的数据
mysql -uroot -p123456 -e "select * from school.class;"
#模拟数据丢失
mysql -uroot -p123456 school < /opt/school_class_2021-02-06.sql
mysql -uroot -p123456 -e "select * from school.class;"
#从位置点628开始恢复数据
mysqlbinlog --no-defaults --start-position='628' /opt/mysql-bin.000002 | mysql -uroot -p123456
#查看class表的数据
mysql -uroot -p123456 -e "select * from school.class;"
#模拟数据丢失
mysql -uroot -p123456 school < /opt/school_class_2021-02-06.sql
mysql -uroot -p123456 -e "select * from school.class;"
#到2021-02-06 15:45:54截止恢复数据
mysqlbinlog --no-defaults --stop-datetime='2021-02-06 15:45:54' /opt/mysql-bin.000002 | mysql -uroot -p123456
#查看class表的数据
mysql -uroot -p123456 -e "select * from school.class;"
#模拟数据丢失
mysql -uroot -p123456 school < /opt/school_class_2021-02-06.sql
mysql -uroot -p123456 -e "select * from school.class;"
#从2021-02-06 15:45:54开始恢复数据
mysqlbinlog --no-defaults--start-datetime='2021-02-06 15:45:54' /opt/mysql-bin.000002 | mysql -uroot -p123456
#查看class表的数据
mysql -uroot -p123456 -e "select * from school.class;"
总结:断点恢复
如果恢复某条SQL语句之前的所有数据,就stop在这个语句的位置节点或者时间点
如果恢复某条SQ语句以及之后的所有数据,就从这个语句的位置节点或者时间点start