# MYSQL2 - Dynamic info - Open or Closed Sessions?

**URL:** <https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934>\
**Category:** General\
**Created:** [19 January 2023 19:35 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934 "2023-01-19T19:35:36Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [19 January 2023 19:35 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/1 "2023-01-19T19:35:36Z")

</div>

I've recently been on a mission to make a system where the implementation of the system won't be mine.

It will be a third-party team.

So I need to make a system that will be configured by someone else.

In this case, I need to manually configure the database information directly through the user interface, such as URL, username and password.

after much searching I found node-red-contrib-mysql2 which works very well.

However, there is a problem with the setup of information that is necessary for each SQL inject.

This slows down the system.

Does anyone have a solution?

Do you know of any way around this?

This node: node-red-contrib-mysql2

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/9/6986bf75bb33d4e8dd7364388a5ada070e6e3dd0.png)

because it is necessary again to pass the setup information

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f6f41b37e923e0e9d8f82a38192c989ab46b8c2.png)

---

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [20 January 2023 02:28 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/2 "2023-01-20T02:28:30Z")

</div>

@Steve-Mcl Any suggestions in that regard?

---

<div class="post-metadata">

**Author:** ![paulkeates](https://avatars.discourse-cdn.com/v4/letter/p/9de053/32.png) [@paulkeates](https://discourse.nodered.org/u/paulkeates)\
**Post date:** [20 January 2023 15:57 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/3 "2023-01-20T15:57:17Z")

</div>

Hi,

A guy on my team wrote the MYSQL2 node - so thanks for the positive feedback!

I've not experienced any slowdowns using MYSQL2. Maybe a few more details would help with 'slowdown'?

you can actually put the yellow 'set msg.topic' node before the Function node and drop the additional timestamp node.

Then simply clicking the Inject node (or ANYTHING that activates the flow) will run the query.

Cheers,

Paul

---

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [21 January 2023 01:49 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/4 "2023-01-21T01:49:51Z")

</div>

My question is the following:

Every time you run a query, it redoes the connection, isn't that inefficient?

Wouldn't it be ideal to leave it connected and inject the queries later or at any time?

The node worked perfectly... I have no questions as to how it works.

My question is about the efficiency of the execution.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [21 January 2023 05:42 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/5 "2023-01-21T05:42:34Z")

</div>

The alternative node-red-node-mysql uses a pool of connections and does keep the connection open.

---

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [22 January 2023 01:50 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/6 "2023-01-22T01:50:56Z")

</div>

I imagine that the default node-red-node-mysql only keeps 1 connection open.

And it runs the pool only when requested.

But those of you who are more experienced could tell me what would actually be more efficient?

Can I then use the node-red-contrib-mysql2 written by the colleague that I will have a good efficiency in the execution?

What do you think from the point of view of better performance?

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [22 January 2023 14:11 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/7 "2023-01-22T14:11:44Z")

</div>

This is an interesting thread as this week I received an email from my ISP informing me I had a large number of sleeping mysql connections (and could I take steps to reduce the number).  
I'm using 'node-red-node-mysql' which seems to keep each connection open after the query has been executed (so I can see how that would annoy my ISP when the number becomes significant).

This morning I installed mysql (mariadb) on one of my RPi-4B home-servers and created a local and some remote users.  
By using SHOW PROCESSLIST; (when logged in to mysql) I can see that 'mysql' leaves each connection open after it has executed the query, and can see why this might annoy my ISP, as in my case I suspect the number was quite high.

The 'mysql2' node closes a connection after execution and never appears on the Processlist.  
I've actually run a flow all morning, that pings devices around my home, and then performs a series of INSERT queries every 90 seconds (to my local server) and not encountered any problems.

 ![ping_to_mysql](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cb9c0b5897d6b0c36353bce99993980a1d5ab285.png)

I guess I'll replace the 'mysql' nodes with 'mysql2' nodes (in all my flows) in order to keep my ISP happy?

---

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [22 January 2023 14:48 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/8 "2023-01-22T14:48:06Z")

</div>

But I wonder the following:

If you are going to develop an application where every time you need to insert a record in an external database, such as capturing data from telemetry, where there are thousands of records, imagine and this opens and does it close all the time of the connection?

For sure the performance will not be the same...

Even more so in professional applications

I'm developing an application that will add at least 10,000 records in about 5 minutes.

I can't plan something that will open and close the connection every time I insert.

If I choose to open and close the connection all the time, it will consume a lot of machine resources

what do you think ?

@dceejay @Steve-Mcl @TotallyInformation @paulkeates

Is there a way to make a flow where all requests are sent to only one connection that is already open?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2023 15:24 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/9 "2023-01-22T15:24:22Z")

</div>

As described above, the standard node keeps it open.

---

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [26 January 2023 13:02 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/10 "2023-01-26T13:02:47Z")

</div>

I still have doubts about which one would be more efficient.

Do you have an opinion on this matter?

@dceejay @knolleary @Steve-Mcl

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 January 2023 13:11 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/11 "2023-01-26T13:11:08Z")

</div>

> [@allacmc](#):
>
> I'm developing an application that will add at least 10,000 records in about 5 minutes.

is that 10000 records EVERY 5 minutes? You do realise the database have 2,880,000 new records per day, 1,051,200,000 new records per year (1,051,200,000 = one billion fifty-one million two hundred thousand)

> [@allacmc](#):
>
> Is there a way to make a flow where all requests are sent to only one connection that is already open?

The node-red-node-mysql node uses a connection pool (as do many / most applications).

> Database connection pooling is a way to reduce the cost of opening and closing connections by maintaining a “pool” of open connections that can be passed from database operation to database operation as needed

  

PS, would you mind not tagging please directly (we are all volunteers here and respond when we can) - thanks.

---

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [26 January 2023 13:18 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/12 "2023-01-26T13:18:50Z")

</div>

> [@Steve-Mcl](#):
>
> is that 10000 records EVERY 5 minutes? You do realise the database have 2,880,000 new records per day, 1,051,200,000 new records per year (1,051,200,000 = one billion fifty-one million two hundred thousand)

This application is not used 24 hours a day. It will be used occasionally.

Maybe 4 to 10 times a day.

---

<div class="post-metadata">

**Author:** ![allacmc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allacmc/32/73549_2.png) [@allacmc](https://discourse.nodered.org/u/allacmc)\
**Post date:** [26 January 2023 13:24 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/13 "2023-01-26T13:24:22Z")

</div>

> [@Steve-Mcl](#):
>
> PS, would you mind not tagging please directly (we are all volunteers here and respond when we can) - thanks.

At no point would I want you to feel pressured.

I tagged you because I know you're pretty experienced. And I would like to count on your excellent and friendly help.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 January 2023 13:36 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/14 "2023-01-26T13:36:08Z")

</div>

No worries, its not really considered great netiquette - I hope you understand.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 March 2023 13:36 UTC](https://discourse.nodered.org/t/mysql2-dynamic-info-open-or-closed-sessions/73934/15 "2023-03-27T13:36:43Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
