Innodb使用`DATA DIRECTORY`注意事項

一. 簡介

Innodb使用DATA DIRECTORY='directory'選項容許建立默認數據目錄以外的獨立表空間php

  • 在指定目錄下建立數據庫名相關的子目錄,在子目錄內建立表空間文件(.ibd),在mysql默認數據目錄下建立table_name.isl文件(至關於軟鏈接,innodb不支持真實軟鏈接)
  • 必須開啓innodb_file_per_table,必須指定完整路徑名
  • 使用該功能,沒有使用--skip-symbolic-links選項啓動mysql服務,操做系統必須支持realpath()調用
  • data directory or index directory不能使用包含mysql數據目錄的路徑名
  • 須要提早規劃好數據目錄,ALTER TABLE沒有DATA DIRECTORY選項
  • 5.7.6支持普通表空間,能夠使用這種方式建立個別表到默認數據目錄外

二. innodb_flush_method引起的問題

root@localhost*5.6.28-log[(none)] >show variables like 'innodb_flush_method';
+---------------------+----------+
| Variable_name       | Value    |
+---------------------+----------+
| innodb_flush_method | O_DIRECT |
+---------------------+----------+
1 row in set (0.00 sec)

root@localhost*5.6.28-log[(none)] >use test
Database changed
root@localhost*5.6.28-log[test] >create table t1 (id int primary key,n varchar(10)) data directory='/data2/mysql56';
ERROR 1030 (HY000): Got error -1 from storage engine
root@localhost*5.6.28-log[test] >show errors;
+-------+------+----------------------------------+
| Level | Code | Message                          |
+-------+------+----------------------------------+
| Error | 1030 | Got error -1 from storage engine |
+-------+------+----------------------------------+
1 row in set (0.00 sec)

root@localhost*5.6.28-log[(none)] >use test
Database changed
root@localhost*5.6.28-log[test] >show variables like 'innodb_flush_method'; -- 去掉`o_direct`
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| innodb_flush_method |       |
+---------------------+-------+
1 row in set (0.00 sec)

root@localhost*5.6.28-log[test] >create table t1 (id int primary key,n varchar(10)) data directory='/data2/mysql56';
Query OK, 0 rows affected (0.00 sec)

5.6.24沒有問題,使用O_DIRECTmysql

root@localhost*5.6.24[(none)] >use test
Database changed
root@localhost*5.6.24[test] >show variables like 'innodb_flush_method';
+---------------------+----------+
| Variable_name       | Value    |
+---------------------+----------+
| innodb_flush_method | O_DIRECT |
+---------------------+----------+
1 row in set (0.00 sec)

root@localhost*5.6.24[test] >create table t1 (id int primary key,n varchar(10)) data directory='/data2/mysql56';
Query OK, 0 rows affected (0.10 sec)

root@localhost*5.6.24[test] >insert into t1 select 1,'a';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

root@localhost*5.6.24[test] >insert into t1 select 2,'b';
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

root@localhost*5.6.24[test] >commit;
Query OK, 0 rows affected (0.00 sec)

root@localhost*5.6.24[test] >select * from t1;
+----+------+
| id | n    |
+----+------+
|  1 | a    |
|  2 | b    |
+----+------+
2 rows in set (0.00 sec)

三. xb備份問題

徹底備份與恢復

進行備份sql

innobackupex --defaults-file=/etc/my.cnf xb_backup/

.160106 04:58:51 >> log scanned up to (1633545)

160106 04:58:51 Executing UNLOCK TABLES
160106 04:58:51 All tables unlocked
160106 04:58:51 Backup created in directory '/root/xb_backup//2016-01-06_04-58-46'
160106 04:58:51 [00] Writing backup-my.cnf
160106 04:58:51 [00]        ...done
160106 04:58:51 [00] Writing xtrabackup_info
160106 04:58:51 [00]        ...done
xtrabackup: Transaction log of lsn (1633545) to (1633545) was copied.
160106 04:58:51 completed OK!

還原數據庫

  • 應用日誌
innobackupex --apply-log --use-memory=512M xb_backup/2016-01-06_04-58-46/

InnoDB: New log files created, LSN=1633555
InnoDB: Highest supported file format is Barracuda.
InnoDB: 128 rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.6.24 started; log sequence number 1633804
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1633814
160106 05:06:52 completed OK!
  • 中止mysql服務、刪除mysql數據
[root@localhost ~]# mysqladmin shutdown
160106 05:08:50 mysqld_safe mysqld from pid file /data/mysqld.pid ended
[1]+  Done                    ./bin/mysqld_safe  (wd: /usr/local/mysql)
(wd now: ~)
[root@localhost ~]# rm /data/
auto.cnf            error.log           ibdata1             ib_logfile0         ib_logfile1         mysql/              performance_schema/ test/               
[root@localhost ~]# rm /data/* -rf
[root@localhost ~]# rm /data2/mysql56/test/ -rf
  • 拷貝備份數據、啓動mysql服務
