Rails - Multiple development instances with a single source and database.yml file - mysql

Can we connect to multiple development instances with a single source and a single database.yml file? I mean, I need to have 4 development instances from same source which connects 4 different MySQL databases. Can I start the servers by using different PORT IDs?
example:
development:
adapter: mysql2
database: TEST1_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-2:
adapter: mysql2
database: TEST2_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-3:
adapter: mysql2
database: TEST3_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-4:
adapter: mysql2
database: TEST4_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
Then, then how we can start 4 different servers (at localhost) connecting these 4 databases simultaneously? (we normally use rails server for single instance in development.)
Thanks :)-

You are not limited to the default rails environments. You could just have development-2, development-3 environments. Read this post on the 37signals blog where DHH talks about that
database.yml can read environment variables.
development:
adapter: postgresql
host: localhost
database: <%= ENV['POSTGRES_DATABASE'] %>
username: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>
then make sure these are set when you start your server.

It is super easy.
Just update your database.yml file as given in the question. Suppose our development instances are development, development-2, development-3 and development-4.
First you just create 3 copies of the environment file development.rb (can find insideconfig/environments/) and rename it to development-2.rb, development-3.rb and development-4.rb.
Then, just do run the below steps before starting the servers.
RAILS_ENV=development rake db:create (OR just rake db:create as it defaults to development)
RAILS_ENV=development-2 rake db:create
RAILS_ENV=development-3 rake db:create
RAILS_ENV=development-4 rake db:create
RAILS_ENV=development rake db:schema:load (OR just rake db:schema:load as it defaults to development)
RAILS_ENV=development-2 rake db:schema:load
RAILS_ENV=development-3 rake db:schema:load
RAILS_ENV=development-4 rake db:schema:load
RAILS_ENV=development rake db:seed (OR just rake db:seed as it defaults to development)
RAILS_ENV=development-2 rake db:seed
RAILS_ENV=development-3 rake db:seed
RAILS_ENV=development-4 rake db:seed
Then, start all instances with different server pid's as given below.
rails s -p 3000 -e development --pid tmp/pids/server.pid (OR rails s by default)
rails s -p 3002 -e development-2 --pid tmp/pids/server.2.pid
rails s -p 3003 -e development-3 --pid tmp/pids/server.3.pid
rails s -p 3004 -e development-4 --pid tmp/pids/server.4.pid
That will start all instances simultaneously from the same source and a single database.yml file. Thank you all for all your replies and help :)-

Related

AWS Elastic Beanstock. rails cannot connect to MYSQL

