How can I import a database with MySQL from terminal?

MysqlImport

Mysql Problem Overview


How can I import a database with mysql from terminal?

I cannot find the exact syntax.

Mysql Solutions


Solution 1 - Mysql

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.

Solution 2 - Mysql

Preferable way for windows:

  1. Open the console and start the interactive MySQL mode

  2. use <name_of_your_database>;

  3. source <path_of_your_.sql>

Solution 3 - Mysql

mysql -u <USERNAME> -p <DB NAME> < <dump file path>

-u - for Username

-p - to prompt the Password

Eg. mysql -u root -p mydb < /home/db_backup.sql

You can also provide password preceded by -p but for the security reasons it is not suggestible. The password will appear on the command itself rather masked.

Solution 4 - Mysql

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

Solution 5 - Mysql

Open Terminal Then

 mysql -u root -p
                 
 eg:- mysql -u shabeer -p

After That Create a Database

 mysql> create database "Name";
 
 eg:- create database INVESTOR;

Then Select That New Database "INVESTOR"

 mysql> USE INVESTOR;

Select the path of sql file from machine

 mysql> source /home/shabeer/Desktop/new_file.sql;

Then press enter and wait for some times if it's all executed then

 mysql> exit

enter image description here

Solution 6 - Mysql

From Terminal:

mysql -uroot -p --default-character-set=utf8 database_name </database_path/database.sql

Solution 7 - Mysql

in the terminal type

mysql -uroot -p1234; use databasename; source /path/filename.sql

Solution 8 - Mysql

Below command is working on ubuntu 16.04, I am not sure it is working or not other Linux platforms.

Export SQL file:

$ mysqldump -u [user_name] -p [database_name] > [database_name.sql]  

> Example : mysqldump -u root -p max_development > max_development.sql

Import SQL file:

$ mysqldump -u [user_name] -p [database_name] < [file_name.sql]

> Example: mysqldump -u root -p max_production < max_development.sql

Note SQL file should exist same directory

Solution 9 - Mysql

I usually use this command to load my SQL data when divided in files with names : 000-tableA.sql, 001-tableB.sql, 002-tableC.sql.

for anyvar in *.sql; do <path to your bin>/mysql -u<username> -p<password>  <database name> < $anyvar; done

Works well on OSX shell.

Solution 10 - Mysql

How to load from command line

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.

Solution 11 - Mysql

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

Solution 12 - Mysql

After struggling for sometime I found the information in https://tommcfarlin.com/importing-a-large-database/

  1. Connect to Mysql (let's use root for both username and password):

     mysql -uroot -proot
    
  2. Connect to the database (let's say it is called emptyDatabase (your should get a confirmation message):

     connect emptyDatabase
    

3 Import the source code, lets say the file is called mySource.sql and it is in a folder called mySoureDb under the profile of a user called myUser:

source /Users/myUser/mySourceDB/mySource.sql

Solution 13 - Mysql

  1. Open the MySQL Command Line Client and type in your password

  2. Change to the database you want to use for importing the .sql file data into. Do this by typing:

     USE your_database_name
    
  3. Now locate the .sql file you want to execute.
    If the file is located in the main local C: drive directory and the .sql script file name is currentSqlTable.sql, you would type the following:

     \. C:\currentSqlTable.sql
    

and press Enter to execute the SQL script file.

Solution 14 - Mysql

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.

Solution 15 - Mysql

First connect to mysql via command line

mysql -u root -p

Enter MySQL PW

Select target DB name

use <db_name>

Select your db file for import

SET autocommit=0; source /root/<db_file>;

commit;

This should do it. (thanks for clearing) This will work even 10GB DB can be imported successfully this way. :)

Solution 16 - Mysql

In Ubuntu, from MySQL monitor, you have already used this syntax:

mysql> use <dbname> -> The USE statement tells MySQL to use dbname as the default database for subsequent statements

mysql> source <file-path>

for example:

mysql> use phonebook;

mysql> source /tmp/phonebook.sql;

Important: make sure the sql file is in a directory that mysql can access to like /tmp

Solution 17 - Mysql

Before running the commands on the terminal you have to make sure that you have MySQL installed on your terminal.

You can use the following command to install it:

sudo apt-get update
sudo apt-get install mysql-server

Refrence here.

After that you can use the following commands to import a database:

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

Solution 18 - Mysql

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

Solution 19 - Mysql

For Ubuntu/Linux user, Extract the sql file paste it somewhere

e.g you pasted on desktop

  1. open the terminal
  2. go to your database and create database name
  3. Create database db_name;
  4. Come out of the Db(In terminal)
  5. cd DESKTOP
  6. mysql username root -p db_name < mysql.sql 7.Enter the password:....

Solution 20 - Mysql

If you want to import a database from a SQL dump which might have "use" statements in it, I recommend to use the "-o" option as a safeguard to not accidentially import to a wrong database.

   •   --one-database, -o

       Ignore statements except those those that occur while the default
       database is the one named on the command line. This filtering is
       limited, and based only on USE statements. This is useful for
       skipping updates to other databases in the binary log.

Full command:

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

Solution 21 - Mysql

There has to be no space between -p and password

mysql -u [dbusername] -p[dbpassword] [databasename] < /home/serverusername/public_html/restore_db/database_file.sql

I always use it, it works perfectly. Thanks to ask this question. Have a great day. Njoy :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaneuryzmView Question on Stackoverflow
Solution 1 - MysqlanonView Answer on Stackoverflow
Solution 2 - MysqlSri Harsha KappalaView Answer on Stackoverflow
Solution 3 - MysqlMukesh Singh RathaurView Answer on Stackoverflow
Solution 4 - MysqlLokesh DasView Answer on Stackoverflow
Solution 5 - MysqlShabeer KView Answer on Stackoverflow
Solution 6 - MysqlMohammad AniniView Answer on Stackoverflow
Solution 7 - MysqlPatrick MutwiriView Answer on Stackoverflow
Solution 8 - MysqlDeepak ChaubeView Answer on Stackoverflow
Solution 9 - MysqlLanoView Answer on Stackoverflow
Solution 10 - Mysqluser42826View Answer on Stackoverflow
Solution 11 - Mysqlvaibhav kulkarniView Answer on Stackoverflow
Solution 12 - MysqlLumasDesignView Answer on Stackoverflow
Solution 13 - Mysqlstudent001View Answer on Stackoverflow
Solution 14 - Mysqlalpha9eekView Answer on Stackoverflow
Solution 15 - MysqlPOGSNETView Answer on Stackoverflow
Solution 16 - MysqlViet HoangView Answer on Stackoverflow
Solution 17 - MysqlWolfackView Answer on Stackoverflow
Solution 18 - MysqlAnkit SaxenaView Answer on Stackoverflow
Solution 19 - Mysqlkaran yadavView Answer on Stackoverflow
Solution 20 - MysqlAlexView Answer on Stackoverflow
Solution 21 - MysqlKamleshView Answer on Stackoverflow