MySQL 错误:#1046 - 未选择数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17126677/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:56:35  来源:igfitidea点击:

MySQL Error: #1046 - No database selected

mysql

提问by user2038001

I ran into an issue using MySQL 5.5 running on xampp (Version 1.8.1). I kept receiving the "#1046 - No database selected" error when trying to add a table to a database via the console window.

我在使用 xampp(版本 1.8.1)上运行的 MySQL 5.5 时遇到了问题。尝试通过控制台窗口向数据库添加表时,我一直收到“#1046 - 未选择数据库”错误。

I had made sure the database was selected "USE database_name;", however I was still receiving the same error.

我已经确保选择了数据库“USE database_name;”,但是我仍然收到相同的错误。

I double checked and found all privileges for the user were enabled. Frustrated, I "Uncheck All" then "Check All" global privileges.

我仔细检查,发现用户的所有权限都已启用。沮丧,我“取消选中全部”然后“全部选中”全局权限。

I logged out, restarted server services, closed the browser, restarted server services, opened browser, and logged in.

我注销,重新启动服务器服务,关闭浏览器,重新启动服务器服务,打开浏览器,然后登录。

Now I am able to create tables via the console. It appears to me resetting the privileges fixed the issue.

现在我可以通过控制台创建表。在我看来,重置权限解决了这个问题。

Can anyone tell me if this is a known bug, if I got lucky, or missed the point completely? Documentation I found concerning this error was mostly for importing a database and using the "Use Database_Name;" to ensure the database was indeed selected. However I don't believe this was the issue.

谁能告诉我这是一个已知的错误,如果我很幸运,还是完全错过了这一点?我发现的有关此错误的文档主要用于导入数据库并使用“Use Database_Name;” 以确保确实选择了数据库。但是,我不相信这是问题所在。

Please Note: While trying to create new databases I am once again receiving the #1046 error. The method I mentioned above was a fluke and is not allowing me to create any new tables inside a new database. However I am still able to work with the first database I created.

请注意:在尝试创建新数据库时,我再次收到 #1046 错误。我上面提到的方法是侥幸,它不允许我在新数据库中创建任何新表。但是,我仍然可以使用我创建的第一个数据库。

回答by rkhff

I know this is a very late answer but for the benefit of anyone coming across this error, it might also pop up when the database name includes a dash. If so you need to put backticks around the database name, like so:

我知道这是一个很晚的答案,但为了任何遇到此错误的人的利益,当数据库名称包含破折号时,它也可能会弹出。如果是这样,您需要在数据库名称周围加上反引号,如下所示:

GRANT ALL ON `database-name`.* TO 'username'@'localhost';

The error message is not very helpful as it suggest you need to select a database in order to grant privileges to a user.

该错误消息不是很有帮助,因为它建议您需要选择一个数据库才能向用户授予权限。

回答by Timothy C. Quinn

Another late answer but this can also happen when the table name (.*) is not specified. I ran fixed by adding the .* after database name to get it to work.

另一个迟到的答案,但这也可能发生在未指定表名 (.*) 时。我通过在数据库名称后添加 .* 来修复它以使其正常工作。

Before:

前:

GRANT ALL PRIVILEGES ON my_db_name TO ...

After:

后:

GRANT ALL PRIVILEGES ON my_db_name.* TO ...

This change fixed the somewhat ambiguous error "ERROR 1046 (3D000): No database selected"

此更改修复了有些模棱两可的错误“ERROR 1046 (3D000): No database selected”

回答by Sam

If privileges have been directly inserted into the table 'user' (many web frontends such as phpMyAdmin do that), one needs to execute FLUSH PRIVILEGES(phpMyAdmin has a button for this) or restart the server to reload the privileges. Neither is required when GRANTis used.

如果权限已直接插入到表“用户”中(许多 Web 前端,例如 phpMyAdmin 都这样做),则需要执行FLUSH PRIVILEGES(phpMyAdmin 有一个用于此的按钮)或重新启动服务器以重新加载权限。使用时两者都不需要GRANT