1. 程式人生 > >Linux(docker)中 mysql 表名大小寫區分

Linux(docker)中 mysql 表名大小寫區分

問題:
在docker中使用mysql,通過sql檔案匯入資料庫的時候,建立的表名都是大寫字母,而程式中都是用的小寫,碰到了下面這種錯誤:

mysql> select * from task_schedule_job;
ERROR 1146 (42S02): Table 'ucenter.task_schedule_job' doesn't exist

在網上查到Linux系統中mysql區分大小寫

mysql> show variables like '%low%';
\+----------------------------+--------------------------------------+
| Variable_name
| Value | +----------------------------+--------------------------------------+ | log_slow_admin_statements | OFF | | log_slow_slave_statements | OFF | | low_priority_updates | OFF | | lower_case_file_system |
OFF | | lower_case_table_names | 0 | | max_allowed_packet | 4194304 | | slave_allow_batching | OFF | | slave_max_allowed_packet | 1073741824 | | slow_launch_time |
2 | | slow_query_log | OFF | | slow_query_log_file | /var/lib/mysql/6a38e59ef95d-slow.log | | transaction_allow_batching | OFF | +----------------------------+--------------------------------------+ 12 rows in set (0.01 sec)

其中 lower_case_table_names 的值是 0
0 代表區分大小寫
1 代表不區分大小寫
(在windows中1 表示不區分大小寫 2表示區分大小寫)

解決:
在配置檔案的[mysqld]後新增lower_case_table_names=1
Linux中的配置檔案應該是在 /etc/mysql/my.cnf

我用的mysql的官方docker映象,在 /etc/mysql/mysql.conf.d/mysqld.cnf

[email protected]:/etc/mysql/mysql.conf.d# cat mysqld.cnf 
...
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
...

mysql映象用的Debian 系統,沒有安裝vi、vim,安裝vim

root@6a38e59ef95d:/etc/mysql/mysql.conf.d# cat /proc/version
Linux version 4.4.52-boot2docker (root@ed11f485244a) (gcc version 4.9.2 (Debian 4.9.2-10) ) #1 SMP Wed Mar 1 23:41:46 UTC 2017

root@6a38e59ef95d:/etc/mysql/mysql.conf.d# apt-get update
...

root@6a38e59ef95d:/etc/mysql/mysql.conf.d# apt-get install vim
...

修改儲存之後重啟mysql服務,不過關閉了mysql容器也跟著關閉瞭如下:

root@6a38e59ef95d:/etc/mysql/mysql.conf.d# service mysql restart
[info] Stopping MySQL Community Server 5.7.17.

docker@default:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
6a38e59ef95d        mysql               "docker-entrypoint..."   2 hours ago         Exited (0) 44 seconds ago                       mysql5.7
9e78e89bdd4a        ubuntu              "/bin/bash"              13 hours ago        Exited (0) 13 hours ago                         kickass_banach

啟動容器,進入容器

docker@default:~$ docker start mysql5.7
mysql5.7
docker@default:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS                     NAMES
6a38e59ef95d        mysql               "docker-entrypoint..."   2 hours ago         Up 3 seconds              0.0.0.0:53306->3306/tcp   mysql5.7
9e78e89bdd4a        ubuntu              "/bin/bash"              13 hours ago        Exited (0) 13 hours ago                             kickass_banach
docker@default:~$ docker exec -it mysql5.7 /bin/bash
root@6a38e59ef95d:/# mysql -uroot -p
Enter password: 

這時lower_case_table_names 的值 已經改成了1

mysql> show variables like '%low%';
\+----------------------------+--------------------------------------+
| Variable_name              | Value                                |
+----------------------------+--------------------------------------+
| log_slow_admin_statements  | OFF                                  |
| log_slow_slave_statements  | OFF                                  |
| low_priority_updates       | OFF                                  |
| lower_case_file_system     | OFF                                  |
| lower_case_table_names     | 1                                    |
| max_allowed_packet         | 4194304                              |
| slave_allow_batching       | OFF                                  |
| slave_max_allowed_packet   | 1073741824                           |
| slow_launch_time           | 2                                    |
| slow_query_log             | OFF                                  |
| slow_query_log_file        | /var/lib/mysql/6a38e59ef95d-slow.log |
| transaction_allow_batching | OFF                                  |
+----------------------------+--------------------------------------+
12 rows in set (0.01 sec)

但是還是有問題

mysql> select * from task_schedule_job ;
ERROR 1146 (42S02): Table 'ucenter.task_schedule_job' doesn't exist

刪除之前的資料庫,出現ERROR 1010 (HY000)錯誤

mysql> drop database ucenter;
ERROR 1010 (HY000): Error dropping database (can't rmdir './ucenter', errno: 39)
mysql> select @@datadir;
+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)

直接查詢資料檔案目錄,刪除資料檔案

root@6a38e59ef95d:/# cd /var/lib/mysql
root@6a38e59ef95d:/var/lib/mysql# rm -r -f ucenter