0

I want to run the init.sql file after mysql container has started completely. I am using the following docker compose file. It is saying no /var/initdb/init.sql file with the following compose file.

services:
    mysqldb:
        container_name: mysql
        image: mysql
        restart: always
        ports:
            - "3306:3306"
        volumes:
            - /mysql-schema :/var/schema
            - /init :/var/initdb
        command: sh -c "mysql -u root < /var/initdb/init.sql"
        environment:
            - MYSQL_ROOT_PASSWORD=password 
            - MYSQL_USER=a12nserver
            - MYSQL_PASSWORD=password
            - MYSQL_DATABASE=a12nserver
        networks: 
            - MY_network

If I use the command: sh -c "mysql -u root -p < /var/initdb/init.sql" it gives me an error error: access denied since password has not been entered. I want to run the init.sql file/files after the mysql container has completely started.

Gary
  • 2,293
  • 2
  • 25
  • 47
  • 1
    In your command section : -p is missing, did you try by providing command: sh -c "mysql -u root -p$MYSQL_ROOT_PASSWORD < /var/initdb/init.sql".... Please share your "docker mysql logs" here – Jethan Jun 16 '21 at 09:26
  • 2
    Anything you put as `command:` runs _instead of_ the main container command. If you can put your script in `/docker-entrypoint-initdb.d`, the image knows how to run it the first time it's launched. But in general there's not a way in Docker to start the main container process, then run some other process after it's "ready". – David Maze Jun 16 '21 at 10:39
  • @Jethan I did try sh -c "mysql -u root -p'password' " but it failed with access denied. – Gary Jun 18 '21 at 02:36
  • @david-maze I did also try `volume - /init.sql : /docker-entrypoint-initdb.d/init.sql` but it started the script even before mysql container was up and running. What I am attempting to do is once the mysql container is up and running/ready it should run the script. But seems docker-entrypoint-initdb.d runs immediately when the container is initialised and when mysql service is not ready – Gary Jun 18 '21 at 02:41
  • @Gary: Did you even try without importing your .sql file and checked whether your container is up and running successfully with the given configurations and able to login with the root account? – Jethan Jun 18 '21 at 11:36
  • I was able to import with volumes. That is all fine. But Mysql container creates a user and then a service and then restarts during container created The issue is when I use docker-entrypoint.sh/init.sql it executes even before a root user is created. I want to run the command when all the activities are done. In PHP composer like package managers there is an option where I can mention when script is supposed to run like post-install. I want to run this SQL script post-install @Jethan – Gary Jun 19 '21 at 05:55
  • @Jethan when I use command sh -c "mysql...." it fails with access denied. The option mysql -u root -p$MYSQL_ROOT_PASSWORD does not work somehow. What is going wrong here? I have added the environment variables of root and root password. Second the script actually runs even before a root username is created. The script runs at the start as soon as the mysql port is up in the basic container – Gary Jun 19 '21 at 05:57
  • @Jethan I am right now waiting for the mysql service to be up. Then attaching a bash shell to running container and then running the commands manually – Gary Jun 19 '21 at 05:58
  • I am not in the system right now but I will update the logs here in sometime . But I definitely am using your suggestion right. And above is what is happening. @Jethan – Gary Jun 19 '21 at 06:03

0 Answers0