ruby-on-rails - 没有找到主要的连接池

标签 ruby-on-rails ruby postgresql google-cloud-sql cloud-sql-proxy

我正在使用 Rails 并且非常随机地遇到连接池错误,它不专门针对任何单个端点。我可以在大约 70% 的时间内命中端点而不会出现此错误。数据库是在谷歌云上运行的 PostgreSQL。这是我遇到的错误的主要内容:

#<ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished>
/usr/local/bundle/gems/activerecord-5.1.5/lib/active_record/connection_handling.rb:112:in `connection_pool'

ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.):

gem 文件:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


gem 'rails', '~> 5.1.5'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.7'
gem 'bcrypt', '~> 3.1.7'
gem 'jwt', '~> 1.5'
gem 'simple_command', '~> 0.0.9'
gem 'swagger-blocks', '~> 2.0.2'
gem 'active_model_serializers', '~> 0.10.2'
gem 'kaminari'
gem 'google-cloud-storage', '~> 1.10.0'
gem 'mini_magick', '~> 4.8.0'
gem 'carrierwave', '~> 1.2.0'
gem 'fog-google', '~> 1.3.0'
gem 'geocoder', '~> 1.4.7'
gem 'paper_trail', '~> 9.2.0'
gem 'paper_trail-association_tracking'
gem 'full-name-splitter', '~> 0.1.2'
gem 'pg_search'
gem 'mailgun-ruby', '~>1.1.6'


 gem 'rack-cors'

group :development, :test, :staging, :dev do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails', '~> 3.7'
  gem 'factory_bot_rails'
  gem 'dotenv-rails'
  gem 'pry-rails'
  gem 'stackprof'
  gem 'flamegraph'
  gem 'rack-mini-profiler'
  gem 'bullet'
  gem 'seed_dump'
end

group :development do
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

 gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
    gem 'devise', '~> 4.4.3'
    gem 'activeadmin', github: 'activeadmin'
    gem 'inherited_resources', git: 'https://github.com/activeadmin/inherited_resources'

数据库.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: rails_development

dev:
  <<: *default
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>
test:
  <<: *default
  database: rails_test

production:
  <<: *default
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>

staging:
  <<: *default
  database: <%= ENV['DB_NAME'] %>
  username: <%= ENV['DB_USER'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>

我们正在使用:

ruby 2.5.0 rails 5.1.5

在 Google CloudSQL 上使用 Postgresql

最佳答案

首先:

$ gem uninstall sqlite

然后在 gemfile 中将其更改为:

gem 'sqlite3', '~> 1.3.6' 

和:

$ bundle install 

$ rake db:migrate

然后重启服务器。 应该有效:)

关于ruby-on-rails - 没有找到主要的连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52106397/

相关文章:

postgresql - 函数返回 "query has no destination for result data"

ruby-on-rails - Rails 3检测来自移动客户端的请求

ruby-on-rails - Rails API 和原始 Rails

ruby - Rspec 字符串截断的最佳解决方法是什么?

ruby-on-rails - Ruby on Rails 和 Colons 的问题

postgresql - pg_restoring 非常大的单表使用 -j 选项需要几个小时

postgresql - 仅将 4D 用作前端应用程序

ruby-on-rails - 预期响应为 <redirect>,但为 <200>

javascript - 在 Rails 中使用 JQuery Jcrop 插件裁剪头像

ruby-on-rails - 在运行时生成新模型和模式