# Error with MYSQL 2 - Status - crash NODE-RED

**URL:** <https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224>\
**Category:** General\
**Created:** [26 January 2023 16:27 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224 "2023-01-26T16:27:24Z")\
**Posts on this page:** 10\
**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:** [26 January 2023 16:27 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/1 "2023-01-26T16:27:24Z")

</div>

I'm using NODE MYSQL2 as a connection to my database.

I put it in a subflow.

I added an OUTPUT Status.

But when you have this option, it completely crashes NODE-RED and I need to manually restart.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/1428a9bc55ee62935e020a8d58b8408a0cf8478d.png)

```auto
[{"id":"4639c7fae2510b5a","type":"subflow","name":"PrepBD","info":"","category":"","in":[{"x":80,"y":100,"wires":[{"id":"8cc0b55b7d0743c0"}]}],"out":[{"x":1020,"y":100,"wires":[{"id":"4135b832b386a610","port":0}]}],"env":[],"meta":{},"color":"#DDAA99","status":{"x":1020,"y":180,"wires":[{"id":"4135b832b386a610","port":0}]}},{"id":"8cc0b55b7d0743c0","type":"change","z":"4639c7fae2510b5a","name":"copy flow/global to msg","rules":[{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.ip","pt":"msg","to":"IP_vGlobalBD","tot":"global"},{"t":"set","p":"payload.porta","pt":"msg","to":"Porta_vGlobalBD","tot":"global"},{"t":"set","p":"payload.usuario","pt":"msg","to":"Usuario_vGlobalBD","tot":"global"},{"t":"set","p":"payload.senha","pt":"msg","to":"Senha_vGlobalBD","tot":"global"},{"t":"set","p":"payload.nomeBD","pt":"msg","to":"NomeBD_vGlobalBD","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":100,"wires":[["effa65099dd2a525"]]},{"id":"4135b832b386a610","type":"mysql2","z":"4639c7fae2510b5a","name":"","server":"","bind":"","topic":"","x":760,"y":100,"wires":[[]]},{"id":"effa65099dd2a525","type":"function","z":"4639c7fae2510b5a","name":"prep_MQTT_Conn","func":"\nconst ip = msg.payload.ip\nconst porta = msg.payload.porta\nconst usuario = msg.payload.usuario\nconst senha = msg.payload.senha\nconst nomeBD = msg.payload.nomeBD\n\nmsg.server = {\n \"host\": ip,\n \"port\": porta,\n \"username\": usuario,\n \"password\": senha,\n \"db\": nomeBD\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":100,"wires":[["4135b832b386a610"]]},{"id":"eb96b8b79efaea17","type":"change","z":"4639c7fae2510b5a","name":"","rules":[{"t":"set","p":"server","pt":"msg","to":"{'host': $globalContext('IP_vGlobalBD'),\t 'port': $globalContext('Porta_vGlobalBD'),\t 'username': $globalContext('Usuario_vGlobalBD'),\t 'password': $globalContext('Senha_vGlobalBD'),\t 'bd': \"TelemetriaEnervision\"\t }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":260,"wires":[[]]},{"id":"4a50ea77c5d0ed5b","type":"comment","z":"4639c7fae2510b5a","name":"Quem sabe futuro usar esse Change","info":"Não consegui fazer funcionar com o mysql2, ele diz que o nome do banco de dados não está selecionado.","x":320,"y":220,"wires":[]}]

```

```auto
Jan 26 16:23:49 DietPi node-red[1957]: at Object.connect (/mnt/dietpi_userdata/node-red/node_modules/node-red-contrib-mysql2/nodes/utils.js:5:12)
Jan 26 16:23:49 DietPi node-red[1957]: at MySql2._inputCallback (/mnt/dietpi_userdata/node-red/node_modules/node-red-contrib-mysql2/nodes/mysql2.js:52:50)
Jan 26 16:23:49 DietPi node-red[1957]: at /mnt/dietpi_userdata/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:210:26
Jan 26 16:23:49 DietPi node-red[1957]: at Object.trigger (/mnt/dietpi_userdata/node-red/node_modules/@node-red/util/lib/hooks.js:166:13)
Jan 26 16:23:49 DietPi node-red[1957]: at Node._emitInput (/mnt/dietpi_userdata/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js:202:11)
Jan 26 16:23:49 DietPi systemd[1]: node-red.service: Main process exited, code=exited, status=1/FAILURE
Jan 26 16:23:49 DietPi systemd[1]: node-red.service: Failed with result 'exit-code'.
Jan 26 16:23:49 DietPi systemd[1]: node-red.service: Consumed 15.428s CPU time.

```

