**摘要:**本文从一个常见现象切入:建表、加字段、建索引时明明对象已存在,脚本却仍然重复执行并报错。文章指出根因在于 MySQL 的 lower_case_table_names 参数,先说明该参数 0、1、2 三种取值在存储与比较方式上的差异,再给出在 Linux 上把参数改为 1 的完整操作流程,包括确认现有表名大小写、全量备份、停库改配置、重建数据目录、启动排错(含 SELinux 上下文修复)以及修改 root 初始密码,帮助读者安全完成大小写不敏感配置的切换。
问题:
除了建表错误,还有添加字段、创建索引错误,都是提示已存在。

已存在为什么还会执行脚本呢
答案就在 MySQL 的一个参数上:lower_case_table_names。
查看参数配置
sql
SHOW VARIABLES LIKE 'lower_case_table_names';

| 值 | 存储方式 | 比较方式 | 典型系统 |
|---|---|---|---|
| 0 | 按指定大小写存储 | 区分大小写 | Linux/Unix |
| 1 | 转为小写存储 | 不区分大小写 | Windows |
| 2 | 按指定大小写存储 | 转为小写后比较 | macOS |
MySQL 版本是 8.0.26,系统 Rocky Linux 10.2。
修改参数配置
修改此参数
lower_case_table_names 不能通过 SET GLOBAL 或动态修改生效,它是 MySQL 在初始化数据目录时写入系统表 / 数据字典的参数,服务启动后不可变更。Linux 上强行在 my.cnf 里改值后重启,会直接启动失败:
Different lower_case_table_names settings for server ('1') and data dictionary ('0')
先确认当前实际存储的表名大小写
sql
SELECT TABLE_SCHEMA, TABLE_NAME FROM information_schema.TABLES
WHERE BINARY TABLE_NAME <> LOWER(TABLE_NAME);
有结果说明存在大写表名,后面必须处理,否则切到 1 后应用里写大写的 SQL 会匹配不到(反过来也一样)。如果应用没有报错也可以不处理。
全量备份数据库所有库
sql
mysqldump -uroot -p --all-databases --routines --triggers --events --single-transaction > all.sql
停库并修改配置文件
停库
bash
systemctl stop mysqld
配置
文件位置:/etc/my.cnf
sql
[mysqld]
lower_case_table_names=1
备份并新建目录
bash
cp /var/lib/mysql /var/lib/mysql.bak
sudo rm -rf /var/lib/mysql/*
启动数据库
bash
sudo systemctl start mysqld
如果启动报错:
bash
2026-10-28T10:08:00.634834Z 0 [System] [MY-015015] [Server] MySQL Server - start.
2026-10-28T10:08:00.822089Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.4.11) starting as process 79975
2026-10-28T10:08:00.826749Z 0 [Warning] [MY-010075] [Server] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 74f52618-d2b7-11f1-b75a-000c2906972b.
mysqld: File '/var/lib/mysql/auto.cnf' not found (OS errno 13 - Permission denied)
2026-10-28T10:08:00.827169Z 0 [ERROR] [MY-010183] [Server] Failed to create file(file: '/var/lib/mysql/auto.cnf', errno 13)
2026-10-28T10:08:00.827177Z 0 [ERROR] [MY-010076] [Server] Initialization of the server's UUID failed because it could not be read from the auto.cnf file. If this is a new server, the initialization failed because it was not possible to generate a new UUID.
2026-10-28T10:08:00.827214Z 0 [ERROR] [MY-010119] [Server] Aborting
2026-10-28T10:08:00.828406Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.4.11) MySQL Community Server - GPL.
2026-10-28T10:08:00.828425Z 0 [System] [MY-015016] [Server] MySQL Server - end.
首先检查目录是否有写权限、属主是否是 MySQL 等,如果都没问题,尝试:
bash
# 临时设为宽容模式
sudo setenforce 0
尝试启动
sudo systemctl start mysqld
如果能启动成功,说明是 SELinux 问题。
修复 SELinux 上下文(不要保持 setenforce 0)
bash
# 恢复默认安全上下文
sudo restorecon -Rv /var/lib/mysql
重新开启 SELinux
sudo setenforce 1
再次启动验证
sudo systemctl start mysqld
修改数据库密码
查看默认密码
bash
grep 'temporary password' /var/log/mysqld.log
修改密码
bash
mysql -u root -p
输入查到的临时密码,登录后立即修改密码:
sql
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewStrongPassword123!';
修改数据库密码可参考:
CentOS 7.9 安装 MySQL 8.3.0 (RPM Bundle) 全攻略:密码管理、冲突解决与故障处理_centos 安装mysql bundle-CSDN博客
MySQL 修改密码策略详解:从基础到高级配置_mysql密码策略设置-CSDN博客
问题

说明没有挂载密码管理策略组件。