Docker Compose mysql import .sql

前端 未结 5 700
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 16:32

I\'m having trouble importing an .sql dump file with docker-compose. I\'ve followed the docs, which apparently will load the .sql file from docker-entrypoint-initdb.d. However,

相关标签:
5条回答
  • 2021-01-31 16:53

    After many attempts with the volumes setting i found a workaround

    I created another image based on mysql with the following in the Dockerfile

    FROM mysql:5.6
    
    ADD dump.sql /docker-entrypoint-initdb.d
    

    Then removed the volumes from compose and ran the new image

    frontend:
      image: myimage
      ports:
       - "80:80"
      links:
       - mysql
    mysql:
      image: mymysql
      ports:
       - "3306:3306"
      environment:
        MYSQL_ROOT_PASSWORD: rootpass
        MYSQL_USER: dbuser
        MYSQL_PASSWORD: userpass
        MYSQL_DATABASE: myimage_db
    

    This way the dump is always copied over and run on startup

    0 讨论(0)
  • 2021-01-31 17:02

    This appears on the documentation page of Docker MySQL image: https://hub.docker.com/_/mysql/

    Initializing a fresh instance

    When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

    0 讨论(0)
  • 2021-01-31 17:07

    Mysql database dump schema.sql is resides in the /mysql-dump/schema.sql directory and it creates tables during the initialization process.

    docker-compose.yml:

    mysql:
        image: mysql:5.7
        command: mysqld --user=root
        volumes:
          - ./mysql-dump:/docker-entrypoint-initdb.d
        environment:
          MYSQL_DATABASE: ${MYSQL_DATABASE}
          MYSQL_USER: ${MYSQL_USER}
          MYSQL_PASSWORD: ${MYSQL_PASSWORD}
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    
    0 讨论(0)
  • 2021-01-31 17:08

    This worked for me,

    version: '3.1'
    
    services:
    
      db:
        image: mysql
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        volumes:
          - ./mysql-dump:/docker-entrypoint-initdb.d
        environment:
          MYSQL_ROOT_PASSWORD: example
          MYSQL_DATABASE: ecommerce
    
      adminer:
        image: adminer
        restart: always
        ports:
          - 8080:8080
    

    mysql-dump must be a directory. All the .sql's in the directory will be imported.

    0 讨论(0)
  • 2021-01-31 17:11

    I was having a similar issue with mysql where I would mount a local directory at /configs/mysql/data containing a mydatabasedump.sql file via docker-compose to the docker-entrypoint-initdb.d volume, the file would get loaded on to the container but not execute or populate the database when the container initialized. My intial docker-compose.yml looke like this:

    #docker-compose.yml
    version: '3'
    services:
        db:
            build: ./build/mysql/ #this is pointing to my Dockerfile
            container_name: MYSQL_Database
            restart: always
            environment:
            MYSQL_PORT: 3306
            MYSQL_ROOT_PASSWORD: admin
            MYSQL_DATABASE: my_app_database
            MYSQL_USER: admin
            MYSQL_PASSWORD: admin
        volumes:
            - ./configs/mysql/data:/docker-entrypoint-initdb.d:
    

    I found two working solutions for this problem:

    The first came after I logged in the running container and confirmed that mydatabasedump.sq file was present and executable in the container's docker-entrypoint-initdb.d directory; I created and added a bash script to my local /configs/mysql/data directory called dump.sh that excuted after the container was initialized. It contains a single mysql command that copies my_database_dump.sql to my_app_database. The bash script looks like this

        #!/bin/bash
        #dump.sh
        mysql -uadmin -padmin my_app_database < my_database_dump.sql 
        #end of dump.sh
    

    I executed this script via my Dockerfile in the ENTRYPOINT directive like this:

        #Dockerfile
        FROM mysql:5.5
        ENTRYPOINT [ "dump.sh" ]
        EXPOSE 80
        #end of Dockerfile
    

    After realizing the initial issue was due to the volumes being mouted after the cotainer is built and therefore not intilizing the database with the dump file (or executing any scripts in that directory) at boot time, the second solution was simply to move the volumes directive in my compose-file above the built directive. This worked and allowed me to remove the dump.sh scrip and the DOCKERENTRY directive in my Dockerfile. The modified docker-compose.yml looks like this

    #docker-compose.yml
    version: '3'
    services:
        db:
            volumes:
              - ./configs/mysql/data:/docker-entrypoint-initdb.d
           build: ./build/mysql/ #this is pointing to my Dockerfile
             container_name: MYSQL_Database
             restart: always
         environment:
             MYSQL_PORT: 3306
             MYSQL_ROOT_PASSWORD: admin
             MYSQL_DATABASE: my_app_database
             MYSQL_USER: admin
             MYSQL_PASSWORD: admin
    
    0 讨论(0)
提交回复
热议问题