tags:

views:

239

answers:

4

I get this everytime I use mysql_connect() no matter what database I choose:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'IP' (111) in filename.php on line 17 A MySQL error has occurred: Can't connect to MySQL server on 'IP' (111)

The exact same file works on my personal website fine. I have tried multiple databases hosted on different servers and it always gives that output.

The database itself is hosted on the same server, but using its full IP in mysql_connect(). Using localhost:port doesn't work either as it says:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in filename.php on line 17 A MySQL error has occurred: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

But using the IP should work as it has worked calling it via the same file hosted on other servers.

This is the code:

    $connect = mysql_connect($db_url,$db_user,$db_pass); // connects
    if ($connect == false) exit("A MySQL error has occurred: " . mysql_error());

Now since the file works on other servers i am guessing it is something to do with the server it is on and might need something changed. I don't personally have root access to the server (just my part of the shared host). Is there anything I can do i php, editing the php.ini file or something I should pass on to someone with root access?

Edit: Ok it turns out that the server doesn't have access to outside databases, so thats why the IP didn't work. Thanks for all your answers but we have decided simply to change hosting provider. We need to be able to access an outside database.

+1  A: 

This is on a hosting service? Check their documentation, there will be something that tells you where to find mysql. It isn't necessarily localhost.

For example, on startlogic.com, you use: yourdomain.startlogicmysql.com

DigitalRoss
Its not really a hosting service so they don't have documentation, more like an aggrement to use a part of someone's server. localhost doesn't work but I can connect to the database from another server using its IP directly. It doesn't have to be localhost. I just tested that because it couldn't connect putting in the IP.
Alasdair Morrison
A: 

Something else to try: Use '127.0.0.1' instead of 'localhost'. I have had issues before where mysql stupidly assumed it could silently change 'localhost:' to '/var/run/mysqld/mysqld.sock'.

too much php
That was one of the things I found online to try and help but sadly the same old: Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '127.0.0.1' (13) in filename.php on line 17A MySQL error has occurred: Can't connect to MySQL server on '127.0.0.1' (13)
Alasdair Morrison
+1  A: 

Can you connect using mysqladmin using the same host, username and password?

mysqladmin -h $db_url -u $db_user -p $db_pass

Replace $db_xxxx with real values. If that works from the same host as your php script, then sudo to the apache User and try the same test. One of those must be failing.

EDIT: nevermind on sudo part, I noticed that you don't have root access.

Scott Lundberg
Unfortunetly I don't have access to the server directly to do that. I am just hosted by them. But I am going to pass that on when I can.
Alasdair Morrison
A: 

Your wording is not very clear, I hope you are not thinking you can connect to the same mysql server from any old web server just because you know the IP address and port number. If the web host is at all competent, they have probably firewalled mysql so it is only accessible through their own web servers.

too much php
I know I'm sorry about the wording, I have spent a while trying to think of how to word it better :/I am aware of that but this mysql database is deliberetly opened up so that it can be used by another server. This is why it is possible for my to upload the same file to another web host and connect to the same database. So I can access the database from an outside source fine, just not with php on the same host.
Alasdair Morrison