若是想要修改MySQL数据库的存储引擎,那么必需要了解这两种引擎,而且清楚的明白这两种引擎的区别。mysql
MySQL数据库支持两种常见的存储引擎:sql
InnoDB引擎:提供了对数据库ACID事务的支持,而且实现了SQL标准的四种隔离级别。该引擎还提供了行级锁和外键约束,它的设计目标是处理大容量数据库系统,它自己其实就是基于MySQL后台的完整数据库系统,MySQL运行时Innodb会在内存中创建缓冲池,用于缓冲数据和索引。可是该引擎不支持FULLTEXT类型的索引,并且它没有保存表的行数,当SELECT COUNT(*) FROM TABLE时须要扫描全表。当须要使用数据库事务时,该引擎固然是首选。因为锁的粒度更小,写操做不会锁定全表,因此在并发较高时,使用Innodb引擎会提高效率。可是使用行级锁也不是绝对的,若是在执行一个SQL语句时MySQL不能肯定要扫描的范围,InnoDB表一样会锁全表。数据库
MyIASM引擎:是MySQL默认的引擎,可是它没有提供对数据库事务的支持,也不支持行级锁和外键,所以当INSERT(插入)或UPDATE(更新)数据时即写操做须要锁定整个表,效率便会低一些。不过和Innodb不一样,MyIASM中存储了表的行数,因而SELECT COUNT(*) FROM TABLE时只须要直接读取已经保存好的值而不须要进行全表扫描。若是表的读操做远远多于写操做且不须要数据库事务的支持,那么MyIASM也是很好的选择。vim
主要区别:
一、MyIASM是非事务安全的,而InnoDB是事务安全的;
二、MyIASM锁的粒度是表级的,而InnoDB支持行级锁;
三、MyIASM支持全文类型索引,而InnoDB不支持全文索引;
四、MyIASM相对简单,效率上要优于InnoDB,小型应用能够考虑使用MyIASM;
五、MyIASM表保存成文件形式,跨平台使用更加方便。安全
应用场景:
一、MyIASM管理非事务表,提供高速存储和检索以及全文搜索能力,若是再应用中执行大量select操做,应该选择MyIASM
二、InnoDB用于事务处理,具备ACID事务支持等特性,若是在应用中执行大量insert和update操做,应该选择InnoDB并发
查看mysql数据库的引擎信息app
mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.00 sec) //InnoDB引擎是默认的存储引擎。
Support列, YES表示当前版本支持这个存储引擎, DEFAULT表示该引擎是默认的引擎。NO表示不支持该存储引擎。
查看系统变量default_storage_engine或storage_engine ide
mysql> show variables like '%storage_engine%'; +----------------------------+--------+ | Variable_name | Value | +----------------------------+--------+ | default_storage_engine | InnoDB | | default_tmp_storage_engine | InnoDB | | storage_engine | InnoDB | +----------------------------+--------+ 3 rows in set (0.00 sec)
default_storage_engine 表示永久表(permanent tables)的默认存储引擎。;
default_tmp_storage_engine 表示临时表的默认存储引擎;
storage_engine这个系统变量不推荐使用,它已经被系统变量default_storage_engine替代了。设计
修改MySQL数据库的默认存储引擎rest
[root@localhost ~]# vim /etc/my.cnf //编写mysql服务的主配置文件 ……………… //省略部份内容,添加以下内容 default-storage-engine=MyISAM [root@localhost ~]# systemctl restart mysqld //从新启动mysql服务 mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | MyISAM | DEFAULT | MyISAM storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | InnoDB | YES | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.00 sec) //再次查看发现MyISAM引擎已经成为默认引擎
在mysql数据库中直接修改存储默认引擎
mysql> set default_storage_engine=InnoDB; Query OK, 0 rows affected (0.00 sec) mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.00 sec) //再次查看发现InnoDB引擎已经成为默认引擎
注意,这个系统变量default_storage_engine是BOTH(全局和临时),并且能够动态修改。可是要注意,即便你修改了系统变量default_storage_engine,重启事后就会失效,若是你要永久修改,最好在my.cnf配置文件里面也设置default-storage-engine的值。
查看表使用的默认引擎
mysql> show create table t1 \G; *************************** 1. row *************************** Table: t1 Create Table: CREATE TABLE `t1` ( `id` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 1 row in set (0.00 sec //能够看出t1表默认使用的引擎是MyISAM。
修改表的默认存储引擎
mysql> ALTER TABLE t1 ENGINE = InnoDB; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table t1 \G; *************************** 1. row *************************** Table: t1 Create Table: CREATE TABLE `t1` ( `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) //能够看出t1表默认使用的引擎是InnoDB。
建立表的时候指定存储引擎
建立表的时候,若是要指定存储引擎,只须要设置参数ENGINE便可。
mysql> create table t2 (id int) engine=InnoDB; Query OK, 0 rows affected (0.00 sec) mysql> create table t3 (id int) engine=MyISAM; Query OK, 0 rows affected (0.00 sec)