what is preventing PHP from connecting to my MySQL database?

橙三吉。 提交于 2019-12-08 04:24:28

问题


[Updated below]

I am having a problem connecting to a MySQL database using a Bitnami LAMP stack. This is the code I used to connect on the shared-hosting server where I originally hosted the database:

DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'database');

if ($dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) { // Make the connnection.

    if (!mysql_select_db (DB_NAME)) { // If it can't select the database.

        // Handle the error.
        trigger_error("Could not select the database!\n<br />MySQL Error: " . mysql_error());

        exit();

    } // End of mysql_select_db IF.

} else { // If it couldn't connect to MySQL.

    trigger_error("Could not connect to MySQL!\n<br />MySQL Error: " . mysql_error());
    exit();

} // End of $dbc IF.

Running this on my Bitnami server, I get no response (not even an error message). (PHP is working fine.)

I then tried

<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

and got

Could not connect: Access denied for user 'username'@'localhost' (using password: YES)

this username/password combination definitely exists and (as far as I can tell) has access to the database in question. I am wondering what I am doing wrong and why the original script does not produce an error (or connect).

Update:

I followed Michael's suggestion below, and was able to connect. I wanted to reproduce the issue so I deleted that user then created a user testuser via

CREATE USER 'testuser'@'%' IDENTIFIED BY  '***';

GRANT SELECT , 
INSERT ,

UPDATE ,
DELETE ,
FILE ON * . * TO  'testuser'@'%' IDENTIFIED BY  '***' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

Then following Michael's suggestion, I executed

GRANT ALL PRIVILEGES ON test . * TO testuser@localhost IDENTIFIED BY  'password'

but again I am getting

Could not connect: Access denied for user 'testuser'@'localhost' (using password: YES)

(This doesn't work whether I use '%' or 'localhost')

this is what the privileges look like for testuser in phpmyadmin

Database    Privileges  Grant   Table-specific privileges
test     ALL PRIVILEGES No  No   Edit Privileges

回答1:


The error message received is clear - the user you're connecting with does not have access via the password you are using. You'll need to grant privileges accordingly:

GRANT ALL PRIVILEGES on dbname.* to username@localhost IDENTIFIED BY 'thepassword';



回答2:


Try it, if you user has not password:

$link = mysql_connect('localhost', 'username');



回答3:


Two things:

  1. Make sure you are using what your shared hosting providers MySQL host path should be. I know for GoDaddy, it is something like DBNAME.db.123456.hostedresource.com. It will be different for yours I am sure. localhost is probably not it.
  2. Make sure that any php file you are using for your connection (where you type in your DB credentials) is OUTSIDE of the webroot. This is a big security risk if you put this file inside your web root directory or below. If PHP crashes on the server for whatever reason, the file WILL be rendered out to the browser for anyone to see.



回答4:


Try

username: 'root'

password: '' (i.e. Without a password)



来源:https://stackoverflow.com/questions/9025182/what-is-preventing-php-from-connecting-to-my-mysql-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!