How to import an SQL file using the command line i

2018-12-31 15:19发布

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 Server 2008 R2 installation. I placed the .sql file on the C drive, and I tried this command

database_name < file.sql

It is not working I get syntax errors.

  • How can I import this file without a problem?
  • Do I need to create a database first?

30条回答
墨雨无痕
2楼-- · 2018-12-31 15:34

I think it's worth mentioning that you can also load a gzipped (compressed) file with zcat like shown below:

zcat database_file.sql.gz | mysql -u username -p -h localhost database_name
查看更多
长期被迫恋爱
3楼-- · 2018-12-31 15:34

The following command works for me from the command line (cmd) on Windows 7 on WAMP.

d:/wamp/bin/mysql/mysql5.6.17/bin/mysql.exe -u root -p db_name < database.sql
查看更多
临风纵饮
4楼-- · 2018-12-31 15:35

A solution that worked for me is below:

Use your_database_name;
SOURCE path_to_db_sql_file_on_your_local;
查看更多
姐姐魅力值爆表
5楼-- · 2018-12-31 15:35
mysql --user=[user] --password=[password] [database] < news_ml_all.sql
查看更多
孤独寂梦人
6楼-- · 2018-12-31 15:35

For information I just had default root + withoutpassword, it didn't works with all above answers.

  • I created a new user with all privileges and a password. It works.

  • -ppassword WITHOUT SPACE.

查看更多
公子世无双
7楼-- · 2018-12-31 15:35
mysql -u root -p password -D database_name << import.sql

Use mysql help for details mysql --help

I think these will be useful options in our context

[~]$ mysql --help
mysql  Ver 14.14 Distrib 5.7.20, for osx10.12 (x86_64) using  EditLine wrapper
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.                                                                                                                                         
Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --bind-address=name IP address to bind to.
  -D, --database=name Database to use.
  --delimiter=name    Delimiter to be used.
  --default-character-set=name Set the default character set.
  -f, --force         Continue even if we get an SQL error.
  -p, --password[=name] Password to use when connecting to server.
  -h, --host=name     Connect to host.
  -P, --port=#        Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306).
  --protocol=name     The protocol to use for connection (tcp, socket, pipe,
  -s, --silent        Be more silent. Print results with a tab as separator, each row on new line.
  -v, --verbose       Write more. (-v -v -v gives the table output format).
  -V, --version       Output version information and exit.
  -w, --wait          Wait and retry if connection is down.

what is fun, if we are importing a large database and not having a progress bar. Use Pipe Viewer and see the data transfer through the pipe

For Mac, brew install pv .For Debian/Ubuntu, apt-get install pv. Others, refer http://www.ivarch.com/programs/pv.shtml

pv import.sql | mysql -u root -p password -D database_name

1.45GiB 1:50:07 [339.0KiB/s]   [=============>      ] 14% ETA 11:09:36
1.46GiB 1:50:14 [ 246KiB/s]     [=============>      ] 14% ETA 11:09:15
1.47GiB 1:53:00 [ 385KiB/s]     [=============>      ] 14% ETA 11:05:36
查看更多
登录 后发表回答