Mysql - 08 Storage Engine

I. Introduction Storage Engine


  • 1, the file system:
    • A mechanism to organize 1.1 operating system and accessing data.
    • 1.2 is a software file system.
  • 2, the file system type: ext2 3 4, xfs data
    • 2.1 No matter what file system, data content will not change
    • The difference is 2.2, the storage space, the size, speed.
  • 3, MySQL engine: can be understood as, MySQL's "File System", but more powerful.
  • 4, MySQL engine features: In addition to providing access to basic functions, there's more transaction capabilities, lock, backup and recovery, optimization, and special features

In short, the characteristics of the storage engine is designed to protect the safety and performance of the database structure.

Two .MySQL own storage engine type

MySQL storage engine provides the following:
01) InnoDB

02)MyISAM
03)MEMORY
04)ARCHIVE
05)FEDERATED
06)EXAMPLE
07)BLACKHOLE
08)MERGE
09)NDBCLUSTER
10)CSV


You can also use a third-party storage engine:
01) among MySQL pluggable storage engine type
02) MySQL two branches
03) perconaDB
04) MariaDB

#查看当前MySQL支持的存储引擎类型
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

#查看innodb的表有哪些
mysql> select table_schema,table_name,engine from information_schema.tables where engine='innodb';
+--------------+----------------------+--------+
| table_schema | table_name           | engine |
+--------------+----------------------+--------+
| mysql        | innodb_index_stats   | InnoDB |
| mysql        | innodb_table_stats   | InnoDB |
| mysql        | slave_master_info    | InnoDB |
| mysql        | slave_relay_log_info | InnoDB |
| mysql        | slave_worker_info    | InnoDB |
+--------------+----------------------+--------+
20 rows in set (0.03 sec)

#查看myisam的表有哪些
mysql> select table_schema,table_name,engine from information_schema.tables where engine='myisam';
+--------------------+---------------------------+--------+
| table_schema       | table_name                | engine |
+--------------------+---------------------------+--------+
| information_schema | COLUMNS                   | MyISAM |
| information_schema | EVENTS                    | MyISAM |
| mysql              | help_category             | MyISAM |
| mysql              | ndb_binlog_index          | MyISAM |
+--------------------+---------------------------+--------+
33 rows in set (0.01 sec)
  • 1 difference, innodb and the myisam

Physical differences:

#进入mysql目录
[root@db01~l]# cd /application/mysql/data/mysql
#myisam
[root@db01 mysql]# ll user.*
-rw-rw---- 1 mysql mysql 10684 Mar  6  2017 user.frm
-rw-rw---- 1 mysql mysql   960 Aug 14 01:15 user.MYD
-rw-rw---- 1 mysql mysql  2048 Aug 14 01:15 user.MYI
#进入word目录
[root@db01 world]# cd /application/mysql/data/world/
#innodb
[root@db01 world]# ll city.*
-rw-rw---- 1 mysql mysql   8710 Aug 14 16:23 city.frm
-rw-rw---- 1 mysql mysql 688128 Aug 14 16:23 city.ibd
  • About 2.innodb storage engine

After MySQL5.5 version, the default storage engine, provide high reliability and performance.

Benefits:
01) Transaction Security (comply with the ACID)
02) MVCC (Multi-Versioning Concurrency Control, multi-version concurrency control)
03) InnoDB row-level locking
04) Oracle-style consistent non-locking read
05) table data collation to optimize based on the primary key query
06) supports foreign key referential integrity constraints
07) for maximum performance on large data volume
08) will table queries and mix different storage engines
09) fast automatic recovery after a failure
10) for caching data in memory and index buffer pool

innodb core features

Key:
MVCC
Transaction
row-level locking
hot backup
Crash Safe Recovery (Auto Recovery)

  • 3. Review the storage engine

1) Use SELECT to confirm the session storage engine

#查询默认存储引擎
mysql> SELECT @@default_storage_engine;
+--------------------------+
| @@default_storage_engine |
+--------------------------+
| InnoDB                   |
+--------------------------+
1 row in set (0.00 sec)

