MySQL: Unknown table engine 'InnoDB' 问题解决

问题出现环境:

CentOS 6.10, MySQL 5.1.73

问题现象:

对表做select操作的时候,出现如下的错误:
1286 - Unknown table engine 'InnoDB’

问题出现经过:

在搭建一个既有系统的开发环境的时候,做了一下mysql的数据转存操作。
操作比较原始一些,就是把数据库文件ibdatan文件,ib_logfilen文件替换原来的文件。从客户端登上去看的时候,数据库里面的表都看不见了。尝试了一下select count(*) from tableA的操作,出现了“ Unknown table engine ‘InnoDB’”这样的错误。

错误原因

主要本次安装的MySQL为5.1,没有安装InnoDB。MySQL 5.5开始,将默认数据库存储引擎从MyISAM改为InnoDB,就不会有这些问题了。这之前的办法要另行安装。
登入mysql客户端,通过show engines命令,可以看到innodb的安装情况。

mysql> show engines;
+------------+---------+-----------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                   | Transactions | XA   | Savepoints |
+------------+---------+-----------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                     | NO           | NO   | NO         |
| CSV        | YES     | CSV storage engine                                        | NO           | NO   | NO         |
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance    | NO           | NO   | NO         |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables | NO           | NO   | NO         |
+------------+---------+-----------------------------------------------------------+--------------+------+------------+
4 rows in set (0.00 sec)

可以看到没有安装innodb。

当然,还有可能安装了之后没有启用。 下面的办法可以确认到安装并已经启用(YES),安装了没有启用(DISABLE),没有安装(NO) 。下表中可以看到没有安装。

mysql> SHOW VARIABLES LIKE 'have_%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| have_community_features | YES      |
| have_compress           | YES      |
| have_crypt              | YES      |
| have_csv                | YES      |
| have_dynamic_loading    | YES      |
| have_geometry           | YES      |
| have_innodb             | NO       |
| have_ndbcluster         | NO       |
| have_openssl            | DISABLED |
| have_partitioning       | YES      |
| have_query_cache        | YES      |
| have_rtree_keys         | YES      |
| have_ssl                | DISABLED |
| have_symlink            | DISABLED |
+-------------------------+----------+
14 rows in set (0.00 sec)
mysql> show plugins;
+------------+--------+----------------+---------+---------+
| Name       | Status | Type           | Library | License |
+------------+--------+----------------+---------+---------+
| binlog     | ACTIVE | STORAGE ENGINE | NULL    | GPL     |
| partition  | ACTIVE | STORAGE ENGINE | NULL    | GPL     |
| CSV        | ACTIVE | STORAGE ENGINE | NULL    | GPL     |
| MEMORY     | ACTIVE | STORAGE ENGINE | NULL    | GPL     |
| MyISAM     | ACTIVE | STORAGE ENGINE | NULL    | GPL     |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL    | GPL     |
+------------+--------+----------------+---------+---------+
6 rows in set (0.00 sec)

解决办法

主要是安装并启用InnoDB引擎。MySQL在5.1.38之后,InnoDB引擎是包含在一起的。主要还相对省事一些。

  1. 确定MySQL的plugin的安装目录
mysql> SHOW variables like "plugin_dir";
+---------------+-------------------------+
| Variable_name | Value                   |
+---------------+-------------------------+
| plugin_dir    | /usr/lib64/mysql/plugin |
+---------------+-------------------------+
1 row in set (0.00 sec)
  1. 查找是否已经存在innodb的so
    在plugin目录中确认是否有InnoDB的so文件。
    如果不存在 则从mysql的安装结构中将innodb so拷贝到plugins_dir目录中。
lrwxrwxrwx 1 root root      25 Apr  5 22:04 ha_innodb_plugin.so -> ha_innodb_plugin.so.0.0.0
lrwxrwxrwx 1 root root      25 Apr  5 22:04 ha_innodb_plugin.so.0 -> ha_innodb_plugin.so.0.0.0
-rwxr-xr-x 1 root root 1259136 Jan 27  2017 ha_innodb_plugin.so.0.0.0
  1. 安装InnoDB引擎
    编辑/etc/my.cnf文件,在[mysqld]下面添加如下内容:
ignore-builtin-innodb
plugin-load=innodb=ha_innodb_plugin.so
plugin_dir=/usr/lib64/mysql/plugin

注意根据1,2的调查结果更改上面相应的内容。

  1. 重启动mysql
service mysqld restart
#或者
/etc/init.d/mysqld restart 
  1. 检查是否正确启用InnoDB.
    使用上文中提到的 show engines / show plugins / SHOW VARIABLES LIKE 'have_%'等方法进行确认。
    如果还是不能正确安装,可以查看mysql错误日志。

  2. 确认错误日志
    如果是service方式启动的mysql服务器,通过下面的命令可以看到错误日志位置。

# ps -ef|grep mysqld
root      4454     1  0 23:09 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql     4565  4454  0 23:09 pts/0    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root      4590  3339  0 23:13 pts/0    00:00:00 grep mysqld

本次报告的是如下错误:日志文件尺寸不一致错误。因为我是用的覆盖的方式刷新数据库文件的。
MySQL: Unknown table engine 'InnoDB' 问题解决_第1张图片
决定删除数据库日志文件试试。

再次确认安装情况,OK!

mysql> show engines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
5 rows in set (0.00 sec)

mysql> show plugins
    -> ;
+------------+--------+----------------+---------------------+---------+
| Name       | Status | Type           | Library             | License |
+------------+--------+----------------+---------------------+---------+
| binlog     | ACTIVE | STORAGE ENGINE | NULL                | GPL     |
| partition  | ACTIVE | STORAGE ENGINE | NULL                | GPL     |
| CSV        | ACTIVE | STORAGE ENGINE | NULL                | GPL     |
| MEMORY     | ACTIVE | STORAGE ENGINE | NULL                | GPL     |
| MyISAM     | ACTIVE | STORAGE ENGINE | NULL                | GPL     |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL                | GPL     |
| InnoDB     | ACTIVE | STORAGE ENGINE | ha_innodb_plugin.so | GPL     |
+------------+--------+----------------+---------------------+---------+
7 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'have_%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| have_community_features | YES      |
| have_compress           | YES      |
| have_crypt              | YES      |
| have_csv                | YES      |
| have_dynamic_loading    | YES      |
| have_geometry           | YES      |
| have_innodb             | YES      |
| have_ndbcluster         | NO       |
| have_openssl            | DISABLED |
| have_partitioning       | YES      |
| have_query_cache        | YES      |
| have_rtree_keys         | YES      |
| have_ssl                | DISABLED |
| have_symlink            | DISABLED |
+-------------------------+----------+
14 rows in set (0.00 sec)

参考:MySQL5.1: 安装启用InnoDB引擎

你可能感兴趣的:(MySQL)