mongodb speed on remote server [duplicate] - ruby-on-rails

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'] %>

Related

How to connect to remote mysql server from Rails application?

I am trying to connect to remote mysql server. This is how I set up my connection:-
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: 13.58.156.50
port: 3306
# username: <%= ENV['FOODCLUBE_DATABASE_USER_DEVELOPMENT'] %>
# password: <%= ENV['FOODCLUBE_DATABASE_PASSWORD_DEVELOPMENT'] %>
username: root
password: Admin123#
socket: /var/run/mysqld/mysqld.sock
variables:
sql_mode: "TRADITIONAL"
development:
<<: *default
database: timer
# food_delivery_development
test:
<<: *default
database: food_club_test
production:
<<: *default
database: food_clube_production
username: <%= ENV['FOODCLUBE_DATABASE_USER_PRODUCTION'] %>
password: <%= ENV['FOODCLUBE_DATABASE_PASSWORD_PRODUCTION'] %>
When I am trying to hit a query from the Rails console, I am getting this error message:-
Mysql2::Error::ConnectionError: Lost connection to MySQL server at 'reading initial communication packet', system error: 0
How can I fix this?

Staring Rails App With Postgresql - PG::ConnectionBad

I Cannot get the welcome screen not matter what I do, all I get is the connection issue with the postgres db error.
I ran this command to start the app.
rails new -T appName --database=postgresql
Here is my database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
adapter: postgresql
encoding: unicode
database: dbname
pool: 5
username: toolshiring
password: toolshiring
host: localhost
port: 5432
production:
<<: *default
database: toolshiring
username: toolshiring
password: <%= ENV['toolshiring'] %>
I used pgAdmin to add a user with full permissions named toolshiring and has the same password. Then created the database with owner toolshiring.
I decided to try something and it seems to have worked for the time being.
I decided to reset the password for the default db 'postgres' to the password 'postgres' as well, and delete the new db that I created....because the wasn't working for some reason.
in my database.yml file i commented everything but this
development:
adapter: postgresql
encoding: unicode
database: postgres
pool: 5
username: postgres
password: postgres
host: localhost

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?

How to set up test database configuration in Rails

I am working on existing rails project and just finished writing some basic RSpec tests for that.
In our database.yml file we have connection to 3-4 different databases and all are either pointing to either production database or staging database.
For testing environment I created a different yml file called database.test.yml and then symlinked it to database.yml file, so that the correct testing databases connections are created.
My database.test.yml looks something like this:
abc:
adapter: mysql2
host: localhost
reconnect: true
username: monty
password: python
database: abc
pqr:
adapter: mysql2
host: localhost
reconnect: true
username: monty
password: python
database: pqr
test:
adapter: mysql2
host: localhost
reconnect: true
username: monty
password: python
pool: 50
database: testing
My problem now is that how can I enforce that other developers/testers use this database.test.yml and not accidentally run the main database.yml file which contains connections to staging and production databases.
EDIT:
To further clarify my question, I have establish_connection in various models that connect to different databases. So I am not sure the earlier suggestions will solve my problem.
Example:
class Abc < ActiveRecord::Base
establish_connection :abc
end
One way I can think of for solving my problem is doing something like this:
class Abc < ActiveRecord::Base
establish_connection "abc_#{Rails.env}"
end
and in the database.yml:
defaults: &defaults
adapter: mysql2
encoding: utf8
username: root
password: secret
abc_production:
database: abc
<<: *defaults
abc_development:
database: abc_devlopment
<<: *defaults
abc_test:
database: abc_test
<<: *defaults
But I am not sure if this the best practice and I really don't feel to change the application code to setup test environment is good practice.
This idea is based on: http://blog.nistu.de/2012/03/25/multi-database-setup-with-rails-and-rspec
Any nudge or pointers towards the correction direction will be greatly helpful.
You could have something, like:
default: &default
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: 127.0.0.1
port: 3306
development:
<<: *default
database: xxxx_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: xxxx_test
production:
<<: *default
database: xxxx_production

Mongoid Production Issue: Failed to connect to a master node

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.

Resources