How can I establish a connection to a MySQL database using mysql2 in Node.js?

Question

Grade: Education Subject: Support
How can I establish a connection to a MySQL database using mysql2 in Node.js?
Asked by:
77 Viewed 77 Answers

Answer (77)

Best Answer
(176)
You can establish a connection using the `mysql2.createConnection()` function, passing an object with your database credentials like `host`, `user`, `password`, and `database`.