Redtama
Redtama

Reputation: 1613

How can I connect my Unity3D application to a MySQL database on Mac OSX

I've been reading a ton about the best way to get my Unity application to be able to retrieve data from a MySQL database. The consensus is that for security purposes, the database connection should be set up in PHP and then the Unity code should interact with the PHP.

So instead of:

Unity -> MySQL

We have:

Unity -> PHP -> MySQL.

That's great and everything, but I'm not really worried about security for my app. I'm working on a project for my final year at university; it's something that could be considered internal software and so the user of the application will also own the database. Therefore I have no concerns over security.

So all I want is to get this Unity -> MySQL connection working. I feel like I'm so close but have gotten stuck with some errors and don't know where to go from here.

My Database Connection Code

public class TweetDAO {

    private string connString;

    public TweetDAO()
    {
        connString = "Server=localhost; Port=3306; Database=tweets; Uid=root; Pwd=root;";
    }

    public string ReadString(string query)
    {
        MySqlConnection conn = new MySqlConnection (connString);
        string result = "";

        try {

            MySqlCommand command = conn.CreateCommand ();
            MySqlDataReader reader;
            command.CommandText = query;
            conn.Open();
            reader = command.ExecuteReader ();
            while (reader.Read())
            {
                result = reader.GetValue(0).ToString();
            }

        } 
        catch (MySqlException ex) {
            Debug.Log(ex.Message);
        }
        finally {
            conn.Dispose ();
            conn = null;
        }
        return result;
    }

}

When I run this I get SocketException: Connection refused and the exception message is Unable to connect to any of the specified MySQL hosts.

I thought that this must just be something wrong with my connection string but I've tried so many different combinations and nothing seems to work.

I would be very grateful for any help with this.

Thank you! :)

Upvotes: 0

Views: 1529

Answers (1)

Oscar Lundberg
Oscar Lundberg

Reputation: 433

As we concluded in the comments, the problem is that the port defined in the connection string differs from the port setup in your server client. (XAMPP, WAMP etc.)

The easiest way to quickly test problems like this is in my opinion to manually check it through the browser. While it won't solve anything, depending on what is shown in your browser you should be able to figure out what or where something went wrong.

Upvotes: 2

Related Questions