一、系统环境VMware suse12
二、mysql 版本 5.6
三、使用工具mysqldump
四、步骤
1.创建备份用户
CREATE USER 'lishu'@'localhost' IDENTIFIED BY 'lishu'
2.给用户赋权限,通常给selec权限就可以了,这里给全部。
Grant all privileges on *.* to lishu@'localhost'identified by 'lishu' with grant option;
权限生效
flush privileges;
3.修改my.cnf 配置文件
注:mysql5.6的配置文件默认地址是/usr/my.cnf, 但是mysqldump查找配置文件的路径依次顺序是 /etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf
所以这里需要copy 一份配置文件到这个下面。
cp /usr/my.cnf /etc/
在配置文件最后配置备份权限
[mysqldump]
user=lishu
password=lishu
4.编写shell脚本mysqldaiybak.sh, 修改权限 chmod 755 mysqldaiybak.sh
#!/bin/bash
#This is mysql mysqldaiybak scripts
#2016-01-26
database=www
databak_dir=/backup/mysql/data
logs_dir=/backup/mysql/logs
dumpbin=/usr/bin/mysqldump
host=192.168.100.155
DATE=$(date +%Y%m%d)
logFile=$logs_dir/backup$DATE.log
echo " " > $logFile
echo "-----------------------------------" >> $logFile
echo ${database}"backup start" >> $logFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $logFile
echo "-----------------------------------" >> $logFile
$dumpbin -h$host $database>$databak_dir/${database}_backup$DATE.sql
#删除10天前的文件
find $databak_dir -mtime +30 -type f -name '*.sql' | xargs rm -f
find $logs_dir -mtime +30 -type f -name '*.log' | xargs rm -f
echo "-----------------------------------" >> $logFile
echo ${database}" backup end" >> $logFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $logFile
echo "-----------------------------------" >> $logFile
5.创建定时执行,每天晚上2:30执行备份 crontab -e
添加以下内容
30 2 * * 1-6 /scripts/mysqldaiybak.sh
重启cron服务 cron restart
四、遇到的问题
1.生成的文件大小为0,里面都是空的。
原因:权限问题,由2、3两个问题引起
2.配置文件配了没有反应,报mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect
原因:mysqldump没有找到配置文件,
解决方法: 复制一份到/etc/下 cp /usr/my.cnf /etc/
3.用户权限问题,报mysqldump: Got error: 1045: Access denied for user 'lishu'@'localhost' (using password: YES) when trying to connect
原因:给栎树权限给的不对,给权限的时候后面没有跟密码,所有数据就不认了,GRANT ALL privileges ON *.* TO
'lishu'
@
'localhost'
;
解决方法:Grant all privileges on *.* to lishu@'%'identified by 'lishu' with grant option;
附:
my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[client]
#password = your_password
port = 3306
socket = /var/lib/mysql/mysql.sock
default_character_set=utf8
[mysqld]
server_id=155
port = 3306
lower_case_table_names=1
character_set_server=utf8
max_connections=1000
#一般日志
general_log=ON
general_log_file=/var/lib/mysql/general_log.log
#慢查询
slow_query_log=ON
slow_query_log_file=/var/lib/mysql/slow_log.log
long_query_time=6 #6秒
log_error = /var/lib/mysql/error_log.log
#设定是否将警告信息记录进错误日志。默认设定为1,表示启用;
#可以将其设置为0以禁用;而其值为大于1的数值时表示将新发起
#连接时产生的“失败的连接”和“拒绝访问”类的错误信息也记录进错误日志。
log_warnings=1
#二进制日志
log_bin = /var/lib/mysql/bin_log.log
#需要备份的数据库
binlog-do-db =tt
#若涉及及同步函数或者存储过程需要配置,否则主备会产生异常不能同步
log_bin_trust_function_creators=TRUE
# 在被回滚前,一个 InnoDB 的事务应该等待一个锁被批准多久.
# InnoDB 在其拥有的锁表中自动检测事务死锁并且回滚事务.
# 如果你使用 LOCK TABLES 指令, 或者在同样事务中使用除了 InnoDB 以外的其他事务安全的存储引擎
# 那么一个死锁可能发生而 InnoDB 无法注意到.
# 这种情况下这个 timeout 值对于解决这种问题就非常有帮助.
innodb_lock_wait_timeout = 120
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#备份用户
[mysqldump]
user=lishu
password=lishu