将 SQL 转储导入 MySQL 时出错:未知数据库/无法创建数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/859313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error importing SQL dump into MySQL: Unknown database / Can't create database
提问by BryanWheelock
I'm confused how to import a SQL dump file. I can't seem to import the database without creating the database first in MySQL.
我很困惑如何导入 SQL 转储文件。如果不先在 MySQL 中创建数据库,我似乎无法导入数据库。
This is the error displayed when database_name
has not yet been created:
这是database_name
尚未创建时显示的错误:
username
= username of someone with access to the database on the original server.database_name
= name of database from the original server
username
= 有权访问原始服务器上数据库的用户的用户名。database_name
= 来自原始服务器的数据库名称
$ mysql -u username -p -h localhost database_name < dumpfile.sql
Enter password:
ERROR 1049 (42000): Unknown database 'database_name'
If I log into MySQL as root and create the database, database_name
如果我以 root 身份登录 MySQL 并创建数据库, database_name
mysql -u root
create database database_name;
create user username;# same username as the user from the database I got the dump from.
grant all privileges on database_name.* to username@"localhost" identified by 'password';
exit mysql
then attempt to import the sql dump again:
然后尝试再次导入 sql 转储:
$ mysql -u username -p database_name < dumpfile.sql
Enter password:
ERROR 1007 (HY000) at line 21: Can't create database 'database_name'; database exists
How am I supposed to import the SQL dumpfile?
我应该如何导入 SQL 转储文件?
采纳答案by Bjorn
Open the sql file and comment out the line that tries to create the existing database.
打开 sql 文件并注释掉尝试创建现有数据库的行。
回答by Max Kielland
This is a known bugat MySQL.
这是MySQL 的一个已知错误。
As you can see this has been a known issue since 2008 and they have not fixed it yet!!!
如您所见,这是自 2008 年以来的一个已知问题,但他们尚未修复!!!
WORK AROUND
You first need to create the database to import. It doesn't need any tables. Then you can import your database.
变通方法
您首先需要创建要导入的数据库。它不需要任何表。然后你就可以导入你的数据库了。
first start your MySQL command line (apply username and password if you need to)
C:\>mysql -u user -p
Create your database and exit
mysql> DROP DATABASE database; mysql> CREATE DATABASE database; mysql> Exit
Import your selected database from the dump file
C:\>mysql -u user -p -h localhost -D database -o < dumpfile.sql
首先启动您的 MySQL 命令行(如果需要,请应用用户名和密码)
C:\>mysql -u user -p
创建数据库并退出
mysql> DROP DATABASE database; mysql> CREATE DATABASE database; mysql> Exit
从转储文件中导入您选择的数据库
C:\>mysql -u user -p -h localhost -D database -o < dumpfile.sql
You can replace localhost with an IP or domain for any MySQL server you want to import to. The reason for the DROP command in the mysql prompt is to be sure we start with an empty and clean database.
您可以将 localhost 替换为要导入到的任何 MySQL 服务器的 IP 或域。在 mysql 提示符下使用 DROP 命令的原因是为了确保我们从一个空的干净的数据库开始。
回答by Paul Tomblin
It sounds like your database dump includes the information for creating the database. So don't give the MySQL command line a database name. It will create the new database and switch to it to do the import.
听起来您的数据库转储包含用于创建数据库的信息。所以不要给 MySQL 命令行一个数据库名称。它将创建新数据库并切换到它进行导入。
回答by MaysaM
If you create your database in direct admin or cpanel, you must edit your sql with notepad or notepad++ and change CREATE DATABASE
command to CREATE DATABASE IF NOT EXISTS
in line22
如果您在直接管理或 cpanel 中创建数据库,则必须使用记事本或记事本++编辑您的 sql 并将CREATE DATABASE
命令更改为第CREATE DATABASE IF NOT EXISTS
22 行
回答by wmlhust
I create the database myself using the command line. Then try to import again, it works.
我自己使用命令行创建数据库。然后再次尝试导入,它起作用了。