简体   繁体   English

Rails:没有 ActiveRecord::Base 的连接池

[英]Rails: No connection pool for ActiveRecord::Base

I'm trying to use rails 4.2.6 to develop an app.我正在尝试使用 rails 4.2.6 开发应用程序。 I'm trying to use postgres for database.我正在尝试将 postgres 用于数据库。 Server starts fine but when I try loading a page it throws this "No connection pool for ActiveRecord::Base" error.服务器启动正常,但是当我尝试加载页面时,它会抛出此“ActiveRecord::Base 没有连接池”错误。

What could it be?可能是什么?

EDIT编辑

The pg gem wasn't working properly. pg gem 无法正常工作。 I had to comment it before starting the server and then uncomment it from my GemFile afterwards.我必须在启动服务器之前对其进行评论,然后在我的 GemFile 中取消评论它。 I realized that I was using Ruby 2.3 instead of Ruby 2.0 (as intended).我意识到我使用的是 Ruby 2.3 而不是 Ruby 2.0(正如预期的那样)。 I removed ruby 2.3 and set up everything under ruby 2.0 environment.我删除了 ruby​​ 2.3 并在 ruby​​ 2.0 环境下设置了所有内容。 It's now working properly.它现在工作正常。

I had read somewhere that there were some issues with the 'pg' gem in newer Rails releases, requiring people to use 'gem install pg --pre' instead for installing the gem.我在某处读到过,在较新的 Rails 版本中,'pg' gem 存在一些问题,要求人们使用 'gem install pg --pre' 来安装 gem。 I tried that, but then my app was requiring the 'pg' gem in my GemFile and, well, the problem stated above showed up again.我试过了,但后来我的应用程序需要我的 GemFile 中的“pg”gem,好吧,上面提到的问题又出现了。

This is how my database.yml file ended up:这就是我的 database.yml 文件的最终结果:

  default: &default
     adapter: postgresql
     encoding: unicode
     host: localhost
     username: -------
     password: -------
     pool: 5

  development:
     <<: *default
     database: myDbName

If you are experiencing this error from a rake task, chances are you are not running the :environment task before your task.如果您在执行 rake 任务时遇到此错误,很可能您没有在执行任务之前运行:environment任务。

Changing:改变:

task :task_name do
end

to:到:

task task_name: :environment do
end

Should fix the issue.应该解决问题。

This issue arises when the server cannot find the corresponding database it depends on to pull data from.当服务器找不到它所依赖的相应数据库来提取数据时,就会出现此问题。

I had same issue from my end, I updated my version of Sqlite3, and it was higher than the version that the current Puma Server version supports.我最后遇到了同样的问题,我更新了我的 Sqlite3 版本,它高于当前 Puma Server 版本支持的版本。

I simply had to uninstall the Sqlite3 version, and then install the version supported by the current version of Puma Server.我只需要卸载Sqlite3版本,然后安装当前版本的Puma Server支持的版本。

gem uninstall sqlite3

This will uninstall the updated version of Sqlite3, and then you run the code below stating the version supported by your current server.这将卸载 Sqlite3 的更新版本,然后运行下面的代码,说明当前服务器支持的版本。

gem install sqlite3

Or you can as well open your Gemfile, and then include the version for the database that you are using或者你也可以打开你的 Gemfile,然后包含你正在使用的数据库的版本

gem 'sqlite3', '~> 1.3.6' 

N/B: The sqlite3 version is the most current version as at the time of writing this answer N/B:在撰写此答案时,sqlite3 版本是最新版本

And then run然后运行

bundle update

to install the version of the database that you specified.安装您指定的数据库版本。

That's all.仅此而已。

I hope this helps.我希望这会有所帮助。

For PostgreSQL your database.yml file should look something like that:对于 PostgreSQL,您的database.yml文件应如下所示:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

development:
  <<: *default
  database: your_db_name

Also make sure that you have the gem installed: in your Gemfile:还要确保您安装了 gem: 在您的 Gemfile 中:

gem 'pg'

Finally, restart your server.最后,重启你的服务器。

Hope that helps希望有帮助

Edit: I almost forgot, make sure you have your PostgresSQL running, check this link for download and setup.编辑:我差点忘了,请确保您的 PostgresSQL 正在运行,请查看此链接以进行下载和设置。

