We help IT Professionals succeed at work.

PHP script can't connect to MySQL server

Eqbal
Eqbal asked
on
I configured my server and allowed my IP to connect remotely.

I entered IP of the server, username and password of mysql user in SQLyog (a GUI for managing mysql server). It connects successfully to database and I can manage everything.

I also have a PHP script to connect to the database. the code is something like this:
 mysql_connect('xx.xx.xx.xx', 'mysql_user', 'pass') or dir(mysql_error());

Open in new window


It displays this error:
Access denied for user 'mysql_user'@'yy.yy.yy.yy' (using password: YES) in /home/.../mysql.inc.php  on line 9

Open in new window


Why Sqlyog can connect and PHP script can't?
I think it's because 'yy.yy.yy.yy' in username. but I can't force PHP not to use it.
Comment
Watch Question

Hi,

The username or password is not correct.

Please double check again.

check in the command line:

mysql -u username -p password

Author

Commented:
username/password in sqlyog and script is same. sqlyog works. php doesn't work.
You need to set up a new privilege for mysql_user along these lines

GRANT SELECT , INSERT , UPDATE , DELETE , FILE , SUPER
ON * . *
TO 'mysql_user'@'yy.yy.yy.yy' IDENTIFIED BY PASSWORD '******'  

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
If you're uploading the script to your server, then you don't need to connect to it using an IP Address. You use LOCALHOST. The script can access the server as localhost when it's on the same server.



mysql_connect('localhost', 'mysql_user', 'pass') or dir(mysql_error());

Open in new window

Avinash ZalaWeb Expert

Commented:
Please double check your database password, user and also the privilege assigned to the user for the DB you are connection.

Hope this helps
Addy

Author

Commented:
@bportlock

Thank you for response. I run these commands:

GRANT ALL PRIVILEGES ON *.* TO 'mysql_user'@'yy.yy.yy.yy' IDENTIFIED BY '********';FLUSH PRIVILEGES;
The first Command displays: Query OK, 0 rows affected (0.00 sec)

Now, The script can connect very good. but at first query it displays that error again:

Access denied for user 'mysql_user'@'yy.yy.yy.yy' (using  password: YES)

The script works fine with local mysql server.

Can you help me?

Author

Commented:
I raised point value.
Avinash ZalaWeb Expert

Commented:
can u please post the code you are using?

Thanks
Addy
Shinesh PremrajanEngineering Manager

Commented:
You need to have root privilege to grant access. at present its not there

Author

Commented:
@AddyWatson:
The script is so big. Which section do you want to see?

@shinuq:
Yes. I used "mysql -uroot -p".
Commented:
Hi Eqbal,
You mentioned that you are able to connect locally. So that implies you are facing problem when you are trying your website after you have hosted it. Am I correct? If so, then you need to check the correct username and password supplied. You can do this via the Database manager section of your web host. Usually all web hosts provide you the ability to create database, assign a username and password via GUI. Using the GUI will simplify your process a lot and you will reduce the chances of encountering any errors in the process.

So once you login into your website hosting account, look for a link that says: Manage databases or Database management, or something similar. Then see what username and password you have supplied. Even a space in the password can lead to failure in authorization so you need to be extra careful. I suggest that you check out these values via GUI and enter the same in your PHP script and try again.

Also the same GUI will give you the ability to view and change permissions for this user. So you can see what kind of permissions are assigned to the user that you are using in the script and modify accordingly.

Hope that helps.
Avinash ZalaWeb Expert

Commented:
mainly DB Connection portion.

Thanks
Addy