Rails db:create Error: Incorrect database name


While attempting to create a new Rails 3 application, I encountered an issue creating a MySQL Database. db:create returns an error message:

rake aborted!

Mysql2::Error: Incorrect database name 'testapp.net_test': CREATE DATABASE `testapp.net_test`

 

Notes

  • Ruby on Rails 3.0.3
  • MySQL version 5.0.77

 

The name of my application is testapp.net, so when rails setup the default database names it  appended _development, _test and _production to create the database names. It looks like MySQL does not like periods in the database names.

To resolve this I opened the database.yml file and edited the database names by replacing periods with underscores. Here is a snippet from the altered yml file:

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: testapp_net_development
  pool: 5
  username: root
  password: ROOTPASSWORDHERE
  socket: /var/lib/mysql/mysql.sock

 

After changing the DB name I was able to run rails db:create RAILS_ENV='development' without difficulty.

 

For reference, here is the complete error text I received:

[user@homeserver testapp.net]$ rake db:create RAILS_ENV='development'
(in /home/rcart/rails/testapp.net)
rake aborted!
Mysql2::Error: Incorrect database name 'testapp.net_test': CREATE DATABASE `testapp.net_test` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `rescue in log'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:194:in `log'
/usr/local/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:314:in `execute'
/usr/local/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:406:in `create_database'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/railties/databases.rake:68:in `rescue in create_database'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/railties/databases.rake:39:in `create_database'
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/railties/databases.rake:33:in `block (2 levels) in <top (required)>'
/usr/local/lib/ruby/1.9.1/rake.rb:634:in `call'
/usr/local/lib/ruby/1.9.1/rake.rb:634:in `block in execute'
/usr/local/lib/ruby/1.9.1/rake.rb:629:in `each'
/usr/local/lib/ruby/1.9.1/rake.rb:629:in `execute'
/usr/local/lib/ruby/1.9.1/rake.rb:595:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/usr/local/lib/ruby/1.9.1/rake.rb:588:in `invoke_with_call_chain'
/usr/local/lib/ruby/1.9.1/rake.rb:581:in `invoke'
/usr/local/lib/ruby/1.9.1/rake.rb:2041:in `invoke_task'
/usr/local/lib/ruby/1.9.1/rake.rb:2019:in `block (2 levels) in top_level'
/usr/local/lib/ruby/1.9.1/rake.rb:2019:in `each'
/usr/local/lib/ruby/1.9.1/rake.rb:2019:in `block in top_level'
/usr/local/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/local/lib/ruby/1.9.1/rake.rb:2013:in `top_level'
/usr/local/lib/ruby/1.9.1/rake.rb:1992:in `run'
/usr/local/bin/rake:31:in `<main>'