我有一个.sql文件,其中包含phpMyAdmin的导出。我想使用命令行将其导入到其他服务器。
我安装了Windows Server 2008 R2。我将.sql文件放在C驱动器上,并尝试了以下命令
database_name < file.sql
它不起作用。我遇到语法错误。
我如何才能毫无问题地导入此文件?我需要先创建数据库吗?
我有一个.sql文件,其中包含phpMyAdmin的导出。我想使用命令行将其导入到其他服务器。
我安装了Windows Server 2008 R2。我将.sql文件放在C驱动器上,并尝试了以下命令
database_name < file.sql
它不起作用。我遇到语法错误。
我如何才能毫无问题地导入此文件?我需要先创建数据库吗?
当前回答
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
其他回答
要导入数据库,请使用以下命令。
mysql> create new_database;
mysql> use new_database;
mysql> source (Here you need to import the path of the SQL file);
E.g.:
mysql> source E:/test/dump.sql;
即使在Windows上,也需要使用正斜杠(/),例如e:/test/dump.sql而不是e:\test\dump.sql
或由于转义而使用双反斜杠(\\),即e:\\test\\dump.sql
要将数据库转储到SQL文件中,请使用以下命令。
mysqldump -u username -p database_name > database_name.sql
要将SQL文件导入数据库(确保与SQL文件位于同一目录中或提供文件的完整路径),请执行以下操作:
mysql -u username -p database_name < database_name.sql
您可以尝试此查询。
导出:
mysqldump -u username –-password=your_password database_name > file.sql
导入:
mysql -u username –-password=your_password database_name < file.sql
以及以下链接的详细信息:
https://chartio.com/resources/tutorials/importing-from-and-exporting-to-files-using-the-mysql-command-line/
类似于vladkras对如何在MySQL中使用命令行导入SQL文件的回答?。
我的主要区别:
数据库必须首先存在-p和密码之间没有空格
shell> mysql -u root -ppassword #note: no space between -p and password
mysql> CREATE DATABASE databasename;
mysql> using databasename;
mysql> source /path/to/backup.sql
我在MariaDB上运行Fedora 26。
以下命令在上的命令行(cmd)中对我有效WAMP上的Windows 7。
d:/wamp/bin/mysql/mysql5.6.17/bin/mysql.exe -u root -p db_name < database.sql