nodejs mysql set query timeout
To set a query timeout in Node.js with MySQL, you can follow the steps below:
First, establish a connection to the MySQL database using the
mysql
module in Node.js.Once the connection is established, you can execute queries using the
connection.query()
method provided by themysql
module.To set the query timeout, you need to enable the
timeout
option for the MySQL connection. You can do this by setting theconnectTimeout
property in thecreateConnection()
method. For example:
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'your_host',
user: 'your_user',
password: 'your_password',
database: 'your_database',
connectTimeout: 10000 // timeout in milliseconds
});
In the above example, the connectTimeout
is set to 10000 milliseconds, which means the connection will timeout if it takes more than 10 seconds to establish.
- After setting the connection timeout, you can execute your queries using
connection.query()
as usual. If a query takes longer than the timeout period specified, an error will be thrown.
That's it! By following these steps, you can set a query timeout in Node.js using the mysql
module. Remember to adjust the timeout value according to your specific needs.