Check the database.yml if all settings are ok.如果所有设置都正常,请检查database.yml If its the first time and you didn't create the database yet use this command to create the database如果是第一次并且您还没有创建数据库,请使用此命令创建数据库

rake db:create

If the database already exists try reset the db migrations,如果数据库已经存在,请尝试重置数据库迁移,

rake db:migrate:reset

Hope it'll solve the problem.希望它能解决问题。 Go to rails console and try something to check if its working or not.转到 rails 控制台并尝试检查其是否正常工作。

我不得不简单地重新启动服务器以使警告消失。

I've encountered the same problem when I try to access the Model before creating the DataBase.在创建数据库之前尝试访问模型时,我遇到了同样的问题。

Make sure you run rake db:migrate at least once.确保您至少运行rake db:migrate一次。

If you already run migration then check the Database as mentioned in previous answers.如果您已经运行迁移,请检查之前答案中提到的数据库。

In my case, the config/database.yml was taking variables from the environment:就我而言, config/database.yml从环境中获取变量:

# ...
test:
  <<: *default
  database: <%= ENV.fetch("DB_NAME") %>_test
  username: <%= ENV.fetch("DB_USER") %>
  password: <%= ENV.fetch("DB_PASS") %>
# ...

The error was coming when the new terminal window where I was running the bundle exec rails spec did not have those variables initialized.当我运行bundle exec rails spec的新终端窗口没有初始化这些变量时,就会出现错误。

Doing,做,

$ export DB_NAME=mydb
$ export DB_USER=myuser
$ export DB_PASS=mypass
$ bundle exec rails spec

fixed the issue.解决了这个问题。

when you are running rails db:migrate in data base rows will be created according to your migration files.当您运行rails db:migrate ,将根据您的迁移文件创建数据库行中的行。 you can see schema for further info.您可以查看架构以获取更多信息。

Rails 5导轨 5

New app新应用

Using Postgresql for both test and development使用 Postgresql 进行测试和开发

Specs run, so Rails can connect to Postgresql规范运行,因此 Rails 可以连接到 Postgresql

But when I started the web app, I got "No connection pool with id primary found."但是当我启动 Web 应用程序时,我收到“未找到 id 主要的连接池”。

Ran 'bin/rails db:migrate:reset' and restarted the app.运行 'bin/rails db:migrate:reset' 并重新启动应用程序。 It worked.它奏效了。 No idea why.不知道为什么。

database.yml:数据库.yml:

    development:
      adapter: postgresql
      encoding: unicode
      database: rails5_development
      pool: 5
      username: foo
      password: bar
      host: localhost
      port: 5432

    test:
      adapter: postgresql
      encoding: unicode
      database: rails5_test
      pool: 5
      username: foo
      password: bar
      host: localhost
      port: 5432

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActiveRecord::ConnectionNotEstablished: ActiveRecord::Base with mongoid 没有连接池 - ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base with mongoid 关闭 Rails ActiveRecord 连接池 - Close Rails ActiveRecord Connection Pool 尝试使用ActiveRecord :: Base.connection_pool.with_connection做一些事情 - Trying to do something with ActiveRecord::Base.connection_pool.with_connection 出现“找不到‘ActiveRecord::Base’的连接池”的间歇性错误 - Getting intermittent error of "No connection pool for 'ActiveRecord::Base' found" 如何使用Ruby on Rails创建新的ActiveRecord连接池? - How to create a new ActiveRecord Connection Pool with Ruby on Rails? 一旦达到连接池限制,带有MySQL的Rails ActiveRecord就会挂起 - Rails ActiveRecord w/ MySQL Hangs Once Connection Pool Limit is Reached 不同模型中的ActiveRecord :: Base。Establishment_connection是否使用相同的连接池? - Does ActiveRecord::Base.establish_connection in different models use same connection pool? ActiveRecord 数组::Base.connection.execute Ruby On Rails - Array Of ActiveRecord::Base.connection.execute Ruby On Rails 如何从Rails中的ActiveRecord :: Base.connection.execute中捕获错误? - How to capture errors from ActiveRecord::Base.connection.execute in Rails? Rails ActiveRecord::Base.connection 在外部文件上执行 - Rails ActiveRecord::Base.connection to execute upon an external file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM