Linux系统时间不对导致mysql初始化失败:Data Dictionary initialization failed.(数据字典版本验证失败)

文章目录

问题描述

mysql数据初始化失败,发现系统时间是1970年,我将系统时间改成2025年,就能初始化成功了。

shell 复制代码
1970-01-01T01:44:08.999267Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33)  MySQL Community Server - GPL.
1970-01-01 09:45:01+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
1970-01-01 09:45:01+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
1970-01-01 09:45:01+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
mysqld: [Warning] World-writable config file '/etc/mysql/conf.d/mysqld.cnf' is ignored.
1970-01-01T01:45:03.045951Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
1970-01-01T01:45:03.049185Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 1
1970-01-01T01:45:03.053492Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
1970-01-01T01:45:03.053523Z 0 [Warning] [MY-013244] [Server] --collation-server: 'utf8mb3_general_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
1970-01-01T01:45:03.074083Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
1970-01-01T01:45:03.338899Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
1970-01-01T01:45:03.348297Z 1 [ERROR] [MY-011096] [Server] No data dictionary version number found.
1970-01-01T01:45:03.348883Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
1970-01-01T01:45:03.349101Z 0 [ERROR] [MY-010119] [Server] Aborting
1970-01-01T01:45:03.949729Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33)  MySQL Community Server - GPL.
1970-01-01 09:46:04+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
1970-01-01 09:46:05+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
1970-01-01 09:46:05+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
mysqld: [Warning] World-writable config file '/etc/mysql/conf.d/mysqld.cnf' is ignored.
1970-01-01T01:46:06.744074Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
1970-01-01T01:46:06.747196Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 1
1970-01-01T01:46:06.751486Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
1970-01-01T01:46:06.751518Z 0 [Warning] [MY-013244] [Server] --collation-server: 'utf8mb3_general_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
1970-01-01T01:46:06.771748Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
1970-01-01T01:46:07.034656Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
1970-01-01T01:46:07.044106Z 1 [ERROR] [MY-011096] [Server] No data dictionary version number found.
1970-01-01T01:46:07.044737Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
1970-01-01T01:46:07.044811Z 0 [ERROR] [MY-010119] [Server] Aborting
1970-01-01T01:46:07.644094Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33)  MySQL Community Server - GPL.
python 复制代码
1970-01-01T01:46:06.771748Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
1970-01-01T01:46:07.034656Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
1970-01-01T01:46:07.044106Z 1 [ERROR] [MY-011096] [Server] No data dictionary version number found.
1970-01-01T01:46:07.044737Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
1970-01-01T01:46:07.044811Z 0 [ERROR] [MY-010119] [Server] Aborting

分析

根据你提供的日志和描述,系统时间错误确实可能导致 MySQL 初始化失败。以下是详细分析和解决方案:


问题原因分析

  1. 系统时间错误

    • 日志中显示的时间戳为 1970-01-01 (Unix 时间起点),表明系统时间未正确配置。MySQL 在初始化时会依赖系统时间进行以下操作:
      • 数据字典版本验证 :MySQL 8.0 引入了新的数据字典(Data Dictionary),其元数据包含版本信息。如果系统时间错误(如设置为 1970 年),MySQL 可能无法正确解析这些信息,导致 No data dictionary version number found 错误。
      • 文件时间戳一致性:MySQL 初始化时会检查数据目录中文件的时间戳。若系统时间错误,可能导致 MySQL 认为文件时间早于预期,从而触发初始化失败。
  2. 配置或数据损坏

    • 如果系统时间错误导致 MySQL 无法正确读取数据目录中的文件(如 ibdata1ib_logfile* 等),可能引发数据字典初始化失败。即使系统时间修正后,仍需检查数据目录是否损坏。

解决方案

1. 修正系统时间

  • 临时修改 (仅用于测试):

    bash 复制代码
    # 设置正确的时间和时区
    sudo date -s "2025-06-20 15:35:00"
    sudo timedatectl set-timezone Asia/Shanghai
  • 永久同步时间 (推荐):
    安装并启用 NTP 服务,确保系统时间自动同步:

    bash 复制代码
    # 安装 NTP
    sudo apt install ntp  # Debian/Ubuntu
    sudo yum install ntp  # CentOS/RHEL
    
    # 启动并启用 NTP 服务
    sudo systemctl enable ntp --now

