MySQL安装
- 添加 MySQL Yum Repository
sudo yum -y localinstall http://mirrors.d.com/software/mysql/5.7/mysql57-community-release-el7-11.noarch.rpm
- 默认是最新版5.7版本的mysql,需要安装5.6版本,所以修改
/etc/yum.repos.d/mysql-community.repo
文件,修改对应版本的enabled字段,如下
[mysql57-community]
enabled=0
[mysql57-community]
enabled=1
- 安装
sudo yum -y install mysql-community-server
,启动sudo service mysqld start
,验证安装是否成功mysqladmin --version
- 设置默认root的用户密码
mysqladmin -u root password "123456";
,登录mysql -u root -p
- 添加用户及权限
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON TUTORIALS.* TO 'username'@'%' IDENTIFIED BY 'password';
,其中%表示所有客户端ip可连接,可指定IP可连接。也可以直接操作mysql数据库下的user表管理用户。
Mysql备份
- 开启
binlog
日志
vim /etc/my.cnf
log_bin=mysql-bin
binlog_format=row
- 常用操作
mysql> show master logs; #查看数据库所有日志文件。
mysql> flush logs; #将内存中log日志写磁盘,保存在当前binlog文件中,并产生一个新的binlog日志文件。
mysql> reset master; #删除所有二进制日志,在(mysql-bin.000001)开始记录。
mysqldump --all-databases > dump.sql
mysqldump --databases db1 db2 db3 > dump.sql
xtrabackup备份
- 1、安装
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
yum install percona-xtrabackup-24
yum -y install perl-DBD-MySQL.x86_64
- 2、创建备份用户
mysql> grant reload,lock tables,replication client on *.* to 'backup'@'localhost' identified by '123456';
mysql> grant process on *.* to backup@'localhost';
mysql> flush privileges;
-
3、全量物理备份
- 生成当前时间戳命名的备份文件
innobackupex --user=backup --password=123456 /data/mysql/backup
- 让备份文件准备,备份文件不能直接用于恢复,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务,此时数据文件处于不一致的状态。因此,我们现在就是要通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。
innobackupex --user=backup --password=123456 --apply-log /data/mysql/backup/2017-11-24_14-11-17
- 将mysql的数据目录链接到这个已经恢复的备份目录,或直接用copy-back拷贝至mysql数据目录
systemctl stop mysqld mv mysql mysql.old ln -s /data/mysql/backup/2017-11-24_14-11-17/ /var/lib/mysql chown mysql:mysql /var/lib/mysql chown -R mysql:mysql /data/mysql/backup/* systemctl start mysqld #重启mysql即可 恢复
4、增量备份
#全备
innobackupex --user=backup --password=123456 /data/mysql/backup
#第一次增量备份
innobackupex --user=backup --password=123456 --incremental /data/mysql/backup/inc --incremental-basedir=/data/mysql/backup/2017-11-24_14-11-17/
#第二次增量备份
innobackupex --user=backup --password=123456 --incremental /data/mysql/backup/inc --incremental-basedir=incremental /data/mysql/backup/inc/2017-11-24_14-18-35
#恢复
innobackupex --apply-log --redo-only ~/data/mysql/backup/full/2017-11-24_16-29-09/
## 注意每次恢复后设置 备份文件的mysql用户权限
innobackupex --apply-log --redo-only /data/mysql/backup/full/2017-01-20_10-52-43 --incremental-dir=/data/mysql/backup/inc/2017-01-20_11-04-31
shell 脚本
- mysql和xtrabackup安装
##### mysql 安装 默认5.7
yum -y install http://mirrors.d.com/software/mysql/5.7/mysql57-community-release-el7-11.noarch.rpm
#echo "#### install mysql:默认安装最新版5.7, 若需要之前版本需要修改/etc/yum.repos.d/mysql-community.repo 中对应版本的enabled值"
yum -y install mysql-community-server
mysqladmin --version
# deta目录 /var/lib/mysql
if [ ! -d /var/lib/mysql ]; then
echo install mysql failed
exit 0;
fi
service mysqld start
###### mysql 设置
#设置 root 密码 123456
mysqladmin -u root password "123456";
#允许所有客户端访问
##mysql -uroot -p123456 -D mysql -e "update user set Host='%' where User='root' and Host='127.0.0.1';"
# Grant all on *.* to 'root'@'%' identified by 'password' with grant option;
mysql -uroot -p123456 -e "Grant all on *.* to 'root'@'%' identified by 'password' with grant option;"
#创建备份用户 backup 123456
mysql -uroot -p123456 -e "grant reload,lock tables,replication client ,process on *.* to 'backup'@'localhost' identified by '123456';"
mysql -uroot -p123456 -e ' flush privileges;'
mysql -uroot -p123456 -D mysql -e 'select host,user from user;'
#xtrabackup安装
yum -y install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
yum -y install percona-xtrabackup-24
#yum -y install perl-DBD-MySQL.x86_64
- 备份脚本,定时执行
#!/bin/bash
# mysql备份文件,该文件每天执行,每周一全备,每天基于本周一的全备做增量备份
#全备最近四周,增备最近七天
# crontab -e
# 00 03 * * 1 /data/mysql/backup/backup.sh
backupfull_dir='/data/mysql/backup/full'
backupinc_dir='/data/mysql/backup/inc'
username='backup'
password='123456'
#需要在备份服务器上对备份文件定期删除处理
backup_host='10.0.12.59'
last4week_day=`date -d -4weeks '+%Y-%m-%d'`
week_day=`date +%w`
lastmon_day=`date -d "last Mon" +"%Y-%m-%d"`
today=`date +%Y-%m-%d`
# Every Monday full back
if [ $week_day == 1 ]; then
#周一删除四周前的基础备份,新增基础备份
rm -rf ${backupfull_dir}/${last4week_day}*
ulimit -n 65535 && innobackupex --user=$username --password=$password $backupfull_dir
#备份文件传到其他服务器
if [ $backup_host ]; then
new_full=`ls ${backupfull_dir} |grep $today`
#tar czvf ${new_full}.tar $new_full
cd $backupfull_dir && tar -cf - ${new_full} | pigz > ${new_full}.tgz
rsync -rl ${new_full}.tar root@10.0.12.59:/data/mysql/backup/full
rm -rf ${new_full}.tar
fi
fi
#每天增量备份,基于周一的全备
full_dir=`ls ${backupfull_dir} |grep $lastmon_day`
if [ $full_dir ]; then
ulimit -n 65535 && innobackupex --user=$username --password=$password --incremental $backupinc_dir --incremental-basedir=${backupfull_dir}/${full_dir}
#备份文件传到其他服务器
if [ $backup_host ]; then
new_inc=`ls ${backupinc_dir} |grep $today`
#tar czvf ${new_inc}.tar $new_inc
cd $backupinc_dir && tar -cf - ${new_inc} | pigz > ${new_inc}.tgz
rsync -rl ${new_inc}.tar root@10.0.12.59:/data/mysql/backup/inc
rm -rf ${new_inc}.tar
fi
fi
#删除 一周之前的增量备份
find $backupinc_dir -maxdepth 1 -mtime +7 | xargs rm -rf
mariadb安装
- yum源
#vim /etc/yum.repo.d/MariaDB.repo
# MariaDB 10.1 CentOS repository list - created 2016-12-01 03:36 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
#yum clean all
#yum makecache
安装
sudo yum -y install MariaDB-server MariaDB-client
使用
mysql_secure_installation
配置MariaDB服务
#由于一开始安装MariaDB数据库后, root用户默认密码为空, 所以只需要按Enter键
Enter current password for root (enter for none):
#是否设置root用户的新密码
Set root password? [Y/n] y
#录入新密码
New password:
#确认新密码
Re-enter new password:
#是否删除匿名用户,生产环境建议删除
Remove anonymous users? [Y/n] y
#是否禁止root远程登录,根据自己的需求选择
Disallow root login remotely? [Y/n] n
#是否删除test数据库
Remove test database and access to it? [Y/n] y
#是否重新加载权限表
Reload privilege tables now? [Y/n] y
用户配置,远程访问设置同mysql
MariaDB的日志在
/etc/logs/messages
[mysqld]
datadir=/data/mysql_test #修改默认路径
socket=/data/mysql_test/mysql.sock
#default-character-set=utf8
character_set_server=utf8
slow_query_log=on
slow_query_log_file=/data/mysql_test/slow_query_log.log
#long_query_time=2
mysql_install_db --user=mysql --basedir=/usr --datadir=/data/mysql_test/