Mongoid Production Issue: Failed to connect to a master node - ruby-on-rails

I've switched away from Mongo_Mapper to Mongoid and am having trouble deploying to production, for some reason. I'm using NGINX, Rails 3.1, and Passenger. I keep getting this message, "Failed to connect to a master node at myusernamehere:27017 (Mongo::ConnectionFailure)".
defaults: &defaults
host: localhost
# slaves:
# - host: slave1.local
# port: 27018
# - host: slave2.local
# port: 27019
development:
<<: *defaults
database: s3uploadergen_development
test:
<<: *defaults
database: s3uploadergen_test
production:
host: localhost
port: 27017
database: mydbnamehere
username: myuserhere
password: mypasswordhere
I've triple-checked all settings and tried the ENV approach as well (adding the ENV variables to production.rb and calling them via the documented mongoid approach but had the same issue):
production:
host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
username: <%= ENV['MONGOID_USERNAME'] %>
password: <%= ENV['MONGOID_PASSWORD'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
Ideally, I want to just specify it either in production.rb or an initializer of some sort.

I'm assuming that by "documented mongoid approach" you mean setting the recommended "uri" param instead of all those different setttings. You might want to try it since it's the recommended way of doing it.
defaults: &defaults
persist_in_safe_mode: true
development:
<<: *defaults
host: localhost
database: app_development
test:
<<: *defaults
host: localhost
database: app_test
production:
<<: *defaults
uri: <%= ENV['MONGOHQ_URL'] %>
Note that I do use Heroku but I don't use the MongoHQ add on. I just use it directly, so I manually set my MONGOHQ_URL. Your uri would look something like:
mongodb://<user>:<password>#<the.db.host.com>:<port>/<database_name>
Looks to me like you can't connect to "localhost" based on the error (like maybe you need the full host name or IP or something?). Anything in your app logs?
Just make sure not to set "host" and "uri" on any of the ENV's because "host" will override the setting that is derived from the uri.

Related

Rails, datatabse creation issue

I am trying to create database using default user 'postgres'
but while executing rails db:create not seleting role name postgres, it's selecting my systme name 'mysystem'.
below is my database.yml file code
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: user_development
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
test:
<<: *default
database: user_test
production:
<<: *default
host: <%= ENV['DATABASE_HOST_URL'] %>
database: user_production
username: <%= ENV['USER_DATABASE_USERNAME'] %>
password: <%= ENV['USER_DATABASE_PASSWORD'] %>
Error while executing rails db:create
rails aborted!
ActiveRecord::ConnectionNotEstablished: FATAL: role "mysystem" does not exist
Caused by:
PG::ConnectionBad: FATAL: role "mysystem" does not exist
There is some helpful information here:
https://guides.rubyonrails.org/v4.2.7/configuring.html#configuring-a-database
sections 3.12 and 3.13
I think your issue might be the host: setting. This should either
be /tmp for a unix domain socket, or an ip address / hostname, if used.
Settings I used in testing:
postgresql 12.3 debian unix.
Using this pg_hba.conf:
local all all ident
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
host all all 192.168.50.0/24 md5
You might want a hostssl entry for data being sent between machines.
# using the same "user" database name you have.
rails new user -d postgresql
#setting the password for testing.
psql
alter role user1 password 'testpass';
cd user/config
emacs database.yml
#example using ident authentication - being logged in as the same unix user.
production:
<<: *default
host: /tmp
database: user_production
username: user1
password: nil
# using a host ip address
production:
<<: *default
host: 192.168.50.6
database: user_production
username: user1
password: testpass
# using a host ip address with env vars
#export USER_DATABASE_USERNAME="user1"
#export USER_DATABASE_PASSWORD="testpass"
production:
<<: *default
host: 192.168.50.6
database: user_production
username: <%= ENV['USER_DATABASE_USERNAME'] %>
password: <%= ENV['USER_DATABASE_PASSWORD'] %>
# hardcoding everything in the url for illustration:
production:
<<: *default
url: postgres://user1:testpass#192.168.50.6/user_production
# best practice according to doc. DATABASE_URL has precedence over database.yaml when set
# we are just indicating we know that here.
# export DATABASE_URL="postgres://user1:testpass#192.168.50.6/user_production"
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
user1#debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:create
Created database 'user_production'
user1#debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:drop
Dropped database 'user_production'
#Seeing what is being used:
RAILS_ENV=production bin/rails runner 'p ActiveRecord::Base.configurations'
#<ActiveRecord::DatabaseConfigurations:0x0000564e40093fa0 #configurations=[#<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e40092f60 #env_name="default", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400928f8 #env_name="development", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_development"}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400921a0 #env_name="test", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_test"}>, #<ActiveRecord::DatabaseConfigurations::UrlConfig:0x0000564e40091b60 #env_name="production", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :username=>"user1", :password=>"testpass", :database=>"user_production", :host=>"192.168.50.6"}, #url="postgres://user1:testpass#192.168.50.6/user_production">]>

POSTGRES Problems : fe_sendauth: no password supplied

I have seen many answers on here to "solve" this similar issue,However my database.yml in my app is not formatted the same.
Rails S: Works
Rails C: Works
Rails db:create -> Error: fe_sendauth:
adapter: postgresql
encoding: unicode
host: ''
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: chic_development
host: localhost
test:
<<: *default
database: chic_test
production:
<<: *default
database: chic_production
username: chic
password: <%= ENV['CHIC_DATABASE_PASSWORD'] %> ```
My Postgres App is on port 5433, not 5432. I am unsure if this is relevant. But in General I am constantly having issues with my PG being flighty.
Please Help.
I looked up my postgres.app and looked to see the server name and used that as username..got my password and put them in my database.yml file like this...
username: xxxxxx
password: xxxxxxxxxxx
...under the "default" block of code then bundled everything and it worked. Thanks

rake not seeing Rails.application.secrets

In short:
seems that rake does not have access to Rails.application.secrets in config/database.yml file
what is the purpose of config/secrets.yml then?
In long:
When I run
RAILS_ENV=production rake db:migrate
I get the error Mysql2::Error: Access denied for user 'root'#'localhost' (using password: NO), though I specified appropriate values in config/database.yml and the user connecting should not be 'root'. This is an excerpt from respective config files:
# config/database.yml
production:
<<: *default
adapter: mysql2
host: localhost
database: <%= Rails.application.secrets[:database][:name] %>
username: <%= Rails.application.secrets[:database][:username] %>
password: <%= Rails.application.secrets[:database][:password] %>
# config/secrets.yml
production:
secret_key_base: very-long-blah-blah-blah
database:
name: app_db_name
username: app_db_user
password: app_db_password
Seems that rake has no access to Rails.application.secrets. Running migration succeeds when I explicitly put necessary values in database.yml, for example, as follows:
production:
<<: *default
adapter: mysql2
host: localhost
database: <%= Rails.application.secrets[:database][:name] || 'app_db_name' %>
username: <%= Rails.application.secrets[:database][:username] || 'app_db_user' %>
password: <%= Rails.application.secrets[:database][:password] || 'app_db_password' %>
The above proves that Rails.application.secrets[:database][:name] resolves to nothing.
How to have access to Rails.application.secrets in rake? Would this be the correct solution?
I know that I can use ENV[VARNAME] to fill in secret sections of config/database.yml. But what the the purpose of config/secrets.yml file then?
Moreover, I am using Passenger, which means that variables in .bashrc will probably not be accessible to the web server (I had this issue with secret_key_base). Therefore I try to avoid using environment variable. Just do not want to have all my secrets spilled all over the server.
rails-4.2.2, Ubuntu LTS 14.04
I haven't seen such nested content for the secrets.yml like you have, also the release notes doesn't have such kind. You should be just fine with the below code
# config/secrets.yml
production:
secret_key_base: very-long-blah-blah-blah
name: app_db_name
username: app_db_user
password: app_db_password
And in the database.yml
# config/database.yml
production:
<<: *default
adapter: mysql2
host: localhost
database: <%= Rails.application.secrets.name %>
username: <%= Rails.application.secrets.username %>
password: <%= Rails.application.secrets.password %>

rails SQlite 3: Cant open the connection

I have existing rails project with PostgreSQL DB. When I am running the same in my local machine I am receiving SQLite exception.
But my database.yml configured with postgres adapter only.
Here is the exception:
SQLite3::CantOpenException: unable to open database file
database.yml
postgres: &postgres
adapter: postgresql
encoding: unicode
host: localhost
pool: 5
username: postgres
password: postgres
min_messages: warning
development:
<<: *postgres
database: dev_development
username: <%= ENV['PG_USER'] || 'postgres' %>
password: <%= ENV['PG_PASSWORD'] || 'postgres' %>
test:
<<: *postgres
database: dev_test
username: <%= ENV['PG_USER'] || 'postgres' %>
password: <%= ENV['PG_PASSWORD'] %>
Perhaps you have an environment variable set (DATABASE_URL) that is overriding your database.yml file? Look at this for more info: http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database
If that's not it, is it possible you recently changed your database.yml file and haven't restarted the server?

mongodb speed on remote server [duplicate]

I have my Yaml configuration file, mongo.yml:
development:
adapter: mongodb
database: fhsclock_development
host: localhost
port: nil
test:
adapter: mongodb
database: fhsclock_test
host: localhost
port: nil
production:
adapter: mongodb
database: fhsclock
hosts:
- - localhost
- nil
- - staff.mongohq.com
- 10015
How do I use this file for configuration and connection with MongoMapper?
MongoMapper will just use the file if it's you're using Rails and the file is at config/mongo.yml. If you're not on Rails, you can adapt this code from the source:
config_file = Rails.root.join('config/mongo.yml')
if config_file.file?
config = YAML.load(ERB.new(config_file.read).result)
MongoMapper.setup(config, Rails.env, :logger => Rails.logger)
end
Also, the "adapter" in your file is extraneous. (See the Getting Started documentation). A mongo.yml from rails g mongo_mapper:config looks like:
defaults: &defaults
host: 127.0.0.1
port: 27017
development:
<<: *defaults
database: my_app_development
test:
<<: *defaults
database: my_app_test
# set these environment variables on your prod server
production:
<<: *defaults
database: my_app
username: <%= ENV['MONGO_USERNAME'] %>
password: <%= ENV['MONGO_PASSWORD'] %>

Resources