How can I import a database with MySQL from termin

2019-01-05 06:40发布

How can I import a database with mysql from terminal?

I cannot find the exact syntax.

标签: mysql import
16条回答
女痞
2楼-- · 2019-01-05 07:14

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>
查看更多
虎瘦雄心在
3楼-- · 2019-01-05 07:16

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

查看更多
劫难
4楼-- · 2019-01-05 07:17

If you are using sakila-db from mysql website, Its very easy on Linux platform just follow below mentioned steps, After downloading zip file of sakila-db, extract it. Now you will have two files, one is sakila-schema.sql and 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.

查看更多
女痞
5楼-- · 2019-01-05 07:20
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

查看更多
叼着烟拽天下
6楼-- · 2019-01-05 07:25
  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.

查看更多
一夜七次
7楼-- · 2019-01-05 07:26

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
查看更多
登录 后发表回答