2) Use SHOW sure each table storage engine

#查看表的存储引擎
mysql> show create table city\G
*************************** 1. row ***************************
...
) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> show table status like 'city'\G
*************************** 1. row ***************************
           Name: city
         Engine: InnoDB
...

3) Use INFORMATION_SCHEMA sure each table storage engine

#查看表的存储引擎
mysql> select table_name,engine from information_schema.tables where table_name='city' and table_schema='world'\G
*************************** 1. row ***************************
table_name: city
    engine: InnoDB
1 row in set (0.00 sec)
  • 4. A storage engine is provided

1) Set the server storage engine in the startup configuration file

#在配置文件的[mysqld]标签下添加/etc/my.cnf
[mysqld]
default-storage-engine=innodb

2) Use the SET command to set the current client session

#在MySQL命令行中临时设置
mysql>  SET @@storage_engine=myisam;
Query OK, 0 rows affected, 1 warning (0.00 sec)
#查看
mysql> select @@default_storage_engine;
+--------------------------+
| @@default_storage_engine |
+--------------------------+
| MyISAM                   |
+--------------------------+
1 row in set (0.00 sec)

(3) specified in the CREATE TABLE statement

#建表的时候指定存储引擎
 create table t (i INT) engine = <Storage Engine>;
#如:建test1表,指定存储引擎为myisam
mysql> create table test1(id int) engine=myisam;
Query OK, 0 rows affected (0.02 sec)

III. The real business case

Background of the project:

The company's existing architecture: Display a website, LAMT, MySQL5.1.77 version (MYISAM), 50M amount of data.

Small problems continue:

  • 1, table lock: in any row of Table class data modification operation, the entire table will be locked, the operation of the other rows are not simultaneously.
  • 2, does not support automatic fault recovery (CSR): There may be a problem of data corruption or loss when power outage occurs.

How to solve:

  • 1, suggestions will replace the existing MYISAM engines Innodb, will replace version 5.6.38
    • 1) If you use MYISAM will have a "small problem", performance, safety can not be guaranteed, use innodb can solve this problem.
    • 2) 5.1.77 version to support innodb engine is not perfect, 5.6.38 version of the innodb support very well.
  • 2, the implementation process and attentions

1) production of library data backup (mysqldump)

[root@db01 test]# mysql -uroot -p1
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| student4       |
| tlbb2          |
+----------------+
2 rows in set (0.00 sec)
#测试环境(先简单创几个存储引擎为myisam)
mysql> create table test1(id int) engine=myisam;
Query OK, 0 rows affected (0.02 sec)

mysql> create table test2(id int) engine=myisam;;
Query OK, 0 rows affected (0.00 sec)

mysql> create table test3(id int) engine=myisam;;
Query OK, 0 rows affected (0.01 sec)

2) Prepare a new database version 5.6.44

#先导库,准备一个环境
[root@db01 test]# mysqldump -uroot -p1 -B test >/tmp/full.sql

3) processing the backup data (fields with the engine)

#方法一
[root@db01 ~]# sed -i 's#ENGINE=MyISAM#ENGINE=InnoDB#g' /tmp/full.sql
#方法二
[root@db01 ~]# vim  /tmp/full.sql 
:%s#MyISAM#InnoDB#g

4) restore the backup modified to the new library

#方法一
[root@db01 test]# mysql -uroot -p123 -h 10.0.0.52 < /tmp/full.sql

5) application testing environment to connect the new library, test all functions

#连接
[root@db02 ~]# mysql -uroot -p123
#查看存储引擎
mysql> select @@default_storage_engine;
+--------------------------+
| @@default_storage_engine |
+--------------------------+
| InnoDB                   |
+--------------------------+
1 row in set (0.00 sec)

6) Stop the application, the new changes after the backup of the production database occurs, compensation to the new library
7) cut to a new database application

Project results:

Solve the "small problem"