2. 检查数据目录完整性

  • 手动检查数据目录

    确保 MySQL 数据目录(通常是 /var/lib/mysql)权限和文件完整性:

    bash 复制代码
    sudo chown -R mysql:mysql /var/lib/mysql
    sudo chmod -R 750 /var/lib/mysql
  • 修复数据文件

    如果数据目录损坏,可以尝试使用 mysqlcheck 工具修复:

    bash 复制代码
    sudo mysqlcheck --all-databases --check-upgrade --auto-repair -u root -p

3. 重新初始化数据目录

如果上述方法无效,可能需要重新初始化数据目录:

  1. 备份现有数据(如有必要):

    bash 复制代码
    sudo cp -r /var/lib/mysql /var/lib/mysql_backup
  2. 删除旧数据目录

    bash 复制代码
    sudo rm -rf /var/lib/mysql/*
  3. 重新初始化

    bash 复制代码
    sudo mysqld --initialize --user=mysql
  4. 启动 MySQL

    bash 复制代码
    sudo systemctl start mysql

4. 调整 MySQL 配置

  • 避免使用过时的字符集配置

    日志中提示 utf8 将在未来版本中映射到 utf8mb4。更新 my.cnf 配置文件:

    ini 复制代码
    [mysqld]
    character-set-server = utf8mb4
    collation-server = utf8mb4_unicode_ci
  • 移除废弃参数

    日志中提示 --skip-host-cache 已弃用,替换为:

    sql 复制代码
    SET GLOBAL host_cache_size = 0;

验证与后续步骤

  1. 检查 MySQL 日志

    查看 /var/log/mysql/error.log/var/log/mysqld.log,确认是否仍有错误。

  2. 测试数据库连接

    bash 复制代码
    mysql -u root -p -e "SHOW DATABASES;"
  3. 监控系统时间

    使用 ntpstattimedatectl 确保系统时间同步:

    bash 复制代码
    timedatectl

注意事项

  • 避免手动修改系统时间:长期依赖手动修改时间可能导致其他服务异常(如证书验证失败、日志混乱等)。务必使用 NTP 自动同步。
  • 备份数据:在重新初始化数据目录前,务必备份重要数据。
  • 升级 MySQL:如果你使用的是较旧版本(如 8.0.33),建议升级到最新版本以修复潜在问题。

如果问题仍未解决,请提供完整的 MySQL 错误日志和 my.cnf 配置文件内容,以便进一步排查。

其他说明

上海107 ubuntu22.04 算能盒子硬件时钟是有电的,能够取得时间,但是初始时间未设置,为1970年,导致mysql数据初始化时失败。
猜测之前的英伟达盒子,可能系统启动的时候做了个判断,如果硬件时间是1970年附近,就把硬件时钟设置为2023年2月,所以一直没问题。

相关推荐
嵩山小老虎9 小时前
Windows 10/11 安装 WSL2 并配置 VSCode 开发环境(C 语言 / Linux API 适用)
linux·windows·vscode
Fleshy数模9 小时前
CentOS7 安装配置 MySQL5.7 完整教程(本地虚拟机学习版)
linux·mysql·centos
a41324479 小时前
ubuntu 25 安装vllm
linux·服务器·ubuntu·vllm
az44yao10 小时前
mysql 创建事件 每天17点执行一个存储过程
mysql
一只自律的鸡11 小时前
【Linux驱动】bug处理 ens33找不到IP
linux·运维·bug
17(无规则自律)11 小时前
【CSAPP 读书笔记】第二章:信息的表示和处理
linux·嵌入式硬件·考研·高考
!chen11 小时前
linux服务器静默安装Oracle26ai
linux·运维·服务器
秦老师Q11 小时前
php入门教程(超详细,一篇就够了!!!)
开发语言·mysql·php·db
REDcker11 小时前
Linux 文件描述符与 Socket 选项操作详解
linux·运维·网络
蒹葭玉树12 小时前
【C++上岸】C++常见面试题目--操作系统篇(第二十八期)
linux·c++·面试