ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
332 stars 133 forks source link

MySQl deoesn't seem to close the connection properly #226

Open CoenBlijker opened 2 months ago

CoenBlijker commented 2 months ago

Dear Mr Bell,

I am running several ESP32 clients against a MariaDB server running on my Synology NAS.

The MariaDB log file is filling up with messages like:

2024-04-21` 14:32:36 170 [Warning] Aborted connection 170 to db: 'unconnected' user: 'coen' host: '192.168.178.4' (Got an error reading communication packets)

2024-04-21 14:33:18 172 [Warning] Aborted connection 172 to db: 'unconnected' user: 'coen' host: '192.168.178.181' (Got an error reading communication packets)

The code running on all these ESP32 is basically the same and comes down to:

WiFiClient` mySQLwifiClient;
MySQL_Connection conn((Client *)&mySQLwifiClient);
MySQL_Cursor cur = MySQL_Cursor(&conn);

bool connectToMySQL()
{
    if (!conn.connected())
    {
        if (!conn.connect(server_addr, 3306, mySQLuser, mySQLpassword))
        {
            logTimedConstMessage("MySQL connect failed..\n");

            return false;
        }
        logTimedConstMessage("MySQL connect succeeded..\n");
    }
    return true;
}

void execQuery(const char *query)
{
    if (!connectToMySQL())
    {
        return;
    }
    // Execute the query
    if (!cur.execute(query, false))
    {
        logTimedMessage("*** Exec query: '%s' failed..\n", query);
    }

    // Note: since there are no results, we do not need to read any data
    cur.close();
    conn.close();
}

After some 'research' on Google the only (possibly) relevant answer I could find is that the client doesn't seems to close the connection properly before exiting (does not call mysql_close()).

Looking at your code I don't see any code that implements some form of formal disconnect from the DB. The conn.close() call just closes the connection.

Am I missing something? Or does MariaDB behave differently than MySQL?

Any help on this issue would be greatly appropriated!

With kind regards,

Coen

ChuckBell commented 1 month ago

Hello,

Sorry for the delay. Yes, MySQL (from Oracle) is not the same (not 100% anyway) in how the client connections work. So, I am not surprised there are differences.

However, I have not encountered this problem before. That said, you should check to ensure max_connections and similar variables have not been changed. Otherwise, I can investigate how to terminate the connection on close. Keep in mind the connector was designed for continued use and lightweight hence the absence of some of the baggage featiures.

If you have a suggestion on how to add such code, feel free to suggest something.

Dr. Bell

On Apr 21, 2024, at 9:37 AM, CoenBlijker @.***> wrote:

Dear Mr Bell,

I am running several ESP32 clients against a MariaDB server running on my Synology NAS.

The MariaDB log file is filling up with messages like:

2024-04-21` 14:32:36 170 [Warning] Aborted connection 170 to db: 'unconnected' user: 'coen' host: '192.168.178.4' (Got an error reading communication packets)

2024-04-21 14:33:18 172 [Warning] Aborted connection 172 to db: 'unconnected' user: 'coen' host: '192.168.178.181' (Got an error reading communication packets)

The code running on all these ESP32 is basically the same and comes down to:

WiFiClient` mySQLwifiClient; MySQL_Connection conn((Client *)&mySQLwifiClient); MySQL_Cursor cur = MySQL_Cursor(&conn);

bool connectToMySQL() { if (!conn.connected()) { if (!conn.connect(server_addr, 3306, mySQLuser, mySQLpassword)) { logTimedConstMessage("MySQL connect failed..\n");

        return false;
    }
    logTimedConstMessage("MySQL connect succeeded..\n");
}
return true;

}

void execQuery(const char *query) { if (!connectToMySQL()) { return; } // Execute the query if (!cur.execute(query, false)) { logTimedMessage("*** Exec query: '%s' failed..\n", query); }

// Note: since there are no results, we do not need to read any data
cur.close();
conn.close();

} After some 'research' on Google the only (possibly) relevant answer I could find is that the client doesn't seems to close the connection properly before exiting (does not call mysql_close()).

Looking at your code I don't see any code that implements some form of formal disconnect from the DB. The conn.close() call just closes the connection.

Am I missing something? Or does MariaDB behave differently than MySQL?

Any help on this issue would be greatly appropriated!

With kind regards,

Coen

— Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/226, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6SHYEVHCAI6SNPN3AZI6LY6O6LPAVCNFSM6AAAAABGRMRMLGVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI2TKMBYGU4DCNA. You are receiving this because you are subscribed to this thread.