Four .Innodb storage engine - table spaces Introduction


Shared table space concept emerged after the 5.5 version

Appearance management table space to store the database easier to expand

The default version 5.6 is a separate table space

  • 1, shared table space

1) View shared table space

#物理查看
[root@db01 ~]# ll /application/mysql/data/
-rw-rw---- 1 mysql mysql 79691776 Aug 14 16:23 ibdata1
#命令行查看
mysql> show variables like '%path%';
+-----------------------+------------------------------------+
| Variable_name         | Value                              |
+-----------------------+------------------------------------+
| innodb_data_file_path | ibdata1:76M;ibdata2:50M:autoextend |
| ssl_capath            |                                    |
| ssl_crlpath           |                                    |
+-----------------------+------------------------------------+
3 rows in set (0.00 sec)


#查看大小
[root@db01 data]# du -sh ibdata1 
76M ibdata1 

Version 5.6 default storage:
1. system data
2.undo
3. temporary table

Version 5.7 will undo default and temporary tables independent, version 5.6 can be independent, but need to be configured during initialization

2) Setting method

#编辑配置文件
[root@db01 ~]# vim /etc/my.cnf
[mysqld]
innodb_data_file_path=ibdata1:50M;ibdata2:50M:autoextend
  • 2, separate tablespaces

Table for user self-created, will adopt this model, each table is managed by a separate table space

1) See separate table space

#物理查看
[root@db01 ~]# ll /application/mysql/data/world/
-rw-rw---- 1 mysql mysql 688128 Aug 14 16:23 city.ibd
#命令行查看
mysql> show variables like '%per_table%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | ON    |
+-----------------------+-------+
1 row in set (0.01 sec)

Business Case

In the case where there is no backup data, a sudden power failure table corruption, open database.

1) copy of the library directory to the new library (first prepare a table structure as the original and the environment)

[root@db01 data]# tar zcf world1.tgz world/
#传到测试环境
[root@db01 data]# scp world1.tgz  172.16.1.52:/application/mysql/data/
#解压
[root@db02 data]# tar xf world1.tgz 

2) Start a new database

[root@db01 ~]# mysqld_safe --defaults-file=/data/3307/my.cnf &

3) Log in to view the database r

#查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| world              |
+--------------------+
5 rows in set (0.00 sec)

The data 4) look-up table

mysql> select * from city;
ERROR 1146 (42S02): Table 'world.city' doesn't exist (表不存在)

5) find the previous table structure to create a table in the new library

mysql> show create table world.city;
#删掉外键创建语句
 CREATE TABLE `city1` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` char(35) NOT NULL DEFAULT '',
  `CountryCode` char(3) NOT NULL DEFAULT '',
  `District` char(20) NOT NULL DEFAULT '',
  `Population` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID`),
  KEY `CountryCode` (`CountryCode`)
  #CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`)
) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1; 

6) Delete the table space file

#删除表空间文件
mysql> alter table city1 discard tablespace;
#在表的物理结构查看
[root@db02 world]# ll
total 1000
-rw-rw---- 1 mysql mysql   8710 Nov  4 10:26 city1.frm
-rw-rw---- 1 mysql mysql   8710 Nov  4 10:26 city.frm
-rw-rw---- 1 mysql mysql 589824 Nov  4 10:26 city.ibd

7) copies of the old table space file

[root@db02 world1]# cp city.ibd  city1.ibd

8) Authorization

[root@db01 world]# chown -R mysql.mysql *

9) into the tablespace

#查看,会报错
mysql> select * from city1;
#导入表空间
mysql> alter table city_new import tablespace;
#再次查看
mysql> select * from city1;

10) to change the table

#改表名
mysql> alter table city1 rename city;
#再次查看
mysql> show tables;
+------------------+
| Tables_in_world1 |
+------------------+
| city             |
| country          |
| countrylanguage  |
+------------------+
3 rows in set (0.00 sec)

Guess you like

Origin www.cnblogs.com/gongjingyun123--/p/11832412.html