Link to home
Start Free TrialLog in
Avatar of wpstech
wpstechFlag for United States of America

asked on

Error Code 1045: Access denied for user 'root'@'127.0.0.1' (using password: YES)

I've installed MySQL 5.0.91. It is running on the same server as the PHP scripting engine.
I'm taking a php class and am working with using forms to create and write to a database. When I fill out the form in my browser and click Submit, I get the following error:

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

Below is the code I'm using to connect ("localhost" and "root" are literal values).
$DBConnect = mysql_connect("localhost", "root", "password");

I've searched and read a number of forums and have tried the following suggestions:
- Changing the config.inc.php file:
      from
      $cfg['Servers'][$i]['host'] = 'localhost';
      to
      $cfg['Servers'][$i]['host'] = 'Localhost';

- Leaving the password blank (the default) and using ("localhost", "root", "");
  Then tried assigning a new password, changing the config.inc.php file
      from
      $cfg['Servers'][$i]['password'] = '';
      to
      $cfg['Servers'][$i]['password'] = 'password';
      
Logged onto MySQL Console (no problem here) and entered the following:
      FLUSH PRIVILEGES;
      SET PASSWORD FOR root@'localhost' = PASSWORD('password');
      USE mysql
      UPDATE user SET Password = PASSWORD('newpassword')
      WHERE Host = 'localhost' AND User = 'root';
Then
      USE [DBName]
      GRANT ALL ON [DBName] TO ROOT;

- This was also suggested - changing
      $cfg['PmaAbsoluteUri'] = '';
      to
      $cfg['PmaAbsoluteUri'] = 'localhost/phpmyadmin/';
      But there was no $cfg['PmaAbsoluteUri'] = ''; line in the config.inc.php file

I've gone back and forth on a number of "solutions" but keep coming up with the same error message. Any help would be appreciated. I'm stuck until I can move beyond this issue.
Avatar of SANDY_SK
SANDY_SK
Flag of India image

try this, create another user

login to mysql console

grant all on *.* to 'root'@'127.0.0.1' identified by 'password';

FLUSH PRIVILEGES;

Open in new window



if that dint work can you run this query and show us the output

use mysql;

SELECT HOST,USER FROM USER;

Open in new window

Avatar of wpstech

ASKER

Didn't work. Query results as follows:
Host: % User: ROOT
Host: % User: Root
Host: 127.0.0.1 User: root
Host:  ::1 User: root
Host: localhost User: [blank]
Host: localhost User: root
I think the problem is you have enabled the anonymous  user hence the last but one record where user is blank.

What you do is  drop that anonymous user like this after logging into mysql console
DROP USER ''@'localhost'; 

Open in new window


then try again
Avatar of wpstech

ASKER

User dropped but still getting error.
That is really strange, can you restart mysql service and then try.

Also make sure the current passwords in the config.inc.php file is the latest ones.
Avatar of Dave Baldwin
Everything that starts with  $cfg['Servers'][$i]... is for phpMyAdmin.  And this $DBConnect = mysql_connect("localhost", "root", "password"); looks like it's in your own program.  When everything is working, the host, username and password set in MySQL controls access.  Every program that accesses MySQL must use that same info (although there can be other users than 'root').
Avatar of wpstech

ASKER

Yes, the $DBConnect code is in my program. In both phpMyAdmin and MySQL Console, host is 'localhost', user is 'root', and password is a hashed value. When I pull up phpinfo(), the host is 'localhost' and the user is 'no value' (it previously had been 'root'). Does the user have to be 'root' here also and, if so, how do I reassign the value?
Yes put it back to root and also instead of localhost try giving '127.0.0.1' in your code and try
Avatar of wpstech

ASKER

Have already tried IP but will do so again. How do I reassign the value 'root' so it shows up in phpinfo()?
phpinfo() has nothing to do with MySQL.  Any user name you find there has no effect on your MySQL login.  

MySQL stores your password as hashed value when you create it to prevent others from getting it.  But you do not use the hashed value for a connection, you use the plain text version.  When a connection is requested, the value you use in the connection is hashed and compared to the stored value.

In all cases, you use the plain text version to make your connection.  And in all cases, the data must be the same.
Avatar of wpstech

ASKER

That's correct. I wasn't using the hashed value, just mentioned it to avoid using the actual password in my explanation. Sorry for the confusion. Have been using the same data - is why I'm confused at the access denied error. Already gave all access to all databases for user.
Avatar of wpstech

ASKER

I am able to log in to MySQL. The issue is that my program runs until it reaches the line $DBConnect = mysql_connect("localhost", "root", "password");  and then I get the access denied error message above. All the values seem to be the same, and I've tried reassigning the values and granting all privileges to all databases but am still coming up with this same error.
$DBConnect = mysql_connect("localhost", "root", "password");

Is your database on the same machine as this code?  If it is Not, then you can't use 'localhost'.  'localhost' is Only valid for the machine you are on.  If the database is on another machine, you have to use the IP address or hostname in place of 'localhost'.
one wild thought, try disabling the windows firewall or antivirus (if any) and then check
Disabling the windows firewall and/or antivirus has never been necessary in my experience.  If you can login to MySQL with phpMyAdmin, then those things are not a problem.
@DaveBaldwin as i mentioned its a wild thought

What you are saying is true as long as it is localhost, but i have faced this problem when the db was getting accessed from other m/c and on disabling the antivirus did help that time.
Another situation, similar to this that I've run into in the past -

If the application is running on a separate host from the MySQL server, if the MySQL installation was performed with mysql_secure_installation and 'Deny remote login for root' was confirmed, then the root user would not be allowed to connect from ANY remote host, even if using the correct password.

Just a thought for you -
ASKER CERTIFIED SOLUTION
Avatar of learningtechnologies
learningtechnologies
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial