Erlang
MySQL/OTP is a driver for connecting Erlang/OTP applications to MySQL and MariaDB databases. It is a native implementation of the MySQL protocol in Erlang.
MySQL/OTP (Erlang/OTP Client for MariaDB/MySQL)
MySQL/OTP is a native Erlang/OTP client designed for connecting Erlang applications to MySQL and MariaDB databases. It implements the MySQL protocol directly in Erlang, offering robust features for database interaction within an OTP framework.
1. What is MySQL/OTP?
MySQL/OTP acts as a client library, enabling Erlang applications to communicate with MySQL and MariaDB servers. It provides a native Erlang implementation of the MySQL protocol, allowing for efficient and idiomatic database operations within the Erlang/OTP ecosystem.
2. Key Features
Mnesia Style Transactions: Supports nestable transactions that align with Erlang's Mnesia database transaction model.
SSL Support: Secure connections using SSL/TLS.
Authentication Methods: Supports
caching_sha2_passwordandmysql_native_password.Parametrized Queries: Utilizes cached unnamed prepared statements for efficient and secure queries.
Interruptible Slow Queries: Allows interrupting slow queries without terminating the connection.
Protocol Support: Implements both the binary protocol (for prepared statements) and the text protocol (for plain queries).
3. Installation
You can add MySQL/OTP as a dependency in your Erlang/OTP project using erlang.mk, rebar (v2 or v3), or mix (for Elixir projects).
a. Example with rebar.config (rebar3):
Add the following to your rebar.config file in the deps section:
Then run rebar3 compile or rebar3 deps get to fetch and compile the dependency.
4. Basic Usage
Here are common operations using the mysql module:
a. Connect to the Database:
Establish a connection to your MariaDB/MySQL server. SSL is optional.
Replace "localhost", "myuser", "mypass", and "mydb" with your database details.
b. Execute a SELECT Query (Parameterized):
c. Manipulate Data (INSERT/UPDATE/DELETE):
d. Mnesia-style Transaction (Nestable):
e. Execute Multiple Queries:
f. Graceful Timeout Handling:
You can specify a timeout for queries. If the query exceeds the timeout, it will be interrupted.
g. Close the Connection:
Always close the connection when it's no longer needed.
Further Resources:
Erlang/OTP Documentation (if available on the official Erlang docs)
Last updated
Was this helpful?

