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

后端 未结 30 2208
不知归路
不知归路 2020-11-22 06:48

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 Ser

相关标签:
30条回答
  • 2020-11-22 07:14

    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.

    0 讨论(0)
  • 2020-11-22 07:15

    If you already have the database, use the following to import the dump or the sql file:

    mysql -u username -p database_name < file.sql
    

    if you don't you need to create the relevant database(empty) in MySQL, for that first log on to the MySQL console by running the following command in terminal or in cmd

    mysql -u userName -p;
    

    And when prompted provide the password.

    Next, create a database and use it:

    mysql>create database yourDatabaseName;
    mysql>use yourDatabaseName;
    

    Then import the sql or the dump file to the database from

    mysql> source pathToYourSQLFile;
    

    Note: if your terminal is not in the location where the dump or sql file exists, use the relative path in above.

    0 讨论(0)
  • 2020-11-22 07:16

    Import a database

    1. Go to drive:

       command: d:
      
    2. MySQL login

       command: c:\xampp\mysql\bin\mysql -u root -p
      
    3. It will ask for pwd. Enter it:

       pwd
      
    4. Select the database

       use DbName;
      
    5. Provide the file name

       \.DbName.sql
      
    0 讨论(0)
  • 2020-11-22 07:16

    Go to the directory where you have MySQL.

     c:\mysql\bin\> mysql -u username -p password database_name <
     filename.sql
    

    Also to dump all databases, use the -all-databases option, and no databases’ name needs to be specified anymore.

    mysqldump -u username -ppassword –all-databases > dump.sql
    

    Or you can use some GUI clients like SQLyog to do this.

    0 讨论(0)
  • 2020-11-22 07:16

    Add the --force option:

    mysql -u username -p database_name --force < file.sql
    
    0 讨论(0)
  • 2020-11-22 07:17

    A common use of mysqldump is for making a backup of an entire database:

    shell> mysqldump db_name > backup-file.sql
    

    You can load the dump file back into the server like this:

    UNIX

    shell> mysql db_name < backup-file.sql
    

    The same in Windows command prompt:

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

    PowerShell

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

    MySQL command line

    mysql> use db_name;
    mysql> source backup-file.sql;
    
    0 讨论(0)
提交回复
热议问题