天天看點

innodb 共享表空間 轉 獨立表空間 詳細說明

在伺服器資源有限,單表資料不是特别多的情況下, 獨立表空間明顯比共享方式效率更高 . 但是mysql 預設是共享表空間

1,檢視一下是共享表空間,還是獨立表空間

innodb 共享表空間 轉 獨立表空間 詳細說明

mysql> show variables like '%per_table%';  

+-----------------------+-------+  

| variable_name         | value |  

| innodb_file_per_table | off   |  

1 row in set (0.00 sec)  

如果是off,肯定不是獨立表空間。如果是on的話,也不一定是獨立表空間。最直接的方法就是檢視硬碟上的檔案,獨立表空間,每個表都對應了一個空間。

innodb 共享表空間 轉 獨立表空間 詳細說明

[root@localhost tg]# ll    

總用量 64    

-rw-rw----. 1 mysql mysql   65 12月 30 20:09 db.opt    

-rw-rw----. 1 mysql mysql 8658 12月 30 23:17 gb.frm    

-rw-rw----. 1 mysql mysql 8658 12月 30 23:19 qr.frm    

-rw-rw----. 1 mysql mysql 8658 12月 30 23:19 qy.frm    

-rw-rw----. 1 mysql mysql 8658 12月 30 23:19 tg.frm    

-rw-rw----. 1 mysql mysql 8658 12月 30 23:19 xcy.frm    

tg是一個資料庫名,裡面的都是innodb的。像這種情況就是共享表空間。

2,停掉mysql

innodb 共享表空間 轉 獨立表空間 詳細說明

/etc/init.d/mysqld stop    

 3,修改my.cnf的配置檔案 [mysqld] 段添加

innodb 共享表空間 轉 獨立表空間 詳細說明

innodb-file-per-table=1    

 4,備份使用innodb引擎的資料庫

innodb 共享表空間 轉 獨立表空間 詳細說明

mysqldump -u tg -p tg >/home/6fan/tg.sql;    

 5,删除使用innodb的資料庫,以及日志檔案

innodb 共享表空間 轉 獨立表空間 詳細說明

cd /var/lib/mysql    //資料庫檔案位置  

rm -f ib*           //删除日志和空間  

rm -rf tg           //删除使用innodb引擎的資料庫檔案夾  

如果不删除使用innodb的資料庫檔案夾,啟動不了innodb引擎,我檢視了一下錯誤日志。如下

111231 20:54:44 innodb: log file ./ib_logfile0 did not exist: new to be created

innodb: setting log file ./ib_logfile0 size to 512 mb

innodb: database physically writes the file full: wait...

innodb: progress in mb: 100 200 300 400 500

111231 20:54:50 innodb: log file ./ib_logfile1 did not exist: new to be created

innodb: setting log file ./ib_logfile1 size to 512 mb

innodb: cannot initialize created log files because

innodb: data files are corrupt, or new data files were

innodb: created when the database was started previous

innodb: time but the database was not shut down

innodb: normally after that.

111231 20:54:55 [error] plugin 'innodb' init function returned error.

111231 20:54:55 [error] plugin 'innodb' registration as a storage engine failed.

111231 20:54:55 [note] event scheduler: loaded 0 events

6,啟動mysql

innodb 共享表空間 轉 獨立表空間 詳細說明

/etc/init.d/mysqld start   

 7,導入資料庫

innodb 共享表空間 轉 獨立表空間 詳細說明

mysql -u root -p  < /home/6fan/tg.sql  

8,在檢視一下,是轉換好了

innodb 共享表空間 轉 獨立表空間 詳細說明

//進入到mysql後的查尋  

| innodb_file_per_table | on   |  

//檢視資料庫目錄下的檔案  

[root@localhost tg]# ll  

總用量 544  

-rw-rw----. 1 mysql mysql    65 12月 31 22:48 db.opt  

-rw-rw----. 1 mysql mysql  8658 12月 31 22:49 gb.frm  

-rw-rw----. 1 mysql mysql 98304 12月 31 22:49 gb.ibd  

-rw-rw----. 1 mysql mysql  8658 12月 31 22:49 qr.frm  

-rw-rw----. 1 mysql mysql 98304 12月 31 22:49 qr.ibd  

-rw-rw----. 1 mysql mysql  8658 12月 31 22:49 qy.frm  

-rw-rw----. 1 mysql mysql 98304 12月 31 22:49 qy.ibd  

-rw-rw----. 1 mysql mysql  8658 12月 31 22:49 tg.frm  

-rw-rw----. 1 mysql mysql 98304 12月 31 22:49 tg.ibd  

-rw-rw----. 1 mysql mysql  8658 12月 31 22:49 xcy.frm  

-rw-rw----. 1 mysql mysql 98304 12月 31 22:49 xcy.ibd  

 從這裡可以看出,每一張表都對應有一個.ibd的檔案,根共享表空間是不一樣的。到這兒就完全配置好了。