2

i have mysql database over cpanel, i want to access through my localhost page..

<?php
$con1 = mysql_connect("mywebsiteip","mysql_username","mysql_user_password");
if(!$con1)
{
    die ("Could not connect " . mysql_error());
}
else
{
    echo "Good connection";
}
mysql_close($con1);
?>

When i run it, it cannot connect to mysql database over cpanel. and i even tried up

$con1 = mysql_connect("mywebsiteip:portnumber","mysql_username","mysql_user_password");

Can any let me know, which one is good way. Below is the image or error enter image description here

5
  • What is the error message returned by mysql_error()? Commented Nov 18, 2011 at 12:08
  • Tried with passing "localhost" in place of website IP ? Commented Nov 18, 2011 at 12:09
  • @Alfred Yes!, i tried with localhost, its not working.. If i use localhost then it will take users localhost database system Commented Nov 18, 2011 at 12:14
  • @Bjoern Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known Warning: mysql_connect() [function.mysql-connect]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is kn (trying to connect via tcp://mywebsiteip:myportnumber) Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known Could not connect php_network_getaddresses: getaddrinfo failed: No such host is known and it only in the connection string line 2 error Commented Nov 18, 2011 at 12:16
  • In that case, don't localhost, but a proper ip address where your mysql server is accessable. Commented Nov 18, 2011 at 12:44

4 Answers 4

2

By default, cPanel-installed mysql databases only allow connections from the localhost (ie the server cpanel/mysql is installed on). To add other hosts, log in to cPanel and go to 'Remote Database Access Hosts' (this may be called something slightly different depending on version).

You can add your IP address, hostname or % to allow connections from any computer. However, specifying % is a big security risk, so use it with caution.

FYI, this question really belongs on webmasters.stackexchange.com, as it involves administration more than programming.

Sign up to request clarification or add additional context in comments.

1 Comment

if i add my ipaddress in there, can i able to access from other computers over internet.
1

You should give the correct connection code!

$con1 = mysql_connect("localhost:/tmp/mysql.sock","mysql_username","mysql_user_password");

I hope this should / must work for you. Enjoy!

Comments

1
Warning: mysql_connect() [function.mysql-connect]: 
php_network_getaddresses: getaddrinfo failed: No such host is known 

This error messages suggests that localhost can't be resolved, therefore the "client" (your php script) doesn't know where to send the connect request to.

Either check your DNS settings or use an IP adress instead, where your MySQL server is accessible.

2 Comments

where can we get the ip address of mysql server, is it same as cpanel Shared IP Address
usually your webspace provider gives you those details.
0

if your trying to connect remote ip

  1. Mysql server need to accept remote connection
  2. Mysql user need to have remote access previlage

if you dont have 1 of them it wont work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.