mysql: Access denied for user - mysql

In my rails app I have this configuration for database
adapter: mysql2
host: *****
username: *****
password: <%= ENV['MYSQL_PW'] %>
database: *****
encoding: utf8
timeout: 5000
pool: 5
It is working perfectly in the server. But recently there was a bug and I tried to access rails console, but I get this error
Access denied for user '****' (using password: NO) (Mysql2::Error).
I also I tried to run migration and I get same error again. I don't understand what is the problem here. How can I solve this?
Also how can I check if ENV['MYSQL_PW'] is set in the unix environment variable?
Here is my log
$ rake db:migrate
DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in <top (required)> at )
DEPRECATION WARNING: You did not specify a `log_level` in `production.rb`. Currently, the default value for `log_level` is `:info` for the production environment and `:debug` in all other environments. In Rails 5 the default value will be unified to `:debug` across all environments. To preserve the current setting, add the following line to your `production.rb`:
config.log_level = :info
. (called from block in tsort_each )
rake aborted!
Mysql2::Error: Access denied for user '****' (using password: NO)

Don't forget to specify RAILS_ENV when you are running your commands.
By default rails assumes the environment is development, but as I see here you want to run those on production.
Simply do
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rails c
# or alternatively
rails c -e production
http://guides.rubyonrails.org/command_line.html

Try the following:
Edit database.yml password to be a string and not an ENV variable, to identify the problem.
To test the ENV Variable, log in the rails console with rails c and input ENV['YourVariable'] to see if it is set
You can solve this problem by sourcing your bash_profile file if you have Unix system (linux/ubuntu/mac) in the terminal input source ~/.bash_profile
If you have Rails 4 upon you should run the following terminal command spring stop
In database.yml you should include the ENV Variable with the following syntax, beacuse yml needs the .erb syntax. <%= ENV['YOUR VARIABLE'] %>
Failing to access environment variables within `database.yml` file
The ENV Variable is Case Sensitive
I had this problems and I was able to solve it, but I keep having them. For this reason I read several discussions:
Rails 4.1 environment variables not reloading
Sorry if I was not able to help more
Fabrizio Bertoglio

Related

How can i Import ruby on rails application to another computer

I'm trying to import ruby on rails project to my computer but am running into all sorts of problems with the database.
Here's the situation:
I have an appname.tar.gz that I got from another developer. I extract it and relocated it to my user directory to work with it.
Next I run bundle install in the directory to install gem
dependencies.
Then I run rake db:create to create the database and
load the schema and structure from the DB folder in the same directory. This is where I'm running into all sorts of issues. When I launch the app I get an error saying DB migration pending.
structure.sql is a MySQL dump 10.13
while the database.yml file had the adapter set to postgres: adapter: postgres. Is this normal?
Is this the best way to import an existing app into your environment?
Any help will be appreciated.
To me this sounds like you got a MySQL dump and the application is configured to use postgres. I am wonderning why this is but you can try using the mysql adapter in your config/database.yml file by setting something like
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
host: localhost
development:
<<: *default
database: foobar_development
The interesting bit is adapter: mysql2.
I have taken this from a brand new rails application. You need mysql running for sure.
Hope this helps. If not you have to reach out to the other person to get some postgresql compatible dump.
I got it to work! Thank you all for your help! Really appreciated it.
Below are the steps that worked for me:
Copy the project to the new computer
Open terminal and change directory to the project
Run bundle install to install all the gem dependencies
Run bundle update to update installed gems
Start the Postgres server by running pg_ctl -D /usr/local/var/postgres start
Run createdb databasename to create the database on the new computer. The database name has to match the database mentioned in the database.yml file in the config folder.
Run rake db:schema:load and that's it!
After following the above steps I was able to run rails s to check out the website.

rake db:create not working for legacy rails app (2.3.5) using MySQL (5.5.28)

