简体   繁体   中英

mysql database cannot connect with cpanel

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 在此处输入图片说明

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.

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.

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM