Check connection = mysql nodejs
I use node js with mysql and want to avoid that the app crash on connection errors.At the moment i use this : Show
so this is blocking because it leads to a hot loop if the connection is closed.my problem is, if i add a setTimeout to reestablish connection just every 2 seconds i could get an fatal error when i do a query with "mysql_connection.query('SELECT ...')".in this case the app crashes. So my question is,if there's a possibility to check the connection before i do a query? asked Mar 26, 2015 at 11:13
3 Try using below code in every microservice before doing anything:
State of connection to DB could fall in 2 states:
So either check state == 'disconnected' or state == 'authenticated'
herve 3,6262 gold badges16 silver badges27 bronze badges answered Mar 13, 2017 at 11:09
Pranay SahaPranay Saha 2983 silver badges12 bronze badges 1 I know this is an old question but I have found answered Mar 26, 2019 at 13:30
MrfksIVMrfksIV 8506 silver badges16 bronze badges Every time, while I'm pushing my code in production, the mysql connection is lost. It is a very common problem in production, or local. Step1: Here is the code for dbConnection.js
Step2: For Router.js I am import the db connection and handle the promise
This is perfectly run If you want to create and close connection at every query .. answered May 2, 2020 at 1:55
I solved this problem like this:
answered Sep 30, 2021 at 20:41
kolserdavkolserdav 1341 silver badge9 bronze badges How do you check database is connected or not in node JS?We will see how to check if the server where our MySQL Database is Hosted is Active or Not. Syntax: database_connection. ping(callback);
How does node js handle MySQL connection?To download and install the "mysql" module, open the Command Terminal and execute the following:. C:\Users\Your Name>npm install mysql.. var mysql = require('mysql');. Run "demo_db_connection.js" C:\Users\Your Name>node demo_db_connection.js.. Connected!. con. connect(function(err) { if (err) throw err; console.. How do you check is MySQL connected or not?To check the database connectivity, you must know the database hostname, normally it is “localhost” then database-name, username and password. If the DB is hosted on a remote server [remote DB server], change the host to that server's hostname. Also the connectivity from this machine to DB machine should be enabled.
CAN node JS connect to MySQL?If you want to connect to a MySQL database using JavaScript, you can use Node. js and a library called mysql.
|