Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Connecting Unity to Mysql

Discussion in 'Getting Started' started by idameyer8, Mar 19, 2022.

  1. idameyer8

    idameyer8

    Joined:
    Feb 7, 2022
    Posts:
    16
    Is it possible to connect Unity to Mysql directly so that I can use SELECT statements from within Unity? I get the impression that this is possible but cannot find any tutorials showing this from start to finish. I have found this but haven't been able to make it work. Step one is to unzip a package but when i unzip I just get a .rar file and not the package (using a mac). So I didn't get further than that... Is this even possible or am I chasing a complete dead end? I have a working database that I am using with the browser-based version of my game already
     
    Last edited: Mar 20, 2022
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,003
    If you're publishing your applications, you shouldn't open up your database for the public, so you shouldn't connect your Unity app/game to the database directly. Write a backend application / PHP script / whatever, which responds only specific queries and connect that to your database.
    For local database, there are much more efficient database solutions than MySQL like (iBoxDb or SQLite, I prefer the first one, it's faster than the second).
     
  3. idameyer8

    idameyer8

    Joined:
    Feb 7, 2022
    Posts:
    16
    I thought it would be easier to just use the database I had... I've heard of Firebase and SQLite, but not iBoxDb. It seems like a really steep learning curve to be able to use these, what do you recommend for someone who is very new at backend coding?
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,003
    Again, if you're writing a backend layer, then use whatever you know and you can set up properly.
    If you want local database (calling from your game), then SQLite or iBoxDB or other non-relational database can be used, many has .NET and/or Unity API.
     
  5. idameyer8

    idameyer8

    Joined:
    Feb 7, 2022
    Posts:
    16
    Ok, if it is possible to use mysql I definitely want to stick with that. I have been working on this tutorial today and i am able to make changes to the database, but are there security risks in accessing the database like this?
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,003
    Again, yes. You're opening your database for everyone. It is highly risky. Write a backend layer application on your server as I described in my first post.
     
  7. idameyer8

    idameyer8

    Joined:
    Feb 7, 2022
    Posts:
    16
    Alright, so the tutorial I linked is not a safe approach? I am really new at this; I am unsure if what I have done by following the tutorial qualifies as having written a backend layer application on my server.

    This is one way I have used this:

    Code (CSharp):
    1. public void RetrievePlayers()
    2.     {
    3.         using(MySqlConnection connection = new MySqlConnection(stringBuilder.ConnectionString))
    4.         {
    5.             try
    6.             {
    7.                 connection.Open();
    8.                 MySqlCommand command = connection.CreateCommand();
    9.                 command.CommandText = "SELECT playername FROM highscores LIMIT 5";
    10.                 var reader = command.ExecuteReader();
    11.                 while (reader.Read())
    12.                 {
    13.                     var ordinal = reader.GetOrdinal("playername");
    14.                     string playername = reader.GetString(ordinal);
    15.                     Debug.Log(playername);
    16.                  
    17.                     GameObject newButton = Instantiate(horseButton) as GameObject;
    18.                     newButton.transform.SetParent(canvas.transform, false);
    19.                     string title = playername;
    20.                     newButton.GetComponentInChildren<Text>().text = title;
    21.                 }
    22.                 connection.Close();
    23.             }
    24.             catch(System.Exception ex)
    25.             {
    26.                 Debug.LogError("DBInterface: Could not retrieve the five! " + System.Environment.NewLine + ex.Message);
    27.             }
    28.         }
    29.     }
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Is this code working for you? Do you have a multiplayer game working already? Otherwise, you are likely getting ahead of yourself. And you should generally never connect directly to an online database, each user would open a separate connection. Unless you don't plan on becoming popular, then it may work. The MySQL docs mention a 151 concurrent user limit. Also, you invite SQL injection. As was mentioned, you probably want to look into a scalable REST web service API or similar that connects to your db with a single connection.