How to connect to MySQL database using Node-Red

How to connect to MySQL database using Node-Red

To connect Node-red to the database (mysql), you first need to download the MySQL software on your computer and install Navicat for easy operation. It is already installed by default.

Download controls in Node-Red

In Node-Red, you need to download the required MySQL control first:

  1. First, click Settings in the upper left corner and find Node Management ;
  2. Click Install in Node Management;
  3. Enter the node-red-node-mysql control in the query window of the installation interface, select Download after querying, and wait for the download to complete;

Download Control
insert image description here

Use of mysql control

Create a new local connection root and set Database to the local connection name

insert image description here

Node information <br /> Define the JavaScript code (body of the function) to process the received message.
The input message is passed in a JavaScript object named msg.
Typically, msg.topic must hold the query to the database, and then return the result in the msg.payload attribute.
This function typically returns a message object (or multiple message objects), but can also return nothing in order to stop the flow. Create a database

The nodes that need to be used are inject , function , mysql , and debug .

insert image description here

//function node function writing: create database Data_test
var sql = "CREATE DATABASE Data_test;";
var topic = {"topic":sql};
return topic;

After completion, click the small square of the inject node to complete the creation of the database Data_test, and refresh and view it in Navicat.

Create a data table table_name

The nodes you need to use are inject , function , mysql , debug

insert image description here

//Function node function writing: create data table table_name
var sql = "CREATE TABLE IF NOT EXISTS `runoob_tbl`( `runoob_id` INT UNSIGNED AUTO_INCREMENT, `runoob_title` VARCHAR(100) NOT NULL, `runoob_author` VARCHAR(40) NOT NULL, `submission_date` DATE, PRIMARY KEY ( `runoob_id` ))ENGINE=InnoDB DEFAULT CHARSET=utf8;";
var topic = {"topic":sql};
return topic;

After completion, click the small square of the inject node to complete the creation of the database table table_name, and refresh and view it in Navicat.

Add student information

Before adding student information, you need to create a new table student in Navicat, which contains name, age, grade, class_name

The nodes you need to use are inject , function , mysql , debug

insert image description here

Method 1

//Function node function writing: add student information var Student="INSERT INTO student(name, age,grade, class_num) VALUES ('wangwu', 11, 4, '3')";
var newMySQLData = { "topic": Student }
return newMySQLData;

Method 2

//Function node function writing: add student information var Student="INSERT INTO student(name, age,grade, class_num) VALUES ('%s', %d, %d, '%s')";
var newMySQLData = {
    "topic": util.format(Student, "lisi",12,6, "1")
}
return newMySQLData;

The student information in method 2 can also be transmitted by selecting {}JSON in the inject node and entering the student information, and parsed in the function in the form of msg.payload.name .

insert image description here

After completion, click the small square of the inject node to complete the addition of student information, and refresh and view it in Navicat.

This is the end of this article about Node-Red to achieve MySQL database connection. For more relevant MySQL database connection content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A convenient way to configure multiple data sources and Mysql databases in the springboot backend
  • Detailed explanation of MySQL DEFINER usage
  • In-depth explanation of MySQL isolation level and locking mechanism
  • Django production environment construction (uWSGI+django+nginx+python+MySQL)
  • A brief analysis of whether MySQL primary key uses numbers or uuids for faster query
  • MySQL permissions and database design case study
  • Why MySQL does not recommend using null columns with default values
  • Detailed explanation of group by and having in MySQL

<<:  Implementation steps for docker deployment lnmp-wordpress

>>:  First experience of creating text with javascript Three.js

Recommend

MySQL 8.0.22 download, installation and configuration method graphic tutorial

Download and install MySQL 8.0.22 for your refere...

Solution to the impact of empty paths on page performance

A few days ago, I saw a post shared by Yu Bo on G...

Solution to uninstalling Python and yum in CentOs system

Background of the accident: A few days ago, due t...

IE8 Developer Tools Menu Explanation

<br />This article has briefly explained the...

How to use HTML form with multiple examples

Nine simple examples analyze the use of HTML form...

WePY cloud development practice in Linux command query applet

Hello everyone, today I will share with you the W...

How to use Tencent slider verification code in Vue3+Vue-cli4 project

Introduction: Compared with traditional image ver...

Vue practice of preventing multiple clicks

Generally, click events will be divided into diff...

Detailed explanation of MySQL semi-synchronization

Table of contents Preface MySQL master-slave repl...

MySQL NULL data conversion method (must read)

When using MySQL to query the database and execut...

Does Mysql ALTER TABLE lock the table when adding fields?

Table of contents Before MySQL 5.6 After MySQL 5....

WeChat applet implements text scrolling

This article example shares the specific code for...

Vue3.x uses mitt.js for component communication

Table of contents Quick Start How to use Core Pri...

How to configure https for nginx in docker

Websites without https support will gradually be ...