Unfortunately this NODE should not have this behavior.

---

<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 16:33 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/2 "2023-01-26T16:33:14Z")

</div>

Why are you using that node and not the one supported by the core team (node-red-node-mysql)

As for this issue, no idea since [that node](https://flows.nodered.org/node/node-red-contrib-mysql2) doesnt have link to its repository so I cannot see exactly why it crashes node-red.

What I can say is any node that does not correctly handle its errors (especially in promises/async code) will crash node-red and we encourage users to raise an issue on the repository asking the developer to properly handle errors.

---

<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 16:37 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/3 "2023-01-26T16:37:26Z")

</div>

I'm using this node because I can insert the database connection data via global variables.

I have an application where this is important: An external professional who is not a programmer. Fill in data such as IP, User, password, port and database.

The current official MYSQL by the NODE-Red team does not allow these modifications dynamically.

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

Do you have any suggestions for using a database connector that does this dynamically and has stability?

---

<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 16:54 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/4 "2023-01-26T16:54:51Z")

</div>

If it needs to be set once, for 1 database, without modifying flows, then simply use ENV VARS. Then instruct the external person the ENV VARS to set before launching node-red

Using ENV VARs for text fields.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/d/5d03630290aa24525a59e2d4b1b69c66882d43c1.png)  
DOCS: [Using environment variables : Node-RED](https://nodered.org/docs/user-guide/environment-variables)

  

If the connection needs to be changed at runtime, then you will need to fork node-red-node-mysql (or another repo) and add runtime setting feature yourself.

---

<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 17:07 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/5 "2023-01-26T17:07:31Z")

</div>

Populating the database property once will partially solve my problem.

I don't have the knowledge to modify the current node to allow receiving these settings dynamically, so I don't even try to fork.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 January 2023 18:16 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/6 "2023-01-26T18:16:33Z")

</div>

You could try it like this instead (suggested [this a couple of days ago](https://discourse.nodered.org/t/psa-setup-tab-in-the-function-node/74044)):

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d85a199defcbc50f26085da4dbe3554390af270.png)

It requires to have external modules to be enabled in settings.  
Then you can make it dynamic.

---

<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 18:26 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/7 "2023-01-26T18:26:26Z")

</div>

I think the concept of the visual language is to use as little code as possible.

In that case your suggestion is very good.

But it also involves typing a lot of code.

Ideally, there should be a mysql NODE that allows dynamically changing connections using the change node.

Or maybe you can do that and I haven't thought of an elegant solution.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 January 2023 18:50 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/8 "2023-01-26T18:50:01Z")

</div>

> But it also involves typing a lot of code.

You type (copy) it once, you can feed the data via `msg.payload.host`, `msg.payload.user` etc....  
See [documentation](https://www.npmjs.com/package/mysql) from which you can copy the code.

replace the `host: "192.168.1.4` with `host:msg.payload.host` etc and you can reuse it (ie: dynamic).

I don't see the issue, or do you want everything to be served on a plate?

The reason (i assume) that it is not dynamic is because of database security and encryption of the password, which will be completely omitted with this and your "solution".

---

<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 20:49 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/9 "2023-01-26T20:49:01Z")

</div>

You are completely right.

That way I can with only one node... Assigned in subflow to act as a function.

Thank you very much.

I think you found an elegant solution.

---

<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 20:49 UTC](https://discourse.nodered.org/t/error-with-mysql-2-status-crash-node-red/74224/10 "2023-03-27T20:49:13Z")

</div>

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