yiisoft/db-mysql
MySQL and MariaDB driver for Yii Database. Adds MySQL-specific connection, schema, query builder, and DDL support to yiisoft/db, letting Yii applications work with MySQL databases through a consistent database abstraction layer.
composer require yiisoft/db-mysql. You need ext-pdo, ext-pdo_mysql, a PSR-16 cache and a PSR-3 logger — all constructor dependencies, not optional at the API level.yiisoft/db's README first. The db-mysql repo is mostly the driver; the docs, query builder guide and changelog live in the parent package, so start there.Connection signature changed across 1.x):use Psr\Log\NullLogger;
use Yiisoft\Cache\ArrayCache;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Mysql\Connection;
use Yiisoft\Db\Mysql\Driver;
$driver = new Driver(
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=app;charset=utf8mb4',
username: 'app',
password: 'secret',
);
$db = new Connection($driver, new SchemaCache(new ArrayCache()), new NullLogger());
$rows = $db->createCommand('SELECT * FROM users WHERE id = :id', ['id' => 1])->queryAll();
illuminate/database.Connection owns the PDO driver, schema cache and transactions; Command is raw SQL with named/positional bindings; QueryBuilder ($db->createQuery()->select(...)) is the fluent path; Schema/ColumnSchemaBuilder handles introspection and DDL. Pick one layer per concern rather than mixing.$db->transaction(fn () => ...) or explicit beginTransaction/commit/rollBack. Nested calls become savepoints. Keep DDL out (see Gotchas).$db->getSchema()->getTableNames(), getTableSchemas(), getViewNames(), index and foreign-key metadata are all exposed. This is the fastest win for a Laravel dev: generate Eloquent migrations or typed DTOs from an existing database.yiisoft/db-migration package. ColumnSchemaBuilder gives you tinyint(1), json, enum, set, currentTimestamp() and friends. Laravel's Schema facade will not read these files, and vice versa — pick one migration owner per database.Connection as a singleton in a service provider, read DSN/user/pass from config('database.connections.mysql'), use a real PSR-16 cache in production and ArrayCache in tests. Do not share the PDO handle with Eloquent; two pools is simpler than reconciling transaction semantics.$db->close() at the end of a job batch to avoid stale session state.Connection objects and route explicitly.Command for very large INSERT ... VALUES batches — the builder's per-row overhead adds up.DB:: helper, no Eloquent, no Model. Everything (cache, logger, container binding) is hand-wired.db-mysql README is thin and the parent guide often lags the 1.x source. If a snippet fails, read Connection, Driver and Schema in vendor/ before assuming your config is wrong. Pin a version.ArrayCache in production means every request re-queries information_schema; pass Redis/file cache in real deployments. Conversely, clear the cache after migrations or Laravel will keep a stale schema.NullLogger by default and swap in a real logger behind a flag; a PSR-3 decorator around Command is the least invasive way to debug.INSERT ... RETURNING (MariaDB only), default collations and information_schema shape all differ. The Schema class branches on server version — test both if you support both.charset=utf8mb4; set PDO::ATTR_EMULATE_PREPARES => false if you want real prepared statements. Pass PDO attributes through the Driver constructor, not the DSN.tinyint(1) vs boolean, enum/set surfacing as strings, and zero-date handling will surprise codegen. Normalise in your generator, not in Schema.select()/where() get quoted as identifiers; wrap raw SQL fragments in Expression or they will break.CREATE/ALTER outside transactional blocks.PDO::ATTR_PERSISTENT unless you audit for it.Driver subclass for PDO attributes and reconnect logic, a Schema subclass for type-mapping overrides, and custom column builders for vendor-specific types. Check the source for exact hook names — they aren't documented.How can I help you explore Laravel packages today?