Search code examples
mysqllaraveldockerlaravel-sail

Import sql file into laravel sail mysql container


I'm using laravel sail and I try to import sql file into mysql container, is it possible to do that?

I tried this command (file.sql is in the same directory where I launched sail command):

sail exec mysql mysql -u username -p database_name < file.sql

But I had this error "the input device is not a TTY"

Thanks


Solution

  • You can use

    sail mysql << file.sql
    

    or

    cat file.sql | sail mysql
    

    If you are using MariaDB just replace mysql with mariadb. Also in the defailt sail docker-compose.yml port 3306 of the database is exposed.

    That means, if you have installed a local mysql client you can connect to localhost but there is a twist: If you use mysql or mysqldump with -h localhost the tries to use a socket but you can force it to use 3306 if you use -h 127.0.0.1

    # Does NOT work
    $ mysql -hlocalhost -usail -p laravel
    Enter password:
    ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2)
    
    # WORKS
    $ mysql -h127.0.0.1 -usail -p laravel
    Enter password:
    

    Cheers