mysql8.0?.ibd文件恢復(fù)表結(jié)構(gòu)的實(shí)現(xiàn)
今天早上啟動了 phpstudy 軟件中的 MySQL 8.0 數(shù)據(jù)庫時(shí),發(fā)現(xiàn)它啟動后約過了 3 秒就自行關(guān)閉了。接著又自動啟動然后再次關(guān)閉,似乎陷入了一個(gè)“開-閉-開”的循環(huán)。這導(dǎo)致我無法正常使用數(shù)據(jù)庫。在嘗試修改配置文件和端口號無果后,我決定卸載數(shù)據(jù)庫并重新安裝。根據(jù)之前的經(jīng)驗(yàn),我直接備份了 MySQL 安裝路徑下的 data 文件夾。之前使用的是 MySQL 5.7 版本,直接備份 data 文件夾下的文件可以保留數(shù)據(jù)庫的庫名、表名、字段、索引等信息。在重新安裝后,將之前備份的數(shù)據(jù)直接導(dǎo)入新的數(shù)據(jù)庫的 data 文件夾下也是順利的,使用像 Navicat 這樣的 MySQL 可視化工具可以看到表名和表結(jié)構(gòu)。但是在 MySQL 8.0 及以上版本,情況就有所不同了,這導(dǎo)致后續(xù)出現(xiàn)了很多問題。
在 MySQL 8.0 中,數(shù)據(jù)庫文件存儲在 data 文件夾中,每個(gè)目錄代表一個(gè)數(shù)據(jù)庫。在每個(gè)數(shù)據(jù)庫目錄下,有多個(gè) .ibd 文件,每個(gè) .ibd 文件對應(yīng)一張表,文件名即表名。.ibd 文件是 MySQL 的表數(shù)據(jù)文件,而在 MySQL 8 之前,表定義存儲在 .frm 后綴的文件中,表數(shù)據(jù)和表索引存儲在 .ibd 文件中。所有這些文件都在 MySQL 的 data 目錄下,即數(shù)據(jù)庫名所在的目錄中。閑來無事記錄一下。
查看mysql數(shù)據(jù)庫data文件夾路徑:
show variables like ‘%datadir%'
.ibd文件是以二進(jìn)制形式存儲的,這個(gè)時(shí)候想要查看存儲內(nèi)容需要用mysql8自帶的 ibd2sdi命令 進(jìn)行解析,首先查看是否能執(zhí)行這個(gè)命令,找不到命令或者不存在就需要配置系統(tǒng)環(huán)境變量了,起初我嘗試到phpstudy環(huán)境下的mysql8的安裝目錄找bin目錄,看看能否執(zhí)行ibd2sdi命令,不行于是我到官網(wǎng)下載一個(gè)mysql在本地安裝,接著配置環(huán)境變量。
ibd2sdi 命令:
G:\PhpStudy\phpstudy_pro\Extensions\MySQL8.0.12\data\ruibaiqiche>ibd2sdi --dump-file rb_position.txt rb_position.ibd
解析內(nèi)容:
["ibd2sdi" , { "type": 1, "id": 1929, "object": { "mysqld_version_id": 80012, "dd_version": 80012, "sdi_version": 1, "dd_object_type": "Table", "dd_object": { "name": "rb_position", "mysql_version_id": 80012, "created": 20231214074915, "last_altered": 20231214074915, "hidden": 1, "options": "avg_row_length=0;key_block_size=0;keys_disabled=0;pack_record=1;stats_auto_recalc=0;stats_sample_pages=0;", "columns": [ { "name": "id", "type": 4, "is_nullable": false, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": true, "is_virtual": false, "hidden": 1, "ordinal_position": 1, "char_length": 11, "numeric_precision": 10, "numeric_scale": 0, "numeric_scale_null": false, "datetime_precision": 0, "datetime_precision_null": 1, "has_no_default": false, "default_value_null": false, "srs_id_null": true, "srs_id": 0, "default_value": "AAAAAA==", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "ID", "generation_expression": "", "generation_expression_utf8": "", "options": "interval_count=0;", "se_private_data": "table_id=2219;", "column_key": 2, "column_type_utf8": "int(11)", "elements": [], "collation_id": 8, "is_explicit_collation": false }, { "name": "position", "type": 16, "is_nullable": true, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 1, "ordinal_position": 2, "char_length": 400, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 1, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "職位名稱", "generation_expression": "", "generation_expression_utf8": "", "options": "interval_count=0;", "se_private_data": "table_id=2219;", "column_key": 1, "column_type_utf8": "varchar(100)", "elements": [], "collation_id": 45, "is_explicit_collation": false }, { "name": "createtime", "type": 19, "is_nullable": true, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 1, "ordinal_position": 3, "char_length": 19, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 0, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "interval_count=0;", "se_private_data": "table_id=2219;", "column_key": 1, "column_type_utf8": "datetime", "elements": [], "collation_id": 8, "is_explicit_collation": false }, { "name": "updatetime", "type": 19, "is_nullable": true, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 1, "ordinal_position": 4, "char_length": 19, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 0, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "interval_count=0;", "se_private_data": "table_id=2219;", "column_key": 1, "column_type_utf8": "datetime", "elements": [], "collation_id": 8, "is_explicit_collation": false }, { "name": "deletetime", "type": 19, "is_nullable": true, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 1, "ordinal_position": 5, "char_length": 19, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 0, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "interval_count=0;", "se_private_data": "table_id=2219;", "column_key": 1, "column_type_utf8": "datetime", "elements": [], "collation_id": 8, "is_explicit_collation": false }, { "name": "DB_TRX_ID", "type": 10, "is_nullable": false, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 2, "ordinal_position": 6, "char_length": 6, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 1, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "", "se_private_data": "table_id=2219;", "column_key": 1, "column_type_utf8": "", "elements": [], "collation_id": 63, "is_explicit_collation": false }, { "name": "DB_ROLL_PTR", "type": 9, "is_nullable": false, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 2, "ordinal_position": 7, "char_length": 7, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 1, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "", "se_private_data": "table_id=2219;", "column_key": 1, "column_type_utf8": "", "elements": [], "collation_id": 63, "is_explicit_collation": false } ], "schema_ref": "ruibaiqiche", "se_private_id": 2219, "engine": "InnoDB", "comment": "職位表", "se_private_data": "autoinc=0;version=2;", "row_format": 2, "partition_type": 0, "partition_expression": "", "partition_expression_utf8": "", "default_partitioning": 0, "subpartition_type": 0, "subpartition_expression": "", "subpartition_expression_utf8": "", "default_subpartitioning": 0, "indexes": [ { "name": "PRIMARY", "hidden": false, "is_generated": false, "ordinal_position": 1, "comment": "", "options": "flags=0;", "se_private_data": "id=1732;root=4;space_id=1162;table_id=2219;trx_id=153333;", "type": 1, "algorithm": 2, "is_algorithm_explicit": false, "is_visible": true, "engine": "InnoDB", "elements": [ { "ordinal_position": 1, "length": 4, "order": 2, "column_opx": 0 }, { "ordinal_position": 2, "length": 4294967295, "order": 2, "column_opx": 5 }, { "ordinal_position": 3, "length": 4294967295, "order": 2, "column_opx": 6 }, { "ordinal_position": 4, "length": 4294967295, "order": 2, "column_opx": 1 }, { "ordinal_position": 5, "length": 4294967295, "order": 2, "column_opx": 2 }, { "ordinal_position": 6, "length": 4294967295, "order": 2, "column_opx": 3 }, { "ordinal_position": 7, "length": 4294967295, "order": 2, "column_opx": 4 } ], "tablespace_ref": "ruibaiqiche/rb_position" } ], "foreign_keys": [], "partitions": [], "collation_id": 45 } } } , { "type": 2, "id": 1167, "object": { "mysqld_version_id": 80012, "dd_version": 80012, "sdi_version": 1, "dd_object_type": "Tablespace", "dd_object": { "name": "ruibaiqiche/rb_position", "comment": "", "options": "", "se_private_data": "flags=16417;id=1162;server_version=80012;space_version=1;", "engine": "InnoDB", "files": [ { "ordinal_position": 1, "filename": ".\\ruibaiqiche\\rb_position.ibd", "se_private_data": "id=1162;" } ] } } } ]
后面想要恢復(fù)數(shù)據(jù)庫中的表結(jié)構(gòu)的話,只能看解析后的字段名進(jìn)行創(chuàng)建表了。解析后的.idb文件是一個(gè)json格式的數(shù)據(jù),再此基礎(chǔ)上key值基本不變,可以用代碼實(shí)現(xiàn)獲取表名和字段名再拼接成SQL語句,接著再拿到數(shù)據(jù)庫中進(jìn)行創(chuàng)建表。
function test(){ // 獲取傳入的json //$data = $this->request->param(); // 替換為你的 JSON 數(shù)據(jù) $jsonData = 'YOUR_JSON_DATA_HERE'; // 解析 JSON 數(shù)據(jù) $data = json_decode($jsonData, true); // 提取表名和字段信息 $tableName = $data[1]['object']['dd_object']['name']; $columns = $data[1]['object']['dd_object']['columns']; // 構(gòu)建創(chuàng)建表的 SQL 查詢語句 $sql = "CREATE TABLE $tableName ("; foreach ($columns as $column) { $columnName = $column['name']; // 如果字段名不是 DB_TRX_ID 和 DB_ROLL_PTR,則構(gòu)建該字段的 SQL if ($columnName !== 'DB_TRX_ID' && $columnName !== 'DB_ROLL_PTR' && $columnName !== 'DB_ROW_ID') { $columnType = $column['column_type_utf8']; $isNullable = $column['is_nullable'] ? 'NULL' : 'NOT NULL'; $default = ($column['default_value_null'] || $column['default_value_utf8_null']) ? '' : "DEFAULT '{$column['default_value']}'"; // 構(gòu)建列的注釋,如果注釋為空,不包含 COMMENT 部分 $comment = isset($column['comment']) && !empty($column['comment']) ? "COMMENT '{$column['comment']}'" : ''; // 構(gòu)建列的 SQL $sql .= "$columnName $columnType $isNullable $default $comment, "; } } // 去除最后的逗號和空格 $sql = rtrim($sql, ', ') . ");"; echo $sql; }
到此這篇關(guān)于mysql8.0 .ibd文件恢復(fù)表結(jié)構(gòu)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)mysql .ibd文件恢復(fù)表結(jié)構(gòu)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL雙層游標(biāo)嵌套循環(huán)實(shí)現(xiàn)方法
要實(shí)現(xiàn)逐行獲取數(shù)據(jù),需要用到MySQL中的游標(biāo),一個(gè)游標(biāo)相當(dāng)于一個(gè)for循環(huán),這里需要用到2個(gè)游標(biāo),如何在MySQL中實(shí)現(xiàn)游標(biāo)雙層循環(huán)呢,下面小編給大家分享MySQL雙層游標(biāo)嵌套循環(huán)方法,感興趣的朋友跟隨小編一起看看吧2024-05-05Mysql中文漢字轉(zhuǎn)拼音的實(shí)現(xiàn)(每個(gè)漢字轉(zhuǎn)換全拼)
這篇文章主要介紹了Mysql中文漢字轉(zhuǎn)拼音的實(shí)現(xiàn),并且每個(gè)漢字會轉(zhuǎn)換全拼,使用Mysql自定義函數(shù)實(shí)現(xiàn),需要的朋友可以參考下2014-06-06mysql分組取每組前幾條記錄(排名) 附group by與order by的研究
mysql分組取每組前幾條記錄(排名) 附group by與order by的研究,需要的朋友可以參考下2012-10-10mysql load data infile 的用法(40w數(shù)據(jù) 用了
測試數(shù)據(jù)的時(shí)候,生成txt文件應(yīng)該快點(diǎn),再用這種方式導(dǎo)入到mysql 速度上快點(diǎn),40w數(shù)據(jù) 用了3-5秒導(dǎo)進(jìn)mysql2013-01-01MySQL MyISAM 優(yōu)化設(shè)置點(diǎn)滴
MyISAM類型的表強(qiáng)調(diào)的是性能,其執(zhí)行數(shù)度比InnoDB類型更快, 只是不提供事務(wù)支持.大部分項(xiàng)目是讀多寫少的項(xiàng)目,而Myisam的讀性能是比innodb強(qiáng)不少的2016-05-05MySQL Limit性能優(yōu)化及分頁數(shù)據(jù)性能優(yōu)化詳解
今天小編就為大家分享一篇關(guān)于MySQL Limit性能優(yōu)化及分頁數(shù)據(jù)性能優(yōu)化詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03與MSSQL對比學(xué)習(xí)MYSQL的心得(五)--運(yùn)算符
MYSQL中的運(yùn)算符很多,這一節(jié)主要講MYSQL中有的,而SQLSERVER沒有的運(yùn)算符2014-06-06