# 安装编译所需的依赖包(参照:https://answers.launchpad.net/mydumper/+faq/349)yum install -y glib2-devel mysql-devel zlib-devel pcre-devel# 将下载的mydumper-0.9.1.tar.gz上传到服务器/tmp目录下,root用户执行以下命令cd /tmptar -xvpf mydumper-0.9.1.tar.gzcd mydumper-0.9.1cmake .make && make install# ------------------------------------------------------------------------------------------Scanning dependencies of target mydumper[ 25%] Building C object CMakeFiles/mydumper.dir/mydumper.c.o[ 50%] Building C object CMakeFiles/mydumper.dir/server_detect.c.o[ 75%] Building C object CMakeFiles/mydumper.dir/g_unix_signal.c.oLinking C executable mydumper[ 75%] Built target mydumperScanning dependencies of target myloader[100%] Building C object CMakeFiles/myloader.dir/myloader.c.oLinking C executable myloader[100%] Built target myloader[ 75%] Built target mydumper[100%] Built target myloaderInstall the project...-- Install configuration: ""-- Installing: /usr/local/bin/mydumper-- Removed runtime path from "/usr/local/bin/mydumper"-- Installing: /usr/local/bin/myloader-- Removed runtime path from "/usr/local/bin/myloader"# ------------------------------------------------------------------------------------------
cd /u01/backup/lltotal 20-rw-rw-r-- 1 mysql mysql 84 Feb 2816:43 jfedu-schema-create.sql.gz-rw-rw-r-- 1 mysql mysql 174 Feb 2816:43 jfedu.t1-schema.sql.gz-rw-rw-r-- 1 mysql mysql 153 Feb 2816:43 jfedu.t1.sql.gz-rw-rw-r-- 1 mysql mysql 134 Feb 2816:43 metadata-rw-rw-r-- 1 mysql mysql 969 Feb 2816:43 mydumper.log# 查看备份日志,可以看出备份开启了多线程vi mydumper.log 2017-02-2816:43:47 [INFO] - Connected to a MySQL server2017-02-2816:43:47 [INFO] - Started dump at: 2017-02-2816:43:472017-02-2816:43:47 [INFO] - Written master status2017-02-2816:43:47 [INFO] - Thread 1 connected using MySQL connection ID 202017-02-2816:43:47 [INFO] - Thread 2 connected using MySQL connection ID 212017-02-2816:43:47 [INFO] - Thread 3 connected using MySQL connection ID 222017-02-2816:43:47 [INFO] - Thread 4 connected using MySQL connection ID 232017-02-2816:43:47 [INFO] - Non-InnoDB dump complete, unlocking tables2017-02-2816:43:47 [INFO] - Thread 1 dumping data for`jfedu`.`t1`2017-02-2816:43:47 [INFO] - Thread 2 dumping schema for`jfedu`.`t1`2017-02-2816:43:47 [INFO] - Thread 3 shutting down2017-02-2816:43:47 [INFO] - Thread 4 shutting down2017-02-2816:43:47 [INFO] - Thread 1 shutting down2017-02-2816:43:47 [INFO] - Thread 2 shutting down2017-02-2816:43:47 [INFO] - Finished dump at: 2017-02-2816:43:47
cd /u01/backup/xtrabackup_20170302lltotal 4165684-rw-r----- 1 mysql mysql 434 Mar 210:23 backup-my.cnf-rw-r----- 1 mysql mysql 33554432 Mar 210:32 ibdata1-rw-r----- 1 mysql mysql 16777216 Mar 210:23 ibdata2-rw-r----- 1 mysql mysql 1048576000 Mar 210:32 ib_logfile0-rw-r----- 1 mysql mysql 1048576000 Mar 210:31 ib_logfile1-rw-r----- 1 mysql mysql 1048576000 Mar 210:32 ib_logfile2-rw-r----- 1 mysql mysql 1048576000 Mar 210:32 ib_logfile3-rw-r----- 1 mysql mysql 12582912 Mar 210:32 ibtmp1drwxr-x--- 2 mysql mysql 4096 Mar 210:23 jfedudrwxr-x--- 2 mysql mysql 4096 Mar 210:23 lyjdrwxr-x--- 2 mysql mysql 4096 Mar 210:23 mysqldrwxr-x--- 2 mysql mysql 4096 Mar 210:23 performance_schema-rw-r----- 1 mysql mysql 20 Mar 210:23 xtrabackup_binlog_info-rw-rw-r-- 1 mysql mysql 20 Mar 210:31 xtrabackup_binlog_pos_innodb-rw-r----- 1 mysql mysql 113 Mar 210:31 xtrabackup_checkpoints # 检查点-rw-r----- 1 mysql mysql 572 Mar 210:23 xtrabackup_info-rw-r----- 1 mysql mysql 8388608 Mar 210:31 xtrabackup_logfile # log日志
备份后DML操作
insert into t1 select * from t1;......commit;select count(*) from t1;+----------+| count(*) |+----------+| 4096 |+----------+
cat /u01/backup/xtrabackup_20170302/xtrabackup_binlog_infobinlog.00002728905477mysqlbinlog --start-position=28905477 /u01/log/3306/binlog/binlog.000027 | mysql -uroot -p# 登陆数据库select count(*) from t1;+----------+| count(*) |+----------+| 4096 |+----------+
随堂笔记
mysqldump常用参数及使用示例
# 查看帮助mysqldump --help# 一些常用参数--single-transaction # 备份执行期间不阻塞DML,在生产环境备份时一定要加此参数-A, --all-databases Dump all the databases. This will be same as --databases # 备份所有的数据库--master-data[=#] # --master-data=2 或 --master-data=1,一般做主从的时侯需要加此参数--add-drop-database Add a DROP DATABASE before each create. # 备份中不生成创建数据库命令--add-drop-table Add a DROP TABLE before each create. # 备份中不生成创建表命令# 示例mysqldump -h127.0.0.1 --single-transaction -P3306 -A > /tmp/all_database.sqlmysqldump -h127.0.0.1 -P3306 -uroot -p --single-transaction --master-data=2 jfedu > /tmp/jfedu.sql--single-transaction# 创建一致性快照(only innodb)# 不能存在其他操作:ALTER TABLE, DROP TABLE, RENAME TABLE,TRUNCATE TABLE# 关闭--lock-tables--master-data1 : 输出change master命令2 : 注释输出change master命令--default-character-setbinarymysqldump -u[USER] -p[PASSWORD] -h [HOST] -P[PORT] --single-transaction --master-data=2 [DB]| pv -q -L 10M | gzip > /tmp/test.gzip备份 :mysqldump限流 :pv 压缩 :gzipgunzip -fc /tmp/test.gz | mysql -u[USER]-h[HOST] -P[PORT] DB 解压 : gunzip恢复 :mysql主备: change master
分析mysqldump的执行流程
# 打开general.log(通常是关闭的)show variables like '%gen%';+------------------+--------------------------+| Variable_name | Value |+------------------+--------------------------+| general_log | OFF || general_log_file | /u01/data/3306/mysql.log |+------------------+--------------------------+set global general_log=1;# 在新窗口追踪日志tail -f /u01/data/3306/mysql.log# 使用mysqldump备份jfedu数据库mysqldump -h127.0.0.1 -P3306 -uroot -p --single-transaction --master-data=2 jfedu > /tmp/jfedu.sql# 分析general.log,下面这段就是追踪到的mysqldump执行流程# ------------------------------------------------------------------------------------------17022815:39:299 Connect root@127.0.0.1 on 9 Query /*!40100 SET @@SQL_MODE='' */ 9 Query /*!40103 SET TIME_ZONE='+00:00' */ 9 Query FLUSH /*!40101 LOCAL */ TABLES 9 Query FLUSH TABLES WITH READ LOCK # 数据库只读锁定命令 9 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ 9 Query START TRANSACTION /*!40100 WITH CONSISTENT SNAPSHOT */ 9 Query SHOW VARIABLES LIKE 'gtid\_mode' 9 Query SHOW MASTER STATUS 9 Query UNLOCK TABLES 9 Query SELECT LOGFILE_GROUP_NAME, FILE_NAME, TOTAL_EXTENTS, INITIAL_SIZE, ENGINE, EXTRA FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'UNDO LOG' AND FILE_NAME IS NOT NULL AND LOGFILE_GROUP_NAME IN (SELECT DISTINCT LOGFILE_GROUP_NAME FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('jfedu'))) GROUP BY LOGFILE_GROUP_NAME, FILE_NAME, ENGINE ORDER BY LOGFILE_GROUP_NAME 9 Query SELECT DISTINCT TABLESPACE_NAME, FILE_NAME, LOGFILE_GROUP_NAME, EXTENT_SIZE, INITIAL_SIZE, ENGINE FROM INFORMATION_SCHEMA.FILES WHERE FILE_TYPE = 'DATAFILE' AND TABLESPACE_NAME IN (SELECT DISTINCT TABLESPACE_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA IN ('jfedu')) ORDER BY TABLESPACE_NAME, LOGFILE_GROUP_NAME 9 Query SHOW VARIABLES LIKE 'ndbinfo\_version' 9 Init DB jfedu 9 Query SAVEPOINT sp 9 Query show tables 9 Query show table status like 't1' 9 Query SET SQL_QUOTE_SHOW_CREATE=1 9 Query SET SESSION character_set_results = 'binary' 9 Query show create table `t1` 9 Query SET SESSION character_set_results = 'utf8' 9 Query show fields from `t1` 9 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `t1` 9 Query SET SESSION character_set_results = 'binary' 9 Query use `jfedu` 9 Query select @@collation_database 9 Query SHOW TRIGGERS LIKE 't1' 9 Query SET SESSION character_set_results = 'utf8' 9 Query ROLLBACK TO SAVEPOINT sp 9 Query RELEASE SAVEPOINT sp 9 Quit# ------------------------------------------------------------------------------------------# 关闭general.logset global general_log=0;
修改隔离级别
show variables like '%iso%';+---------------+-----------------+| Variable_name | Value |+---------------+-----------------+| tx_isolation | REPEATABLE-READ |+---------------+-----------------+set global tx_isolation='read-committed'; # 重启mysql失效set session tx_isolation='read-committed'; # 只在当前会话中有效# 修改my.cnf永久有效vi /etc/my.cnf#-------------------------------------transaction_isolation=read-committed#-------------------------------------show variables like '%iso%';+---------------+----------------+| Variable_name | Value |+---------------+----------------+| tx_isolation | READ-COMMITTED |+---------------+----------------+