尝试 rake db:migrate 时未在 Rails 项目中选择数据库

发布于 2024-10-16 08:35:35 字数 482 浏览 4 评论 0原文

使用 Rails 应用程序,遇到某种奇怪的数据库/rake 问题。

当我执行时:

rake db:migrate

我收到以下错误:

Mysql2::Error: No database selected: SHOW TABLES

(See full trace by running task with --trace)

跟踪没有透露太多有用的信息。可以在这里看到: http://pastebin.com/WdsguudC

配置文件看起来是正确的,用户正在获取登录,否则我会收到某种访问错误。数据库存在,用户有正确的权限,我可以手动访问和操作它。我已经做了很多谷歌搜索,但没有发现任何有用的东西。不确定是否需要提供任何其他代码,因为这似乎是相当低级别的问题。

Working with a rails app, having some manner of weird database / rake issues.

When I execute:

rake db:migrate

I am getting the following error:

Mysql2::Error: No database selected: SHOW TABLES

(See full trace by running task with --trace)

The trace isn't revealing much useful information. Can be seen here: http://pastebin.com/WdsguudC

The config file looks right, and the user is getting logged in, or I would have gotten some kind of access error. The database exists, the user has correct permission, and I can access and manipulate it manually. I have done a bunch of googling, and haven't found anything helpful. Not sure if there is any other code that needs provided, because this seems like fairly low level problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

ゞ花落谁相伴 2024-10-23 08:35:35

毕竟这是 yaml 中的间距问题。

after all that it was a spacing issue in the yaml.

春花秋月 2024-10-23 08:35:35

请注意,ruby 在最近的 1.9.2 版本中交换了其 YAML 解析器。

这也可能会导致此问题。

为了切换回旧的 YAML 解析器 syck,请在 boot.rb 中使用:

require 'yaml'
YAML::ENGINE.yamler= 'syck'

Note that ruby has exchanged its YAML parser in a recent 1.9.2 version.

This might also cause this problem.

In order to switch back to the old YAML parser syck, use this in boot.rb:

require 'yaml'
YAML::ENGINE.yamler= 'syck'
吻泪 2024-10-23 08:35:35

嗯,这是我们初学者的常见问题。这个问题是在您在 Rails 中创建新项目时出现的。假设有一个示例,

$ rails new toy –d mysql
  • 在完成捆绑并启动服务器后,很可能会出现错误。要更正它,您需要转到 database.yml 并修改以下内容:

在密码字段中添加密码,如下所示,这是您用来保护 mysql 的密码。

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: mypassword
  socket: /tmp/mysql.sock

