所以你需要在这种情况下使用Dockerfile并像下面那样处理它
FROM mysql:5.7.21
RUN echo "USE mysql;" > /docker-entrypoint-initdb.d/timezones.sql && mysql_tzinfo_to_sql /usr/share/zoneinfo >> /docker-entrypoint-initdb.d/timezones.sql
这确保了当mysql容器加载时它将加载所有时区信息.现在您可以使用环境变量来使用它
环境变量
mysqldb:
#image: mysql:5.7.21
build: .
#container_name: mysql_container
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- TZ=Europe/Sofia
将它与配置文件一起使用是一个问题.当你启动数据库时,它会给你一个错误
mysqldb_1 | 2018-04-24T12:29:43.169214Z 0 [Warning] InnoDB: New log files created, LSN=45790
mysqldb_1 | 2018-04-24T12:29:43.215187Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
mysqldb_1 | 2018-04-24T12:29:43.281229Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 2a87fec6-47bb-11e8-9f1e-0242ac110002.
mysqldb_1 | 2018-04-24T12:29:43.284010Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
mysqldb_1 | 2018-04-24T12:29:43.284404Z 0 [ERROR] Fatal error: Illegal or unknown default time zone 'Europe/Sofia'
mysqldb_1 | 2018-04-24T12:29:43.284567Z 0 [ERROR] Aborting
这是因为它需要已经加载时区.也可以解决这个问题,但是太麻烦了.我将仅使用环境变量,因为这意味着容器启动时已经设置了时区.