How to auto-execute a SQL Script after starting a docker-compose sql instance?

  docker, linux, mysql, ubuntu

After starting the docker container I have a ready sql5.7 server. But I need to create a root user with the name "root" and grant him all privileges and access from outside. So I need these sql commands to auto-execute after the container will be created.

This is my docker-compose.yml

version: '3.1'
services:
  mysql:
    container_name: mysql5.7
    restart: always
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 'mypassword' 
    ports:
     - 33063:3306
    volumes:
      - sql-data:/var/lib/mysql
volumes:
  sql-data: 

I need this sql commands auto-executed:

FLUSH PRIVILEGES;
CREATE LOGIN root WITH PASSWORD = "mypassword"
GRANT ALL PRIVILEGES *.* TO 'root'@'%';

Source: Docker Questions

LEAVE A COMMENT