title: `AdapterNotSpecified` database is not configured
body: <p>My database is a MySQL running on <code>localhost</code>. The <code>mysql2</code> gem installed without errors.  I never managed to connect, regardless of which login I used.</p>
  
  <p>The root password contains a <code>#</code> which is the comment character in Sublime Text 2 editor so I think it might not be passing my password correctly.</p>
  
  <p>I tried using double- and single quotes, but it doesn't seem to make any difference. I also tried creating a new user and giving that user privileges on the command line.</p>
  
  <p>I have read a lot of similiar posts here but haven't found solution yet. </p>
  
  <p>Below is a stack trace of the error:</p>
  
  <pre><code>C:\inetpub\wwwroot\siteone\simple_cms&gt;rake db:schema:dump --trace
  DL is deprecated, please use Fiddle
  ** Invoke db:schema:dump (first_time)
  ** Invoke environment (first_time)
  ** Execute environment
  ** Invoke db:load_config (first_time)
  ** Execute db:load_config
  rake aborted!
  ActiveRecord::AdapterNotSpecified: 'mysql2' database is not configured. Available: ["default", "adapter", "encoding", "pool", "username", "password", "host", "development", "database", "socket"]
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol
  _connection'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:231:in `resolve_string_connection'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:213:in `resolve_connection'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-.2.1/lib/active_record/connection_adapters/connection_specification.rb:139:in `resolve'C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:151:in `block in resolve_all'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:150:in `each'C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/connection_specification.rb:150:in `resolve_all'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:69:in `resolve'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/core.rb:46:in `configurations='
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/railtie.rb:117:in `block (2 levels) in &lt;class:Railtie&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:44:in `each'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activesupport-4.2.1/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/base.rb:316:in `&lt;module:ActiveRecord&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/base.rb:26:in `&lt;top (required)&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/tasks/mysql_database_tasks.rb:8:in `&lt;class:MySQLDatabaseTasks&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/tasks/mysql_database_tasks.rb:3:in `&lt;module:Tasks&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/tasks/mysql_database_tasks.rb:2:in `&lt;module:ActiveRecord&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/tasks/mysql_database_tasks.rb:1:in `&lt;top (required)&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:50:in `&lt;module:DatabaseTasks&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:37:in `&lt;module:Tasks&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:4:in `&lt;module:ActiveRecord&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:3:in `&lt;top (required)&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.2.1/lib/active_record/railtie.rb:41:in `block (3 levels) in &lt;class:Railtie&gt;'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-0.4.2/lib/rake/task.rb:240:in `call'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-0.4.2/lib/rake/task.rb:235:in `each'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-0.4.2/lib/rake/task.rb:201:in `block in invoke_prerequisites'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `each'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:199:in `invoke_prerequisites'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
  C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/bin/rake:33:in`&lt;top (required)&gt;'
  C:/RailsInstaller/Ruby2.0.0/bin/rake:23:in `load'
  C:/RailsInstaller/Ruby2.0.0/bin/rake:23:in `&lt;main&gt;'
  Tasks: TOP =&gt; db:schema:dump =&gt; db:load_config
  
  C:\inetpub\wwwroot\siteone\simple_cms&gt;
  </code></pre>
  
  <hr>
  
  <p>I edited <code>database.yml</code> and now the error has changed slightly.</p>
  
  <pre><code>default: 
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: simple_cms
  password: mypass
  host: localhost
  
  development:
  adapter: mysql2
  encoding: utf8
  pool: 5
  database: simple_cms_development
  username: simple_cms
  password: 
  host: localhost
  socket: /tmp/mysql.sock
  </code></pre>
  
  <hr>
  
  <p>And finally, my <code>connection_specification.rb</code> file, where I believe the issue lies.</p>
  
  <pre><code>require 'uri'
  require 'active_support/core_ext/string/filters'
  
  module ActiveRecord
    module ConnectionAdapters
      class ConnectionSpecification #:nodoc:
        attr_reader :config, :adapter_method
  
        def initialize(config, adapter_method)
          @config, @adapter_method = config, adapter_method
        end
  
        def initialize_dup(original)
          @config = original.config.dup
        end
  
        # Expands a connection string into a hash.
        class ConnectionUrlResolver # :nodoc:
  
          # == Example
          #
          #   url = "postgresql://foo:bar@localhost:9000/foo_test?pool=5&amp;timeout=3000"
          #   ConnectionUrlResolver.new(url).to_hash
          #   # =&gt; {
          #     "adapter"  =&gt; "postgresql",
          #     "host"     =&gt; "localhost",
          #     "port"     =&gt; 9000,
          #     "database" =&gt; "foo_test",
          #     "username" =&gt; "foo",
          #     "password" =&gt; "bar",
          #     "pool"     =&gt; "5",
          #     "timeout"  =&gt; "3000"
          #   }
          def initialize(url)
            raise "Database URL cannot be empty" if url.blank?
            @uri     = uri_parser.parse(url)
            @adapter = @uri.scheme.tr('-', '_')
            @adapter = "postgresql" if @adapter == "postgres"
  
            if @uri.opaque
              @uri.opaque, @query = @uri.opaque.split('?', 2)
            else
              @query = @uri.query
            end
          end
  
          # Converts the given URL to a full connection hash.
          def to_hash
            config = raw_config.reject { |_,value| value.blank? }
            config.map { |key,value| config[key] = uri_parser.unescape(value) if value.is_a? String }
            config
          end
  
          private
  
          def uri
            @uri
          end
  
          def uri_parser
            @uri_parser ||= URI::Parser.new
          end
  
          # Converts the query parameters of the URI into a hash.
          #
          #   "localhost?pool=5&amp;reaping_frequency=2"
          #   # =&gt; { "pool" =&gt; "5", "reaping_frequency" =&gt; "2" }
          #
          # returns empty hash if no query present.
          #
          #   "localhost"
          #   # =&gt; {}
          def query_hash
            Hash[(@query || '').split("&amp;").map { |pair| pair.split("=") }]
          end
  
          def raw_config
            if uri.opaque
              query_hash.merge({
                "adapter"  =&gt; @adapter,
                "database" =&gt; uri.opaque })
            else
              query_hash.merge({
                "adapter"  =&gt; @adapter,
                "username" =&gt; uri.user,
                "password" =&gt; uri.password,
                "port"     =&gt; uri.port,
                "database" =&gt; database_from_path,
                "host"     =&gt; uri.hostname })
            end
          end
  
          # Returns name of the database.
          def database_from_path
            if @adapter == 'sqlite3'
              # 'sqlite3:/foo' is absolute, because that makes sense. The
              # corresponding relative version, 'sqlite3:foo', is handled
              # elsewhere, as an "opaque".
  
              uri.path
            else
              # Only SQLite uses a filename as the "database" name; for
              # anything else, a leading slash would be silly.
  
              uri.path.sub(%r{^/}, "")
            end
          end
        end
  
        ##
        # Builds a ConnectionSpecification from user input.
        class Resolver # :nodoc:
          attr_reader :configurations
  
          # Accepts a hash two layers deep, keys on the first layer represent
          # environments such as "production". Keys must be strings.
          def initialize(configurations)
            @configurations = configurations
          end
  
          # Returns a hash with database connection information.
          #
          # == Examples
          #
          # Full hash Configuration.
          #
          #   configurations = { "production" =&gt; { "host" =&gt; "localhost", "database" =&gt; "foo", "adapter" =&gt; "sqlite3" } }
          #   Resolver.new(configurations).resolve(:production)
          #   # =&gt; { "host" =&gt; "localhost", "database" =&gt; "foo", "adapter" =&gt; "sqlite3"}
          #
          # Initialized with URL configuration strings.
          #
          #   configurations = { "production" =&gt; "postgresql://localhost/foo" }
          #   Resolver.new(configurations).resolve(:production)
          #   # =&gt; { "host" =&gt; "localhost", "database" =&gt; "foo", "adapter" =&gt; "postgresql" }
          #
          def resolve(config)
            if config
              resolve_connection config
            elsif env = ActiveRecord::ConnectionHandling::RAILS_ENV.call
              resolve_symbol_connection env.to_sym
            else
              raise AdapterNotSpecified
            end
          end
  
          # Expands each key in @configurations hash into fully resolved hash
          def resolve_all
            config = configurations.dup
            config.each do |key, value|
              config[key] = resolve(value) if value
            end
            config
          end
  
          # Returns an instance of ConnectionSpecification for a given adapter.
          # Accepts a hash one layer deep that contains all connection information.
          #
          # == Example
          #
          #   config = { "production" =&gt; { "host" =&gt; "localhost", "database" =&gt; "foo", "adapter" =&gt; "sqlite3" } }
          #   spec = Resolver.new(config).spec(:production)
          #   spec.adapter_method
          #   # =&gt; "sqlite3_connection"
          #   spec.config
          #   # =&gt; { "host" =&gt; "localhost", "database" =&gt; "foo", "adapter" =&gt; "sqlite3" }
          #
          def spec(config)
            spec = resolve(config).symbolize_keys
  
            raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)
  
            path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter"
            begin
              require path_to_adapter
            rescue Gem::LoadError =&gt; e
              raise Gem::LoadError, "Specified '#{spec[:adapter]}' for database adapter, but the gem is not loaded. Add `gem '#{e.name}'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)."
            rescue LoadError =&gt; e
              raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.", e.backtrace
            end
  
            adapter_method = "#{spec[:adapter]}_connection"
            ConnectionSpecification.new(spec, adapter_method)
          end
  
          private
  
          # Returns fully resolved connection, accepts hash, string or symbol.
          # Always returns a hash.
          #
          # == Examples
          #
          # Symbol representing current environment.
          #
          #   Resolver.new("production" =&gt; {}).resolve_connection(:production)
          #   # =&gt; {}
          #
          # One layer deep hash of connection values.
          #
          #   Resolver.new({}).resolve_connection("adapter" =&gt; "sqlite3")
          #   # =&gt; { "adapter" =&gt; "sqlite3" }
          #
          # Connection URL.
          #
          #   Resolver.new({}).resolve_connection("postgresql://localhost/foo")
          #   # =&gt; { "host" =&gt; "localhost", "database" =&gt; "foo", "adapter" =&gt; "postgresql" }
          #
          def resolve_connection(spec)
            case spec
            when Symbol
              resolve_symbol_connection spec
            when String
              resolve_string_connection spec
            when Hash
              resolve_hash_connection spec
            end
          end
  
          def resolve_string_connection(spec)
            # Rails has historically accepted a string to mean either
            # an environment key or a URL spec, so we have deprecated
            # this ambiguous behaviour and in the future this function
            # can be removed in favor of resolve_url_connection.
            if configurations.key?(spec) || spec !~ /:/
              ActiveSupport::Deprecation.warn(&lt;&lt;-MSG.squish)
                Passing a string to ActiveRecord::Base.establish_connection for a
                configuration lookup is deprecated, please pass a symbol
                (#{spec.to_sym.inspect}) instead.
              MSG
  
              resolve_symbol_connection(spec)
            else
              resolve_url_connection(spec)
            end
          end
  
          # Takes the environment such as +:production+ or +:development+.
          # This requires that the @configurations was initialized with a key that
          # matches.
          #
          #   Resolver.new("production" =&gt; {}).resolve_symbol_connection(:production)
          #   # =&gt; {}
          #
          def resolve_symbol_connection(spec)
            if config = configurations[spec.to_s]
              resolve_connection(config)
            else
              raise(AdapterNotSpecified, "'#{spec}' database is not configured. Available: #{configurations.keys.inspect}")
            end
          end
  
          # Accepts a hash. Expands the "url" key that contains a
          # URL database connection to a full connection
          # hash and merges with the rest of the hash.
          # Connection details inside of the "url" key win any merge conflicts
          def resolve_hash_connection(spec)
            if spec["url"] &amp;&amp; spec["url"] !~ /^jdbc:/
              connection_hash = resolve_url_connection(spec.delete("url"))
              spec.merge!(connection_hash)
            end
            spec
          end
  
          # Takes a connection URL.
          #
          #   Resolver.new({}).resolve_url_connection("postgresql://localhost/foo")
          #   # =&gt; { "host" =&gt; "localhost", "database" =&gt; "foo", "adapter" =&gt; "postgresql" }
          #
          def resolve_url_connection(url)
            ConnectionUrlResolver.new(url).to_hash
          end
        end
      end
    end
  end
  </code></pre>