0、环境:
centos7、docker 17.12.1-ce、mysql 8.0.21
docker安装mysql命令:
代码语言:javascript复制docker run --name mysql --restart=always
-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf
-v /home/mysql/data:/var/lib/mysql
-p 3317:3306
-e MYSQL_ROOT_PASSWORD="root"
-e TZ=Asia/Shanghai
-d mysql:8.0
代码语言:javascript复制[root@shamee shamee]# docker run --name mysql --restart=always -e MYSQL_ROOT_PASSWORD="root" -d mysql:8.0
222f3969bdb4a3c58da50707a73866c48cfd26f4c7b2feed32b56ae7de09b036
[root@shamee shamee]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
222f3969bdb4 mysql:8.0 "docker-entrypoint.s…" 2 seconds ago Up 1 second 3306/tcp, 33060/tcp mysql
[root@shamee shamee]# docker exec -it mysql bash
root@222f3969bdb4:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> show variables like '�se%';
------------------------ -------
| Variable_name | Value |
------------------------ -------
| lower_case_file_system | OFF |
| lower_case_table_names | 0 |
------------------------ -------
2 rows in set (0.01 sec)
mysql>
看到mysql启动成功,同时也能正常连接。(这里是本地虚拟机测试,所以docker run没有外挂宿主目录,显然这是个不好的习惯)。用navicat试一下,
Nice,docker安装mysql8.0一分钟搞定。那不是有一个下午的时间玩游戏?那就打一把吃鸡压压惊吧。
可是到了第二天,服务器报错了:
代码语言:javascript复制Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'biz.XXL_JOB_QRTZ' doesn't exist
很显然,表名不存在,是因为表名是大写的问题。 于是乎,立马查看mysql的大小写敏感配置:
代码语言:javascript复制mysql> show variables like '�se%';
------------------------ -------
| Variable_name | Value |
------------------------ -------
| lower_case_file_system | OFF |
| lower_case_table_names | 0 |
------------------------ -------
2 rows in set (0.01 sec)
mysql>
果然,区别大小写。于是修改my.cnf:
代码语言:javascript复制[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
lower_case_table_names=1
# Custom config should go here
!includedir /etc/mysql/conf.d/
重启mysql,发现mysql不仅没有启动成功,反而不断的在重启。查看log:
代码语言:javascript复制[root@iZuf68t6hada0ayijajs45Z ~]# docker logs -f -t --tail 100f mysql
2020-09-02T08:47:27.840720958Z 2020-09-02 16:47:27 08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
2020-09-02T08:47:27.897290758Z 2020-09-02 16:47:27 08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-09-02T08:47:27.901725465Z 2020-09-02 16:47:27 08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.21-1debian10 started.
2020-09-02T08:47:27.960172822Z mysqld: [Warning] Skipping '!includedir /etc/mysql/conf.d/' directive as maximum include recursion level was reached in file /etc/mysql/conf.d/my.cnf at line 30.
2020-09-02T08:47:28.197969455Z 2020-09-02T08:47:28.190817Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.21) starting as process 1
2020-09-02T08:47:28.199468695Z 2020-09-02T08:47:28.199360Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-09-02T08:47:28.432824695Z 2020-09-02T08:47:28.429540Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-09-02T08:47:28.434242966Z 2020-09-02T08:47:28.434080Z 1 [ERROR] [MY-011087] [Server] Different lower_case_table_names settings for server ('1') and data dictionary ('0').
2020-09-02T08:47:28.434477428Z 2020-09-02T08:47:28.434339Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2020-09-02T08:47:28.434584287Z 2020-09-02T08:47:28.434551Z 0 [ERROR] [MY-010119] [Server] Aborting
2020-09-02T08:47:28.954820885Z 2020-09-02T08:47:28.949232Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.21) MySQL Community Server - GPL.
显然,报错的第8行提示,设置的lower_case_table_names=1与默认的字典值0不一致,接着就拒绝了我的设置。
翻看Mysql官网,MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables有这个一段说明:
在大多数情况下,这需要在第一次启动MySQL服务器之前在MySQL选项文件中配置lower_case_table_names。
1、修改my.cnf(无效):
按照官网的说法,Mysql8.0一旦你在初始化的时候没有给与相应的值,则默认lower_case_table_names=0。后续如果你想要在my.cnf中修改此项,Mysql8.0是不允许两者不一致的情况发生(5.7的版本是可以的,所以5.7的版本依然可以直接修改my.cnf)。所以事后修改这个配置是不允许的。
2、docker指定--lower-case-table-names=1(特定情况无效):
docker启动mysql修改配置:
代码语言:javascript复制docker run --name mysql --restart=always
-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf
-v /home/mysql/data:/var/lib/mysql
-p 3317:3306
-e MYSQL_ROOT_PASSWORD="root"
-e TZ=Asia/Shanghai
-d mysql:8.0 --lower-case-table-names=1
网上很多说法都是在docker启动mysql的时候配置--lower-case-table-names。但是我这边尝试了很多遍都无效,原因是因为我的mysql初始化过了,/var/lib/mysql不是默认的初始化数据库,有人为修改的痕迹。
3、解决方案
重新启动mysql,如果是第一次安装,那么docker后面直接加--lower-case-table-names=1是可以的。
如果不是第一次装,需要卸载掉原有的mysql,-v /home/mysql/data:/var/lib/mysql 这里指定一个新的,干净的数据仓库。然后再--lower-case-table-names=1即可。
如:
代码语言:javascript复制原有:
docker run --name mysql --restart=always
-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf
-v /home/mysql/data:/var/lib/mysql
-p 3317:3306
-e MYSQL_ROOT_PASSWORD="root"
-e TZ=Asia/Shanghai
-d mysql:8.0
修改:
docker run --name mysql --restart=always
-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf
-v /home/mysql/data2:/var/lib/mysql
-p 3317:3306
-e MYSQL_ROOT_PASSWORD="root"
-e TZ=Asia/Shanghai
-d mysql:8.0 --lower-case-table-names=1
其中/home/mysql/data2为新建的目录即可, 但是这种做法,最好数据要备份,防止丢失。