简体   繁体   English

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

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

actually im having list of medicine in a file and i want to insert it on ec2 amazon server database which is mysql.I run this file on localhost.when im trying my code, which is in php it show an error as follows:im not getting. 实际上我在文件中有医学列表,我想将其插入到mysql的ec2亚马逊服务器数据库中。我在localhost上运行此文件。当我尝试我的代码(在php中)时显示错误如下:im not得到。

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com' (10060) in C:\\websites\\medicine\\filehandling.php on line 17 Can't connect to MySQL This is a servername:ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com here is my code... and let me know where im wrong. 警告: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;
?>

There are many possible reasons why you are not able to connect to the server. 有许多可能的原因导致您无法连接到服务器。

  1. Firewall (iptables) settings on the server prevent external access 服务器上的防火墙(iptables)设置阻止外部访问
  2. The MySQL server only listens for local connections MySQL服务器仅侦听本地连接
  3. There is no MySQL remote user created on the server 服务器上没有创建MySQL远程用户
  4. Your $hostname should be 'ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com'; 您的$hostname应该是'ec2-122-248-220-105.ap-southeast-1.compute.amazonaws.com';

There might be other reasons as well. 可能还有其他原因。

Also, it is generally a bad idea to connect to databases over the internet for reasons varying from security to latency. 同样,由于从安全性到延迟的各种原因,通过Internet连接到数据库通常是一个坏主意。

Good luck! 祝好运!

I'm getting 我越来越

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

What about to try change your "hosname" to IP adress? 怎样尝试将您的“姓氏”更改为IP地址? Would it help ? 这有帮助吗 ?

When I'm trying to ping it, it's unreachable 当我尝试ping时,无法访问

If you are using a BitNami AMI you will not be able to connect to the MySQL database remotely in a default configuration. 如果您使用的是BitNami AMI ,则将无法以默认配置远程连接到MySQL数据库。 For security reasons, we don't enable it (I'm a BitNami developer). 出于安全原因,我们不启用它(我是BitNami开发人员)。 In any case if you want to allow remote connections for the database 'fortis' I would recommend to create a different user that will only have access to that database instead of using the user 'root'. 无论如何,如果您想允许数据库“ fortis”的远程连接,我建议创建一个其他用户,该用户只能访问该数据库,而不能使用“ root”用户。 That would be a bit more secure although as Sukumar mentions is better to avoid to give access to MySQL in public machines. 尽管如Sukumar所言,最好避免在公共计算机中访问MySQL,但这会更加安全。

To give remote access to a different user you will need to connect (with putty) to your amazon machine and execute the following commands. 要为其他用户提供远程访问权限,您将需要(使用腻子)连接到亚马逊计算机并执行以下命令。 I suppose that you are using BitNami (because of your password). 我想您正在使用BitNami(因为您的密码)。

. /opt/bitnami/scripts/setenv.sh . /opt/bitnami/scripts/setenv.sh (notice that space between the dot and the command, this will load the BitNami environment). . /opt/bitnami/scripts/setenv.sh (注意点和命令之间的空格,这将加载BitNami环境)。

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

The above will create the fortis_user for the database with access from localhost. 上面将为具有本地主机访问权限的数据库创建fortis_user。

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

This above will give access to the fortis_user for the database from any other ip. 上面的代码将允许从任何其他ip访问数据库的fortis_user。

Now you should be able to connect with: 现在您应该可以连接:

$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