Jump to content

AsyncSQL m_stHost and MYSQL_OPT_RECONNECT deprecation


Recommended Posts

  • Contributor
Posted (edited)

I just updated my server to be compatible with MySQL80. I have two question:

1. in AsyncSQL.cpp CAsyncSQL::Connect function the m_stHost value is lost after mysql_init.

	fprintf(stdout, "before m_stHost %s\n", m_stHost.c_str());
	if (0 == mysql_init(&m_hDB))
	{
		fprintf(stderr, "mysql_init failed\n");
		return false;
	}
	fprintf(stdout, "after m_stHost %s\n", m_stHost.c_str());

Output:

before m_stHost 127.0.0.1
after m_stHost

Why is it happening? I can't see any relation to m_stHost. There's surely something I'm not seeing here. The other variable like m_stUser doesn't affected.

It doesn't cause any problem because later the mysql_real_connect function convert the empty host value to "localhost", so it works except the console print at the function end will display

"AsyncSQL: connected to  "

(I know i can do something like copy m_stHost to m_stHost2 and display that in the change "AsyncSQL: connected to %s" but I really want to know why it's happening..)

My whole AsyncSQL.cpp if needed: https://pastebin.com/unwHifXi

--------------------------------------------------------------------

2. MYSQL_OPT_RECONNECT is deprecated. "WARNING: MYSQL_OPT_RECONNECT is deprecated and will be removed in a future version."
Has anyone dealt with this yet? It's just a warning as of now but my guess is they'll remove it pretty soon since it related to some security issue.

Edited by TMP4
  • Love 1
Link to comment
Share on other sites

  • Contributor

Mysql includes should be fine.

LIBS += /usr/local/lib/mysql/libmysqlclient.a /usr/lib/libz.a
INCDIR += -I/usr/local/include

Some line from Depend about MySQL:

.obj/NetBase.o: PeerBase.h DBManager.h /usr/local/include/mysql/mysql.h
.obj/NetBase.o: /usr/include/stdbool.h /usr/include/stddef.h
.obj/NetBase.o: /usr/include/stdint.h /usr/include/machine/_stdint.h
.obj/NetBase.o: /usr/include/x86/_stdint.h
.obj/NetBase.o: /usr/local/include/mysql/field_types.h
.obj/NetBase.o: /usr/local/include/mysql/my_list.h
.obj/NetBase.o: /usr/local/include/mysql/mysql_com.h
.obj/NetBase.o: /usr/local/include/mysql/my_command.h
.obj/NetBase.o: /usr/local/include/mysql/my_compress.h
.obj/NetBase.o: /usr/local/include/mysql/mysql/udf_registration_types.h
.obj/NetBase.o: /usr/local/include/mysql/mysql/client_plugin.h
.obj/NetBase.o: /usr/local/include/mysql/mysql/plugin_auth_common.h
.obj/NetBase.o: /usr/local/include/mysql/mysql_version.h
.obj/NetBase.o: /usr/local/include/mysql/mysql_time.h

Fresh BSD, only MySQL80 was installed.

 

Link to comment
Share on other sites

Straight from ChatGPT:

Quote

 In MySQL 8, the MYSQL_OPT_RECONNECT option is deprecated, and instead, you should use MYSQL_OPT_SSL_VERIFY_SERVER_CERT. This option enables or disables SSL server certificate verification. When enabled, it verifies the server's Common Name value in the certificate against the hostname used when connecting. This helps prevent man-in-the-middle attacks. When disabled, it does not verify the server certificate.

Here's how you can use it in C++:

#include <mysql/mysql.h>

// Example function to set MYSQL_OPT_SSL_VERIFY_SERVER_CERT option
void setSslVerifyServerCert(MYSQL *mysql, bool verify) {
    my_bool value = verify ? 1 : 0;
    mysql_options(mysql, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &value);
}

int main() {
    MYSQL *mysql = mysql_init(NULL);

    // Connect to MySQL server
    if (mysql_real_connect(mysql, "host", "user", "password", "database", 0, NULL, 0)) {
        // Set SSL server certificate verification
        setSslVerifyServerCert(mysql, true); // Set to true or false based on your requirement

        // Your code here...

        // Close connection
        mysql_close(mysql);
    } else {
        fprintf(stderr, "Error connecting to MySQL: %s\n", mysql_error(mysql));
    }

    return 0;
}

Replace "host", "user", "password", and "database" with your actual MySQL server details. This code demonstrates setting the MYSQL_OPT_SSL_VERIFY_SERVER_CERT option to either true or false based on your requirement.

Remember to link against the MySQL client library when compiling your C++ program.

from MySQL dev pages

https://dev.mysql.com/doc/c-api/8.0/en/c-api-auto-reconnect.html

Quote

Some client programs might provide the capability of controlling automatic reconnection. For example, mysql reconnects by default, but the --skip-reconnect option can be used to suppress this behavior.

 

Link to comment
Share on other sites

  • Contributor
Posted (edited)
On 5/1/2024 at 7:28 PM, A Man Has No Name said:

Straight from ChatGPT:

from MySQL dev pages

https://dev.mysql.com/doc/c-api/8.0/en/c-api-auto-reconnect.html

 

Did you read what ChatGPT said to you? It talked about a completly different thing. Like asking about a paint of a car then the person starts talking about the motor of the car 🤣

----------------

However my 1. question is still a mystery for me.
Anyone using MySQL8.0.35 can check it for me please if they have the host? It's enough to check it when starting the db and see "AsyncSQL: connected to  " if there is an ip or empty string.

Edited by TMP4
  • kekw 1
Link to comment
Share on other sites

  • Contributor
Posted (edited)
8 hours ago, Exynox said:

I also encountered the MYSQL_OPT_RECONNECT deprecation issue, pretty much the only options would be to either implement the reconnection code yourself, or migrate to using the MariaDB connector instead of the MySQL one, as it doesn't seem to be deprecated over there.

Yes that's what I did too in the meantime. But I have a feeling it'll be deprecated too in MariaDB soon.

Edited by TMP4
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.