Persistence

Database MySQL Driver

MySQL connection backend, runtime driver and MySQL schema grammar behavior.

Provider responsibility

MysqlDatabaseServiceProvider registers MysqlConnectionInterface, MysqlDatabaseDriver, MySQL identifier/sql escapers and MysqlSchemaGrammar. It also registers the mysql driver and mysql schema grammar factories in DatabaseDriverRegistry.

Configuration

Use driver mysql for the native MySQL backend. DatabaseConfig defaults dialect to mysql for this driver, but setting dialect explicitly keeps configuration clear.

DB_DRIVER=mysql
DB_DIALECT=mysql
DB_HOST=localhost
DB_PORT=3306
DB_NAME=app
DB_USER=root
DB_PASS=
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci

Runtime API

MySQL runtime is available through Database, DatabaseDriverInterface and QueryBuilder. MysqlDatabaseDriver implements query execution, escaping, identifier protection, affected rows, insert id, platform/version and transaction support through the shared transaction trait.

Schema grammar

MysqlSchemaGrammar compiles MySQL DDL. It supports inline indexes in CREATE TABLE, AUTO_INCREMENT, UNSIGNED numeric columns, column comments, FIRST/AFTER positioning, table options such as ENGINE/CHARSET/COLLATE, fulltext/spatial indexes and foreign keys.

Transactions

Use Database::transaction() for atomic work. For lower-level transaction control, ConnectionInterface exposes beginTransaction(), commit(), rollBack() and inTransaction().

$db->transaction(static function (Database $db): void {
    $db->statement('UPDATE users SET active = ? WHERE id = ?', [1, 10]);
    $db->statement('INSERT INTO audit_logs (message) VALUES (?)', ['Activated user']);
});

Portability

MySQL-specific QueryBuilder methods such as insertOrIgnore(), upsert(), lockForUpdate() and sharedLock() generate MySQL-oriented SQL. Use them when the target backend supports the generated syntax.