Node-RED MySQL Database Connector

3.0.0 · active · verified Sun Apr 19

This package provides a Node-RED node for connecting to and interacting with MySQL databases. It allows users to execute SQL `query` operations, including `INSERT`, `UPDATE`, `DELETE`, and `SELECT` statements, by passing the SQL query in `msg.topic` and parameters in `msg.payload`. The results are returned in `msg.payload`, typically as an array of rows or null if no results are found. The current stable version is 3.0.0. While a specific release cadence isn't published, the project maintains an active development presence on GitHub. A key differentiator is its seamless integration into the Node-RED visual programming environment, abstracting direct `mysql` library usage into a configurable, GUI-driven node. It supports parameterized queries, both positional and named, which helps mitigate basic SQL injection risks, but explicitly warns users to remain vigilant about security. The node also offers configuration options for database reconnection timeouts and character sets.

Common errors

Warnings

Install

Imports

Quickstart

This Node-RED Function node prepares `msg.topic` and `msg.payload` to perform an `INSERT` query using named parameters, which can then be fed into a configured Node-RED MySQL database node. It includes commented-out examples for `UPDATE` and `SELECT` operations.

{
  "id": "b1c2d3e4.f5g6h7",
  "type": "function",
  "name": "Prepare MySQL Query",
  "func": "// This code is designed to be placed inside a Node-RED Function node.\n// It prepares the 'msg.topic' and 'msg.payload' objects\n// which are then passed to a `MySQL` database node (from this package).\n\n// First, ensure you have a `MySQL` node configured in your Node-RED flow\n// with appropriate database credentials and connection details.\n// Connect the output of this Function node to the input of your MySQL node.\n\n// Example: Inserting a new user with a unique username\n// Make sure your 'users' table exists, e.g.:\n// CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255) UNIQUE NOT NULL);\n\nconst uniqueUsername = `user_${Date.now()}_${Math.floor(Math.random() * 1000)}`;\nmsg.payload = { username: uniqueUsername };\nmsg.topic = \"INSERT INTO users (username) VALUES (:username);\";\n\n// --- Alternative: Updating an existing user ---\n// Uncomment the following lines to demonstrate an update operation.\n// This requires a user with a specific ID to exist.\n/*\nmsg.payload = { userId: 1, newUsername: \"updated_user_name\" };\nmsg.topic = \"UPDATE users SET username = :newUsername WHERE id = :userId;\";\n*/\n\n// --- Alternative: Selecting data ---\n// Uncomment the following lines to demonstrate a select operation.\n/*\nmsg.payload = { searchTerm: \"%user%\" };\nmsg.topic = \"SELECT id, username FROM users WHERE username LIKE :searchTerm;\";\n*/\n\nreturn msg;",
  "outputs": 1,
  "noerr": 0,
  "initialize": "",
  "finalize": "",
  "libs": [],
  "x": 100,
  "y": 100,
  "wires": [
    [
      "a8b9c0d1.e2f3g4" // Connect this to your Node-RED MySQL node's ID
    ]
  ]
}

view raw JSON →