[root@localhost ~]# cp -a xb_backup/2016-01-06_04-58-46/* /data/ 
[root@localhost ~]# chown mysql.mysql -R /data
[root@localhost ~]# ll /data
total 112660
-rw-r----- 1 mysql mysql      386 Jan  6 04:58 backup-my.cnf
-rw-r----- 1 mysql mysql 12582912 Jan  6 05:06 ibdata1
-rw-r--r-- 1 mysql mysql 50331648 Jan  6 05:06 ib_logfile0
-rw-r--r-- 1 mysql mysql 50331648 Jan  6 05:06 ib_logfile1
drwx------ 2 mysql mysql     4096 Jan  6 04:58 mysql
drwx------ 2 mysql mysql     4096 Jan  6 04:58 performance_schema
drwx------ 2 mysql mysql       45 Jan  6 04:58 test
-rw-r----- 1 mysql mysql      113 Jan  6 05:06 xtrabackup_checkpoints
-rw-r----- 1 mysql mysql      420 Jan  6 04:58 xtrabackup_info
-rw-r----- 1 mysql mysql  2097152 Jan  6 05:06 xtrabackup_logfile

[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# ./bin/mysqld_safe &
[1] 28470
[root@localhost mysql]# 160106 05:10:20 mysqld_safe Logging to '/data/error.log'.
160106 05:10:20 mysqld_safe Starting mysqld daemon with databases from /data

[root@localhost mysql]# 
[root@localhost mysql]# msyql
-bash: msyql: command not found
[root@localhost mysql]# mysql
root@localhost*5.6.24[(none)] >select * from test.t1;
+----+------+
| id | n    |
+----+------+
|  1 | a    |
|  2 | b    |
+----+------+
2 rows in set (0.00 sec)

root@localhost*5.6.24[(none)] >\! ls -lh /data/test
total 112K
-rw-r----- 1 mysql mysql 8.4K Jan  6 04:58 t1.frm
-rw-r----- 1 mysql mysql  96K Jan  6 04:58 t1.ibd
-rw-r----- 1 mysql mysql   26 Jan  6 04:58 t1.isl

root@localhost*5.6.24[(none)] >insert into test.t1 select 3,'c';
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

root@localhost*5.6.24[(none)] >select * from test.t1;
+----+------+
| id | n    |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
+----+------+
3 rows in set (0.00 sec)
  • 怎麼將恢復的表移到以前的數據目錄中?
[root@localhost mysql]# mysqladmin shutdown
160106 05:12:12 mysqld_safe mysqld from pid file /data/mysqld.pid ended
[1]+  Done                    ./bin/mysqld_safe
[root@localhost mysql]# mv /data/test/t1.
t1.frm  t1.ibd  t1.isl  
[root@localhost mysql]# mkdir /data2/mysql56/test
[root@localhost mysql]# chown mysql.mysql -R /data2/mysql56/
[root@localhost mysql]# mv /data/test/t1.ibd /data2/mysql56/test/
[root@localhost mysql]# ./bin/mysqld_safe &
[root@localhost mysql]# mysql
root@localhost*5.6.24[(none)] >select * from test.t1;
+----+------+
| id | n    |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
+----+------+
3 rows in set (0.01 sec)

root@localhost*5.6.24[(none)] >insert into test.t1 select 4,'d';
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

root@localhost*5.6.24[(none)] >select * from test.t1;
+----+------+
| id | n    |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
+----+------+
4 rows in set (0.00 sec)

root@localhost*5.6.24[(none)] >\! ls -l /data/test/
total 16
-rw-r----- 1 mysql mysql 8580 Jan  6 04:58 t1.frm
-rw-r----- 1 mysql mysql   26 Jan  6 04:58 t1.isl
root@localhost*5.6.24[(none)] >\! ls -l /data2/mysql56/test
total 96
-rw-r----- 1 mysql mysql 98304 Jan  6 05:13 t1.ibd
root@localhost*5.6.24[(none)] >
  • 查看錶空間是否能自動擴容
root@localhost*5.6.24[test] >delimiter //
root@localhost*5.6.24[test] >CREATE PROCEDURE `insert_t1` (IN count int)
BEGIN
	declare start int;
    declare i int;
SELECT 
    MAX(id)
FROM
    test.t1 INTO start;
    set i = start+1;
	while i < count do
		insert into test.t1 select i,repeat('a',10);
        set i = i+1;
    end while;
    
END //

root@localhost*5.6.24[test] >delimiter ;
root@localhost*5.6.24[test] >call insert_t1(200);
Query OK, 1 row affected (0.53 sec)

root@localhost*5.6.24[test] >\! ls -l /data2/mysql56/test
total 96
-rw-r----- 1 mysql mysql 98304 Jan  6 05:33 t1.ibd

4、總結

  • 建立innodb表使用data directory選項時,若是innodb_flush_method=O_DIRECT,將會遇到1030錯誤
  • 使用非默認數據目錄,使用xb備份工具能夠進行備份,恢復後數據在默認數據目錄
相關文章
相關標籤/搜索