Linux下MySQL大小写敏感问题

11 篇文章 0 订阅
2 篇文章 0 订阅


window下的MySQL是忽略大小写的而在Linux下MySQL默认是区分大小写,在Linux下建表时候如果表名使用大写,查询时候使用小写查询会提示表不存在。

解决办法

mysql是通过lower_case_table_names变量来处理大小写问题的。
首先查询该变量

show variables like '%case%';
--或者使用如下查询
select @@lower_case_table_names;

结果如下:

Variablevalue
lower_case_table_names0

lower_case_table_name变量是只读变量所以mysql启动之后是无法进行更改的,只能去my.conf中修改变量值,首先找到MySQL的配置文件,MySQL配置文件使用ini文件格式,找到[mysqld]部分添加配置,如果写错位置会导致MySQL无法启动或者启动成功但是忽略该配置。

#Ubuntu下配置文件是/etc/mysql/mysql.conf.d/mysqld.cnf
#CentOS下配置文件是/etc/my.cnf
#在[mysqld]下添加配置
[mysqld]
lower_case_table_names=1

重启MySQL使配置生效

#ubuntu
systemctl restart mysql.service
#centos
systemctl restart mysqld

原理

查阅mysql文档会发现对lower_case_table_name变量的描述如下

How table and database names are stored on disk and used in MySQL is affected by the lower_case_table_names system variable, which you can set when starting mysqld. lower_case_table_names can take the values shown in the following table. This variable does not affect case sensitivity of trigger identifiers. On Unix, the default value of lower_case_table_names is 0. On Windows, the default value is 1. On macOS, the default value is 2.

valuemeaning
0Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement. Name comparisons are case sensitive. You should not set this variable to 0 if you are running MySQL on a system that has case-insensitive file names (such as Windows or macOS). If you force this variable to 0 with --lower-case-table-names=0 on a case-insensitive file system and access MyISAM tablenames using different lettercases, index corruption may result.
1Table names are stored in lowercase on disk and name comparisons are not case-sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.

2|Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement, but MySQL converts them to lowercase on lookup. Name comparisons are not case sensitive. This works only on file systems that are not case-sensitive! InnoDB table names and view names are stored in lowercase, as for lower_case_table_names=1.
---------------[MySQL 5.7 Reference Manual]

大致意思就是说数据库名和表名大小写的存储受到lower_case_table_names这个系统变量的影响,但这个变量并不影响触发器名称,Unix下该变量默认为0,Window下默认是1(这也是window下不会产生大小写问题的原因),mac下默认为2。
该变量有3个值可选

含义
0数据库名和表名使用建表时指定的大小写格式,即大小写敏感,查找比较时也时区分大小写。
1数据库名和表名在建库建表时自动存储为小写,查找时也自动转换为小写进行匹配,且应用于表的别名
2库名表名使用建库建表时指定的大小写,即名称区分大小写,但是在查找和比较时不区分大小写统一转换为小写进行查找比较

注意

如果在lower_case_table_names=0时候建的表,表名是区分大小的即使后来更改变量设置为1,已经建的表是不会变的,使用小写进行查该表时还是会提示该表不存在。所以数据库中如果有数据,首先备份数据然后清空数据库,修改MySQL配置后,在将数据全部导入。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值