Skip to content

MySQL_Connect

imshooter edited this page Oct 24, 2026 · 1 revision

Connect to a database.

The connection is made before the native returns. When it fails, the handle is still given and every query tries to connect again, half a second after the first failure and twice as long after each next one, up to five seconds. options takes settings as key=value separated by semicolons:

  • charset, latin1 by default, which is the code page of the game.
  • multi_statements, false by default, and pool, the connections for parallel queries, 0 by default.
  • connect_timeout, read_timeout and write_timeout, in seconds. A query whose answer takes longer than read_timeout fails, and the connection opens again.
  • slow_query, the milliseconds from which a query is logged, and log_errors, true by default.
  • max_query_length in bytes, max_pending, the queries waiting or running, and max_queries_per_second. A query past one of them is refused: it is not sent, the native returns false, and a line is logged, at most one a second. 0, the default, sets no limit.
  • ssl to require TLS. The connection then never runs without it, and fails when the server answers without TLS. ssl_verify also verifies the certificate of the server and its name, against the certificate authorities of ssl_ca, a file, or ssl_capath, a folder, and the revocation lists of ssl_crl or ssl_crlpath. On localhost, 127.0.0.1 and ::1 the name is not checked, and the authority only when ssl_ca or ssl_capath is given. ssl_fingerprint requires the certificate of the server to have that SHA1 to SHA512 fingerprint, in hexadecimal with or without colons, which suits a certificate signed by no authority. ssl_cert and ssl_key give the certificate of the client, ssl_cipher the ciphers allowed and tls_version the versions, as TLSv1.2,TLSv1.3. Any of these settings requires TLS too. Without them the connection does not use TLS, as a database on the same machine needs none.
native MySQL:MySQL_Connect(
    const host[],
    const user[],
    const password[],
    const database[],
    port = 3306,
    const options[] = ""
);

Parameters

Parameter Description Default
const host[] Name or address of the server.
const user[] Account to log in with.
const password[] Password of the account.
const database[] Database the queries use.
port Port of the server. 3306
const options[] Settings of the connection. ""

Returns

  • The handle.
  • INVALID_MYSQL_ID when an option is not valid.

Remarks

The first connection is opened inside the native, so the handle can run queries at once. A handle that could not connect still works. Every query tries to connect again, half a second after the first failure and twice as long after each next one, up to five seconds, and fails with the error of the connection until it connects. See Configuration for every option.

The handle is closed when the script that made it unloads.

Example

new MySQL:gDatabase;

public OnGameModeInit() {
    gDatabase = MySQL_Connect("127.0.0.1", "game", "secret", "game", 3306, "pool=2;slow_query=50");

    if (!MySQL_IsConnected(gDatabase)) {
        print("The database is not there yet. Queries will connect when it is.");
    }

    return 1;
}

Related

Clone this wiki locally