mysql建表时报错ERROR 1286 (42000) Unknown storage engine 'MyISAM'

时间:2022-09-21 08:52:12

一.问题描述
mysql版本为:mariadb 10.1.11。
 在建myisam表时,报错ERROR 1286 (42000): Unknown storage engine 'MyISAM'
dba@192.168.64.30:3306 >create table inno_myisam_test (`id` int(11) NOT NULL AUTO_INCREMENT,
    -> a int(2),b varchar(10),PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;
ERROR 1286 (42000): Unknown storage engine 'MyISAM'

二.问题分析
检查mysql与enforce相关的参数:
dba@192.168.64.30:3306>show variables like 'enforce%';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| enforce_storage_engine | InnoDB |
+------------------------+--------+
1 row in set (0.00 sec)
 enforce_storage_engine参数为InnoDB,表示所有表建立时的engine只能为Innodb。若有避开这一限制,还有什么办法?

三.问题解决
 在会话级更改参数为myisam,如下:
dba@192.168.64.30:3306>set session enforce_storage_engine='myisam';
Query OK, 0 rows affected (0.00 sec)

dba@192.168.64.30:3306>show variables like 'enforce%';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| enforce_storage_engine | MyISAM |
+------------------------+--------+
1 row in set (0.00 sec)

再建表,正常完成。
dba@192.168.64.30:3306: zeng 10:53:26>create table inno_myisam_test (`id` int(11) NOT NULL AUTO_INCREMENT,
    -> a int(2),b varchar(10),PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)

查看表定义:
dba@192.168.64.30:3306 zeng 10:53:51>show create table inno_myisam_test\G
*************************** 1. row ***************************
       Table: inno_myisam_test
Create Table: CREATE TABLE `inno_myisam_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `a` int(2) DEFAULT NULL,
  `b` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
登出mysql,重新登入:

对表插入数据正常:
dba@192.168.64.30:3306zeng 11:22:01>insert into inno_myisam_test(b) values('zengxuewen');
Query OK, 1 row affected (0.00 sec)

查询表的记录正常:
dba@192.168.64.30:3306zeng 11:23:01>select * from inno_myisam_test;
+----+------+------------+
| id | a    | b          |
+----+------+------------+
|  1 | NULL | zengxuewen |
+----+------+------------+
1 row in set (0.00 sec)

再查看会话级参数设置为InnoDB
dba@192.168.64.30:3306 : zeng 11:23:19>show variables like 'enforce%';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| enforce_storage_engine | InnoDB |
+------------------------+--------+
1 row in set (0.00 sec)

参考mariadb对参数的说明:
https://mariadb.com/kb/en/mariadb/server-system-variables/#enforce_storage_engine
enforce_storage_engine
Description: Force the use of a particular storage engine for new tables. Used to avoid unwanted creation of tables using another engine. For example, setting to InnoDB will prevent any MyISAM tables from being created. If another engine is specified in a CREATE TABLE statement, the outcome depends on whether the NO_ENGINE_SUBSTITUTION sql_mode has been set or not. If set (the default from MariaDB 10.1.7), the query will fail, while if not set, a warning will be returned and the table created according to the engine specified by this variable. The variable has a session scope, but is only modifiable by a user with the SUPER privilege.
Commandline: None
Scope: Session
Dynamic: Yes
Data Type: string
Default Value: none
Introduced: MariaDB 10.1.4

四.问题延伸
 若用导出导入方法(含mysqldump、mydumper/myloader)将含有myisam引擎的表迁移到强制innodb的mysql库中,会遇到同样报错问题,需要将表的定义sql文件更改为innodb:
 sed -i "s/)ENGINE=MyISAM;/)ENGINE=InnoDB;/g" *schema.sql