MySQL No database selected rails - mysql

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

I don't have enough point to ask in comment, but it seems you're trying to run migration without creating a schema first.
Run rails db:create to create the schema first. For example, if you don't have the a database named "dev" in your MySQL, this command will do so.
rails db:create
To set specific environment
rails db:create RAILS_ENV=test
Then you run canrails db:migrate.
To destroy a schema if you need one:
rails db:drop
You're Using ENV
Though it might not be related, have you set up your ENV properly before running the command?
For Windows, you can add those, for example, DB_USER to system environment
Just press Start and type system environment >> Environment Variables... >> New...
Variable Name: DB_USER
Variable Value: your_username
Note: You might have to restart your system for the environment to work.
Or if you don't have time, simply in your terminal you're running rails s:
C:\Sites\ProjectName> set DB_USER=your_username
C:\Sites\ProjectName> set DB_PASSWORD=your_username
C:\Sites\ProjectName> rails s
The same steps for Linux, but I don't know the specific command. Some prefer to use .env file.

I've run into this trouble a couple days ago, and it's very annoying because Rails tried to create dev and test db's when in dev environment. Nothing worked.
I ended up hard-coding database: into database.yml, and let Figaro manage credentials.

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

Mysql2::Error "Access denied for user" using env variable in database.yml

My System is macOX Mojave 10.14
MySQL is MySQL:8.0.16
My database.yml is:
development:
adapter: mysql2
encoding: utf8
database: dev_database
reconnect: false
pool: 5
username: <%= ENV['RAILS_DEV_DB_USSERNAME'] %>
password: <%= ENV['RAILS_DEV_DB_PASSWORD'] %>
socket: /tmp/mysql.sock
and I have confirmed if the variable is valid. Using
Command-Line: erb config/database.yml
I can get:
development:
adapter: mysql2
encoding: utf8
database: dev_database
reconnect: false
pool: 5
username: root
password: Wle3S#23sv
socket: /tmp/mysql.sock
But when I start rails s -e developent, and browse to my page, I can not connect to my database.
What can I do now?
As Author arpiagar suggested rails 3, how use an ENV config vars in a Settings.yml file? check his example (26)
when you introduce a scriptlet tag in .yml file, it is more of erb template. So read it as a erb template first and then load the yml
You can use dotenv or figaro gems (inspired by the Twelve-Factor App methodology) to loads environment variables.
For example if you use dotenv gem
By default, load will look for a file called .env in the current working directory. Pass in multiple files and they will be loaded in order. The first value set for a variable will win.
require 'dotenv'
Dotenv.load('file1.env', 'file2.env')
HOSTNAME = ENV['HOSTNAME']
More info please check below mentioned links
http://railsapps.github.io/rails-environment-variables.html
https://www.honeybadger.io/blog/ruby-guide-environment-variables/

When I run rake db:seed, I keep getting "rake aborted! ActionView::Template::Error: Missing host to link to! "

I am trying to run our company product source code on a local host. I have tried placing
config.action_mailer.default_url_options = { host: "localhost:3000" }
in all development.rb, test.rb and production.rb.
But when I run
rake db:seed
I still keep getting
ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true.
I am adding my config/database.yml file as suggested in the edit
config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: ******
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
database: company_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: coompany_test
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass#localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
database: company_production
username: company
password: <%= ****** %>
Any help would be much appreciated. Thank You.
I guess you are trying to populate the development environment with seed, if that was the case then try this
rake db:seed RAILS_ENV=development
I got it. I forgot to answer it sorry. The solution might vary for this particular error for everyone. But for me I had to do the following changes in the following files.
In application.yml I added
SMTP_HOST: 'localhost:3000'
And in development.rb I added
config.action_mailer.raise_delivery_errors = false
Seeding done.

Transition from mysql to postgress error

