如何在docker-compose.yml中使用多行命令标志配置MySQL实例

时间:2019-10-24 07:08:09

标签: mysql docker docker-compose

我想使用docker镜像启动MySQL本地实例。

到目前为止,我的docker-compose.yml文件看起来像:

version: '3.3'

services:
   db:
     image: mysql:8
     command: --default-authentication-plugin=mysql_native_password
     volumes:
       - ./mysql-dump:/docker-entrypoint-initdb.d
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: admin
       MYSQL_DATABASE: sqlPractice
       MYSQL_USER: admin
       MYSQL_PASSWORD: admin
     ports:
      - '7142:3306'
     security_opt:
      - seccomp:unconfined

到目前为止,一切正常。我的数据库已提供数据,但是在执行一次查询后如:

SELECT DISTINCT surname, name, data FROM exams e JOIN students s ON e.`id-student` = s.`id-student` WHERE passed='Y' AND (data, `id-centre`) = (SELECT MIN(data), e.`id-centre` FROM exams e JOIN centers c ON e.`id-center` = c.`id-center` WHERE e.passed='Y' AND c.`center-name` = 'IT Institute') 

我收到如下错误:

ER_MIX_OF_GROUP_FUNC_AND_FIELDS: In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'sqlPractice.e.id-center'; this is incompatible with sql_mode=only_full_group_by

根据此帖子: click 我想在sql_mode中设置参数docker-compose.yml,因为据称这可以解决问题。

我的docker-compose.yml在命令标志中有两个参数,看起来像:

version: '3.3'

services:
   db:
     image: mysql:8
     command: 
       - default-authentication-plugin=mysql_native_password 
       - sql_mode=""
     volumes:
       - ./mysql-dump:/docker-entrypoint-initdb.d
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: admin
       MYSQL_DATABASE: sqlPractice
       MYSQL_USER: admin
       MYSQL_PASSWORD: admin
     ports:
      - '7142:3306'
     security_opt:
      - seccomp:unconfined

但是毕竟我在启动时收到错误

db_1  | /usr/local/bin/docker-entrypoint.sh: line 363: exec: default-authentication-plugin=mysql_native_password: not found

,并且所有容器都无法正常工作。

我要实现的是通过docker-compose.ymlcommand的{​​{1}}参数中使用这两个参数配置MySQL实例,以使我的查询按预期工作。

我将非常感谢您提供有关实现目标的建议。

2 个答案:

答案 0 :(得分:2)

命令应为:

--default-authentication-plugin=mysql_native_password
--sql_mode=

--

,您不能将“”作为值提供给--sql_mode,否则会出现错误:

 Error while setting value '""' to 'sql_mode'

答案 1 :(得分:1)

services:
  db:
    image: mysql:8
    command: ['--sql_mode=', '--default-authentication-plugin=mysql_native_password']