1. 程式人生 > 其它 >MySQL設定Windows上的大小寫敏感

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:\Windows\system32>d:

D:\>cd \WorkTools\mysql-5.7.33-winx64\bin

D:\WorkTools\mysql
-5.7.33-winx64\bin>mysqld --remove mysql Service successfully removed. D:\WorkTools\mysql-5.7.33-winx64\bin>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檔案刪除(注意刪除資料前先備份!!) D:\WorkTools\mysql
-5.7.33-winx64\bin>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:\WorkTools\mysql-5.7.33-winx64\bin>mysqld --initialize-insecure --user=mysql 
D:\WorkTools\mysql
-5.7.33-winx64\bin>mysqld --install mysql --defaults-file=d:\WorkTools\mysql-5.7.33-winx64\my.ini
Service successfully installed.
D:\WorkTools\mysql
-5.7.33-winx64\bin>net start mysql
mysql 服務正在啟動 .
mysql 服務已經啟動成功。
D:\WorkTools\mysql
-5.7.33-winx64\bin>
  • 資料庫密碼可以去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型別的欄位。