search

Home  >  Q&A  >  body text

How to import SQL files using command line in MySQL?

<p>I have a <code>.sql</code> file that contains an export from <code>phpMyAdmin</code>. I want to import it into a different server using the command line. </p> <p>I installed Windows Server 2008 R2. I placed the <code>.sql</code> file on <strong><em>C drive</em></strong> and tried this command</p> <pre class="brush:php;toolbar:false;">database_name < file.sql</pre> <p>It doesn't work. I'm getting a syntax error. </p> <ul> <li>How can I successfully import this file? </li> <li>Do I need to create a database first? </li> </ul><p><br /></p>
P粉459578805P粉459578805517 days ago537

reply all(2)I'll reply

  • P粉926174288

    P粉9261742882023-08-24 09:06:53

    mysqldump A common use is to back up the entire database:

    mysqldump db_name > backup-file.sql
    

    You can load the dump file back to the server as follows:

    Unix

    mysql db_name < backup-file.sql
    

    Same in Windows Command Prompt:

    mysql -p -u [user] [database] < backup-file.sql
    

    PowerShell

    cmd.exe /c "mysql -u root -p db_name < backup-file.sql"
    

    MySQL command line

    mysql> use db_name;
    mysql> source backup-file.sql;
    

    reply
    0
  • P粉103739566

    P粉1037395662023-08-24 00:28:35

    try:

    mysql -u username -p database_name < file.sql

    Check MySQL Options.

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

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

    Note 3 If the database does not exist yet and the exported SQL does not contain CREATE DATABASE (exported using --no-create-db or -n option) before importing it.

    reply
    0
  • Cancelreply