/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秒
3.查看通用查询日志是否开启
systemctl restart mysqld
mysql -u root -p
show variables like 'general%';
例:[root@www data]# systemctl restart mysqld
[root@www data]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like 'general%';
+------------------+-----------------------------------------+
| Variable_name | Value |
+------------------+-----------------------------------------+
| general_log | ON |
| general_log_file | /usr/local/mysql/data/mysql_general.log |
+------------------+-----------------------------------------+
2 rows in set (0.00 sec)
4.查看二进制日志是否开启
show variables like 'log_bin%';
例:mysql> show variables like 'log_bin%';
+---------------------------------+---------------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------------+
| log_bin | ON |
| log_bin_basename | /usr/local/mysql/data/mysql-bin |
| log_bin_index | /usr/local/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+---------------------------------------+
5 rows in set (0.01 sec)
5.查看慢查询日功能是否开启
show variables like '%slow%';
例:mysql> show variables like '%slow%';
+---------------------------+--------------------------------------------+
| Variable_name | Value |
+---------------------------+--------------------------------------------+
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /usr/local/mysql/data/mysql_slow_query.log |
+---------------------------+--------------------------------------------+
5 rows in set (0.00 sec)
6.查看慢查询时间设置
show variables like 'long_query_time';
例:mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 5.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
7.在数据库中设置开启慢查询的方法
set global slow_query_log=ON;
例:mysql> set global slow_query_log=ON;
Query OK, 0 rows affected (0.00 sec)
记录数据库的操作明细,默认关闭,开启后会降低数据库性能
1.备份的主要目的是灾难恢复
2.在生产环境中,数据的安全性至关重要任何数据的丢失都可能产生严重的后果
3.造成数据丢失的原因
1.从物理与逻辑的角度
2.数据库备份的分类
3.常见的备份方法
(1)物理冷备
use tour;
create table if not exists info1 (
id int(4) not null auto_increment,
name varchar(10) not null,
sex char(10) not null,
hobby varchar(50),
primary key (id));
insert into info1 values(1,'user1','male','running');
insert into info1 values(2,'user2','female','singing');
例:mysql> use tour;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table if not exists info1 (
-> id int(4) not null auto_increment,
-> name varchar(10) not null,
-> sex char(10) not null,
-> hobby varchar(50),
-> primary key (id));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into info1 values(1,'user1','male','running');
Query OK, 1 row affected (0.01 sec)
mysql> insert into info1 values(2,'user2','female','singing');
Query OK, 1 row affected (0.00 sec)
systemctl stop mysqld
yum -y install xz
tar Jcvf /opt/mysql_all_$(date +%F).tar.xz /usr/local/mysql/data/
mv /usr/local/mysql/data/ ~
tar Jxvf /opt/mysql_all_2020-11-22.tar.xz
mv /opt/usr/local/mysql/data/ /usr/local/mysql/
例:[root@www data]# systemctl stop mysqld
[root@www data]# yum -y install xz
已加载插件:fastestmirror, langpacks
file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"
正在尝试其它镜像。
Loading mirror speeds from cached hostfile
软件包 xz-5.2.2-1.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@www data]# tar Jcvf /opt/mysql_all_$(date +%F).tar.xz /usr/local/mysql/data/
[root@www data]# mv /usr/local/mysql/data/ ~
[root@www opt]# tar Jxvf /opt/mysql_all_2023-06-19.tar.xz
[root@www opt]# mv /opt/usr/local/mysql/data/ /usr/local/mysql/
(2)mysqldump专用备份工具
mysqldump -u root -p[密码] --databases 库名1 [库名2] … > /备份路径/备份文件名.sql #导出的就是数据库脚本文件
例:[root@www opt]# systemctl restart mysqld.service
[root@www opt]# mysqldump -u root -p --databases tour > /opt/tour.sql
Enter password:
[root@www opt]# mysqldump -u root -p --databases mysql tour > /opt/mysql-tour.sql
Enter password:
mysqldump -u root -p[密码] --all-databases > /备份路径/备份文件名.sql
例:[root@www opt]# mysqldump -u root -p --all-databases > /opt/all.sql
Enter password:
mysqldump -u root -p[密码] 库名 [表名1] [表名2] … > /备份路径/备份文件名.sql
例:[root@www opt]# mysqldump -u root -p -d tour info1 sunset > /opt/tour_info1.sql
Enter password:
grep -v "^--" /opt/kgc_info1.sql | grep -v "^/" | grep -v "^$"
例:[root@www opt]# grep -v "^--" /opt/tour_info1.sql | grep -v "^/" | grep -v "^$"
DROP TABLE IF EXISTS `info1`;
CREATE TABLE `info1` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`sex` char(10) NOT NULL,
`hobby` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `sunset`;
CREATE TABLE `sunset` (
`id` int(11) DEFAULT NULL,
`name` char(4) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` char(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(3)启用二进制日志进行增量备份
(4)第三方工具备份
4.MySQL完全备份
5.MySQL完全备份优点
6.MySQL完全备份缺点
7.MySQL完全恢复
(1)恢复数据库
systemctl start mysqld
mysql -u root -p -e 'drop database tour;'
mysql -u root -p -e 'SHOW DATABASES;'
mysql -u root -p < /opt/tour.sql
mysql -u root -p -e 'SHOW DATABASES;'
例:[root@www opt]# systemctl start mysqld
[root@www opt]# mysql -u root -p -e 'drop database tour;'
Enter password:
[root@www opt]# mysql -u root -p -e 'SHOW DATABASES;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| sun |
| sys |
+--------------------+
[root@www opt]# mysql -u root -p < /opt/tour.sql
Enter password:
[root@www opt]# mysql -u root -p -e 'SHOW DATABASES;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| sun |
| sys |
| tour |
+--------------------+
(2)恢复数据表
当备份文件中只包含表的备份,而不包含创建的库的语句时,执行导入操作时必须指定库名,且目标库必须存在。
mysqldump -u root -p tour info1 > /opt/tour_info1.sql
mysql -u root -p -e 'drop table tour.info1;'
mysql -u root -p -e 'show tables from tour;'
mysql -u root -p tour < /opt/tour_info1.sql
mysql -u root -p -e 'show tables from tour;'
例:[root@www opt]# mysqldump -u root -p tour info1 > /opt/tour_info1.sql
Enter password:
[root@www opt]# mysql -u root -pabc123 -e 'drop table tour.info1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@www opt]# mysql -u root -pabc123 -e 'show tables from tour;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------+
| Tables_in_tour |
+----------------+
| account |
| kiki |
| moon |
| stars |
| sunset |
| t1 |
+----------------+
[root@www opt]# mysql -u root -pabc123 tour < /opt/tour_info1.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@www opt]# mysql -u root -pabc123 -e 'show tables from tour;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------+
| Tables_in_tour |
+----------------+
| account |
| info1 |
| kiki |
| moon |
| stars |
| sunset |
| t1 |
+----------------+
五.Mysql 增量备份
1.开启二进制日志功能
vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog_format = MIXED #默认格式:STATEMENT(基于SQL语句),ROW(基于行)、MIXED(混合模式)
server-id = 1
systemctl start mysqld
ls -l /usr/local/mysql/data/mysql-bin.*
例:[root@www opt]# systemctl start mysqld
[root@www opt]# ls -l /usr/local/mysql/data/mysql-bin.*
-rw-r----- 1 mysql mysql 1042 6月 19 15:11 /usr/local/mysql/data/mysql-bin.000001
-rw-r----- 1 mysql mysql 177 6月 19 15:52 /usr/local/mysql/data/mysql-bin.000002
-rw-r----- 1 mysql mysql 10154 6月 19 16:07 /usr/local/mysql/data/mysql-bin.000003
-rw-r----- 1 mysql mysql 57 6月 19 15:52 /usr/local/mysql/data/mysql-bin.index
2.可每周对数据库或表进行完全备份
mysqldump -u root -p kgc info1 > /opt/kgc_info1_$(date +%F).sql
mysqldump -u root -p --databases kgc > /opt/kgc_$(date +%F).sql
例:[root@www opt]# mysqldump -u root -p tour info1 > /opt/tour_info1_$(date +%F).sql
Enter password:
[root@www opt]# mysqldump -u root -pabc123 --databases tour > /opt/tour_$(date +%F).sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
3.可每天进行增量备份操作,生成新的二进制日志文件(例如 mysql-bin.000002)
mysqladmin -u root -p flush-logs
例:[root@www opt]# mysqladmin -u root -pabc123 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
4.插入新数据,以模拟数据的增加或变更
use kgc;
insert into info1 values(3,'user3','male','game');
insert into info1 values(4,'user4','female','reading');
例:[root@www opt]# mysql -troot -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use tour;
Database changed
mysql> insert into info1 values(3,'user3','male','game');
Ignoring query to other database
mysql> insert into info1 values(4,'user4','female','reading');
Ignoring query to other database
5.再次生成新的二进制日志文件(例如 mysql-bin.000003)
mysqladmin -u root -p flush-logs
例:[root@www opt]# mysqladmin -u root -pabc123 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
6.查看二进制日志文件的内容
cp /usr/local/mysql/data/mysql-bin.000002 /opt/
mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
例:[root@www opt]# cp /usr/local/mysql/data/mysql-bin.000002 /opt/
[root@www opt]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#230619 15:31:31 server id 1 end_log_pos 123 CRC32 0xcf970784 Start: binlog v 4, server v 5.7.41-log created 230619 15:31:31 at startup
ROLLBACK/*!*/;
# at 123
#230619 15:31:31 server id 1 end_log_pos 154 CRC32 0x519bcca2 Previous-GTIDs
# [empty]
# at 154
#230619 15:52:29 server id 1 end_log_pos 177 CRC32 0xf8dd4e78 Stop
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
六.Mysql 增量恢复
1.一般恢复
use kgc;
delete from info1 where id=3;
delete from info1 where id=4;
mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -u root -p
例:mysql> use tour;
Database changed
mysql> delete from info1 where id=3;
Ignoring query to other database
mysql> delete from info1 where id=4;
Ignoring query to other database
mysql> quit
Bye
[root@www opt]# mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -u root -p
Enter password:
use kgc;
drop table info1;
mysql -u root -p kgc < /opt/kgc_info1_2020-11-22.sql
mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -u root -p
例:mysql> use tour;
Database changed
mysql> drop table info1;
Ignoring query to other database
mysql> quit
Bye
[root@www opt]# mysql -u root -p tour < /opt/tour_info1_2023-06-19.sql
Enter password:
[root@www opt]# mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -u root -p
Enter password:
2.断点恢复
mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
例:[root@www opt]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#230619 15:31:31 server id 1 end_log_pos 123 CRC32 0xcf970784 Start: binlog v 4, server v 5.7.41-log created 230619 15:31:31 at startup
ROLLBACK/*!*/;
# at 123
#230619 15:31:31 server id 1 end_log_pos 154 CRC32 0x519bcca2 Previous-GTIDs
# [empty]
# at 154
#230619 15:52:29 server id 1 end_log_pos 177 CRC32 0xf8dd4e78 Stop
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
mysqlbinlog --no-defaults --stop-position='623' /opt/mysql-bin.000002 | mysql -uroot -p
mysqlbinlog --no-defaults --start-position='623' /opt/mysql-bin.000002 | mysql -uroot -p
例:[root@www opt]# mysqlbinlog --no-defaults --stop-position='623' /opt/mysql-bin.000002 | mysql -uroot -p
Enter password:
[root@www opt]# mysqlbinlog --no-defaults --start-position='623' /opt/mysql-bin.000002 | mysql -uroot -p
Enter password:
mysqlbinlog --no-defaults --stop-datetime='2020-11-22 16:41:24' /opt/mysql-bin.000002 |mysql -uroot -p
mysqlbinlog --no-defaults --start-datetime='2020-11-22 16:41:24' /opt/mysql-bin.000002 |mysql -uroot -p
例:[root@www opt]# mysqlbinlog --no-defaults --stop-datetime='2023-06-19 16:41:24' /opt/mysql-bin.000002 |mysql -uroot -p
Enter password:
[root@www opt]# mysqlbinlog --no-defaults --start-datetime='2023-06-19 16:41:24' /opt/mysql-bin.000002 |mysql -uroot -p
Enter password: