MySQL设置Windows上的大小写敏感

电脑上的MySQL安装的时候没有配置大小写敏感,导致表明全小写,这严重影响了开发的效率

故此来记录一下Windows上Mysql配置大小写流程

1.找到MySQL的安装目录

  •  这里先SHOW global VARIABLES like '%lower_case%' 查询一下mysql配置。可以看到结果lower_case_file_system为ON,而lower_case_table_names为1;其中lower_case_file_system为ON表示大小写不敏感,为OFF表示大小写敏感,lower_case_table_names为1表示mysql会先把表名转为小写,再执行操作,为0表示mysql会根据表名直接操作(大小写不敏感)

  • 按Win + R 打开运行窗口,在输入框中输入services.msc,打开服务窗口,找到MySQL的服务右键选择属性

 2. 找到配置文件修改属性  lower_case_table_names=2 (注意Windows上区分大小写配置2)保存重启MySQL服务。

3. 重启如果遇到  本地计算机上的mysql服务启动停止后,某些服务在未由其他服务或程序使用时将自动停止。

  • 需要把原来的服务删除( cmd(管理员身份运行) ->)
C:Windowssystem32>d:

D:>cd WorkToolsmysql-5.7.33-winx64in

D:WorkToolsmysql-5.7.33-winx64in>mysqld --remove mysql
Service successfully removed.

D:WorkToolsmysql-5.7.33-winx64in>mysqld --initialize-insecure --user=mysql
2021-06-30T02:45:04.775205Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-30T02:45:04.777011Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2021-06-30T02:45:04.777271Z 0 [ERROR] Aborting

#这里报错是因为没有把目录下的data文件删除(注意删除数据前先备份!!),如果没有data目录,请自行创建一个空目录,起名为data D:WorkToolsmysql
-5.7.33-winx64in>mysqld --initialize-insecure --user=mysql 2021-06-30T02:45:50.000543Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-06-30T02:45:50.002495Z 0 [ERROR] The server option 'lower_case_table_names' is configured to use case sensitive table names but the data directory is on a case-insensitive file system which is an unsupported combination. Please consider either using a case sensitive file system for your data directory or switching to a case-insensitive table name mode. 2021-06-30T02:45:50.002713Z 0 [ERROR] Aborting

#这里报错误是因为一开始参照网上的将配置lower_case_table_names设置成0了,Windows上要设置成2为大小写敏感

 

D:WorkToolsmysql-5.7.33-winx64in>mysqld --initialize-insecure --user=mysql 
D:WorkToolsmysql
-5.7.33-winx64in>mysqld --install mysql --defaults-file=d:WorkToolsmysql-5.7.33-winx64my.ini
Service successfully installed.
D:WorkToolsmysql
-5.7.33-winx64in>net start mysql
mysql 服务正在启动 .
mysql 服务已经启动成功。
D:WorkToolsmysql
-5.7.33-winx64in>
  • 数据库密码可以去data目录下找.err后缀的日志里面


注意:

[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

这个警告其原因是从 5.6开始,timestamp 的默认行为已经是 deprecated 了。

在MySQL 5.6.6之前,TIMESTAMP的默认行为:

•TIMESTAMP列如果没有明确声明NULL属性,默认为NOT NULL。(而其他数据类型,如果没有显示声明为NOT NULL,则允许NULL值。)设置TIMESTAMP的列值为NULL,会自动存储为当前timestamp。
•表中的第一个TIMESTAMP列,如果没有声明NULL属性、DEFAULT或者 ON UPDATE,会自动分配 DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP 属性。
•表中第二个TIMESTAMP列,如果没有声明为NULL或者DEFAULT子句,默认自动分配'0000-00-00 00:00:00′。插入行时没有指明改列的值,该列默认分配'0000-00-00 00:00:00′,且没有警告。

要关闭警告,需要加入下面的参数:

[mysqld]
explicit_defaults_for_timestamp=true

重启MySQL后错误消失,这时TIMESTAMP的行为如下:

•TIMESTAMP如果没有显示声明NOT NULL,是允许NULL值的,可以直接设置改列为NULL,而没有默认填充行为。
•TIMESTAMP不会默认分配DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP属性。
•声明为NOT NULL且没有默认子句的TIMESTAMP列是没有默认值的。往数据表中插入列,又没有给TIMESTAMP列赋值时,如果是严格SQL模式,会抛出一 个错误,如果严格SQL模式没有启用,该列会赋值为'0000-00-00 00:00:00′,同时出现一个警告。(这和MySQL处理其他时间类型数据一样,如DATETIME)
(参见:https://www.jb51.net/article/71054.htm

也就是 explicit_defaults_for_timestamp 关闭了 timestamp 类型字段锁拥有的一些会让人感到奇怪的默认行为,加入了该参数之后,如果还需要为 timestamp类型的字段指定默认行为,那么就需要显示的在创建表时显示的指定。explicit_defaults_for_timestamp 也就是这个意思:显示指定默认值为timestamp类型的字段。

原文地址:https://www.cnblogs.com/dztHome/p/14953772.html