seanbrhn3
seanbrhn3

Reputation: 128

How to connect my unity project to mysql server on ec2 instance

I am trying to get data from my MySQL server to my Unity project. I tried to get the data like so:

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.
        } 
}

I have a php script on my server to handle the interaction because I am having trouble using the MySqlConnection library for my unity project. Here is my php script:

    $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();

But every time I run the code I get this error:There was an error getting aws: Failed to connect to ec2-174-129-82-141.compute-1.amazonaws.com port 80: Timed out

Thanks for looking at the code and please let me know if there is anything else I can do to help.

Upvotes: 1

Views: 651

Answers (1)

jesusnoseq
jesusnoseq

Reputation: 381

You have to add a rule to open the port 80 using security groups (Inbound tab)

How your rule should look:

How should look your rule

By default the port 80 is close in EC2 AWS instances.

And the URL is malformed in Unity Add http:// to your server URL In editor will work ok without http:// but it will fail in android.

Upvotes: 1

Related Questions