No connection could be made because the target machine actively refused it.

So trying to get a connection from my game to the database on my server, and Unity complains with this error message:

SocketException: No connection could be made because the target machine actively refused it.

I thought at first maybe I gave it the wrong username and password, but that's not the case. Code:

import System;
import System.Data;
import System.Data.SqlClient;

var DBi : String;
var DBs: String;
var DBu: String;
var DBp: String;

function Start () {
    var dbcon : IDbConnection;
    var connectionString : String = "Server=****; Database=****; Uid=****; Pwd=****";
    dbcon = new SqlConnection(connectionString);

Anyone know what I've done wrong? Or if the problem is even on Unity's side?

Did you open the port / or allow Unity3D or allow your game to receive connections in Windows Firewall?

I did. 3306. But they're both on the same network, so that shouldn't matter.

Try disable the firewall.

Does it work?
If not -> Something wrong with the codes.

Firewall's already disabled. Also, is no one seeing the significance of the wording "actively refused it"? This means the server was connected to, but the server itself said no. It's not a failed connection in the normal sense, it's a disallowed connection. Besides, I was following the instructions in this thread almost verbatim...

1 Like

I'm seriously at the end of my line here... I honestly have no idea what to do, I've tried everything. I've tried two different MySQL clients on my iPhone, and they can both connect, and a friend of mine made a PHP connect script that worked.. I am at a complete loss..

If you accidentally build with WebPlayer settings, the player will fire a policy request which might fail.
Is the db address you enter available from the machine? You said you tested with iPhone and some PHP. It's unclear if the connection from the Unity machine will do. If it's in another local net, it might not connect while others do.

I've gotten a friend who's working on his own project to test the code. He has no luck either, and he's very decidedly outside of the network. It seems like everything BUT Unity is able to connect...

I've updated the code as well, so as to make it ready when it actually.. you know... WORKS...

import System;
import System.Data;
import System.Data.SqlClient;

static var DBi : String = "";
static var DBu: String = "";
static var DBp: String = "";

function Start () {
    QueryDB("", "");

static function QueryDB
(dbSchema : String, dbQueryString : String) {
    var dbConnection : IDbConnection;
    var dbConnectionString : String =
    "Server=" + DBi + ";" +
    "Database=" + dbSchema + ";" +
    "Uid=" + DBu + ";" +
    "Pwd=" + DBp;
    dbConnection = new SqlConnection(dbConnectionString);

You are confusing two things. MySQL and MSSQL.

The person who made the demo used MSSQL. You are using MySQL.

is that all it was?

Are you srs?

Are you bloody kidding me?

I've been chasing this for three days

excuse me while i go question every decision i've ever made.

Every. Decision.




1 Like


The uLink vendor is around, did you try uLink and connect to MySQL?

The Photon vendor is around, did you try Photon Server and connect to MySQL?

You also need to question why you need to have a MySQL client on the iPhone too.


  1. game access server software,
  2. the server software accesses the MySQL to update, delete, create records.


  1. PC game accesses MySQL client directly from remote clients
  2. Android game accesses MySQL client directly from remote clients
  3. iPhone game accesses MySQL client directly from remote clients

If you leave your MySQL client open, it can be very easily hacked or defaced.

The iPhone client was just to see if my database was even accessible or if the problem was with something I did, it has nothing to do with the project itself. Also, any code I posted was just making sure I could actually get in, I'll get it together once I figure out how to work with these libraries.

I'm not even going to bother looking for those for a few reasons:

1) I have no funds, and the typical trend here is that if any community-made plugins have a reputation (which these do), they're pretty expensive.
2) I'm already going mental from trying to figure out how to use the NORMAL Unity networking system
3) I'm sick of Unity in general, halfway regret buying the Pro license, and am considering just giving my current project the finger and going to a different engine, because I'm sick of the fact that I can get more features from engines that I don't even have to pay for.

I had the same problem and its because unity is blocking it. In order to connect sometimes you need to put this before connecting to the server.

  • void Connect()
  • {
  • //before connecting I need to tell unity to turn security off for the port im going to use
  • Security.PrefetchSocketPolicy(IP,PORT);
  • //now I can connect to the server with out any problems hopefully
  • ClientSocket.Connect(IP,PORT);
  • }

I am facing same problem, but the remedy u mentioned Security.PrefetchSocketPolicy(IP,PORT), has been obsolete now. any other workaround

The entire old webplayer is gone as of 5.40, so there's no way you're facing the same problem that iamthecoolguy11 worked around with the webplayer's Security class. Your issue must be due to a different problem.

I solved it this way:
In Server Configuration Manager under your server protocols section make sure TCP/IP is enabled. Right click on TCP/IP and go to properties and check for IPAII dynamic port number. You need to mention this port in the connection string. For more details refer this page