Note
MySQL is experimental and already runs in production. Its natives may still change from one version to the next. It was made with Claude Opus 5.5, a model by Anthropic.
A MySQL and MariaDB plugin for open.mp and SA-MP. Its natives keep the names SA-MP scripts have long used for MySQL, as mysql_tquery and cache_get_value_name_int, so a script that uses them needs no change, and they come again with open.mp names, as MySQL_ThreadedQuery. It adds prepared statements sent in bulk, transactions, JSON cells, passwords hashed with Argon2id or bcrypt, and less work on the game thread.
- The usual natives, with their names, signatures and behaviour:
mysql_tquery,mysql_pquery,mysql_query, the cache, the ORM,mysql_format, options andmysql.ini. - open.mp names in full words, as
MySQL_ThreadedQuery,MySQL_ExecuteStatementandCache_GetValueNameInt, in the same include. Both names call the same native. - One binary that loads as an open.mp component or as a legacy plugin, on open.mp and on SA-MP 0.3.7.
- Queries formatted by the plugin with no length limit, as
mysql_tquery_format, the values of the public first and those of the query after. A query with no values is sent as it is, soMySQL_QueryandMySQL_SendQuerytake either. - Queries built in pieces, of any length, joined into one with
%Q. - Prepared statements with the same specifiers as queries. A batch gathers rows and sends them to MariaDB as arrays in one command, split to fit
max_allowed_packet. - Transactions of queries and statements that apply all together or not at all, run in order on one connection.
- JSON cells read by key or JSON Pointer, parsed once when first read. Text in the code page of the game is converted to and from UTF-8.
- Passwords hashed with Argon2id or bcrypt and verified on up to four threads of the plugin.
- Limits on the length of a query, the queries waiting and the queries per second, and timeouts for connecting, reading and writing.
- TLS that never falls back to plain text once asked for, with the certificate checked against an authority or a fingerprint.
- Settings in a file, with
${NAME}taken from the environment, so a password stays out of it.
- Download the latest release from Releases, the
windows-x86or thelinux-x86zip. - On open.mp, place
mysql.dllormysql.soin thecomponentsfolder. On SA-MP, place it inpluginsand add it to thepluginsline ofserver.cfg. - Place
mysql.incin the include folder of the compiler, and include it with#include <mysql>.
Tested with open.mp 1.6.7 and SA-MP 0.3.7-R2, with MariaDB 10.4 on Windows and MariaDB 10.11 on Ubuntu 20.04, Debian 12 and Ubuntu 24.04. The Linux build needs glibc 2.28 or later, and OpenSSL 3.0 is linked into it, so it needs no other library.
Scripts that use the usual natives compile and run with #include <mysql>. What may differ:
POOL_SIZEis4by default, and connections of the pool open when they first have work.%fwrites a float with as many digits as it needs, and%.4fwith four decimals.ERROR_CALLBACK, an option of the connection orerror_callbackinmysql.ini, also calls the public of a failed query, afterOnQueryError, withmysql_errnoandcache_get_errnogiving its error.mysql_tquery_fileandmysql_query_filesplit the file at semicolons outside quotes and comments, and stop at the first query that fails.
#include <open.mp>
#include <mysql>
new MySQL:g_SQL;
forward OnScoreRead(playerid);
public OnGameModeInit() {
g_SQL = mysql_connect_file();
return 1;
}
public OnPlayerConnect(playerid) {
new
name[MAX_PLAYER_NAME + 1]
;
GetPlayerName(playerid, name, sizeof (name));
mysql_tquery_format(g_SQL, "SELECT score FROM players WHERE name = %q", "OnScoreRead", "i", playerid, name);
return 1;
}
public OnScoreRead(playerid) {
new
score
;
if (cache_get_value_name_int(0, "score", score)) {
SetPlayerScore(playerid, score);
}
return 1;
}mysql.ini, in scriptfiles or in the folder of the server:
hostname = 127.0.0.1
username = game
password = ${MYSQL_PASSWORD}
database = gameTwo examples of accounts register and log players in with Argon2id hashes, load them with prepared statements, record each log in with a transaction and save everyone in one batch:
examples/nativesuses the snake_case names and plain callbacks, with nothing but open.mp.examples/inlineuses the open.mp names and the inlines of SSI, witherror_callbackon.
new
MySQL:gDatabase,
Statement:gSaveScore,
gPlayerId[MAX_PLAYERS]
;
stock PrepareStatements() {
gSaveScore = MySQL_PrepareStatement(gDatabase, "UPDATE players SET score = %i WHERE id = %i");
}
stock SaveScores() {
new const
Batch:batch = MySQL_CreateBatch(gSaveScore)
;
for (new i; i != MAX_PLAYERS; ++i) {
if (IsPlayerConnected(i)) {
MySQL_AddBatchRow(batch, GetPlayerScore(i), gPlayerId[i]);
}
}
MySQL_ExecuteBatch(batch);
}
stock BuyItem(playerid, item, price) {
new const
Transaction:transaction = MySQL_CreateTransaction(gDatabase)
;
MySQL_AddTransactionQuery(transaction, "UPDATE players SET money = money - %i WHERE id = %i", price, gPlayerId[playerid]);
MySQL_AddTransactionQuery(transaction, "INSERT INTO inventory (player_id, item) VALUES (%i, %i)", gPlayerId[playerid], item);
MySQL_ExecuteTransaction(transaction, "OnItemBought", "i", playerid);
}The values of a statement are copied when a row is added, and never escaped or parsed by the server again. A statement is prepared again when its connection is lost. A transaction that fails is rolled back and calls OnQueryError with the statement that failed.
Measured on one computer against MariaDB 10.4 on the same machine, open.mp 1.6.7 with PawnJIT, scripts compiled with -d2, medians of five runs.
| Time | |
|---|---|
| Sending a query, on the game thread | 0.8 µs |
| 500 queries in order | 45 ms |
| 500 parallel queries | 16 ms |
| A formatted query into an array | 0.64 µs |
| Six cells of a row by name | 0.6 µs |
| A query waited for | 64 µs |
| 2000 rows of a batch | 15 ms |
| 5000 inserts in order, in one transaction | 310 ms |
| An Argon2id hash, 19 MiB and two passes | 25 ms |
| Eight Argon2id hashes at once | 55 ms |
The wiki has a page for every native, the settings and guides to queries, results, JSON cells, prepared statements, passwords and TLS.
The tests in tests run as a gamemode against a database, and shut the server down when they end. Set the database at the top of tests/test.inc, or give the port to the compiler as TEST_PORT=3307. They pass with the binary as an open.mp component, as a legacy plugin on open.mp and on SA-MP 0.3.7-R2.
tests/queries.pwn: queries, the cache, callbacks, JSON cells, built queries and files.tests/statements.pwn: prepared statements and batches, with the open.mp names.tests/transactions.pwn: transactions committed and rolled back.tests/orm.pwn: the ORM.tests/passwords.pwn: hashes and known vectors of Argon2 and bcrypt.tests/settings.pwn: limits, timeouts, environment variables and required TLS, against a server without TLS.
CMake 3.19 or later fetches the open.mp SDK, the Pawn headers and MariaDB Connector/C 3.4.11. The server is 32 bit, so the plugin must be too.
On Windows, with Visual Studio 2022 or its Build Tools:
cmake -S . -B build -A Win32
cmake --build build --config Release --target mysql
On Linux, with GCC and its 32 bit libraries, and a static 32 bit OpenSSL 3:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS=-m32 -DCMAKE_CXX_FLAGS=-m32 -DCMAKE_SHARED_LINKER_FLAGS=-m32 -DOPENSSL_ROOT_DIR=/opt/openssl
cmake --build build --target mysql
The plugin is written to build/Release/mysql.dll or build/mysql.so. The Linux release is built on Ubuntu 20.04 with OpenSSL 3.0, as .github/workflows/main.yml does, and GitHub Actions runs the tests on Linux at every push.
- @mariadb-corporation for MariaDB Connector/C, linked into the plugin under the LGPL 2.1.
- @ibireme for yyjson, which reads the JSON cells.
- @P-H-C for the reference implementation of Argon2.
- @solardiz for crypt_blowfish.
- @openssl for OpenSSL, linked into the Linux build under the Apache License 2.0.
- @openmultiplayer for the SDK and the Pawn compiler.
Released under the MIT License. The libraries in external keep their own licenses, MariaDB Connector/C is under the LGPL 2.1, and OpenSSL, in the Linux build, under the Apache License 2.0.