一、数据库备份
脚本内容如下:
#backup address
backupdir=/usr/bin/mysql_data_bak
time=_`date +%Y_%m_%d_%H_%M_%S`
#mysql usrname
db_user=root
#mysql password
db_pass=服务器密码
#排除指定数据库test_mysql_log和test_mysql
mysql -e "show databases;" | grep -E -v "Database|information_schema|mysql|test_mysql_log" | xargs mysqldump --skip-lock-
tables --databases --compress | gzip > $backupdir/alldatabases$time.sql.gz
#排除指定数据库的表test_mysql_report1和test_mysql_report2
mysqldump --databases test_mysql --ignore-table=test_mysql.test_mysql_report1 --ignore-table=test_mysql_report2 --compress | gzip > $backupdir/test_mysql$time.sql.gz
#delete older than 3 days
find $backupdir -name alldatabases"*.sql.gz" -type f -mtime +3 -exec rm -rf {} \; > /dev/null 2>&1
find $backupdir -name test_mysql"*.sql.gz" -type f -mtime +3 -exec rm -rf {} \; > /dev/null 2>&1
二、数据库还原
脚本内容如下:
方式1:gunzip < test_mysql_2018_05_04_01_01_02.sql.gz | mysql -uroot -p test_mysql
注:建议-p后面不带密码,手动输入密码,否则会提示ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
方式2:通过source命令
use test_mysql
然后输入
source /usr/bin/mysql_data_bak/test_mysql_2018_05_04_01_01_02.sql
三、数据库压缩
1、修改字符集
先执行mysql> set global innodb_file_format=Barracuda;
然后在vi /etc/my.cnf 添加 innodb_file_format=Barracuda (不添加的话,下次重启后前面的设置不会生效)
2、查看字符集
mysql> show global variables like 'innodb_file_format%';
3、修改表压缩
alter table test_mysql_report1 row_format=COMPRESSED;
或 者建表
drop table test_mysql_report1;
create table test_mysql_report1(id int,a varchar(10)) ENGINE=InnoDB ROW_FORMAT=COMPRESSED
参考地址:https://blog.csdn.net/wll_1017/article/details/54860881