I'm a new Rails Developer, and I'm working on a legacy Rails app. Whenever I run the rake db:create command, I get an error that the database couldn't be created. I have found many StackOverflow questions related to this, but in troubleshooting nearly all permutations of solutions, I couldn't resolve the issue.
I created the three Dbs (dev, prod, test), created the user with all access privileges to these dbs, and ran rake db:create.
I'm running Mac OS X Lion, MySQL 5.5.28, Rails 2.3.5, Ruby 1.8.7. Here are my settings
development:
adapter: mysql
encoding: utf8
database: adva_development
username: adva
password: ****
host: localhost
socket: /tmp/mysql.sock
Here's the error:
Couldn't create database for {"adapter"=>"mysql", "username"=>"adva", "host"=>"localhost", "encoding"=>"utf8", "database"=>"adva_development", "socket"=>"/tmp/mysql.sock", "password"=>"****"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)
I have done the following troubleshooting:
Verified user and password are correct, and the user has access to the DB. (Double checked user access with SELECT * FROM mysql.db WHERE Db = 'adva_development' \G; User has all privileges.)
Verify the socket is correct. I don't really understand sockets, but I can plainly see it at /tmp/mysql.sock.
Checked collation and character set. I found out I had created the DB in latin charset and collation, so I recreated them. I ran show variables like "collation_database"; and show variables like "character_set_database"; and came back with utf8 and utf8_unicode_ci respectively.
I followed the instructions in this question. After uninstalling mysql gem, I ran the following but came up with the same error:
gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/usr/local/mysql-5.5.28-osx10.6-x86_64/bin --with-mysql-config=/usr/local/mysql-5.5.28-osx10.6-x86_64/bin/mysql_config
Following Matt's suggestion, here's what a rake --trace db:create reveals:
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:create
Couldn't create database for {"database"=>"adva_development", "adapter"=>"mysql", "host"=>"127.0.0.1", "password"=>"woof2adva", "username"=>"adva", "encoding"=>"utf8"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)
After 3 days and six or seven hours, I have pretty much run out of options. I tried various random things, like replacing localhost with 127.0.0.1 to no avail.
Could there be something wrong related to my specific environment? Mac OS X Lion + MySQL 5.5.28? I plan on trying on setting up everything in a Linux environment.
Thanks!
You can try changing localhost to 127.0.0.1 and removing socket to use TCP/IP instead of a local socket.
Or try to verify if your database server uses the same socket as Rails:
mysqladmin variables | grep socket
If this doesn't help, then try to connect to the database server with pure Ruby and create a new database:
require 'rubygems'
require 'mysql'
begin
db = Mysql.new('localhost', 'root', 'yourpassword')
db.query('create database sample_database;');
rescue Mysql::Error => e
puts e
ensure
db.close unless db.nil?
end
and see if this runs. You should have mysql gem installed (gem install mysql)
Make sure you running a more recent version of the mysql gem. I ran into this problem with version 2.8. Upgrading to 2.9 fixed the issue.
change 'localhost' into '127.0.0.1' (loop back address) , hope its works

Rake db:create continues to use /tmp/mysql.sock despite my.cnf settings

