import sql file into mysql docker container using command line

    By: Thad Mertz
    3 years ago

    Ok Follow these steps

    First get container list

    docker ps
    

    Get into the "MYSQL" container

    docker exec -it MYSQL_CONTAINER_ID sh
    
    docker exec -it e601d0b82a15 sh
    

    Now once inside container run


    bash
    

    // then 

    mysql -u USERNAME -p
    
    mysql -u root -p   // this will ask password so be ready and root is username
    

    Now you can run command to create table

    //replace DATABASE_TABLE_NAME with database table name
    
    create database DATABASE_TABLE_NAME character set utf8mb4 collate utf8mb4_unicode_ci;
    

    After running above command you will have a new database.

    then run these commands

    exit   // type exit and hit enter to exit mysql screen to back to docker container
    
    then
    
    cd /var/lib/mysql 
    
    // our setup has mysql directory setup this way below code is from docker-compose file
    
    db:
        image: mysql:8.0.23
        deploy:
         replicas: 1       
         restart_policy:         
            condition: on-failure
        command: --default-authentication-plugin=mysql_native_password
        ports:
          - "3378:3378"
        volumes:
          - ./data/mysql:/var/lib/mysql  // here we have data folder/ then mysql where we need to put sql file 
    

    check above image. And once. Once you are in "data/mysql".

    Run this command

    In below given command 
    
    -pPASSSWORD means replace "PASSWORD" with Your password
    
    mysql -u USERNAME -pPASSWORD DATABASE_NAME < SQL_FILE_NAME.sql
    


    This will insert the data from sql file to database.