欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mysql8.0?.ibd文件恢復表結構的實現(xiàn)

 更新時間:2024年10月17日 11:45:40   作者:子非花  
本文主要介紹了mysql8.0?.ibd文件恢復表結構的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

今天早上啟動了 phpstudy 軟件中的 MySQL 8.0 數(shù)據(jù)庫時,發(fā)現(xiàn)它啟動后約過了 3 秒就自行關閉了。接著又自動啟動然后再次關閉,似乎陷入了一個“開-閉-開”的循環(huán)。這導致我無法正常使用數(shù)據(jù)庫。在嘗試修改配置文件和端口號無果后,我決定卸載數(shù)據(jù)庫并重新安裝。根據(jù)之前的經(jīng)驗,我直接備份了 MySQL 安裝路徑下的 data 文件夾。之前使用的是 MySQL 5.7 版本,直接備份 data 文件夾下的文件可以保留數(shù)據(jù)庫的庫名、表名、字段、索引等信息。在重新安裝后,將之前備份的數(shù)據(jù)直接導入新的數(shù)據(jù)庫的 data 文件夾下也是順利的,使用像 Navicat 這樣的 MySQL 可視化工具可以看到表名和表結構。但是在 MySQL 8.0 及以上版本,情況就有所不同了,這導致后續(xù)出現(xiàn)了很多問題。

在 MySQL 8.0 中,數(shù)據(jù)庫文件存儲在 data 文件夾中,每個目錄代表一個數(shù)據(jù)庫。在每個數(shù)據(jù)庫目錄下,有多個 .ibd 文件,每個 .ibd 文件對應一張表,文件名即表名。.ibd 文件是 MySQL 的表數(shù)據(jù)文件,而在 MySQL 8 之前,表定義存儲在 .frm 后綴的文件中,表數(shù)據(jù)和表索引存儲在 .ibd 文件中。所有這些文件都在 MySQL 的 data 目錄下,即數(shù)據(jù)庫名所在的目錄中。閑來無事記錄一下。

查看mysql數(shù)據(jù)庫data文件夾路徑:

show variables like ‘%datadir%' 

.ibd文件是以二進制形式存儲的,這個時候想要查看存儲內容需要用mysql8自帶的 ibd2sdi命令 進行解析,首先查看是否能執(zhí)行這個命令,找不到命令或者不存在就需要配置系統(tǒng)環(huán)境變量了,起初我嘗試到phpstudy環(huán)境下的mysql8的安裝目錄找bin目錄,看看能否執(zhí)行ibd2sdi命令,不行于是我到官網(wǎng)下載一個mysql在本地安裝,接著配置環(huán)境變量。

ibd2sdi 命令:

G:\PhpStudy\phpstudy_pro\Extensions\MySQL8.0.12\data\ruibaiqiche>ibd2sdi --dump-file rb_position.txt rb_position.ibd

解析內容:

["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;"
            }
        ]
    }
}
}
]

后面想要恢復數(shù)據(jù)庫中的表結構的話,只能看解析后的字段名進行創(chuàng)建表了。解析后的.idb文件是一個json格式的數(shù)據(jù),再此基礎上key值基本不變,可以用代碼實現(xiàn)獲取表名和字段名再拼接成SQL語句,接著再拿到數(shù)據(jù)庫中進行創(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'];
        // 構建創(chuàng)建表的 SQL 查詢語句
        $sql = "CREATE TABLE $tableName (";

        foreach ($columns as $column) {
            $columnName = $column['name'];
            // 如果字段名不是 DB_TRX_ID 和 DB_ROLL_PTR,則構建該字段的 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']}'";

                // 構建列的注釋,如果注釋為空,不包含 COMMENT 部分
                $comment = isset($column['comment']) && !empty($column['comment']) ? "COMMENT '{$column['comment']}'" : '';

                // 構建列的 SQL
                $sql .= "$columnName $columnType $isNullable $default $comment, ";
            }
        }
        // 去除最后的逗號和空格
        $sql = rtrim($sql, ', ') . ");";
        echo $sql;
    }

到此這篇關于mysql8.0 .ibd文件恢復表結構的實現(xiàn)的文章就介紹到這了,更多相關mysql .ibd文件恢復表結構內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家! 

相關文章

最新評論