-1

I'm getting the 'access denied error while trying to connect to the database. Here is the code:

    <?php
    mysql_connect("00.00.00.00:00000","user","pass");
    mysql_select_db("dbname");

    $q=mysql_query("CALL  GetMaterialInfo('".$_REQUEST['user']."','".$_REQUEST['pass']."','6c3e91d3')");
    while($e=mysql_fetch_assoc($q))
        $output[]=$e;

    print(json_encode($output));

    mysql_close();
    ?>

I changed the ip adress of the host ofcourse (its not all zero's) and the username and password and database name. I'm sure the user and password, aswel as the host ip and portnumber and the database name are correct. I managed to connect to the database allready with JDBC connector but it gives the following error through PHP script:

<b>Warning</b>:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'username'@'192.168.1.1' (using password: YES) in <b>/www/zxq.net/t/e/s/testphpbach/htdocs/Connection.php</b> on line <b>2</b><br />


Nanne
  • 64,065
  • 16
  • 119
  • 163
user1337210
  • 241
  • 3
  • 6
  • 11
  • do you have the entry for 192.168.1.1 in /etc/hosts – Satya May 03 '12 at 13:12
  • 2
    You are *not* using the correct username and password *or* the user is not allowed access from your host. This is a configuration issue and the error message tells you everything you need to know. – hakre May 03 '12 at 13:13
  • You are either not using the correct username and password, or the selected username has no access from that host – Anigel May 03 '12 at 13:14
  • possible duplicate of [Access denied for user 'root'@'localhost' (using password: YES) (Mysql::Error)](http://stackoverflow.com/questions/6081339/access-denied-for-user-rootlocalhost-using-password-yes-mysqlerror) – hakre May 03 '12 at 13:15
  • Check if the username is in mysql.users table and it has correct `GRANT`s. – web-nomad May 03 '12 at 13:15
  • Yes it has the correct GRANT's, also connections from ALL hosts are allowed. username and password are correct (copied straight from the database) – user1337210 May 03 '12 at 13:19
  • 'username'@'192.168.1.1' (using password: YES) means you have no permission to connect with password. try removing your password. Or use grant query. – Dhruvisha May 03 '12 at 13:53

3 Answers3

4

you need to do the following checks :

  1. Check that 192.168.1.1 is included in /etc/hosts . If your db is on a localhost machine , try using localhost instead of ip.
  2. Check if the port is open . Try using telnet ( e.g. ip/localhost) . e.g. telnet localhost 2222
  3. Check if the database has the granted privileges for your username and password. For checking privilege , use SHOW GRANTS [FOR user] . More info can be found here If none of this work, perhaps try using a different user other than the default one.
    Let know what happens
CyprUS
  • 4,159
  • 9
  • 48
  • 93
3

you are probably logging in with the same username/password from phpmyadmin successfully, but can't log in from php if the host is not localhost. I had the same problem. Go to mysql and grant permission for that user to log in from all hosts (%), or the specific host's IP you run your php code on.

Gavriel
  • 18,880
  • 12
  • 68
  • 105
1

Gavriel, you have saved me a world of hurt.

I was previously getting the mysql error message 'access denied for user...'

The solution for my remote access problem in php was to grant access to all hosts in cPanel '%'

Also, to figure out your mysql webhost and port, execute sql command 'show variables' on your database. variables 'hostname' and 'port' will tell you what you need to know for the mysql database connection.

Another thing to note is to ensure the user you have created is actually added to the database.

Byrne
  • 11
  • 1