I cannot solve this issue, mysql will not connect. using eb deploy. And I am using Ruby v 2.3.1
+ su -s /bin/bash -c 'leader_only bundle exec rake db:migrate' webapp
rake aborted! Mysql2::Error: Can't connect to MySQL server on 'labr.cyp2t1256323.us-west-2.rds.amazonaws.com' (4)
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `load'
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace) (ElasticBeanstalk::ExternalInvocationError)
caused by: ++ /opt/elasticbeanstalk/bin/get-config container -k script_dir
Database.yml
production:
adapter: mysql2
encoding: utf8
reconnect: false
pool: 20
database: ***
username: *****
password: *****
host: labr.cyp2t1256323.us-west-2.rds.amazonaws.com
port: 3306
3306 is enabled on Amazon EC2 console, rails-env is at health red
Add a new security group that accept all traffic. And add your db instance to this group.
http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_SecurityGroups.html

MySQL No database selected rails

I got something like this when i hit rake db:migrate
Im using figaro in my app, its my first time with this gem any mysql so I cant get what excatly is wrong.
Thanks in advance :)
Mysql2::Error: No database selected: CREATE TABLE schema_migrations (version varchar(255) PRIMARY KEY) ENGINE=InnoDB
database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: <%= ENV["DB_USER"] %>
password: <%= ENV["DB_PASSWORD"] %>
development:
<<: *default
database: <%= ENV["DB_DATABASE_DEV"] %>
test:
<<: *default
database: <%= ENV["DB_DATABSE_TEST"] %>
production:
<<: *default
database: <%= ENV['DB_DATABASE_PRODUCTION'] %>
application.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: password
development:
<<: *default
database: dev
test:
<<: *default
database: test_db
I don't have enough point to ask in comment, but it seems you're trying to run migration without creating a schema first.
Run rails db:create to create the schema first. For example, if you don't have the a database named "dev" in your MySQL, this command will do so.
rails db:create
To set specific environment
rails db:create RAILS_ENV=test
Then you run canrails db:migrate.
To destroy a schema if you need one:
rails db:drop
You're Using ENV
Though it might not be related, have you set up your ENV properly before running the command?
For Windows, you can add those, for example, DB_USER to system environment
Just press Start and type system environment >> Environment Variables... >> New...
Variable Name: DB_USER
Variable Value: your_username
Note: You might have to restart your system for the environment to work.
Or if you don't have time, simply in your terminal you're running rails s:
C:\Sites\ProjectName> set DB_USER=your_username
C:\Sites\ProjectName> set DB_PASSWORD=your_username
C:\Sites\ProjectName> rails s
The same steps for Linux, but I don't know the specific command. Some prefer to use .env file.
I've run into this trouble a couple days ago, and it's very annoying because Rails tried to create dev and test db's when in dev environment. Nothing worked.
I ended up hard-coding database: into database.yml, and let Figaro manage credentials.
I runned the same issue today.
Ensure you have dotenv-rails gem installed and that "DB_DATABASE_DEV" has a value in your .env file.
Also, make sure your database has been created.
It happens when your database.yml does not include database: some_database part or when database name is empty. In your case you can use one of few options:
Option 1: run migrations with database name env variable included
DB_DATABASE_DEV=your_dev_db_name rails db:<task>
Option 2: use hard-coded db name in your database.yml file
# database.yml
development:
<<: *default
database: my_development_database
Option 3: make sure that env variables are correctly loaded
To do so in linux, simply write in terminal:
echo ${DB_DATABASE_DEV}
On windows terminal you need to write this:
echo %DB_DATABASE_DEV%
If you get no content then this env variable is not set

Transition from mysql to postgress error

I wanted to change from mysql to postgresql.
I don't care about data so I have changed database.yml:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.postgresql
test:
<<: *default
database: db/test.postgresql
production:
<<: *default
database: db/production.postgresql
And run
rails db:reset db:migrate
But I get error
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I have restarted both IDE (using c9) and server, but it gave no effect.
Any idea how to solve that problem?
#Edit After running sudo service postgresql start servers starts, and then when I run rails db:create
I get error:
psql: FATAL: role "ubuntu" does not exist
Just run this in your terminal :
$ cd /usr/local/var/postgres/
$ rm postmaster.pid
It's because last time your computer was shut off, Postgresql did not exit gracefully (usually due to running out of battery).

Rails connects to MySQL from WEBrick but not from Passenger

I just created a new Rails 4 app with MySql as follows:
rails new mysqltest -d mysql
And modified the database.yml with the right credentials.
I generated a sample contoller and updated the routes for root route.
When I start using WEBrick in production,
rails s -e production
The site works. I see the index page.
When I start using Passenger without 3000 port, I see the following error:
database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
Passsenger is running in Production environment.
My database.yml
# 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
database: sample
pool: 5
username: sample
password: sample
socket: /var/run/mysqld/mysqld.sock
# 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: mysql2
encoding: utf8
database: sample
pool: 5
username: sample
password: sample
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
database: sample
pool: 5
username: sample
password: sample
socket: /var/run/mysqld/mysqld.sock
The case I used in database.yml file and the apache config file were different.
Both should be same. eg. production.
In apache config, I gave as Production. After changing it to production it worked.
Source - https://groups.google.com/forum/#!topic/phusion-passenger/Kr-R0gSw6i8
Did you create your local production database?
rake db:create:all
RAILS_ENV=production bundle exec rake db:migrate
Is your database.yml file setup properly to use a production database?
Do you have a mysql gem in your gem file.
Common problems.

mysql error help... while running rake db:setup RAILS_ENV="production"

I am diligently trying to get mysql up and running for my first rails app ever.
I keep getting the following error when running rake db:setup RAILS_ENV="production":
rake aborted!
dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dyl Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle
Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2 /mysql2.bundle
/Users/chris/rails_projects/sienab/Rakefile:4
(See full trace by running task with --trace)
I am running snow leopard, mysql 5.5, gem mysql2, rails 3.
Any help is great. many thanks.
database.yml below
# SQLite version 3.x
# gem install 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: mysql2
encoding: utf8
reconnect: false
database: sienab_production
pool: 5
username: username
password: password
host: localhost
Looks like you need to install the mysql2 libs:
sudo apt-get install libmysqlclient-dev libmysqlclient16
Also, I suggest you use gem 'mysql2','0.2.7' in your Gemfile if you're using Rails 3.0.x.
Don't forget to run bundle install after making this change.