MySQL No database selected rails

Viewed 5738

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
4 Answers

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

Related