Ruby on Rails
database.yml (Version #10)

database.yml is the database configuration file. It contains the required information for rails to connect to your database.

By default it contains three configurations: production, development, and test.

The data is formated in YAML, so watch for tabs, as they are significant.

How do I access the database configuration from my code?

Since YAML, by design, is parsed conveniently into native data types, traversing your configuration data is as easy as accessing any other hash or array.

All you need to know is that the parsed data can be found at ActiveRecord::Base.configurations.

Examples

<pre>

  1. Get the name of the development DB
    ActiveRecord::Base.configurations‘development’‘database’
  1. Get the schema_search_path for the current environment
    ActiveRecord::Base.configurations[RAILS_ENV]‘schema_search_path’

How do I fix `No such file or directory – /tmp/mysql.sock` ?

Check that the socket exists, and if not, check your mysql configuration. Next make sure your database config file(database.yml) points to the correct file.

socket: /path/to/mysql.sock

for example, under Gentoo, the socket is likely at /var/run/mysqld/mysqld.sock. Try locate mysqld.sock if you’re not sure where yours is.

If you’d rather use TCP/IP sockets instead of Unix sockets, change the server directive to “127.0.0.1” instead of “localhost”.

My database.yml settings are correct, but it still won’t connect!

If you get a database connection error like:

Access denied for user 'root'@'localhost' (using password: NO)

This may happen even though you’ve set the appropriate db settings in database.yml, they can be verified using the console, and you can log in with ‘mysql’ using the same username as password.

This can occur because you’ve made a change to the file while the web server was running, because the contents of database.yml get cached. Make sure that you restart your web server once making changes to database.yml so that the new settings are loaded.

database.yml is the database configuration file. It contains the required information for rails to connect to your database.

By default it contains three configurations: production, development, and test.

The data is formated in YAML, so watch for tabs, as they are significant.

How do I access the database configuration from my code?

Since YAML, by design, is parsed conveniently into native data types, traversing your configuration data is as easy as accessing any other hash or array.

All you need to know is that the parsed data can be found at ActiveRecord::Base.configurations.

Examples

<pre>

  1. Get the name of the development DB
    ActiveRecord::Base.configurations‘development’‘database’
  1. Get the schema_search_path for the current environment
    ActiveRecord::Base.configurations[RAILS_ENV]‘schema_search_path’

How do I fix `No such file or directory – /tmp/mysql.sock` ?

Check that the socket exists, and if not, check your mysql configuration. Next make sure your database config file(database.yml) points to the correct file.

socket: /path/to/mysql.sock

for example, under Gentoo, the socket is likely at /var/run/mysqld/mysqld.sock. Try locate mysqld.sock if you’re not sure where yours is.

If you’d rather use TCP/IP sockets instead of Unix sockets, change the server directive to “127.0.0.1” instead of “localhost”.

My database.yml settings are correct, but it still won’t connect!

If you get a database connection error like:

Access denied for user 'root'@'localhost' (using password: NO)

This may happen even though you’ve set the appropriate db settings in database.yml, they can be verified using the console, and you can log in with ‘mysql’ using the same username as password.

This can occur because you’ve made a change to the file while the web server was running, because the contents of database.yml get cached. Make sure that you restart your web server once making changes to database.yml so that the new settings are loaded.