需要备份mysql的主机 172.16.155.23
存放备份mysql的主机 172.16.155.22
目的:将155.23主机上mysql数据库每天全量备份一次,并且同步到远程主机中
1.安装备份工具
yum localinstall -y percona-xtrabackup-2.2.9-5067.el6.x86_64.rpm
2.编写备份数据库脚本:
vim /data/scripts/backdb.sh #!/bin/bash back_time=`date +%Y%m%d%H%M%S` backpath="/app/mysqlbak" cd $backpath && /bin/mkdir $back_time echo "start back ${back_time}" >> /app/mysqlbak/backdb.log # completely backup /usr/bin/innobackupex --defaults-file="/etc/my.cnf" --user=root --password=pass --socket=/tmp/mysql.sock $backpath/$back_time /usr/bin/scp -P 58422 -l 200000 -r $backpath/$back_time 172.16.155.22:/app/mysqlbak/ # delete 15 days ago backup /bin/find /app/mysqlbak -type d -mtime +15 -exec rm -rf {} \; echo "end back ${back_time}" >> /app/mysqlbak/backdb.log
chmod +x /data/scripts/backdb.sh
3.配置155.23免密码访问155.22
[[email protected] 20171127]# ssh-keygen -t rsa -P ‘‘
如果ssh端口非默认的22端口,使用如下命令:
ssh-copy-id -i /root/.ssh/id_rsa.pub "-p 58422 [email protected]"
计划任务,每天晚上1点1分备份一次:
1 1 * * * /bin/bash /data/scripts/bakdb.sh >/dev/null 2>&1
4.目标备份服务器配置,删除7天以前的备份:
mkdir -p /app/mysqlbak/
2 2 * * * /bin/find /app/mysqlbak/ -mtime +7 -exec rm -rf {} \;
数据库的临时方案:
1.配置两台服务器都启动mysql服务,其中一台是目前正在运行的服务器,另外一台作为备用
2.每天备份一次数据,然后传送到远程服务器,删除10天以前的备份
3.如果正在使用的mysql宕机则恢复最新数据到备用mysql,然后恢复最新数据到备用mysql
记得修改解析/etc/hosts文件
172.16.155.23 db.mysql.com
恢复的具体操作:
恢复准备工作:
目标服务器安装和原服务器一样的Mysql版本(见最下面的附录)
# 1.完整备份现有mysql的数据库
# completely backup
/usr/bin/innobackupex --defaults-file="/etc/my.cnf" --user=root --password=pass --socket=/tmp/mysql.sock /app/mysqlbak/back_time
# 2.传送备份数据到目标机器
scp -P 58422 -r 2017-11-27_16-52-50 172.16.155.22:/app/mysqlbak/
# 3.目标机器执行:
# 因此,我们现在就是要通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态
innobackupex --user=root --password=pass --defaults-file=/tmp/my.cnf --apply-log /app/mysqlbak/2017-11-27_16-52-50
# 4.关掉服务,迁移已有的数据目录
service mysqld stop
# cd /app/data
[[email protected] data]# mv mydata mydata_old
[[email protected] data]# mkdir mydata
[[email protected] data]# chown -R mysql.mysql mydata
# 5.执行innobackupex恢复命令
innobackupex --defaults-file=/etc/my.cnf --user=root --password=pass --copy-back /app/mysqlbak/2017-11-27_16-52-50
# 6.修改权限,启动服务
chown -R mysql.mysql /app/data/mydata
service mysqld start
安装数据库mysql5.6二进制版本
安装包mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
groupadd -r mysql
useradd -g mysql -r -s /sbin/nologin mysql
tar -zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv mysql-5.6.36-linux-glibc2.5-x86_64 mysql
cd mysql
chown -R root.mysql ./*
mkdir /app/data/mydata -p
chown -R mysql.mysql /app/data/mydata
mkdir /app/data/binlogs
chown -R mysql.mysql /app/data/binlogs
cd /usr/local/mysql
scripts/mysql_install_db --datadir=/app/data/mydata --user=mysql
将服务脚本拷贝到启动项
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --list mysqld
\cp my.cnf /etc/my.cnf
vim /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8
[mysqld]
port = 3306
innodb_file_per_table = 1
init-connect = ‘SET NAMES utf8mb4‘
character-set-server = utf8mb4
default_storage_engine = InnoDB
skip-name-resolve
skip-external-locking
datadir = /app/data/mydata
log-bin=/app/data/binlogs/master-bin
binlog_format=row
socket=/tmp/mysql.sock
interactive_timeout = 28800
wait_timeout = 28800
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
# 启动服务
service mysqld start
删除匿名用户,配置root密码
mysql> delete from user where host=‘::1‘;
mysql> delete from user where host=‘sdtw03‘;
mysql> delete from user where host=‘localhost‘ and user=‘‘;
mysql> update user set password=PASSWORD(‘pass‘) where user=‘root‘;
mysql> flush privileges;
添加yun具有所有权限
mysql>grant all privileges on *.* to [email protected] identified by ‘pass‘;
mysql>flush privileges;