另外,在名称前添加井号标记 (#) 注释掉数据库,如下所示。

development:
 : *default
   database: #toy_development
  • 然后重新启动命令行并转到应用程序的根目录并键入:
$ rails s 

You have to see the Ruby on Rails 欢迎页面。

  • 之后,您需要创建数据库。

创建数据库。

问题消息表明未选择数据库。这是因为我没有创建一个。当您使用 MySQL 时,您必须创建一个,因此:

  • 转到我的应用程序的根目录并输入:
$ mysql –u root –p 
$ Passwor: mypassword (Enter your password, this is the one you entered to secure MySQL)

注意:此示例适用于名为 toy 的项目和用户我想授予的权限是mark,我要提供的密码是45mark。下面您将看到我在哪里应用这些元素。请记住在声明的每个部分应用您自己的元素。

为此项目创建并用户

  • 进入后,您将看到指针(mysql>),因此在其后键入:
mysql> GRANT ALL PRIVILEGES ON toy_development.* TO 'mark'@'localhost' IDENTIFIED BY '45mark';
  • 然后键入:
mysql> exit;
  • 通过键入检查它是否正常工作:
$ mysql –u mark –p toy_development
Enter password: 45mark (You enter the one you gave)
  • 打开database.yml文件并配置所需内容并根据需要进行修复。就我而言,我会将用户名标记为 45mark,密码标记为 45mark
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: mark
  password: 45mark
  socket: /tmp/mysql.sock

之前添加的井号标签 (#)

development:
  : *default
     database: toy_development 

- 另外,删除在保存

  • 。转到应用程序的根目录并输入
$ rake db:schema:dump

Done!!

我希望这有帮助。快乐编码!!

谢谢

Well, it is a common issue for us beginners. This issue comes from the moment when you create your new project in rails. Let’s say to have an example

$ rails new toy –d mysql
  • After you do the bundle and start your server, most likely you will have an error. To correct it you need to go to your database.yml and modify the following:

Add a password in the password field as shown below, this is the password you use to secure mysql.

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: mypassword
  socket: /tmp/mysql.sock

Also, comment out the database adding a hash tag (#)before the name as shown below

development:
 : *default
   database: #toy_development
  • Then restart your command line and go to the root of your application and type:
$ rails s 

You have to see the Ruby on Rails welcome page..

  • After, you need to create a database.

Create a DATABASE.

The issue message is saying that not DATABASE is selected. It is because I didn’t create one. When you work with MySQL you have to create one, so:

  • Go to the root of my application and type:
$ mysql –u root –p 
$ Passwor: mypassword (Enter your password, this is the one you entered to secure MySQL)

Note: This example works wit a project called toy and the user I wanted to grant privileges is mark and the password I’ll give is 45mark. Below you will see where I apply these elements. Remember to apply your own elements on each part of the statement.

Create and user for this project

  • Once you are in, you will see the pointer (mysql> ), so type after it:
mysql> GRANT ALL PRIVILEGES ON toy_development.* TO 'mark'@'localhost' IDENTIFIED BY '45mark';
  • Then type:
mysql> exit;
  • Check that it is working by typing:
$ mysql –u mark –p toy_development
Enter password: 45mark (You enter the one you gave)
  • Open database.yml file and configure what is needed and fix as required. In my case I will chance the username to mark and the password to 45mark
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: mark
  password: 45mark
  socket: /tmp/mysql.sock

- Also, REMOVE the hash tag (#) added before

development:
  : *default
     database: toy_development 

Save it.

  • Go to the root of the application and type
$ rake db:schema:dump

Done!!

I hope this helps. Happy coding!!

Thanks

如歌彻婉言 2024-10-23 08:35:35

我对 ruby​​ 1.9.2-p180 遇到了同样的问题,升级到 p290 并且它可以工作

I had the same issue with ruby 1.9.2-p180 , upgraded to p290 and it works

傲世九天 2024-10-23 08:35:35

只需重启服务器即可;在命令行中:

  1. Ctrl + C

  2. 执行:

    rails
    

Just restart the server; in the command line:

  1. Press Ctrl + C

  2. execute:

    rails s
    
給妳壹絲溫柔 2024-10-23 08:35:35

当我输入 rake db:schema:dump 时,我遇到了类似的错误,结果我只需注释掉我的 yaml 文件中的所有数据库,除了我的 development 数据库。

I had a similar error when i typed rake db:schema:dump and it turns out that I just have to comment out all the databases on my yaml file except my development one.

终陌 2024-10-23 08:35:35

尝试一下这个。

rake db:test:prepare

安装它以查看您是否确实创建了表。打开 db 文件夹中的“development.sqlite3

http://sqlitebrowser.org/

Give a try to this.

rake db:test:prepare

Install this to see if you have actually created a table or not. Open the "development.sqlite3" in db folder

http://sqlitebrowser.org/

雨后咖啡店 2024-10-23 08:35:35

这是一个简单的错误,检查整个database.yml文件,看看默认描述在哪里给出,如果没有给出数据库名称,那么看看下面,还会给出另一个开发名称,其中数据库的配置是使用检查,给你的其中数据库名称

default: &default
     adapter: mysql2
     encoding: utf8
     pool: 5
     username: root
     password: 12345
     host: localhost

development:
      <<: *default
      database: db_name

Its a simple error checkout the entire database.yml file and see that where is default decription is given database name is given or not if not then look below it there will another development name is also given where configuration of database is use check that give your database name in it

default: &default
     adapter: mysql2
     encoding: utf8
     pool: 5
     username: root
     password: 12345
     host: localhost

development:
      <<: *default
      database: db_name
美胚控场 2024-10-23 08:35:35

一个潜在原因是定义了DATABASE_URL环境变量。

$ echo $DATABASE_URL
=> mysql2://root@localhost:3306

如果您得到与上述 url 类似的输出(即数据库名称不存在),那么您可能需要将数据库名称添加到字符串中或取消设置环境变量。

$ export DATABASE_URL=mysql2://root@localhost:3306/my_rails_app_development
$ unset DATABASE_URL

如果您取消设置 var,您可能想在database.yml 中指定数据库详细信息。

One potential cause is that there is a DATABASE_URL environment variable defined.

$ echo $DATABASE_URL
=> mysql2://root@localhost:3306

If you get a similar output to the above url (i.e., the database name is not present), then you might want to add the database name to the string or unset the env var.

$ export DATABASE_URL=mysql2://root@localhost:3306/my_rails_app_development
$ unset DATABASE_URL

If you unset the var, you probably want to specify the database details in database.yml instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文