Importing and Exporting of MYSQL Database with Command Line SSH

Importing MYSQL Database

Syntax

$ mysql -u username -p database_name < path_to_mysql_file_here

Importing existing MYSQL database with filename "wordpress.sql" stored at directory "/home/nishant/", Username is "root", terminal will prompt for asking password.

$ mysql -u root -p wordpress < /home/nishant/wordpress.sql

Exporting MYSQL Database

Syntax

$ mysqldump -p -u username database_name > path_to_mysql_file_here

Exporting MYSQL database to filename "db.sql" stored at directory "/home/nishant/", Username is "root", terminal will prompt for asking password.

$ mysqldump -p -u root wordpress > /home/nishant/db.sql

Exporting MYSQL Database by creating file with TimeStamp

Learn here => How To Create file with TimeStamp in Shell Script

Following shell script written for exporting mysql database to file along with timestamp.

#!/bin/sh
# Database backup configuration
DB_USER="root"
DB_PASS="root"
FILE_PATH=~/Desktop/
cd $FILE_PATH

# WordPress Backup configuration
DATABASE="wordpress"
FILE="$DATABASE_$(date +%d%m%Y_%H%M%S).sql"
ZIPFILE="$FILE.zip"
mysqldump -p$DB_PASS -u $DB_USER $DATABASE > $FILE_PATH$FILE
zip $FILE_PATH$ZIPFILE $FILE
sleep 10
rm $FILE

Nishant Vaity

Knowledgeable and skilled Technology Lead with an ability of software development and supervision. Possess a Bachelor of Science (BS) in Information Technology along with 11+ years of experience with hands-on coding and team management. By profession, I am a Software Engineer, Technology Mentor & Entrepreneur. Passionate about the technologies I use and always eager to share & learn more from that passion.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.