Getting a PDO error when connecting - php

So I'm working on my PDO skills and so far progress isn't so good. So here's the code corresponding to my issue.
$handler = new PDO('mysql:host=localhost;dbname=s', 'root', '*');
That's basically my connection to my database. Now here's the error I get
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)
I'm not sure why my access is being denied since the credentials are 100% correct. I've also tested that through the GUI for the database and was able to sign in successfully. Any ideas? I've also tried variables in the username and password section, but it didn't work nor does 127.0.0.1. I'm on localhost on a Mac. Suggestions?

I've got same problems recently, what helped me was reseting my mysql root password:
First, login to user using mysql console:
mysql -u root -p[password]
(no space between -p and your password.
Then, type in command:
UPDATE user SET password=PASSWORD('newpassword') WHERE user='root';

Related

MAMP and PHPMyAdmin: #1045 - Access denied for user 'root'#'localhost' (using password: YES) [duplicate]

This question already has answers here:
MySQL ERROR 1045 (28000): Access denied for user 'bill'#'localhost' (using password: YES)
(43 answers)
Closed 4 years ago.
I have MAMP installed and I've never had a problem with it. Today I tried logging in to PHPMyAdmin and I got the following error message:
1045 - Access denied for user 'root'#'localhost' (using password: YES)
In my config.inc.php file, the following options are set:
$cfg['Servers'][$i]['auth_type'] = 'config'; // Authentication method (config, http or cookie based)?
$cfg['Servers'][$i]['user'] = 'root'; // MySQL user
$cfg['Servers'][$i]['password'] = 'root';
I've never changed my password. This is a local install, so I've always used 'root' for username and password.
I've tried accessing MySQL through the terminal:
/Applications/MAMP/Library/bin/mysql
But I get this:
ERROR 1045 (28000): Access denied for user 'boneill'#'localhost' (using password: NO)
Not sure what could have happened. I haven't changed passwords or anything like that. It just stopped working all of a sudden. Any ideas?
Start MAMP
The MySQL server must be running to change the password, open and start MAMP now. keep it running until the end of this.
Mac Terminal
With the MySQL server running, open the terminal (located in '/Applications/Utilities') enter or copy/paste the following:
/Applications/MAMP/Library/bin/mysqladmin -u root -p password
You'll be prompted for a password. Enter the current password for the root user. If you changed it in phpMyAdmin, use that password.
Mosty password is blank so just press Enter and Enter Your New Password.
$ /Applications/MAMP/Library/bin/mysqladmin -u root -p password
Enter password:
$ /Applications/MAMP/Library/bin/mysqladmin -u root -p password
Enter password:
New password:
Confirm new password:
Now You Have to make changes in some file:
You need to replace all occurrences of the old root password with the new one you just created. To keep it easy here is a list of the files, you can open them in your favorite text editor and search for 'root', changing
'-proot' to '-p NewPassword', replacing 'NewPassword' with your own.
/Applications/MAMP/bin/phpMyAdmin/config.inc.php
/Applications/MAMP/bin/checkMysql.sh
/Applications/MAMP/bin/quickCheckMysqlUpgrade.sh
/Applications/MAMP/bin/repairMysql.sh
/Applications/MAMP/bin/stopMysql.sh
/Applications/MAMP/bin/upgradeMysql.sh
Stop and restart MAMP
I hope that works/make sense because this works for me.
If I were you, I would just take your htdocs folder and just reinstall mamp. I've had this issue occur installing wordpress locally, setting the config file with a db user without a password, and then changing the password after the config was created in the installation process. I don't know why I did this but this installed wordpress with a user without a password, when the database required a password, so it then was giving me an authentication error.
Taking into consideration that you seem to have trouble debugging the issue, I think the most easiest way to rule out some of the probabilities is to install:
MySQL Workbench
It's free and you can use it as a GUI to try logging in to "localhost" or better "127.0.0.1" port 3306, user "root", password as you've chosen.
If you still notice an error connecting to the database, go to control panel -> services and check the status of the MySQL database. It might have crashed. Try to restart the service. If it shuts down again immediately, check your disk space. If you are running low on disk space, tables might have got corrupted. Also, check your firewall rules. If you are working in a corporate environment, somebody may have deployed new security polices - this might lead to port blocking, application networking issues etc. pp. -- even on your local host. Automatic windows updates might cause those changes too.
If it works using MySQL workbench, you can rule out any issue on database side. You can focus on PHP / MAMP then. Check the version and if any update has been installed (probably automatically). Is php mysql module installed and configured (php.ini, ensure php mysql dll is loaded).
Go to MAMP dashboard and Click on MySQL tab.
There button called "Change password for user "root":
Click on the button and change it.
Thats it.

MySQL telling me credentials are incorrect when accessed through PHP script

I can access my database with
mysql -u root -p
and then by entering my password.
But when I try to login through a php script
$link = mysqli_connect('localhost', 'root', 'mypassword');
I get the following error
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: YES) in /Library/WebServer/Documents/connect/index.php on line 6
When I run
SELECT USER(),CURRENT_USER();
in MySQL, I get the following (reformatted)
root#localhost | root#localhost
So I know the three parameters in my login function are correct, but I am not able to sign in.
I would appreciate any help.
Edit: Here are the grants
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD '*0503AA35367B049092549C66DFE0D6C3AA3C4970' WITH GRANT OPTION || GRANT PROXY ON ''#'' TO 'root'#'localhost' WITH GRANT OPTION
EDIT: just to be explicit, what solved my problem was using 127.0.0.1 instead of localhost.
This error is usually because of an incorrect username password combination.
User root normally has no password unless you've added one. Try going into PHPMYADMIN or other config files and dig up the password for root.
Another issuer is, you could try connecting to a specific database (the fourth parameter of mysqli_connect and do a SHOW GRANTS on user root) to ensure you have the privileges. root may not have the necessary grants, and I think that is what could be happening.
https://dev.mysql.com/doc/refman/5.0/en/show-grants.html
Edit: Answer in the comments, changed localhost to 127.0.0.1, likly some configuration stuff in the server hosting environment