I wanted to change from mysql to postgresql.
I don't care about data so I have changed database.yml:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.postgresql
test:
<<: *default
database: db/test.postgresql
production:
<<: *default
database: db/production.postgresql
And run
rails db:reset db:migrate
But I get error
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I have restarted both IDE (using c9) and server, but it gave no effect.
Any idea how to solve that problem?
#Edit After running sudo service postgresql start servers starts, and then when I run rails db:create
I get error:
psql: FATAL: role "ubuntu" does not exist
Just run this in your terminal :
$ cd /usr/local/var/postgres/
$ rm postmaster.pid
It's because last time your computer was shut off, Postgresql did not exit gracefully (usually due to running out of battery).

Rails - Multiple development instances with a single source and database.yml file

Can we connect to multiple development instances with a single source and a single database.yml file? I mean, I need to have 4 development instances from same source which connects 4 different MySQL databases. Can I start the servers by using different PORT IDs?
example:
development:
adapter: mysql2
database: TEST1_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-2:
adapter: mysql2
database: TEST2_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-3:
adapter: mysql2
database: TEST3_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-4:
adapter: mysql2
database: TEST4_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
Then, then how we can start 4 different servers (at localhost) connecting these 4 databases simultaneously? (we normally use rails server for single instance in development.)
Thanks :)-
You are not limited to the default rails environments. You could just have development-2, development-3 environments. Read this post on the 37signals blog where DHH talks about that
database.yml can read environment variables.
development:
adapter: postgresql
host: localhost
database: <%= ENV['POSTGRES_DATABASE'] %>
username: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>
then make sure these are set when you start your server.
It is super easy.
Just update your database.yml file as given in the question. Suppose our development instances are development, development-2, development-3 and development-4.
First you just create 3 copies of the environment file development.rb (can find insideconfig/environments/) and rename it to development-2.rb, development-3.rb and development-4.rb.
Then, just do run the below steps before starting the servers.
RAILS_ENV=development rake db:create (OR just rake db:create as it defaults to development)
RAILS_ENV=development-2 rake db:create
RAILS_ENV=development-3 rake db:create
RAILS_ENV=development-4 rake db:create
RAILS_ENV=development rake db:schema:load (OR just rake db:schema:load as it defaults to development)
RAILS_ENV=development-2 rake db:schema:load
RAILS_ENV=development-3 rake db:schema:load
RAILS_ENV=development-4 rake db:schema:load
RAILS_ENV=development rake db:seed (OR just rake db:seed as it defaults to development)
RAILS_ENV=development-2 rake db:seed
RAILS_ENV=development-3 rake db:seed
RAILS_ENV=development-4 rake db:seed
Then, start all instances with different server pid's as given below.
rails s -p 3000 -e development --pid tmp/pids/server.pid (OR rails s by default)
rails s -p 3002 -e development-2 --pid tmp/pids/server.2.pid
rails s -p 3003 -e development-3 --pid tmp/pids/server.3.pid
rails s -p 3004 -e development-4 --pid tmp/pids/server.4.pid
That will start all instances simultaneously from the same source and a single database.yml file. Thank you all for all your replies and help :)-

Rails make database mysql

I'm using Rails 3, and Ubuntu. I want to change the default Rails SQLite3 into a mysql database. I ran these commands:
sudo apt-get install mysql-server mysql-client libmysqlclient15-dev
sudo apt-get install libmysql-ruby1.8
These were successful. Then I did this in the gemfile
#gem 'sqlite3'
gem 'mysql'
This worked, now, what else should I do to complete the MySQL setup. I know for one the database.yml needs to change, but how does it need to look for Ubuntu and how do I fill in the parameters?
Your config/database.yml must look like :
base: &base
adapter: mysql
username: YourUserName
password: YourPassword
host: localhost
encoding: utf8
pool: 5
timeout: 5000
development:
database: YourApp_dev
<<: *base
test:
database: YourApp_test
<<: *base
production:
database: YourApp_prod
<<: *base
Is the database on the same host?
You need to log into mysql, set a user, create a database. If the database is on a database server, outside of the application server, then you will need to make accessible from the net.
http://www.slac.stanford.edu/BFROOT/www/Computing/Online/Databases/CfgDB/CfgDB-MySQL-Setup.html
After you're done setting up the database (not just installing it), then you're database.yml files need to be modified per the above.