繁体   English   中英

在php中读取文件并插入ec2亚马逊服务器mysql数据库

[英]read file in php and insert into ec2 amazon server mysql database

实际上我在文件中有医学列表,我想将其插入到mysql的ec2亚马逊服务器数据库中。我在localhost上运行此文件。当我尝试我的代码(在php中)时显示错误如下:im not得到。

警告:mysql_connect()[function.mysql-connect]:无法连接到C中“ ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com”(10060)上的MySQL服务器:第17行的\\ websites \\ medicine \\ filehandling.php无法连接到MySQL这是服务器名称:ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com,这是我的代码...让我知道我哪里错了。

<?php

$hostname='http://ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com';
//$port='3306';
$user='root';//// specify username
$pass='bitnami'; //// specify password
$dbase='fortis'; //// specify database name
set_time_limit(1000);
$connection = mysql_connect("$hostname" , "$user" , "$pass" ,"$port") 
or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
$file = fopen("med4","r") or exit ("File not found");
$c =0 ;
      while (!feof($file)){
            ++$c;
        $val = fgets($file);
        mysql_query("insert into patientinfo_medicines (medicine) values ('$val')");    
      }
fclose($file);
echo $c;
?>

有许多可能的原因导致您无法连接到服务器。

  1. 服务器上的防火墙(iptables)设置阻止外部访问
  2. MySQL服务器仅侦听本地连接
  3. 服务器上没有创建MySQL远程用户
  4. 您的$hostname应该是'ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com';

可能还有其他原因。

同样,由于从安全性到延迟的各种原因,通过Internet连接到数据库通常是一个坏主意。

祝好运!

我越来越

ERROR 2005 (HY000): Unknown MySQL server host 'http://ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com/' (1)

怎样尝试将您的“姓氏”更改为IP地址? 这有帮助吗 ?

当我尝试ping时,无法访问

如果您使用的是BitNami AMI ,则将无法以默认配置远程连接到MySQL数据库。 出于安全原因,我们不启用它(我是BitNami开发人员)。 无论如何,如果您想允许数据库“ fortis”的远程连接,我建议创建一个其他用户,该用户只能访问该数据库,而不能使用“ root”用户。 尽管如Sukumar所言,最好避免在公共计算机中访问MySQL,但这会更加安全。

要为其他用户提供远程访问权限,您将需要(使用腻子)连接到亚马逊计算机并执行以下命令。 我想您正在使用BitNami(因为您的密码)。

. /opt/bitnami/scripts/setenv.sh . /opt/bitnami/scripts/setenv.sh (注意点和命令之间的空格,这将加载BitNami环境)。

mysql -u root -p -e "grant all privileges on fortis.* to
'fortis_user'@'localhost' identified by 'fortis_password'"  

上面将为具有本地主机访问权限的数据库创建fortis_user。

mysql -u root -p -e "grant all privileges on fortis.* to
'fortis_user'@'%' identified by 'fortis_password'"

上面的代码将允许从任何其他ip访问数据库的fortis_user。

现在您应该可以连接:

$hostname='ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com';
//$port='3306';
$user='fortis_user';//// specify username
$pass='fortis_password'; //// specify password
$dbase='fortis'; //// specify database name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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