把 Windows 下的應用部署到 Linux 下,使用到了 Quartz 集群的特性,所以建了 MySql 的中間表,一啟動看到報錯:
Invocation of init method failed; nested exception is org.quartz.JobPersistenceException: Couldn't retrieve trigger: Table 'unmijob.QRTZ_TRIGGERS' doesn't exist [See nested exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'unmijob.QRTZ_TRIGGERS' doesn't exist]
用 MySQL 客戶端連接到那個數據庫,看到 qrtz_triggers 表確確實實是存在的,MySQL 也是安裝在 Linux 下的。在大小寫敏感的操作系統中,估摸着會不會是表名大小的因素呢? 於是把 qrtz_triggers 改為大寫的 QRTZ_TRIGGERS,再次啟動應用服務器,這時候出現的是:
Invocation of init method failed; nested exception is org.quartz.JobPersistenceException: Couldn't retrieve trigger: Table 'unmijob.QRTZ_CRON_TRIGGERS' doesn't exist [See nested exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'unmijob.QRTZ_CRON_TRIGGERS' doesn't exist]
說明表 QRTZ_TRIGGERS 表是找到了,找不到下面的 QRTZ_CRON_TRIGGERS 表。這樣的現像面前說明在 Linux 下 MySQL 表明是區分大小寫是暴露無疑了,以前多是在 windows 下寫程序,而且代碼中的 sql 語句表明也都是用小寫,所以未碰到。現在 Quartz 偏喜歡大寫,問題也就這樣來了。
知道原因,找解決辦法就可有的放矢了,google mysql linux 表名不區分大小寫,答案立馬找到:
需要改 MySQL 的配置文件,Linux 下 MySQL 的配置文件可能是 /etc/my.cnf,或 /etc/mysql/my.cnf,這依賴於你的安裝方式。假如是 /etc/my.cnf,那么執行
sudo vi /etc/my.cnf
在 [mysqld] 節中添加:
lower_case_table_names=1
然后保存,用 sudo /etc/init.d/mysql restart 重啟 MySQL 服務便讓 Linux 系統對表名大小寫不敏感了。
其實准確來說不是說 Linux 對於 MySQL 表名忽略大小寫,而是應用上面的配置后,MySQL 服務程序會來自於應用程序里的請求的表名轉換為小寫,如你查詢 select* UNMI_TABLE,MySQL 會認為是查詢的 select * from unmi_table,所以在加入
lower_case_table_names=1
之前時你必須把表名都改為小寫。也就是在創建表時都用小寫名字,如果創建的表名為 UNMI_TABLE,那么程序中無論是執行 select * from UNMI_TABLE 還是執行 select * from unmi_table 都會碰到類似下面的錯誤:
Invocation of init method failed; nested exception is org.quartz.JobPersistenceException: Couldn't retrieve trigger: Table 'unmijob.qrtz_triggers' doesn't exist [See nested exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'unmijob.qrtz_triggers' doesn't exist]
因為 MySQL 服務程序認為表名就是小寫的 unmi_table,而在數據庫中的表名不是這樣子的。
而且此時在 MySQL 客戶端都無法把 QRTZ_TRIGGERS 改成 qrtz_triggers. 因為表名的大小寫是與文件系統中的數據目錄下的 frm 文件相對應的。