Registry / database / loopback-connector-mysql2

loopback-connector-mysql2

JSON →
library6.0.0jsnpmunverified

MySQL connector for LoopBack's datasource-juggler, using the mysql2 driver instead of mysqljs. Version 6.0.0 requires Node >=10. It provides LoopBack ORM integration with MySQL databases, supporting features like connection pooling, big number handling, and Unix socket connections. Released as a community fork of loopback-connector-mysql.

npm install loopback-connector-mysql2
INSTALL
IMPORT
SIG · LOOPBACK-CONNECTOR
L
loopback-connector-mysql2
databasejavascriptv6.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DataSource
import { DataSource } from 'loopback-datasource-juggler'; const ds = new DataSource('mysql2', { host: 'localhost', port: 3306, database: 'test', user: 'root', password: 'pass' });
import { DataSource } from 'loopback-connector-mysql2'; const ds = new DataSource();
The connector itself is not directly instantiated; use loopback-datasource-juggler's DataSource with connector name 'mysql2'.
MySQL connector configuration
const config = { name: 'db', connector: 'mysql2', host: 'localhost', port: 3306, user: 'root', password: 'pass', database: 'testdb' };
const config = { name: 'db', connector: 'mysql', ... };
The connector name must be 'mysql2' to use this package instead of the original mysql connector.
Model definition
import { Model } from 'loopback-datasource-juggler'; const MyModel = ds.define('MyModel', { name: String });
import { Model } from 'loopback-connector-mysql2';
Model definitions come from loopback-datasource-juggler, not the connector.

Create a MySQL2 data source, define a model, auto-migrate, and insert a record.

import { DataSource } from 'loopback-datasource-juggler'; const ds = new DataSource('mysql2', { host: process.env.DB_HOST || 'localhost', port: parseInt(process.env.DB_PORT || '3306', 10), database: process.env.DB_NAME || 'test', user: process.env.DB_USER || 'root', password: process.env.DB_PASSWORD || '', }); const User = ds.define('User', { email: { type: String, required: true }, name: String, }); ds.automigrate('User', (err) => { if (err) throw err; User.create({ email: 'test@example.com', name: 'Test' }, (err, user) => { console.log('Created user:', user); }); });
Debug
Known issues
gotchaConnector name must be 'mysql2' not 'mysql' when using this package.
fix
Use connector: 'mysql2' in your datasource configuration.
affects: >=0.0.0
gotchaThis package is a community fork of loopback-connector-mysql using mysql2 driver. Some behaviors may differ, especially with prepared statements and large numbers.
fix
Test thoroughly and refer to mysql2 documentation for differences.
affects: >=0.0.0
deprecatedLoopBack 3 (datasource-juggler) is deprecated; LoopBack 4 uses @loopback/repository.
fix
Migrate to LoopBack 4 and use @loopback/repository-tests or loopback-connector-mysql with LB4.
affects: >=4.0.0
Errors
Common errors & fixes
Error: Connection lost: The server closed the connection.
MySQL server or network timeout.
fix
Ensure `connectionLimit` is set appropriately and handle reconnection logic in your application.
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'@'localhost' (using password: YES)
Invalid credentials or host permissions.
fix
Verify database user, password, and host permissions in MySQL.
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MariaDB client
MySQL 8+ default auth plugin (caching_sha2_password) not supported by older mysql2 versions.
fix
Update mysql2 to latest version or change MySQL user to use mysql_native_password.
Upgrade
Version history
6.0.0latest on npm
Audit
Dependencies
loopback-datasource-jugglerrequiredRequired as peer dependency for LoopBack ORM integration
mysql2requiredUnderlying MySQL driver
Agent activity
9 hits · last 30 days
node
8
Amazon
1
Resources
loopback-connector-mysql2 — npm install loopback-connector-mysql2 · libregistry