duanping1632 2017-12-09 22:57
浏览 76

AWS LAMP使用PHP问题与MySQL数据库的堆栈连接

I have been at this for days trying to get this to work. I just followed the AWS tutorial and installed a LAMP stack on an EC2 instance of AMAZON LINUX. I have been trying to just do basic connections to it. Just easy Insert statements and I continually get this error:

The server is working.. i have a test file that works.

My database is set up and ready. It will receive an id, a username, a password, and an email address. But I cannot connect to mysql. I have: 1) set up a user and granted all privelages 2) checked the my.cnf file. There is no "bind-address" listed to change, but I added one in just to check. I added my server IP. Still did not work. 3) Security groups are wide open. I have SSH, HTTP, HTTPS, and MySQL accepting inbound connects from "Anywhere".

Here is the simple task I'm trying to accomplish:

<?php

$servername = "xx.xx.xxx.xx;
$username = "ec2-user";
$password = "112233445566";
$dbname = "db";


$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO MyGuests (id, firstname, lastname, email) VALUES(1, Fake, User, fake3mail@gmail.com);

if ($conn->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

Task I'm trying to accomplish: connect to MySQL database and insert a row of data.

Actual result: Error and the inability to connect.

I have scoured the internet for DAYS now trying to get this to work. What the heck am I missing here?! Are there even more configurations that must be done? I have tried every single thing that I've read and it does not work .

  • 写回答

2条回答 默认 最新

  • dongzou3751 2017-12-09 23:18
    关注

    When I go to this page, it is working so I will assume that you fixed that issue: ec2-54-174-71-33.compute-1.amazonaws.com.

    For your database, change the $servername to "localhost". There is no need to do a DNS lookup to resolve your own system.

    If you are having problems with your PHP code (you do from the last comment), look at your PHP.INI file. In this file will be the location for your error logs. Look for the line "error_log = " This file will store the errors for your PHP programs (scripts). Just look at the bottom of the file for the latest error messages. If error_log is not configured, then configure it.

    One problem that I do see with your code is that you are not put single quotes around your strings. Your values should look like this: VALUES(1, 'Fake;', 'User', 'fake3mail@gmail.com')

    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计