MySQL Error 1045 Access Denied

Good morning,

I wrote the code block below on my local PC with Windows 7 and tried to run it. Sorry, I received:

Connect Error (1045) Access denied for user 'dbuser'@'myhost(using password: YES) 

I provided dbuser Insert, Select, Update, and Execute using localhost and% for this database schema. I can also mysql -u dbuser -p from the command line on the server.

Here's the code block:

 <?php /* Set Variables */ $host="serveripaddress"; $db="dbname"; $username="dbuser"; $pass="pass"; /* Attempt to connect */ $mysqli=new mysqli($host,$username,$pass,$db); if (mysqli_connect_error()){ die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); echo 'Success... ' . $mysqli->host_info . "\n"; $mysqli->close(); } ?> 

I am having difficulty understanding whether the above code block causes my error or if something needs to be done on the server. Can anyone suggest some areas of investigation?

Thanks Sid

+1
source share
2 answers

Make sure that if you use the host name for GRANT in MySQL, MySQL can correctly resolve this host name to the IP address from which you are connecting.

For example, if you do

 GRANT blah ON *.* to user@somehost 

you must remember that MySQL will not see "somehost", it will see the IP address. He will have to do a reverse lookup to get the host name, and if the IP either does not have a reverse mapping or displays something completely different, MySQL will not give access.

If you cannot guarantee that the reverse mapping is stable, it is best to use the IP addresses for remote access accounts in MySQL.

+1
source
 <?php /* Set Variables */ $host="127.0.0.1:3306"; $db="dbname"; $username="dbuser"; $pass="pass"; /* Attempt to connect */ $mysqli=new mysqli($host,$username,$pass,$db); if (mysqli_connect_error()){ die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } else { echo 'Success... ' . $mysqli->host_info . "\n"; $mysqli->close(); } ?> 

First of all, add these curly braces for the if / else statement. Secondly, try hardcoding IP. I just ran this with the IP set to the variable (did not work), and then I hard-coded it, it worked just fine.

+1
source

Source: https://habr.com/ru/post/1346668/


All Articles