I am writing this on the train, so I’ll be relatively quick.
It’s been a while since I wrote my import sql file via command line post and thought I’d post the updated version which I now use.
It is possible to execute an SQL file without logging into the mysql client first. Previously, I used login to mysql and use the source command to execute commands from an SQL text file.
Now, I execute it all straight from bash or windows shell without having to login to the MySQL client.
To import an SQL file by passing it as if it were an arguement, you need to use the < sign. So for example, say that I wanted to import an external database from production and wanted to import it into my development environment, I can do so by doing the following:
How to Import an SQL file directly from the Command Line:
Step 1: Download the source sql file.
Step 2: Extract the SQL file sif required – usually, you’ll get the file in an archived form since an uncompressed SQL file is hideously large.
Step 3: Execute the following in the command line:
$ mysql -uusername -p -h databasehostname databasename < locally-extracted-sql-file.sql;
Step 4: Enter your password at the prompt.
Step 5: Check that your changes have been applied and make adjustments as required.
You should now see that the sql commands in the external file has been executed.
- Importing SQL dumps into MySQL through Command line
- MySQL Funny/Strange Characters replacing apostrophes after import
- Execute Linux commands in PHP by using backticks
- How to fix phpmyadmin error: “Connection for controluser as defined in your configuration failed”