MySQL - Export / Import database

By xngo on February 16, 2020

Export database

USER="root"
PASSWORD="password"
HOST="127.0.0.1"
DB_NAME="database_mame"
 
# Export database.
    mysqldump --opt -u ${USER} -p${PASSWORD} -h ${HOST} ${DB_NAME} > ${DB_NAME}.sql

Note: To export tables, simply list your table names after the database name.

Import database

USER="root"
PASSWORD="password"
HOST="127.0.0.1"
DB_NAME="database_mame"
 
# Delete database before importing it.
    mysql -u ${USER} -p${PASSWORD} <<< "DROP DATABASE IF EXISTS ${DB_NAME};"
    mysql -u ${USER} -p${PASSWORD} <<< "CREATE DATABASE ${DB_NAME};"
 
# Import database from *.sql file.
    mysql -u ${USER} -p${PASSWORD} ${DB_NAME} < ${DB_NAME}.sql &&

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.