I'm on a Mac and used brew to install an updated MySQL. In an effort to streamline my local database creation, I took the password off my local root user (I know, I know), and after creating a new Rails app (rails new myapp -d mysql), should just be able to run rake db:create, right?
However, despite having /etc/my.cnf set to define the standard mysql.sock location...
[mysqld]
socket=/usr/local/var/mysql/mysql.sock
[client]
socket=/usr/local/var/mysql/mysql.sock
... Rails still won't use the newly defined mysql.sock file on a rake db:create:
rake db:create
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "reconnect"=>false, "database"=>"delayed_jobs_development", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost"}, charset: , collation:
Running mysqladmin variables lists the correct socket file location, as does mysql --help. I've restarted the MySQL server and even recreated the Rails app.
So my question: What else could be defaulting the socket file to /tmp/mysql.sock, or is there an alternate, preferably global, config file where I could specify its location that the rake task will honor? Could the rake task be calling a different commandline MySQL tool that uses a different config file?
Obviously, I could either create a symlink from the /tmp location to my real location, or edit my database.yml file to refer to it. I understand how to get Rails to talk to the DB server correctly, but the point is to have the proper defaults set up once, so any future Rails app I create locally is good to go without extra edits (/tmp/mysql.sock gets cleared on reboot).
In fact, I'm not even sure why it's trying to connect via the socket file at all, since my database.yml file tells it to use the hostname:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_development
pool: 5
username: root
password:
host: localhost
As the MySQL documentation says, /tmp/mysql.sock is the default location for the socket file. This is also what rails defaults to (more below).
As you mentioned, you can always set the socket (the one you configured and that is returned by mysqladmin variables) explicitly in your database.yml file:
socket: /usr/local/var/mysql/mysql.sock
I think the connection is made via the socket file as you set host: localhost. If you change that to 127.0.0.1, the connection should by made over TCP/IP.
I found rather old information (a really old version of the mysql adapter and a blog entry from 2007) which implies that the MYSQL_UNIX_ADDR environment variable is used in resolving the socket. Also see this comment on GitHub by the developer of Ruby/MySQL, in which he says to set the MYSQL_UNIX_PORT variable instead. A third env variable is MYSQL_SOCKET mentioned in the source of the Ruby/MySQL connector.
export MYSQL_UNIX_ADDR=/usr/local/var/mysql/mysql.sock
export MYSQL_UNIX_PORT=/usr/local/var/mysql/mysql.sock
export MYSQL_SOCKET=/usr/local/var/mysql/mysql.sock
I do not know if these variables get evaluated somewhere along the way. Still, you could try them out.
Right now, rails mysql_adapter.rb says that the configured socket is used and that it defaults to /tmp/mysql.sock (set in app_generator.rb). No other locations seem to get checked. I did not found anything else in the default Ruby/MySQL adapter used by rails.
So basically – without being an expert on this topic – I don't think there is a way to set a global default location for the MySQL socket file that gets evaluated by rails or the used default adapter.

active record error in Ruby on rails

I am having trouble in rails, I have just installed it but when I update after updating mysql settings and running
rake db:create
and then
rails server
It started server and then when I tried viewing it via browser there errors saying active record connection not established error in strange way. I am new to both ruby and rails so that's why not understanding by debugging info. I assume that there is some thing wrong in MySQL configuration. I am using it on windows and using railsinstaller and using MySQL that came with XAMPP.
So can anyone tell that what is wrong with it and how can it be solved? Or is it better to use Linux for RoR? I do many things on windows thats why if there will be some solution at windows then that would be helpeful.
thanks for your time, following is attached output image.
I also observed that rake db:create command is not creating db, I had to do this manually. Following is my configurations for db:
adapter:mysql2
host:localhost
encoding:utf8
database:kaasib_new
pool:5
username:root
password:~
So is this fine? I don't have password on local machine db and do I need to mention 3306 for mysql in it?
A couple of things to try:
If the tilde character in the password field above is a typo, ok,
but there shouldn't be anything there.
Not sure if it's a function
of posting here, but Whitespace matters in YAML files. It should be
set up with indents like below (socket is optional):
.
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: app_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
Open Gemfile from your project.
add line => gem 'mysql2'
run command => bundle update
restart your server.

Rails / MySQL2: Error - Unknown database

I'm following a basic tutorial in Linda.
I have been able to install everything properly now, but when I start my Rails server I get this message when I visit localhost:3000:
Unknown database 'simple_cms_development'
and then
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (23.5ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (29.8ms)
Have you created the database in MySQL? You should be able to run rake db:create and have Rails create it for you.
I had the same error, please run the following command on the Command Prompt:
rake db:create
to solve the problem.
Look for the answers of these questions:
Have you installed the mysql2 gem?
Is it mentioned in your Gemfile?
Did you run the command rake db:create ?
Sometimes creating database with rake causes issues.
You can also create the database inside mysql
Make sure mysql is in the root %PATH% in command prompt type echo %PATH% to check.
If it isn't in your PATH. Then do a quick google search on windows PATH to get instructions
Open command prompt
type mysql -u root -p
type your password that you created for your root
To create database
create database simple_cms_development
done
was getting the same error but caused was different
Mysql2::Error: Unknown database 'rdddd_development'
/Users/.rvm/gems/ruby-2.6.3/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in connect'
/Users/commeasure/.rvm/gems/ruby-2.6.3/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:ininitialize
faced this error due to the created method dynamically, code is here
Role.all.map(&:name).map(&:parameterize).map(&:underscore).each do |name|
define_method("#{name.to_sym}?") do
role.name == name.upcase
end
end
How do I fix this for the temporary purpose just comment it out