在linux下,mysql的备份数据库 脚本的编写

一.mysql修改数据库配置,重启mysql服务,实现数据库的备份脚本


##################step1:#######################
1.查看mysql的安装目录
[[email protected] t14-jurf]# which mysql
/usr/local/mysql-5.7.32/bin/mysql

2.备份情况一:命令中不带密码:
[[email protected] t14-jurf]# /usr/local/mysql-5.7.32/bin/mysqldump -uroot -p  -x  unifiedportal > /home/t14-jurf/backup/sql/unifiedportal_$(date +%Y%m%d_%H%M%S).sql
Enter password: 
3.备份情况二: 将密码写入命令中,提示:Using a password on the command line interface can be insecure
[[email protected] t14-jurf]# /usr/local/mysql-5.7.32/bin/mysqldump -uroot -p123  -x  unifiedportal > /home/t14-jurf/backup/sql/unifiedportal_$(date +%Y%m%d_%H%M%S).sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
#############################step2:解决办法:###############################
思路:
https://blog.csdn.net/navioo/article/details/51699032?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
有的mysql为my.cnf,有的为my.conf,进入/etc目录下进行查看:
1、打开my.cnf配置文件,进入/etc/my.cnf

[[email protected] t14-jurf]# cd /etc
[[email protected] etc]# vi my.cnf
2.编辑内容:

[mysqldump]
user=root
password=123


3.按esc,输入wq保存;
4.重新启动服务:
[[email protected] etc]# service mysql restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS! 
5.重新执行:(不用再输入用户名,密码这两项了)
[[email protected] etc]# /usr/local/mysql-5.7.32/bin/mysqldump   -x  unifiedportal > /home/t14-jurf/backup/sql/unifiedportal_$(date +%Y%m%d_%H%M%S).sql
6.编写脚本bkunifiedportal.sh的内容:
#!/bin/bash
#/usr/local/mysql-5.7.32/bin/mysqldump   -x  unifiedportal > /home/t14-jurf/backup/sql/unifiedportal_$(date +%Y%m%d_%H%M%S).sql

7.crontab -e 设置定时任务,内容如下:1分钟执行一次
*/1 * * * * /usr/bin/sh    /home/t14-jurf/backup/bkunifiedportal.sh

二.mysql修改数据库配置,不重启mysql服务,实现数据库的备份脚本

2.1 查看mysql的安装目录

 [[email protected] bin]# which mysql
/usr/bin/mysql


2.2 在命令行中,携带密码,提示不安全

2.3 解决办法
https://blog.csdn.net/qq_41399976/article/details/94395946

2.3.1 修改数据库配置文件

有些在/etc/my.cnf,有些是在/etc/my.conf

1.修改配置文件,my.cnf

2.修改配置文件的内容:

3.在加命令中加入了–defaults-extra-file=/etc/my.cnf,可以不启服务器,使用我们配置的信息

4.编写脚本:

/usr/bin/mysqldump  --defaults-extra-file=/etc/my.cnf  unifiedportal > /home/backup/sql/unifiedportal_$(date +%Y%m%d_%H%M%S).sql

5.添加定时器:crontab -e

*/60  * * * * /usr/bin/sh /home/backup/bkunifiedportal.sh

6.查看结果