Docker. MySQL image. Can't change my.cnf file

21,383

Solution 1

Also I have changed

- ./docker/my.cnf:/etc/my.cnf

to

- ./docker/my.cnf:/etc/alternatives/my.cnf

It's not related to the issue, but looks like it's considered to be a "better practice".

Solution 2

You can add this to your db service:

command: --sql_mode=""

so its:

    database:
    container_name: test_db
    image: mysql:5.7
    volumes:
        - ./docker/my.cnf:/etc/my.cnf
    environment:
        - "MYSQL_ROOT_PASSWORD=root"
        - "MYSQL_DATABASE=test_db"
    ports:
        - "3306:3306"
    volumes:
      - test_db_data:/var/lib/mysql
    command: --sql_mode=""

Solution 3

Try to use only one "volumes" directive. Maybe it uses only the last one.

Share:
21,383
D.R.
Author by

D.R.

Updated on July 20, 2022

Comments

  • D.R.
    D.R. almost 2 years

    I have such a docker-compose.yml:

        database:
            container_name: test_db
            image: mysql:5.7
            volumes:
                - ./docker/my.cnf:/etc/my.cnf
            environment:
                - "MYSQL_ROOT_PASSWORD=root"
                - "MYSQL_DATABASE=test_db"
            ports:
                - "3306:3306"
            volumes:
              - test_db_data:/var/lib/mysql
    
    volumes:
        test_db_data:
    

    and I want to edit the sql_mode.

    ./docker/my.cnf contains:

    [mysqld]
    sql_mode=""
    

    But when I connect to db from my local machine and run SELECT @@sql_mode; I receive:

    ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    

    What am I doing wrong?

  • Raghwendra Singh
    Raghwendra Singh over 6 years
    Rather than answering your own question and asking a new one in answer, I would suggest editing/updating your question.
  • D.R.
    D.R. over 6 years
    Thank you for the suggestion, but I don't think so. The original issue was Can't change my.cnf file. I have solved it and posted an answer. I don't understand the root of the bug, but I have solved it. If sb can explain it — ok. No — I'm ok with that and don't need a new SO question for that.
  • D.R.
    D.R. over 6 years
    OMG. I have not noticed. I've solved the issue, but the second time it was in one volume "command" (don't know how to name it).
  • D.R.
    D.R. over 5 years
    Interesting idea. Thanks.
  • agucho
    agucho over 5 years
    @D.R. it worked in my situation, not sure if it will in yours.
  • slashdottir
    slashdottir over 5 years
    Please show what the finished docker-compose file looks like? Did you remove the second volumes command?
  • D.R.
    D.R. over 5 years
    @slashdottir sorry, but I don't have it anymore :(
  • medina
    medina about 5 years
    For me this was the only approach that worked. Thanks heaps Brad.
  • gvanto
    gvanto over 4 years
    This is a great solution, clean
  • Diego Santa Cruz Mendezú
    Diego Santa Cruz Mendezú over 3 years
    This worked for me, i agree command: --sql_mode="" and works.