What's wrong with this MySQL connection?

I have setup a MySQL database and I am trying to connect to it from another website.
I get this error when I try to connect to it:
Connection failed: Access denied for user 'myUsername'#'localhost' (using password: YES)
It works fine when I do it in the same website where the database is hosted.
I tried this SQL query:
GRANT SELECT, DELETE, UPDATE ON database.myDatabaseName TO 'myUsername'#'localhost' IDENTIFIED BY 'myPassword';
But I got this error:
#1142 - SELECT,UPDATE,DE command denied to user 'Steve'#'localhost' for table 'myDatabaseName'
I went to the MySQL management in the built-in control panel where I modified permissions of Steve and added the Grant permission, however I still get the same #1142 error.
How can I fix this?
Thanks in advance.
Access denied for user 'myUsername'#'localhost' (using password: YES)
That's where the error is. It says you are attempting to connect to localhost with the username myUsername and a supplied password. However, usually on localhost, especially on windows systems, the user is Root and the password is not set, just an empty string. Check your username on your localhost and your password.

Connect to database, got access denied error code 1045

I am learning php now. I got a database information from my supervisor. But I have no idea how to use it. I am trying to access database to create some forms for my php.
all I have: Database Host, Database Name, Database User, Database password.
I was trying to connect to that host by ssh.
shell> ssh username#database host
Then, I logged into server. And, I tried:
shell> mysql -h databasehost -u username -p
then, I inputed database password.
I got a Access Denied Error.
ERROR 1045 (28000): Access denied for user 'vincent.ma'#'localhost' (using password: NO)
I have no idea how it works.
How can I use my database?

Why do I get "Access denied for user 'root'#'myhostname" when connecting to my database in PHP?

I have set up an Apache server on mandriva linux 2010.1. But the problem is whenever I'm trying to connect with the database, it's giving me the following error:
Error:Database error: SQLSTATE[28000]
[1045] Access denied for user
'root'#'myhostname' (using password:
YES)
Normally for a web application, you shouldn't connect to the database as root. However you tagged your post as [phpmyadmin] so I assume your issue is with, well, phpMyAdmin, in which case you might be connecting as root.
If this is the case, I see that you mentioned myhostname in your error message. Have you tried connecting to localhost instead? Sometimes the MySQL root user cannot connect from remote hosts.
you need to set some configuration variables for phpmyadmin to work:
http://www.phpmyadmin.net/documentation/Documentation.html#config
GRANT ALL PRIVILEGES ON *.* TO monty#localhost
IDENTIFIED BY 'indian' WITH GRANT OPTION;
Replace your username and password and execute it in your phpmyadmin by selecting your database.
Take a look at "5.4.7. Causes of Access-Denied Errors" in the MySQL online documentation and Using authentication modes" in the PHPMyAdmin documentation.

Categories