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

后端 未结 30 2213
不知归路
不知归路 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:29

    Sometimes the port defined as well as the server IP address of that database also matters...

    mysql -u user -p user -h <Server IP> -P<port> (DBNAME) < DB.sql 
    
    0 讨论(0)
  • 2020-11-22 07:30

    We can use this command to import SQL from command line:

    mysql -u username -p password db_name < file.sql
    

    For example, if the username is root and password is password. And you have a database name as bank and the SQL file is bank.sql. Then, simply do like this:

    mysql -u root -p password bank < bank.sql
    

    Remember where your SQL file is. If your SQL file is in the Desktop folder/directory then go the desktop directory and enter the command like this:

    ~ ? cd Desktop
    ~/Desktop ? mysql -u root -p password bank < bank.sql
    

    And if your are in the Project directory and your SQL file is in the Desktop directory. If you want to access it from the Project directory then you can do like this:

    ~/Project ? mysql -u root -p password bank < ~/Desktop/bank.sql
    
    0 讨论(0)
  • 2020-11-22 07:30

    To import a single database, use the following command.

    mysql -u username -p password dbname < dump.sql
    

    To import multiple database dumps, use the following command.

    mysql -u username -p password < dump.sql
    
    0 讨论(0)
  • 2020-11-22 07:30

    You do not need to specify the name of the database on the command line if the .sql file contains CREATE DATABASE IF NOT EXISTS db_name and USE db_name statements.

    Just make sure you are connecting with a user that has the permissions to create the database, if the database mentioned in the .sql file does not exist.

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

    I thought it could be useful for those who are using Mac OS X:

    /Applications/xampp/xamppfiles/bin/mysql -u root -p database < database.sql
    

    Replace xampp with mamp or other web servers.

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

    I kept running into the problem where the database wasn't created.

    I fixed it like this

    mysql -u root -e "CREATE DATABASE db_name"
    mysql db_name --force < import_script.sql
    
    0 讨论(0)
提交回复
热议问题