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 无法正确读取数据目录中的文件(如 ibdata1、ib_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. 监控系统时间 :

    使用 ntpstat 或 timedatectl 确保系统时间同步:

    bash 复制代码
    timedatectl

注意事项

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

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

其他说明

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

相关推荐
夜听莺儿鸣13 小时前
502-002_Linux驱动开发模块化编程
linux·驱动开发·模块化编程
wuminyu14 小时前
C++协程实现接收端的零拷贝Buffer管理原理剖析
java·linux·c语言·jvm·c++
科力锐品牌君15 小时前
应用级灾备 | 海量非结构化数据如何实现高效数据保护
linux·运维·网络·安全·系统安全·数据安全·灾备
zhangrelay15 小时前
ROS项目设计案例智能大模型正经乱答案例
linux·笔记·学习·ubuntu·机器人
流浪00116 小时前
Linux系统篇39——线程(四) pthread库的由来和线程的创建与等待
linux·操作系统·线程·pthread库
Lancker16 小时前
chnroute-linux — 中国大陆 IP 段每日自动更新 + 「仅允许国内访问」落地
linux·运维·tcp/ip
夜雪一千16 小时前
MySQL外键彻底入门:是什么、怎么用、什么时候不该用
数据库·mysql
高山有多高16 小时前
【Linux笔记】Linux进程与守护进程
linux
大侠归来17 小时前
Ubuntu 搭建 APUE 环境:从源码编译到第一个示例跑通
linux·运维·ubuntu
FYKJ_201017 小时前
SSM校园失物招领系统41452-计算机课程设计、毕业设计
java·vue.js·spring boot·python·mysql·typescript·spark