A cold backup:
Cold backup parts, stop taking service , the tar command, directly to the package directory data mysql
mysqldump hellodb (library name)> hellodb.sql this way is not the way to create a backup library, so when the hellodb after deleting the database, you can put inside the library inside the table and data recovery, but can not restore the library;
mysqldump -B hellodb (library name)> hellodb.sql this way not only will the backup repository and data inside the table, and the process of creating the library will also back up;
mysqldump backup parts data library -A, backup parts mysql and automatically given sense data database , it is recommended for shooting. Mysql libraries and library will be custom creation, including data backup and down;
mysqldump backup parts data library , -R backup copies of all memory storage through the process and automatically set -defined function;
mysqldump -A full backup + mysqlbinlog composition View binary log before the current full backup flag, if it continues to operate the library will be used to make binary log incremental backups after a full backup
> Mysqldump -A> xx.sql (see case show master logs are mysql-bin.000001 8099)
> mysqlbinlog --start-position=8099 /data/bin/mysql-bin.000001 > /data/backup/inc.sql(这个操作会将标记位8099后的对数据库的操作记载下来,不会记载8099前的)
> 在恢复数据时,先恢复全局备份,然后再恢复增量备份;
InnoDB备份选项:
支持热备,可用温备但不建议用 --single-transaction 此选项Innodb中推荐使用,不适用MyISAM,此选项会开始备份前,先执行START TRANSACTION指令开启事务 此选项通过在单个事务中转储所有表来创建一致的快照。 仅适用于存储在支持多版本控制的存储引擎中的表(目 前只有InnoDB可以); 转储不保证与其他存储引擎保持一致。 在进行单事务转储时,要确保有效的转储文件(正 确的表内容和二进制日志位置),没有其他连接应该使用以下语句:ALTER TABLE,DROP TABLE,RENAME TABLE,TRUNCATE TABLE此选项和--lock-tables(此选项隐含提交挂起的事务)选项是相互排斥备份大型表 时,建议将--single-transaction选项和--quick结合一起使用
InnoDB建议备份策略 mysqldump –uroot –A –F –E –R --single-transaction --master-data=1 --flush-privileges -- triggers --default-character-set=utf8 --hex-blob > $BACKUP/fullbak_$BACKUP_TIME.sql
推荐的InnoDB备份选项 mysqldump -A --single-transaction --master-data=1 --hex-blob > /data/backup/fullbak_`date +%F`.sql
一个Innodb分库备份的脚本
#!/bin/bsah
for db in `mysql -e 'show databases'|grep -Evi 'information_schema|performance_schema|test|Database'`;do
mysqldump -B ${db} --single-transaction --master-data=2 |gzip > /data/backup/${db}_`date +%F`.sql.gz
done
MyISAM备份选项:
支持温备;不支持热备,所以必须先锁定要备份的库,而后启动备份操作 锁定方法如下: -x,--lock-all-tables:加全局读锁,锁定所有库的所有表,同时加--singletransaction或--lock-tables 选项会关闭此选项功能 注意:数据量大时,可能会导致长时间无法并发访问数据库 -l,--lock-tables:对于需要备份的每个数据库,在启动备份之前分别锁定其所有表,默认为on,--skip-locktables选项可禁用,对备份MyISAM的多个库,可能会造成数据不一致 注:以上选项对InnoDB表一样生效,实现温备,但不推荐使用
MyISAM建议备份策略 mysqldump –uroot –A –F –E –R –x --master-data=1 --flush-privileges --triggers --defaultcharacter-set=utf8 --hex-blob > $BACKUP/fullbak_$BACKUP_TIME.sql
mysql的主从
在主节点上开启二进制日志,从节点上开启中继日志,而且server_id的编号主从不能相同,
主例子:
从:
在主服务器上创建一个有权限同步的用户
GRANT REPLICATION SLAVE ON *.* TO repluser@'192.168.23.148' IDENTIFIED BY '123456';
From the server:
CHANGE MASTER TO MASTER_HOST='master2.mycompany.com', MASTER_USER='replication', MASTER_PASSWORD='bigs3cret', MASTER_PORT=3306, MASTER_LOG_FILE='master2-bin.001', MASTER_LOG_POS=4;
start slave;