安装 mysql 备份工具 Percona XtraBackup
1). 针对InnoDB表恢复
2). 开启了参数innodb_file_per_table 此参数修改InnoDB为独立表空间模式,每个数据库的每个表都会生成一个数据空间
MariaDB [(none)]> create user 'dbbackup'@'%' identified by '123456';
MariaDB [(none)]> grant reload,lock tables,replication client,create tablespace,process,super on *.* to dbbackup@'%' ;
MariaDB [(none)]> grant create,insert,select on percona_schema.* to dbbackup@'%' ;
MariaDB [(none)]> flush privileges;
MariaDB [cndba]> create table test(id int);
Query OK, 0 rows affected (0.02 sec)
MariaDB [cndba]> insert into test values(10),(20),(30);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [cndba]> select * from tb1;
ERROR 1146 (42S02): Table 'cndba.tb1' doesn't exist
MariaDB [cndba]> select * from test;
+------+
| id |
+------+
| 10 |
| 20 |
| 30 |
MariaDB [cndba]> show create table test/G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.03 sec)
如果忘记表结构可以安装工具:mysql-utilities,其中mysqlfrm可以读取表结构。
进行mysql-utilities安装:
yum install mysql-utilities -y
查看表结构
[[email protected] yum.repos.d]# mysqlfrm --diagnostic /backup/2018-01-12_00-10-40/cndba/test.frm
[[email protected] yum.repos.d]# innobackupex --host=localhost --user=dbbackup --password=123456 /backup/
[[email protected] backup]# innobackupex --apply-log --export /backup/2018-01-12_00-10-40/
MariaDB [cndba]> drop table test;
Query OK, 0 rows affected (0.04 sec)
MariaDB [cndba]> show create table test/G
ERROR 1146 (42S02): Table 'cndba.test' doesn't exist
MariaDB [cndba]> CREATE TABLE `test` ( `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
MariaDB [cndba]> ALTER TABLE test DISCARD TABLESPACE;
[[email protected] cndba]# cp /backup/2018-01-12_00-10-40/cndba/test.{ibd,exp,cfg} /data//mysql/cndba/
赋值权限
[[email protected] cndba]# chown -R mysql.mysql /data/mysql/cndba/
MariaDB [cndba]> alter table test import tablespace;
MariaDB [cndba]> select * from test;
+------+
| id |
+------+
| 10 |
| 20 |
| 30 |
+------+