Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import an SQL file using the command line in MySQL?

I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the command line.

I have a Windows Server 2008 R2 installation. I placed the .sql file on the C drive, and I tried this command

database_name < file.sql 

It is not working. I get syntax errors.

  • How can I import this file without a problem?
  • Do I need to create a database first?
like image 339
Jaylen Avatar asked Jul 16 '13 00:07

Jaylen


People also ask

How do I import a database into MySQL?

Importing a database from a file To import a file, open Workbench and click on + next to the MySQL connections option. Fill in the fields with the connection information. Once connected to the database go to Data Import/Restore. Choose the option Import from Self-Contained File and select the file.

How do I import a script into MySQL?

Open MySQL Workbench, select the models view from the sidebar in the home screen, click (>) next to Models, and then click Reverse Engineer MySQL Create Script. Find and import the sakila-schema. sql file. This is the script that contains the data definition statements for the sakila database.

How do I run a SQL file from the command line?

To run SQL files from the terminal, you can use the source or the backslash and dot command ( \. ) Next, enter the password for your root user. The path /Users/nsebhastian/Desktop/test/main. sql above needs to be changed to the SQL file path on your computer.


1 Answers

Try:

mysql -u username -p database_name < file.sql 

Check MySQL Options.

Note 1: It is better to use the full path of the SQL file file.sql.

Note 2: Use -R and --triggers to keep the routines and triggers of original database. They are not copied by default.

Note 3 You may have to create the (empty) database from MySQL if it doesn't exist already and the exported SQL don't contain CREATE DATABASE (exported with --no-create-db or -n option), before you can import it.

like image 105
bansi Avatar answered Sep 21 '22 16:09

bansi