日期:2014-05-16  浏览次数:20534 次

innodb 共享表空间 转 独立表空间 详细说明

关于 MySQL 中的 Innodb 引擎就不多说了, 毕竟除了MyISAM 之外, 它的应用最广泛. Innodb 存储数据有两种方式: 共享表空间 和 独立表空间. 顾名思义, 共享表空间 就是把所有数据库数据放在一个或多个文件中( 这种方式的话,使用裸设备倒是很方便); 独立表空间 就是采用和MyISAM 相同的方式, 每个表拥有一个独立的数据文件( .idb )。
在服务器资源有限,单表数据不是特别多的情况下, 独立表空间明显比共享方式效率更高 . 但是MySQL 默认是共享表空间

1,查看一下是共享表空间,还是独立表空间

mysql> show variables like '%per_table%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | OFF   |
+-----------------------+-------+
1 row in set (0.00 sec)

如果是OFF,肯定不是独立表空间。如果是ON的话,也不一定是独立表空间。最直接的方法就是查看硬盘上的文件,独立表空间,每个表都对应了一个空间。

[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

/etc/init.d/mysqld stop  

?3,修改my.cnf的配置文件 [mysqld] 段添加

innodb-file-per-table=1  

?4,备份使用innodb引擎的数据库

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

?5,删除使用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: Database physically writes the file full: wait...
InnoDB: Progress in MB: 100 200 300 400 500
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

/etc/init.d/mysqld start 

?7,导入数据库

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

8,在查看一下,是转换好了

//进入到mysql后的查寻
mysql> show variables like '%per_table%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | ON   |
+-----------------------+-------+
1 row in set (0.00 sec)

//查看数据库目录下的文件
[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 xc