Mysql has rich features for users like they can easily import and export database, create table, relations, store procedures, triggers etc.
In the same way mysql has own command to import and export the mysql database.
- First find the mysql executable.
you can open the terminal in ubuntu/Mac or command line in windows to check whether mysql is installed globally or not.mysql --version
Output:mysql Ver 8.0.19 for macos10.15 on x86_64 (MySQL Community Server - GPL)
if the output is same as above then it’s installed globally. If the output is not as above then you need to find the installed mysql version and executable file.
Steps to find mysql executable in ubuntu
1. Install mysql-clientsudo apt-get install mysql-client
Steps to find mysql executable in windows
1. Go to the folder of wamp/wampE:\wamp\bin\mysql\
and run the terminal in the folder. - for Import database type in terminal
mysql -u username -p database_name < path_of_sql_file
Example:mysql -u root -p test < /var/www/html/test.sql
- For export database
mysql -u username -p database_name > path_to_sql_file
Example:mysql -u root -p test > /var/www/html/test.sql
If you are getting errors while importing like
mysql foreign key constraint error
then you can find the line no. and remove that
OR
You can Disable foreign key checks in command linemysql --init-command="SET SESSION FOREIGN_KEY_CHECKS=0;" -u root -p test < /var/www/html/test.sql
Still getting errors?
you can force mysql command to import with errors with option —force or -fmysql --init-command="SET SESSION FOREIGN_KEY_CHECKS=0;" -u root -p -f test < /var/www/html/test.sql
Let’s start with quick easy steps: