如何将我的Unity项目连接到ec2实例上的mysql服务器

时间:2017-12-03 00:17:50

标签: c# php mysql unity3d amazon-ec2

我正在尝试从我的MySQL服务器获取数据到我的Unity项目。我试着像这样得到数据:

public string awsurl = "ec2-174-129-82-141.compute-1.amazonaws.com/connect_to_server.php";
IEnumerator GetScores()
{
        print("get scores start");
        WWW aws_get = new WWW(awsurl);
        yield return aws_get;
        print("getscore here");
        if (aws_get.error != null)
        {
            print("There was an error getting aws: " + aws_get.error);
        }
        else
        {
            print(aws_get.text); // this is a GUIText that will display the scores in game.
        } 
}

我的服务器上有一个PHP脚本来处理交互,因为我在使用MySqlConnection库进行统一项目时遇到了麻烦。 这是我的PHP脚本:

    $address = "localhost"
$dbusername = "root";
$dbpassword = "root";
$db_name = "watshoes";
$db_conn = new mysqli($address, $dbusername, $dbpassword, $db_name);
    if(isset($_POST['username'])) $username = $_POST['username'];
    if(isset($_POST['user_id'])) $user_id = $_POST['user_id'];
    if(isset($password)) $password =$_POST['password'];

    $stmt = $db_conn->prepare("SELECT image FROM ImageText");
    // "s" means the database expects a string
    $stmt->bind_param("s", $user_id);
    if($stmt->execute())
    {
        /* bind result variables */
        $stmt->bind_result($image);
        /* fetch value */
        $stmt->fetch();
        echo $image;
    }
    else
    {
        echo "query failed";
    }
    $stmt->close();
    $db_conn->close();

但是每次运行代码时都会收到此错误:There was an error getting aws: Failed to connect to ec2-174-129-82-141.compute-1.amazonaws.com port 80: Timed out

感谢您查看代码,如果我还有其他任何可以提供的帮助,请与我们联系。

1 个答案:

答案 0 :(得分:1)

您必须使用安全组(入站标签)

添加规则以打开端口80

您的规则应如何显示:

How should look your rule

默认情况下,端口80在EC2 AWS实例中关闭。

在Unity中,网址格式错误 将http://添加到您的服务器URL 在编辑器中可以正常工作而不需要http://但它会在android中失败。