MySQL 错误 1046 未选择数据库,如何解决?

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

Error 1046 No database Selected, how to resolve?

sqlmysqlmysql-error-1046

提问by steph

Error SQL query:

错误 SQL 查询:

--
-- Database: `work`
--
-- --------------------------------------------------------
--
-- Table structure for table `administrators`
--
CREATE TABLE IF NOT EXISTS `administrators` (

`user_id` varchar( 30 ) NOT NULL ,
`password` varchar( 30 ) NOT NULL ) ENGINE = InnoDB DEFAULT CHARSET = latin1;

MySQL said:

MySQL 说:

#1046 - No database selected

need some help here.

这里需要一些帮助。

回答by codaddict

You need to tell MySQL which database to use:

你需要告诉 MySQL 使用哪个数据库:

USE database_name;

before you create a table.

在创建表之前。

In case the database does not exist, you need to create it as:

如果数据库不存在,您需要将其创建为:

CREATE DATABASE database_name;

followed by:

其次是:

USE database_name;

回答by Shay Anderson

You can also tell MySQL what database to use (if you have it created already):

您还可以告诉 MySQL 要使用哪个数据库(如果您已经创建了它):

 mysql -u example_user -p --database=example < ./example.sql

回答by Mina Fawzy

I faced the same error when I tried to import a database created from before. Here is what I did to fix this issue:

当我尝试导入之前创建的数据库时,我遇到了同样的错误。这是我为解决此问题所做的工作:

1- Create new database

1-创建新数据库

2- Use it by usecommand

2-通过use命令使用它

enter image description here

在此处输入图片说明

3- Try again

3-再试一次

This works for me.

这对我有用。

回答by OMG Ponies

If you're trying to do this via the command line...

如果您尝试通过命令行执行此操作...

If you're trying to run the CREATE TABLE statement from the command line interface, you need to specify the database you're working in before executing the query:

如果您尝试从命令行界面运行 CREATE TABLE 语句,则需要在执行查询之前指定您正在使用的数据库:

USE your_database;

Here's the documentation.

这是文档

If you're trying to do this via MySQL Workbench...

如果您尝试通过 MySQL Workbench 执行此操作...

...you need to select the appropriate database/catalog in the drop down menu found above the :Object Browser: tab. You can specify the default schema/database/catalog for the connection - click the "Manage Connections" options under the SQL Development heading of the Workbench splash screen.

...您需要在 :Object Browser: 选项卡上方的下拉菜单中选择适当的数据库/目录。您可以为连接指定默认架构/数据库/目录 - 单击 Workbench 初始屏幕的 SQL Development 标题下的“管理连接”选项。

Addendum

附录

This all assumes there's a database you want to create the table inside of - if not, you need to create the database before anything else:

这一切都假设有一个数据库要在其中创建表 - 如果没有,则需要先创建数据库:

CREATE DATABASE your_database;

回答by Roanna

If you are doing this through phpMyAdmin:

如果您通过 phpMyAdmin 执行此操作:

  • I'm assuming you already Created a new MySQL Database on Live Site (by live site I mean the company your hosting with (in my case Bluehost)).

  • Go to phpMyAdmin on live site - log in to the database you just created.

  • Now IMPORTANT! Before clicking the "import" option on the top bar, select your database on the left side of the page (grey bar, on the top has PHP Myadmin written, below it two options:information_schema and name of database you just logged into.

  • once you click the database you just created/logged into it will show you that database and then click the import option.

  • 我假设您已经在实时站点上创建了一个新的 MySQL 数据库(实时站点是指您托管的公司(在我的情况下为 Bluehost))。

  • 转到实时站点上的 phpMyAdmin - 登录到您刚刚创建的数据库。

  • 现在很重要!在点击顶部栏的“导入”选项之前,在页面左侧选择您的数据库(灰色栏,顶部写有 PHP Myadmin,其下方有两个选项:information_schema 和您刚刚登录的数据库名称。

  • 单击刚刚创建/登录的数据库后,它将显示该数据库,然后单击导入选项。

That did the trick for me. Really hope that helps

这对我有用。真的希望有帮助

回答by Ayham AlKawi

  • Edit your SQLfile using Notepador Notepad++
  • add the following 2 line:
  • SQL使用记事本记事本++编辑您的文件
  • 添加以下 2 行:

CREATE DATABASE NAME; USE NAME;

CREATE DATABASE NAME; USE NAME;

回答by ivan n

If importing a database, you need to create one first with the same name, then select it and then IMPORT the existing database to it.

如果导入数据库,需要先创建一个同名的数据库,然后选中它,然后将现有的数据库导入其中。

Hope it works for you!

希望这对你有用!

回答by Eric Korolev

For MySQL Workbench

对于 MySQL 工作台

  1. Select database from Schemas tab by right mouse clicking.
  2. Set database as Default Schema
  1. 通过鼠标右键单击从 Schemas 选项卡中选择数据库。
  2. 将数据库设置为默认架构

enter image description here

在此处输入图片说明

回答by iversoncru

quoting ivan n : "If importing a database, you need to create one first with the same name, then select it and then IMPORT the existing database to it. Hope it works for you!"

引用 ivan n :“如果导入数据库,您需要先创建一个同名的数据库,然后选择它,然后将现有数据库导入其中。希望它对您有用!”

These are the steps: Create a Database, for instance my_db1, utf8_general_ci. Then click to go inside this database. Then click "import", and select the database: my_db1.sql

这些是步骤: 创建一个数据库,例如 my_db1、utf8_general_ci。然后点击进入这个数据库。然后点击“导入”,选择数据库:my_db1.sql

That should be all.

这应该是全部。

回答by user3338098

be careful about blank passwords

小心空白密码

mysqldump [options] -p '' --databases database_name

will ask for a password and complain with mysqldump: Got error: 1046: "No database selected" when selecting the database

会要求输入密码并抱怨 mysqldump: Got error: 1046: "No database selected" when selecting the database

the problem is that the -poption requires that there be no space between -pand the password.

问题是该-p选项要求-p和 密码之间没有空格。

mysqldump [options] -p'' --databases database_name

solved the problem (quotes are not needed anymore).

解决了问题(不再需要引号)。