Correct MySQL configuration for Ruby on Rails Database.yml file
You should separate the host from the port number.
You could have something, like:
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: 127.0.0.1
port: 3306
How to correctly setup a database.yml file in Rails 4
The second database.yml you posted is actually equivalent to the first, it just copies values from the development block.
To answer your other questions:
1) Should I be setting usernames and passwords in ALL environments
you can if you wish, or you can leave it as you have above where it takes the credentials all from one block.
2)If I'm using clear DB with Heroku as my database then should I be uncommenting
heroku actually completely disregards this file (which shouldn't be checked into source control in the first place anyway). Heroku has its own mechanism to handle databases which you can read more about here: https://devcenter.heroku.com/articles/heroku-postgresql
Essentially, treat "heroku's databases" and local databases that you define in this file completely different.
How to manage Rails database.yml
First, move database.yml
to a template file.
If you're on Git:
git mv config/database.yml config/database.yml.example
git commit -m "moved database.yml to an example file"
Or, if you're on Subversion:
svn move config/database.yml config/database.yml.example
svn ci -m "moved database.yml to an example file"
Second, ignore the .yml version.
If you're on Git:
cat > .gitignore
config/database.yml
git add .gitignore
git commit -m "ignored database.yml"
If you're on Subversion:
svn propset svn:ignore config "database.yml"
Third, install Where's your database.yml, dude?:
script/plugin install git://github.com/technicalpickles/wheres-your-database-yml-dude
That plugin alerts developers before any Rake tasks are run if they haven't created their own local version of config/database.yml
.
Fourth, set up a Capistrano deploy task:
# in RAILS_ROOT/config/deploy.rb:
after 'deploy:update_code', 'deploy:symlink_db'
namespace :deploy do
desc "Symlinks the database.yml"
task :symlink_db, :roles => :app do
run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"
end
end
Fifth, upload the server's version of database.yml:
scp config/database.yml user@my_server.com:/path_to_rails_app/shared/config/database.yml
Why is `database.yml` file in config folder, rather than in db folder in ruby on rails?
Because you use the database.yml file to configure your database. For example if you wish to change your main database from Sql Lite(default) to Mysql, you need to change your database configuration, which is found in your database.yml file.
Ruby script connect to Mysql2 using database.yml without Rails
Any method that accept a hash can be fed with the result of a YAML parsing.
You may have two issue here :
- YAML.load_file returns a hash where the keys are strings and not symbols. From what I read from https://github.com/brianmario/mysql2/blob/master/lib/mysql2/client.rb#L17 and https://github.com/brianmario/mysql2/blob/master/lib/mysql2.rb#L36 it accepts both symbols and strings, so this isn't the problem.
- Mysql2 connect method uses different keys, like host instead of hostname in database.yml, you might consider creating a method that converts the keys if you don't or can't modifiy the database.yml.
The following code should work :
config = YAML::load_file("config/database.yml")["development"]
config["host"] = config["hostname"]
client = Mysql2::Client.new(config)
YAML syntax error occurred while parsing config/database.yml Ruby on Rails
You have 2 issues there. Wrong level of indentation below adapter in the production label. And the value of the database label can't start with an @
, so you have to place it between quotation marks:
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: b6f4e1d86a2a08
password: 25205573
host: us-cdbr-iron-east-05.cleardb.net
development:
<<: *default
database: DBProj_development
test:
<<: *default
database: DBProj_test
production:
<<: *default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: b6f4e1d86a2a08
password: 25205573
host: us-cdbr-iron-east-05.cleardb.net
database: "@localhost"
That will fix your parsing issues.
Rails database.yml configuration with different sockets on dev, test and production
You can specify the Rails environment when you run the Rake task.
rake db:migrate RAILS_ENV=production
Related Topics
How to Select the First Row of Each Group
Get a List of Dates Between Two Dates
Is the SQL Where Clause Short-Circuit Evaluated
Condition Within Join or Where
Listagg in Oracle to Return Distinct Values
MySQL Results as Comma Separated List
Foreign Key to Non-Primary Key
Ordering by Specific Field Value First
Get Top N Records For Each Group of Grouped Results
How to Implement One-To-One, One-To-Many and Many-To-Many Relationships While Designing Tables
How to Combine Multiple Rows into a Comma-Delimited List in Oracle
Error Code: 2013. Lost Connection to MySQL Server During Query