I have a rails app using 2 databases (say DB1, DB2). Also the DB1 is replicated to DB3, DB2 to DB4. I am using octopus gem for replication i.e. all write operations are on DB1 and read operations from DB3.
Following is my setting for database.yml
production:
adapter: mysql2
database: DB1
username: root
host: localhost
password: root
pool: 5
timeout: 5000
reconnect: true
DB2_production:
adapter: mysql2
database: DB2
username: root
host: localhost
password: root
pool: 5
timeout: 5000
reconnect: true
Now I have file shards.yml as following
octopus:
replicated: true
environments:
- production
- DB2_production
production:
slave1:
adapter: mysql2
database: DB3
username: root
host: localhost
password: root
pool: 5
timeout: 5000
reconnect: true
DB2_production:
slave2:
adapter: mysql2
database: DB4
username: root
host: localhost
password: root
pool: 5
timeout: 5000
reconnect: true
Also I am using 'octopus_establish_connection' to connect to DB2 in my model
class MyModel1 < ActiveRecord::Base
octopus_establish_connection "DB2_#{Rails.env.to_s}"
end
class MyModel2 < ActiveRecord::Base
octopus_establish_connection "DB2_#{Rails.env.to_s}"
end
So here if there is a read query from MyModel1 the DB3 is queried and for write query DB1 is queried.
But in MyModel2 for both cases DB2 is queried, while DB4 should be queried.
Thanks
Related
I am new to rails, don't know how to access multiple databases in rails single application.
I will try like this
config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: root
socket: /var/run/mysqld/mysqld.sock
reconnect: true
development:
<<: *default
database: connection_development
<<: *default
database: connection_test
second database
log_database_production:
adapter: mysql2
encoding: utf8
pool: 5
host: 192.168.100.97
port: 3306 #ip address of server with other postgres database
username: root
password: root
database: hrms_development
reconnect: true
then i don't know how to proceed..
I'm attempting to run a RoR app and it seems to be ignoring my username and password if it isn't root, any thoughts?
If it is configured as below, it works:
development:
adapter: mysql2
database: dbname
user: root
password: root_password
pool: 5
timeout: 5000
However, if it is set up with a non root user, it fails with the message `connect': Access denied for user 'root'#'localhost' (using password: YES) (Mysql2::Error)
development:
adapter: mysql2
database: dbname
user: username
password: username_password
pool: 5
timeout: 5000
mysql -uusername -p<username password> <dbname> connects without any issue. Specifying a host or hostname doesn't fix it.
Update
database.yml
development:
adapter: mysql2
database: dbname
user: root
# username
password: root_password
# username_password
pool: 5
timeout: 5000
production:
adapter: mysql2
database: dbname
user: username
password: username_password
pool: 5
timeout: 5000
results of running RAILS_ENV=development bundle exec rails c
Loading development environment (Rails 3.2.11)
irb(main):001:0>
My database.yml was out dated(set up incorrectly). user is not the correct key, it is username.
development:
adapter: mysql2
database: dbname
username: root
# username
password: root_password
# username_password
pool: 5
timeout: 5000
production:
adapter: mysql2
database: dbname
username: username
password: username_password
pool: 5
timeout: 5000
I created a simple rails application:
1
$ rails new myapp1 -d postrgresql
2. Removed /public/index.html
3. Created the Homecontroller and the actions
4. Wrote the routes
myapp1::Application.routes.draw do
root :to => "home#index"
match 'about' => 'home#about'
match 'contacts' => 'home#contacts'
match 'projects' => 'home#projects'
end
5. Created the views and the default layout
6. The bottom line. Here is the database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp1_db
host: localhost
pool: 5
username: myapp1_user
password: 1234
test:
adapter: postgresql
encoding: unicode
database: myapp1_db
host: localhost
pool: 5
username: myapp1_user
password: 1234
production:
adapter: postgresql
encoding: unicode
database: myapp1_db
host: localhost
pool: 5
username: myapp1_user
password: 1234
and of course I created user and database
sudo -u postgres psql
postgres=# \password
postgres=# create user myapp1_user with password '1234';
postgres=# create database myapp1_db owner myapp1_user;
But there are some worries:
1. Now I don't use any database, but in spite of this the application gives an error
PG::Error FATAL: Peer authentication failed for user "myapp1"
2. It seems like rails turns a blind eye to database.yml. Why does it use the user myapp1 instead of myapp1_user?
Whitespace is significant in YAML.
production:
adapter: postgresql
encoding: unicode
...
password: 1234
Should be:
production:
adapter: postgresql
encoding: unicode
...
password: 1234
Provide host and port in the database.yml file, like this:
development:
adapter: postgresql
encoding: unicode
database: myapp1_db
host: localhost
pool: 5
username: myapp1_user
password: 1234
host: localhost
port: 5432
Works like a charm on Ubuntu 12.04 LTS with Rails 3.2.x and PostgreSQL 9.1.x
i get this error when i try to create a database using rake command.
this is my database.yml file
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MYSQL driver
# gem install mysql2
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: SIMS_development
pool: 5
username: root
password:
host: localhost
port: 3306
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: SIMS_test
pool: 5
username: root
password:
host: localhost
port: 3306
now i am not sure why i am getting this error.
I think you have old mysql server. So please install new one if you have old version.
Substitute host: localhost with host: 127.0.0.1
you can use socket connection instead of a port for your connection, you can use of this form, it's very easy
for Linux:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: your_database_name
pool: 5
username: your_username_db
password: your_password_db
socket: /var/run/mysqld/mysqld.sock
for MAC
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: your_database_name
pool: 5
username: your_username_db
password: your_password_db
socket: /tmp/mysql.sock
I hope this method will serve
Rails new app.
The current database.yml is like that:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I need to edit this for postgresql database.
How can I do this?
Simply:
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
host: localhost
Source: Configuring Rails Applications
development:
adapter: postgresql
encoding: utf8
database: name
username: hading
password: my_db_password
pool: 5 # not mandatory
timeout: 5000 # not mandatory
host: localhost
port: your postgresql port number (5432 or 5433)
As Zabba said it's
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
As mentioned in the Configuring Rails Applications. But you might want an additional min_messages: WARNING, to get rid of the nasty NOTICE messages postgresql gives you during a migration. So my database.yml entry looks like this
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
min_messages: WARNING
You might be interested in generating new app with postgres default:
rails new myapp --database=postgresql
as mentioned here: https://devcenter.heroku.com/articles/getting-started-with-rails4
development:
adapter: postgresql
encoding: utf8
database: name
username: hading
password: my_db_password
host: localhost # not mandatory
pool: 5 # not mandatory
timeout: 5000 # not mandatory
Simply use
rails new app_name --database=postgresql
or if existing application try
development:
adapter: postgresql
encoding: unicode
database: app_dev
pool: 5
username: username
password: password
host: localhost
Another way is to have the common values in &default and then have individual values for the other environments, which can be based on environment variables:
default: &default
adapter: postgresql
encoding: unicode
port: <%= ENV.fetch("POSTGRESQL_PORT", "5432") %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= ENV['POSTGRESQL_USER_NAME'] %>
password: <%= ENV.fetch("POSTGRESQL_PASSWORD", "myS3cr3tP4ssw0rd") %>
host: <%= ENV['POSTGRESQL_HOST'] %>
development:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>-development
host: db
test:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>-test
host: db
production:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>
Here all the values can come from environment variables (if you use Docker or Bitbucket Pipelines) or you can have them in your .env files.