Databases

Import a .sql file into MySQL

Load a database dump or a script into MySQL from the command line.

Fill these in once
  1. Create the database if it doesn't exist
    Terminal
    mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS myapp;"

    On Ubuntu, the root user logs in with sudo mysql instead.

  2. Import the file
    Terminal
    mysql -u root -p myapp -e "source backup.sql"

Check it worked

  1. List the imported tables
    Terminal
    mysql -u root -p myapp -e "SHOW TABLES;"
  1. Create the database if it doesn't exist
    Terminal · Ubuntu / Debian
    mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS myapp;"

    On Ubuntu, the root user logs in with sudo mysql instead.

  2. Import the file
    Terminal · Ubuntu / Debian
    mysql -u root -p myapp -e "source backup.sql"

Check it worked

  1. List the imported tables
    Terminal · Ubuntu / Debian
    mysql -u root -p myapp -e "SHOW TABLES;"
  1. Create the database if it doesn't exist
    PowerShell
    mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS myapp;"

    On Ubuntu, the root user logs in with sudo mysql instead.

  2. Import the file
    PowerShell
    mysql -u root -p myapp -e "source backup.sql"

Check it worked

  1. List the imported tables
    PowerShell
    mysql -u root -p myapp -e "SHOW TABLES;"