For example, you export the schema and data or only the schema of the tables of apple
database to backup.sql
with mysqldump as shown below. *My answer explains how to export the schema and data of the tables of a database:
mysqldump -u john -p apple > backup.sql
Or:
mysqldump -u john -p -d apple > backup.sql
Or, you export only the data of the specific tables person
and animal
of apple
database to backup.sql
with INSERT statement which has column names as shown below. *By default, INSERT
statement doesn't have column names and my answer explains how to export only data more:
mysqldump -u john -p -t -c apple person animal > backup.sql
Then, you need to input a password after running the command above:
Enter password:
Now, you can import backup.sql
into orange
database with MySQL as shown below. *When importing the schema and data or only the schema, selected orange
database must exist and when importing only the data, selected orange
database and the tables must exist otherwise there is error and when importing only the data, you need to delete all the data from apple database otherwise there will be error. The documentation explains how to import databases and my answer explains how to create a database:
mysql -u john -p orange < backup.sql
Or:
mysql -u john -p -D orange < backup.sql
Or:
mysql -u john -p --database orange < backup.sql
Then, you need to input a password after running the command above:
Enter password:
Or, after login, you can import backup.sql
into orange
database with \.
or source
selecting orange
database as shown below:
mysql -u john -p
...
mysql> USE orange;
mysql> \. backup.sql
Or:
mysql -u john -p
...
mysql> USE orange;
mysql> source backup.sql
Be careful, you cannot import backup.sql
into orange
database not selecting orange
database as shown below:
So, this below gets error:
mysql -u john -p < backup.sql
ERROR 1046 (3D000) at line 22: No database selected
And, these below get error:
mysql -u john -p
...
mysql> \. backup.sql
Or:
mysql -u john -p
...
mysql> source backup.sql
ERROR 1046 (3D000): No database selected
In addition, you can import backup.sql
into orange
database without a password prompt by setting a password(e.g., banana
) to -p(--password=) as shown below. *Don't put any space just after -p
(--password=
) because there is error and my answer explains how to import a database without a password prompt in detail:
mysql -u john -pbanana orange < backup.sql
database < file.sql
does not look like any command to me, and if you see some syntax errors, please share them – Fisken