0

I'm using this code to connect to a MySQL database:

username="root"
password="admin"
host="localhost"
db="one"
Mysql.new(host,username,password,db)

I got this error while executing the code:

Access denied for user 'root'@'localhost' (using password: YES)

If I use Mysql.new('localhost','root','admin','one') then it works fine.

When I print all the variables the correct values are printed.

Please help me figure out what's wrong.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Jaydipsinh
  • 491
  • 1
  • 9
  • 27
  • I don't understand why this is a Sinatra or Ruby question. It looks like your MySQL database password is different than what you think it is. Unless you're going to show us the configuration for your root account, and its password in the database so we can compare it to what you're sending, we can't really help you a whole lot. I'd recommend carefully reading through the [MySQL account management documentation](http://dev.mysql.com/doc/refman/5.1/en/user-account-management.html); MySQL isn't the most straightforward DBM when it comes to managing accounts. – the Tin Man Aug 26 '13 at 15:09
  • i'm using this code in ruby. no mysql database password is not different.. If I use Mysql.new('localhost','root','admin','one') then it works fine. – Jaydipsinh Aug 26 '13 at 15:09
  • This smells a lot like: http://stackoverflow.com/questions/6081339/access-denied-for-user-rootlocalhost-using-password-yes-mysqlerror?rq=1 Read through the "Related" questions on the right. – the Tin Man Aug 26 '13 at 15:34

1 Answers1

4

Basic trouble-shooting 101:

If the literal strings work, and variable assignments don't, then suspect your variable assignments; Something could be wrong with one of them, such as an invisible character.

Try:

vars = ('localhost','root','admin','one')
Mysql.new(*vars)

Then try:

vars = %w(localhost root admin one)
Mysql.new(*vars)

or:

host, username, password, db = %w(localhost root admin one)
Mysql.new(host, username, password, db)

or:

host, username, password, db = 'localhost','root','admin','one'
Mysql.new(host, username, password, db)

The idea is to start with what you know works, and work backwards until assigning to variables works, or it breaks. At that point you've either fixed the problem (possibly without knowing why), or you've learned what broke it. Either way you're ahead.

You say:

When I print all the variables the correct values are printed.

HOW are you printing the variables? We can't see your terminal, and you didn't give us that information. If it's an invisible character, which are common culprits when copying and pasting text, it might not show up when printing. Inspecting the variables should help, as they should display as escaped values:

password = "root\u00a0"

puts password
# >> root 

puts password.inspect
# >> "root "

puts password.chars
# >> r
# >> o
# >> o
# >> t
# >>  

puts password.unpack('C*').map{ |s| s.to_s(16) }
# >> 72
# >> 6f
# >> 6f
# >> 74
# >> c2
# >> a0

Otherwise, I revert back to my original comment, that this is a problem with how you set up your MySQL root account and you should read "Access denied for user 'root'@'localhost' (using password: YES) (Mysql::Error)".

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • ohh yes when i execute bellow statement puts password.inspect i got "admin\n" . can you tell me how to remove that \n ?? thanks a lot. you have given such a great tricks of debugging :) – Jaydipsinh Aug 27 '13 at 05:06
  • ya i got password.gsub("\n",''). this statement solved my problem. Thanks buddy! – Jaydipsinh Aug 27 '13 at 05:12
  • If you would have included a little piece of code that *accurately* reproduced the problem, we could have fixed the problem in a minute. It sounds like you are using `gets` to read the information from the keyboard, or incorrectly reading a file. Either is a trivial fix. – the Tin Man Aug 27 '13 at 15:46
  • ya u r ri8. i have to put piece of code. yes i'm reading that username and password from configuration file. – Jaydipsinh Aug 28 '13 at 04:40