Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing SQL Mode on Mariadb Image with docker-compose

I have an issue with the official dockerized image of Mariadb.

When my applications tries to make some queries I got the following error :

DB Error: unknown error QUERY : INSERT INTO

It seems this error comes from the SQL_MODE, which is set as follow in this image :

STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,
NO_ENGINE_SUBSTITUTION

I have a normal Linux Server and with mariadb installed and i don't have this STRICT_TRANS_TABLES value in my SQL_mode. And my application is working without any problem.

How can I remove the STRICT_TRANS_TABLES value in my container when I run docker-compose with my docker-compose file without the need of a custom dockerfile?

like image 521
Aurelien Avatar asked Feb 22 '18 10:02

Aurelien


People also ask

How do I change SQL mode in MySQL?

To change the SQL mode at runtime, set the global or session sql_mode system variable using a SET statement: SET GLOBAL sql_mode = 'modes'; SET SESSION sql_mode = 'modes'; Setting the GLOBAL variable requires the SUPER privilege and affects the operation of all clients that connect from that time on.

How do I access MariaDB in Docker container?

Execute the following to connect to MariaDB using the command-line client: > docker exec -it mdb mariadb --user root -pPassword123! And that's it! That's all you need to connect to and start using (querying) MariaDB.

How do I download a MariaDB image for Docker?

You can download a MariaDB image for Docker from the Offical Docker MariaDB, or choose another image that better suits your needs. You can search Docker Hub (the official set of repositories) for an image with this command: Once you have found an image that you want to use, you can download it via Docker.

Does docker-compose work with MySQL images?

Getting docker-compose to work with MySQL images is a little tricky, as the database needs too much time to start up. Below is a configuration that starts one application host and one database host.

How do I get MariaDB to work with older versions of SQL?

The most important ways for doing this are using SQL_MODE (controlled by the sql_mode system variable) and OLD_MODE (the old_mode system variable). SQL_MODE is used for getting MariaDB to emulate behavior from other SQL servers, while OLD_MODE is used for emulating behavior from older MariaDB or MySQL versions.

How do I run multiple MariaDB servers in a docker container?

Multiple MariaDB servers running in separate Docker containers can connect to each other using TCP. This is useful for forming a Galera cluster or for replication. When running a cluster or a replication setup via Docker, we will want the containers to use different ports.


1 Answers

In your docker-compose.yml set command: --sql_mode="".
Here is an example:

db-service:
    build:
      context: .
      dockerfile: db.dockerfile
    image: example/repo:db
    ports:
      - "3306:3306"
    volumes:
      - ./data/db-data:/var/lib/mysql
      - ./data/db-init:/docker-entrypoint-initdb.d/
    ports:
      - "3306:3306"
    environment:
        MYSQL_USER: root
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: your_database
    command: mysqld --sql_mode="" --character-set-server=utf8 --collation-server=utf8_slovenian_ci --init-connect='SET NAMES UTF8;' --innodb-flush-log-at-trx-commit=0
    restart: on-failure
    networks:
      - yournet

It works fine for me.

like image 140
Heril Muratovic Avatar answered Sep 24 '22 11:09

Heril Muratovic