{"id":43823,"library":"serverless-mysql","title":"Serverless MySQL","description":"A wrapper for mysql2 that manages MySQL connections in serverless environments like AWS Lambda, Google Cloud Functions, and Azure Functions. Current stable version is 2.1.0, released regularly. It prevents connection exhaustion by monitoring and limiting concurrent connections, cleaning up zombie connections, and retrying with exponential backoff. Differentiators include built-in async/await support, simplified transactions, and optional SQL query logging for debugging. Ships TypeScript types and works with any standards-based MySQL server including RDS, Aurora, and Aurora Serverless.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/jeremydaly/serverless-mysql","tags":["javascript","serverless","mysql","max_connections","scalability","rds","aurora serverless","aurora","typescript"],"install":[{"cmd":"npm install serverless-mysql","lang":"bash","label":"npm"},{"cmd":"yarn add serverless-mysql","lang":"bash","label":"yarn"},{"cmd":"pnpm add serverless-mysql","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core MySQL client library that serverless-mysql wraps for connection pooling and query execution.","package":"mysql2","optional":false}],"imports":[{"note":"The package exports a factory function that must be called with options. ESM import works but requires default import from the factory.","wrong":"import mysql from 'serverless-mysql'","symbol":"default","correct":"const mysql = require('serverless-mysql')({ config: {...} })"},{"note":"The default export is the factory function; named export 'mysql' does not exist. In TypeScript, use `import serverlessMysql from 'serverless-mysql'`.","wrong":"import { mysql } from 'serverless-mysql'","symbol":"mysql","correct":"import mysql from 'serverless-mysql'"},{"note":"The type is exported for TypeScript usage only. The main export is a factory function, not a class.","wrong":"import { ServerlessMySQL } from 'serverless-mysql' (if used as value)","symbol":"ServerlessMySQL","correct":"import type { ServerlessMySQL } from 'serverless-mysql'"}],"quickstart":{"code":"const mysql = require('serverless-mysql')({\n  config: {\n    host     : process.env.DB_HOST ?? 'localhost',\n    database : process.env.DB_NAME ?? 'test',\n    user     : process.env.DB_USER ?? 'root',\n    password : process.env.DB_PASS ?? ''\n  }\n});\n\nasync function query() {\n  let results = await mysql.query('SELECT * FROM users WHERE id = ?', [1]);\n  console.log(results);\n  await mysql.end();\n  return results;\n}\n\nquery().catch(err => console.error(err));","lang":"javascript","description":"Initializes the connection manager with environment variables, runs a parameterized query, and cleans up resources."},"warnings":[{"fix":"Initialize `const mysql = require('serverless-mysql')({...})` at module scope, not inside the handler.","message":"The module must be initialized outside the handler function (global scope) to reuse connections across invocations. Initializing inside the handler will create new connections per request and cause connection leaks.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always call `await mysql.end()` after finishing all queries in a handler invocation.","message":"Calling `mysql.end()` after each query is required to release the connection back to the pool. Forgetting to call `end()` will exhaust connections quickly.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use `mysql.end()` instead of deprecated `mysql.quit()`. Update to Node 10+.","message":"Version 2.0.0 dropped Node.js 8 support and changed internal connection management. Existing code using `mysql.quit()` instead of `mysql.end()` will break.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Replace `mysql.quit()` with `mysql.end()`.","message":"The `mysql.quit()` method was deprecated in v2.0.0 and removed in v2.1.0. Using it will throw an error.","severity":"deprecated","affected_versions":">=2.1.0"},{"fix":"Check for `sql` property in results if you iterate over keys, or disable the option if not needed.","message":"If `returnFinalSqlQuery` is enabled, the results object and error objects will have an extra `sql` property, which may conflict with existing code that iterates over result keys.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'2.1.0':27 'async/await':54 'aurora':79,81,90,92 'aw':15 'azur':21 'backoff':48 'base':74 'built':52 'built-in':51 'clean':40 'cloud':18 'concurr':38 'connect':10,32,39,43,87 'current':23 'debug':64 'differenti':49 'environ':13 'exhaust':33 'exponenti':47 'function':19,22 'googl':17 'includ':50,77 'javascript':83 'lambda':16 'like':14 'limit':37 'log':62 'manag':8 'max':86 'monitor':35 'mysql':2,9,75,85 'mysql2':6 'option':59 'prevent':31 'queri':61 'rds':78,89 'regular':29 'releas':28 'retri':45 'scalabl':88 'server':76 'serverless':1,12,82,84,91 'ship':65 'simplifi':56 'sql':60 'stabl':24 'standard':73 'standards-bas':72 'support':55 'transact':57 'type':67 'typescript':66,93 'version':25 'work':69 'wrapper':4 'zombi':42","created_at":"2026-06-05T17:01:38.628356+00:00","updated_at":"2026-06-05T17:01:38.628356+00:00","problems":[{"fix":"Ensure each handler invocation initializes the module once at global scope and only calls `mysql.end()` once per invocation.","cause":"Query attempted on a connection that was already ended by a previous invocation's `mysql.end()` call.","error":"Error: Can't add new command when connection is in closed state"},{"fix":"Use `const mysql = require('serverless-mysql')({...})` with parentheses, not just `require('serverless-mysql')`.","cause":"The factory function was not called, so the returned object is the factory itself, not the configured instance.","error":"TypeError: mysql.query is not a function"},{"fix":"Increase idle timeout or use `mysql.end()` to release connections promptly. Adjust `maxRetries` and `backoff` options.","cause":"Idle connection timed out due to default MySQL wait_timeout or too many zombie connections.","error":"TimeoutError: Connection lost: The server closed the connection."},{"fix":"Call `mysql.end()` after each request, reduce `maxConcurrentQueries` option, or increase MySQL max_connections. Ensure initialization is outside the handler.","cause":"Serverless function instances exceeded the MySQL max_connections limit because connections were not released or managed properly.","error":"Error: ER_CON_COUNT_ERROR: Too many connections"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/jeremydaly/serverless-mysql#readme","github":"https://github.com/jeremydaly/serverless-mysql","docs":null,"changelog":null,"pypi":null,"npm":"serverless-mysql","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database","devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}