我有一个.sql文件,其中包含phpMyAdmin的导出。我想使用命令行将其导入到其他服务器。

我安装了Windows Server 2008 R2。我将.sql文件放在C驱动器上,并尝试了以下命令

database_name < file.sql

它不起作用。我遇到语法错误。

我如何才能毫无问题地导入此文件?我需要先创建数据库吗?


当前回答

我们可以使用此命令从命令行导入SQL:

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

例如,如果用户名是root,密码是password。数据库名为bank,SQL文件为bank.SQL

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

请记住SQL文件的位置。如果SQL文件位于桌面文件夹/目录中,请转到桌面目录并输入如下命令:

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

如果您位于Project目录中,并且SQL文件位于Desktop目录中。如果您想从Project目录访问它,可以执行以下操作:

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

其他回答

转到wamp或xampp目录实例cd d:/wamp/bin/mysql/mysql5.7.24/binmysql-u root-p DATABASENAME<路径数据库文件

Use:

mysql -u root -p password -D database_name << import.sql

有关详细信息,请使用MySQL帮助-MySQL-help。

我认为在我们的背景下,这些将是有用的选择:

[~]$ 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.

如果我们正在导入一个大型数据库,而没有进度条,那有什么好玩的呢。使用管道查看器并查看通过管道传输的数据

对于Mac,brew install pv

对于Debian/Uuntu,apt-get-install-pv。

有关其他信息,请参阅pv-管道查看器

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

导入数据库

转到驱动器:d:MySQL登录c: \xamp\mysql\bin\mysql-u root-p它将要求pwd。输入:压水堆选择数据库使用DbName;提供文件名\.DbName.sql

您可以使用:

mysql -u<user> -p<pass> <db> < db.sql

例子:

mysql -uroot -proot db < db.sql

关于导入巨大文件所需的时间:最重要的是,它需要更多的时间,因为MySQL的默认设置是autocommit=true。您必须在导入文件之前将其设置为关闭,然后检查导入是如何像宝石一样工作的。

您只需执行以下操作:

mysql> use db_name;

mysql> SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;