当前位置: 动力学知识库 > 问答 > 编程问答 >

Could not connect to amazon my sql rds using php

问题描述:

I am connecting amazon rds to make web services using php but it does not connect to the servers here is my code

<?php

try{

$dbhost = 'myrds.cjahuqpumuov.us-east-1.rds.amazonaws.com:3306';

$username = 'myname';

$password = 'mypassword';

$dbname = 'mydb';

$link = mysql_connect($dbhost, $username, $password, $dbname);

mysql_select_db($dbname);

}

catch(Exception e){

echo e

}

?>

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in /home/content/i/h/u/ihus235/html/cs/pah_brd_v1/productivo/selectTest.php on line 17

Error Trace

 Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 110 in /home/content/i/h/u/ihus235/html/cs/pah_brd_v1/productivo/selectTest.php on line 12

Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/i/h/u/ihus235/html/cs/pah_brd_v1/productivo/selectTest.php on line 13

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/content/i/h/u/ihus235/html/cs/pah_brd_v1/productivo/selectTest.php on line 13

网友答案:

I think the host name with port number is only not allowing you to connect.. Try by removing port number like :

$dbhost = 'myrds.cjahuqpumuov.us-east-1.rds.amazonaws.com'; 

I also faced the same problem few days before.

网友答案:
<?php

try{
$dbhost = 'myrds.cjahuqpumuov.us-east-1.rds.amazonaws.com';
$username = 'myname';
$password = 'mypassword';
$dbname = 'mydb';

$link = mysql_connect($dbhost, $username, $password, $dbname, 3306);
mysql_select_db($dbname);    
}
catch(Exception $e){    
echo 'Caught exception: ',  $e->getMessage(), "\n";    
}
?>
网友答案:
dbhost = "hostname:3306";     
$link = mysql_connect($dbhost, $username, $password);

try this. as you can not pass 5 parameter in mysql_connect and it, we have to pass port no with host on Ec2

网友答案:

I had a similar problem. For me the problem was resolved by changing selinux flags

http://serverfault.com/questions/240015/how-do-i-allow-mysql-connections-through-selinux

I hope this is helpful for you too

分享给朋友:
您可能感兴趣的文章:
随机阅读: