How can I import a database with MySQL from terminal?

后端 未结 18 1282
盖世英雄少女心
盖世英雄少女心 2020-11-27 09:14

How can I import a database with mysql from terminal?

I cannot find the exact syntax.

相关标签:
18条回答
  • 2020-11-27 09:15

    If you are using sakila-db from mysql website, It's very easy on the Linux platform just follow the below-mentioned steps, After downloading the zip file of sakila-db, extract it. Now you will have two files, one is sakila-schema.sql and the other one is sakila-data.sql.


    1. Open terminal
    2. Enter command mysql -u root -p < sakila-schema.sql
    3. Enter command mysql -u root -p < sakila-data.sql
    4. Now enter command mysql -u root -p and enter your password, now you have entered into mysql system with default database.
    5. To use sakila database, use this command use sakila;
    6. To see tables in sakila-db, use show tables command

    Please take care that extracted files are present in home directory.

    0 讨论(0)
  • 2020-11-27 09:16

    Explanation:

    1. First create a database or use an existing database. In my case, I am using an existing database

    2. Load the database by giving <name of database> = ClassicModels in my case and using the operator < give the path to the database = sakila-data.sql

    3. By running show tables, I get the list of tables as you can see.

    Note : In my case I got an error 1062, because I am trying to load the same thing again.

    0 讨论(0)
  • 2020-11-27 09:19

    Assuming you're on a Linux or Windows console:

    Prompt for password:

    mysql -u <username> -p <databasename> < <filename.sql>
    

    Enter password directly (not secure):

    mysql -u <username> -p<PlainPassword> <databasename> < <filename.sql>
    

    Example:

    mysql -u root -p wp_users < wp_users.sql
    
    mysql -u root -pPassword123 wp_users < wp_users.sql
    

    See also:

    4.5.1.5. Executing SQL Statements from a Text File


    Note: If you are on windows then you will have to cd (change directory) to your MySQL/bin directory inside the CMD before executing the command.

    0 讨论(0)
  • 2020-11-27 09:19

    The simplest way to import a database in your MYSQL from the terminal is done by the below-mentioned process -

    mysql -u root -p root database_name < path to your .sql file
    

    What I'm doing above is:

    1. Entering to mysql with my username and password (here it is root & root)
    2. After entering the password I'm giving the name of database where I want to import my .sql file. Please make sure the database already exists in your MYSQL
    3. The database name is followed by < and then path to your .sql file. For example, if my file is stored in Desktop, the path will be /home/Desktop/db.sql

    That's it. Once you've done all this, press enter and wait for your .sql file to get uploaded to the respective database

    0 讨论(0)
  • 2020-11-27 09:22

    Directly from var/www/html

    mysql -u username -p database_name < /path/to/file.sql
    

    From within mysql:

    mysql> use db_name;
    mysql> source backup-file.sql
    
    0 讨论(0)
  • 2020-11-27 09:22
    mysql -u username -ppassword dbname < /path/file-name.sql
    

    example

    mysql -u root -proot product < /home/myPC/Downloads/tbl_product.sql
    

    Use this from terminal

    0 讨论(0)
提交回复
热议问题