简体   繁体   中英

Unable to access remote MySql Server from local PHP script

Hoping you experienced folks can clear up my frustrations! I've searched & researched and found multiple sources discussing similar issues to what I'm trying to troubleshoot, but none seem to help the situation. Without giving an entire historical listing of what I've tried, here's where things currently stand:

  • I have a simple Debian server set up with a MySql database. All relevant firewall ports have been opened and specific GRANT PRIVILEGES have been applied for 'username'@'mylocalglobalip' (username not being root).
  • On my local Apache instance (I'm currently running it on OS X), I have the latest PHP installed and working. I've confirmed this by going to localhost/website/phpinfo.php - PHP itself is working fine.
  • I've gone through all the tutorials on setting up a simple MySql connection using mysqli. Here's my simple code (with the credentials deleted):

    $connection = mysqli_connect($mylocalglobalip,$username,$password,$dbname);

    No matter what I've tried, I keep getting the friendly

"mysqli_connect(): (HY000/1045): Access denied for user 'username'@'mylocalglobalip' (using password: YES)" warning.

  • If I SSH into the remote server, I can get into the MySql console from both my non-root and root logins. However, I can only login if I use...

    mysql -u username -p

    ... and supply my password when prompted. However, if I enter...

    mysql -u username -pPassword

    ... no luck. I again get Access Denied.

  • Finally, to make sure everything is open on my firewalls and that my server and MySql are accessible remotely, I installed a local instance of MySqlWorkbench. I put in the same credentials and I can login to the remote server without a problem.

Why would I be able to login through the Workbench, but not through my local PHP script? Is there an obvious parameter I need to enable in a local and/or remote config file? From the php and mysql config files I've glanced at, I can't see anything that would be blocking the PHP connection.

I'll be happy to provide config and code snippets upon request!

Thanks for reading!

Thanks to the comments and responses, the solution presented itself. Michael (sqlbot) mentioned using -p'your-password' in single quotes to encapsulate any special characters in the password. Then it dawned on me (being a PHP noob), to check my $password variable in the PHP code. Sure enough I had something to the effect of:

$password = "some random pa$$word goes here";

Whoops. The $ character within double-quotes in PHP signifies a variable insertion within a string. This was most likely being interpreted as:

"some random pa goes here" // Since $ is empty and $word isn't a declared variable!

Putting the password with a $ special character in between single quotes solved the simple PHP syntax problem.

Thanks for stopping me from going down the path of server reconfiguration and encouraging me to search my PHP code more closely!

It's a bug hunt, and it sounds like credentials are wrong in at least some cases.

Check the mysql logs and verify it is showing the same user when you log in successfully from the command-line and when you do it from PHP or when you fail from the command line. You may sometimes be showing up as 'username' or 'username'@'localhost' or 'username'@'fqdn' and be being treated differently based on that.

Check carefully letter-by-letter to be sure you are not typing the credentials differently in two different places. Verify credentials are being imported into your php file correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM