Rails is not using password provided in database.yml - mysql

I have a problem connecting to DB from my rails app.
I have provided the database.yml file with credentials:
development: &development
adapter: mysql2
encoding: utf8
database: myapp_dev
pool: 5
username: root
password: passoword
test:
<<: *development
database: myapp_test
production:
<<: *development
database: myapp_prod
staging:
<<: *development
database: myapp_staging
But when I try run the app it gives me the following error:
Access denied for user 'root'#'localhost' (using password: NO)
using mysql -u root -p with same credentials I can access my db without issues. can someone tell me what could be a problem here?
EDIT:
I've changed the user from root to some other user but it still gives me the same error. so apparently it uses some other credentials to connect to db.

I ran into the same issue. It turns out it was a mistake in my database.yml file. Here is what my database.yml file looked like:
production:
adapter: mysql2
encoding: utf8
database: my_db
username: my_db_user
password: !strong_password
host: localhost
port: 3306
pool: 5
timeout: 5000
The ! character at the beginning of the password is not valid YAML markup (for at least what I was trying to achieve). Wrapping quotes ('') around the password fixes the issue. Here is the updated database.yml file:
production:
adapter: mysql2
encoding: utf8
database: my_db
username: my_db_user
password: '!strong_password'
host: localhost
port: 3306
pool: 5
timeout: 5000

Try the following code, if it works we can go forward
Comment all the previous code in database.yml and paste the following
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_dev
pool: 5
username: root
password: passoword
host: localhost

Related

Mysql2::Error::ConnectionError: Access denied for user 'root'#'localhost' (using password: NO)

Got a new rails demo and i can't start the server
i tried "mysql -u root -p" and its didn't work
here is my database.yml:
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: ''
host: localhost
development:
<<: *default
database: buk_store_dev
production:
adapter: mysql2
host: 127.0.0.1
database: buk_store_production
Check your root password on your MySQL server. Looks like you are using the wrong password.

Rails rake:migrate gives access denied for 'root'#'localhost' *Ubuntu 16.04*

I've been trying to get this figured out for two days now. Any help would be appreciated. I'm trying to just make a sample app using mysql, and rails. But, every time I try to rake db:migrate I get a no access error. I've checked my database.yml file 100 times, and everything looks right. Logging into mysql through the terminal with the exact same login credentials works fine. It seems like mysql is working for everything except for Rails.
default: &default
adapter: mysql2
encoding: utf8
host: localhost
port: 3306
database: my_ror
pool: 50
username: root
password: password
socket: /opt/lampp/var/mysql/mysql.sock
development:
adapter: mysql2
encoding: utf8
host: localhost
port: 3306
database: my_ror_development
pool: 50
username: root
password: password
socket: /opt/lampp/var/mysql/mysql.sock
What am I doing wrong?

Setting up my rails app to MySQL Database

I am having trouble connecting my rails app to MySQL. I am using SQL Pro as my GUI.
Heres my database.yml
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: ecommerce-app-development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: ecommerce-app-test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: ecommerce-app-production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
I tried creating a database called 'ecommerece-app-developent' in SQL Pro, but heres the error message it gives me:
Connected to host, but unable to connect to database ecommerce-app-development.
Be sure that the database exists and that you have the necessary privileges.
MySQL said: Unknown database 'ecommerce-app-development'
You need to create the database before connecting to it. rake db:create will do this for you, then you can connect to it using SQL Pro or whatever.

rails app shows access denied in webbrowser

Access denied for user 'root'#'localhost' (using password: YES)
I setup a new environment where mysql does not have a password and how I am getting this error. Where do I change
'root'#'localhost' (using password: YES)
my database.yml is show below my database is sample_app
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: mysql2
database: db/development.mysql2
pool: 5
timeout: 5000
database: sample_app
username: root
socket: /tmp/mysql.sock
# 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:
adapter: mysql2
database: db/test.mysql2
pool: 5
timeout: 5000
database: sample_app
username: root
socket: /tmp/mysql.sock
production:
adapter: mysql2
database: db/production.mysql2
pool: 5
timeout: 5000
database: sample_app
username: root
socket: /var/run/mysqld/mysqld.sock
You should not be using root to connect to a db with an app. Create a new user/password with a name to recognize that it is being used for a specific app\task then use that instead of root.
add this for empty password password:
production:
adapter: mysql2
database: db/production.mysql2
pool: 5
timeout: 5000
database: sample_app
username: root
password:
socket: /var/run/mysqld/mysqld.sock
If you still get access denied, then the db is saying that the user\password is not valid
If this,
$ mysql -u root
takes you to mysql then this will work,
production:
adapter: mysql2
database: db/production.mysql2
pool: 5
timeout: 5000
database: sample_app
username: root
password:
socket: /var/run/mysqld/mysqld.sock
If you have set some password to your root user, then try
$ mysql -u root -p
<<It will prompt for a password, key it in>>
If that takes you to mysql then this will work,
production:
adapter: mysql2
database: db/production.mysql2
pool: 5
timeout: 5000
database: sample_app
username: root
password: <<password>>
socket: /var/run/mysqld/mysqld.sock
Moral of the story:
First, try logging into mysql using mysql command from command-line and see if it works.
Then, use those credentials in database.yml and log into mysql from rails application.

Ruby on rails mysql problem

I'm creating my first ROR application.
Details:
creating new app ->
rails new simple_cms -d mysql
creating a controller and a corresponding view -->
rails generate controller demo index
Then when after i started the rails server, the http://localhost:3000 page works perfectly fine. but when i try to go to the page that I just created, http://localhost:3000/demo/index, it spurts out a MYSQL error:
Access denied for user 'root'#'localhost' (using password: NO)
At first, I think it's just a database connection problem, so I've gone through the MYSQL interface and create a new database called: simple_cms_development
and I have also made a corresponding user: simple_cms
with granted privileges.
and lastly, I have configured the database.yml file to fit the details:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: simple_cms_development
pool: 5
username: simple_cms
password: developer
host: localhost
But still, it spits out the same mysql problem:
Access denied for user 'root'#'localhost' (using password: NO)
Please. to anyone who knows how to use mysql as a database for rails app..A help would be really appreciated. Thank you so much!
UPDATE: This is all what's inside on the database.yml file
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql
encoding: utf8
reconnect: false
database: simple_cms_development
pool: 5
username: root
password: developer
host: localhost
# 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:
adapter: mysql2
encoding: utf8
reconnect: false
database: simple_cms_test
pool: 5
username: root
password:
host: localhost
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: simple_cms_production
pool: 5
username: root
password:
host: localhost
I found that you need to add the following line to config/database.yml:
host: localhost
production:
adapter: mysql2
encoding: utf8
reconnect: false
host: your_host # <----- normally localhost
database: the_db_I_made
pool: 5
username: the_user_I_made
password: the_password
socket: /var/lib/mysql/mysql.sock
Only reason that can cause your issue is that you are loading your server with different environment. I think that in your case you are loading your server under production mode.