{"id":930,"date":"2022-05-04T11:01:16","date_gmt":"2022-05-04T11:01:16","guid":{"rendered":"https:\/\/www.workfall.com\/learning\/blog\/?p=930"},"modified":"2025-10-06T07:31:53","modified_gmt":"2025-10-06T07:31:53","slug":"how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js","status":"publish","type":"post","link":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/","title":{"rendered":"How to connect to MySQL database and run CRUD operations in Node.js?"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<p><img src=\"https:\/\/lh6.googleusercontent.com\/Hz47JPrGcNsT507pC9lZucYed3ypIV2qeANnH7mkrK6e7Bz_TqL2YUOiTKrtTuqXyRGW7dBaX7G4yX3lN5fPsP19h_vJ4arS7r0HvD3GFH5DPajuHsO0v1mLH9O7LaQYUbABABPHmuKYIIh2-w\" style=\"width: 1600px;\"><\/p>\n\n\n\n<p class=\"has-text-align-justify\">In this blog, we will have a look at getting started with the MySQL npm package which is a Node.js client for connecting to MySQL, written in JavaScript. We will see how to use the package to connect to a MySQL database and perform the CRUD operations. We will begin with creating a new directory onto the local machine followed by initializing a node.js app to be able to code the way to connect to the database. Navigating to the MySQL Workbench software, we will see how to create a new connection and create a new schema. We will then install the MySQL package and insert a code snippet to create a connection with the database. Then in the MySQL Workbench, creating a new SQL file, we will create a new table and insert some junk data in it. Moving back to the code editor, we will then write code for performing the CRUD operations and parallelly test out the results.<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>Note:<\/strong> Please ensure that Node.js and MySQL Workbench are properly installed onto the local machine or else it might display multiple errors while following the process.<\/p>\n\n\n\n<h2>Required installations<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/MXhHK5hWg9brMDeIgNksB4ADMe2GVK3MYW1NobmLzLPWZMPhT3bqpBH9jdQsOF6MKEulmhQtYvFk9IZJpZ17UnZ5bHdYvZ8WGO55nXZvR1xw2a58-po8ziMOBcSvwdBSbgLxTTz-F1T6v_u4YQ\" alt=\"\"\/><\/figure>\n\n\n\n<ul><li><strong>Node.js: <\/strong>It is a JavaScript runtime environment that executes JavaScript code outside the browsers.<\/li><li><strong><a href=\"https:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Mysql<\/a>: <\/strong>It is an npm package known to be a Node.js client for connecting to MySQL and performing the different operations. The package is written in JavaScript.<\/li><li><strong>MySQL Workbench: <\/strong>It is a visual database design tool used to create a connection with the SQL databases and manage the design and overall operations for the same.<\/li><\/ul>\n\n\n\n<h2>Hands-on<\/h2>\n\n\n\n<p>Create a new directory on your local machine.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/sVb9SygWcYkeIL6OBd7fGCMUjB2jKW0SdRwN8v_gJbqzR8h2wOvWGLKSPidzockzmwxczAfxNUyb8tTDOjfyeO49UeiFeuqTYvk3nIbGRoSMjhzzmuYaah3sXA4y4Qm9VhMBSxrfyHmCKlrI3w\" alt=\"\"\/><\/figure>\n\n\n\n<p>Open the newly created directory into a code editor.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/b39lq3DU2t0Ml52VcReDw44b3A6S0XCHKF6nfbx5Q_uo86CMx4VRnMRihSRxciP0kn2JUeKaDFvDOrKKO_3I-ZReCnZfIOH05YO5RgUMyxTVIpum1OpSuzvFagvb1UEtud9IzjlyUcUU6VmWiw\" alt=\"\"\/><\/figure>\n\n\n\n<p>Right-click in the left navigation pane and click on New File.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/p2o_Hs_67-VBuZI4ys14Xt0USMH1xa542PUli7CxaGDKHVoTRSGkNwoyB-g5IQPopkt14NtUrBnC6GJ0LBAEryRM-x4oHo7OrFoAiDAkmTUKkoFQ7aROmee06ZJRyEhKT1ZqID_YNN8W7pCABQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Create a new file with the name package.json.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/UqMaCKKBB5jf2kZ5ppQ9ztAYdxVNER6GjjGp7JPtABbMLxfWrFuZuTLYon6_7jAclJ-3ULdn5jjC4xzUxEGvE7wr9d7tuzbVVWQ86_sHu32SEOTNdZo7GM3GiDxysaIXpY84W120_K39LZ714g\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>Right-click in the newly created directory and click on Git Bash Here.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/-iASXnGWhurHYZ7WnPq6Br1MyQZu5CF68n_yEv34qJlywg3YutQQkUSkJCfaknU9hEYF32oB6SI_mKu4QolQdjeiIuxrEBqw5WVTFWhZ_laTZaKq9gIJHx-GoWSOF5bbTbgabUFcCyx8zEePhg\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>Run the command: <code>npm init<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/SK2UX2CIDlsTmfIOAWjHI4YFuWQs05UZVbMiS3E6vtN8muQrbW8dxkEQQRYcjKHokKmfyMOoJCw4g1nFp43Y1RhOZ78b5xN3Edt93M3potWKJEMwpZWk0sC2Ro3Ee6tC__yCz-Ns0MXxbqvnhw\" alt=\"\"\/><\/figure>\n\n\n\n<p>Press yes after selecting the default values.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/3lRgSjbvxxJboSSrUSLqV_RaTaVk1e21eWuq0eIIiWR5CDLkQqGprXr08ZDdfGE1QDz6x8FNisCmNjkM2VdwOJ_mA8Tk6RPKdeJlXXWGwo4vtDVmonbdu3lpTkkTlMgPrcF9Vw0NrAA24xnUgA\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>Verify the configuration setup in the package.json file created above.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/hLwfEGYqzeSa1VbdW_4NpRumsrPve0dJcKO2LyCn7Zgs3oPOW2p9a8KpNzrq3s0i_6GQiaFZNOvJhowON6wxE7xQ__HNYPOyhgAjiTzeJqWnWNrsryYjrs9cJNbuDsp_rkjJNiHNqsyPDbBtsg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now run the command: <code>npm install mysql<\/code><\/p>\n\n\n\n<p>A new package-lock.json file will be created in the directory.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/TsoU8_ZF2zqE4XUbwo9sal7FBGP1uTjIQrVWUDJH2rpR7_8r5e2_o7d3xVwdI2w_AqB0T0u0IaPkIg9_6aSTQVVMLbiVpl_Xch5i71zpYx5z5kj-rfybcmFuAPbE3tRs2_G-fKb7cjo2Emapaw\" alt=\"\"\/><\/figure>\n\n\n\n<p>Verify the package installation by navigating to the package.json file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/dfhDWsxbGv9GTPSjXdgJ32m2NbJV8lmd3puKOycIFzXNYvkRozaBgbt5GkHkEA3Z0LVoilihQZKKAUpLuceFszBSxYN81MVYCHQRE2wAhYiWo1IOr_u6VfntpRLkalVx1EfmWHGWlVouyt9bbA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Create a new file with the name app.js.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/wmF6cMfheuB2rELM7UW0SdiEOGTdQRJxDrHrZaIv2lrbSRgMt9FvHNgxCTuqh2JS_F9FFqWWEujwPJokBH1foZasBoNykownYFE939BfVYY-65-8gMry74KDoDr89yQfzyM7MOEMtooLs-ObaA\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>Now, open MySQL Workbench software. Click on the plus sign to create a new connection.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/ukUPxvdW2Tdy46t7BGqwAQDqxzaPpWAnR_7Y82IEu9w7oi_CfX77HDKkPuvR_ipLUcV9FFPmCta8ksrTZRurZQYj-8BW2PR4eyIqhBZazBLMbaNXCsFodzfxEVbv8LheRu6yBl1qY6IiWQBd1A\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>Enter in the required configuration and store a password if needed. Click on Ok once done.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/iLdIzO4ZvImca4bWzLML3EExMBXk4gLd66oWyxDcbvWy69H-qQetacyoEsPNpB31HJK8122ZaPP1JmG9W7KI29R7a8sR3N_xx9SniViYQLuxervPG8MVkJjO2XCm6WZzyz9-ktdIzAmNtD9TEg\" alt=\"\"\/><\/figure>\n\n\n\n<p>On the top nav bar, you will see the option to create a new schema in the connected server. Click on it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/epeM30nNQXy48_om-c8MLP8yWaoA4EhnGz5qp_J5cAfXj5ZAS67lVCaDDjjwd7aketQk5qnMbZpVL8Y6OAefElf4jpceASA6d9IAWX7rgCMTmX3V4Jk4luAifaKtfcOXjY1uz1fc1bACmr1CXw\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>Enter a name for the schema and click on Apply at the bottom.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/-2x3qI_aMOB9_PwzAmOqIhmMbph_WtZSLzQo2acIE0sD9Vx4ewkDAsZdwwLPMSUv-MPf3pg6-_4s_pvrAeUVUPIfX31RvHieuReZU29rFDp61sEFtcN3azVqH4YYk1_75k412GNgg3K4YFuwFA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Verify the command and click on Apply.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/Q3ybSnv_C3IEjN4Wigyyg2rGaqCqjxIninYrhwM4ES3VWJgTVQrsfJqQzaDIzZDFPEZKtO88Y6NYlGkEeJI_1VWlOHswz1rUItCO8faGhaJjV2_zfK3kjJkE7R0YEBF8IcQW42ZCzKqRbT4j3A\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, you will see the screen as shown in the image below. Click on Finish.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/5BbRhDsr4k2nS5sJLfM-E3kFeGcx7p5PLZwAmYS0tBJqg9EZaGnSzv8OME0Ja-3oamA15x24VjoJtDyne2jBHK3cgLrEHIb52UKWjqjROrVn2j8OV5NVTl4006gLf0uo-M5ulwqPeW_d6hMNkA\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>Verify the creation of the schema in the left navigation pane.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/-nAsR_HVF5YF9rjMoNpRmBAKeXACeEzpM1U__RTIOS2OqnBWNeQQjRXyIWl9ldJtI8nJpistSkVJNvVnJT-TGtJskzmyiOw2_u_R2TF5EKrqvwOsINPqGQHIjPt0aI_YMXEywb9wJcYajSmS5w\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, navigate to the code editor and add the following code in the app.js file to create a connection with the db.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/U5BbkYJO4qHOZf-HXuFo6AQjJmw770aTtZuP08Rb8lD2TybJai9RnxRzzuoieLcl9Csg9rUXbZ5jGRirSXTJoFMLGzSbWP_Hsrlt_8Z-ZR6Vbw-KVu-sDFGJyc82sAIHFzlPNF1f7FbePpPOog\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>Run the command:<\/p>\n\n\n\n<p><code>node app.js<\/code><\/p>\n\n\n\n<p>On success, it will display the connected messages.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/wu3RaunJnrjgE2lDEWnjfZWBjxfcCpZk3-Cl11iLWfHDcASpn96haM04lBs3Yrd11P962zoAgO7npgPK9DWIUl_ElVqMZjBiRjh-2uYIG_xuEru01jdYnEWHgrUjcBwFvA5lgCs5dZiagTszJg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, modifying the connection creation process, alter the code with the below code.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/BfbuPpquEwY3Xw5qpogPc4Q2aroodz-gJHQtPdZ2mB1Gjqr8tuHtddWhJWQOoKr9_he3FuPH-Kgwr4ZRwKDwy6xe1duPT7iiMwfLHB_-ONaIv_lm3VUbwtbNkL314ZTXmJ6-FqwVLqjoZZpYog\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run the command:<\/p>\n\n\n\n<p><code>node app.js<\/code><\/p>\n\n\n\n<p>Check if the connection was successful or not.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/aUjdBFXq0vKImVdtEII29pKLoT6iBVhWC8KBZ2q_WcK0tH5Cp9dcS2rwVHGoS38R7N32ID2DMFZjMvjwZ8Klap62Y4d3C6ewhT5NkXUPpAN6j9i8oJ1-fftg_jxrxLP3B092UKc0vevA-g5pyg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, navigate back to the MySQL Workbench and create a new SQL file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/CLAsGi7shWgML4fQu6OUeTvCJGSBEOrZB77FWcUNNL8jOq1eedTrBbNPzPo59bScafpETgd-WNz1oSyYF1dIDRSLwG-NiVQUyDvVve0sdyPt-Sqde9BUqSKmJz6QlG5svc-eoQDmihQogUMtBA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Add the table creation query as shown below in the file and execute the query.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/beBqSp6sG1eeA3-wTFR8-7F2_pz0F2aMgXexbyyJBSs7IK-y2xFCeuAWV-D2Cp5SQr0CAflJRdQalLUQ629fP5HZQfO-y_MPWwwMmXpgxlO6pWcC6NZc5l5JEmglvNYMRCYTMs94eOiOnLJWag\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>On success, you will see the newly created table under the schema.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/AUrfW92r4adszVos9bAzqW5oThlGlNu3VH2696OrkxyNh9X4ck7ZwOByq7IaiBME-yCsQB86VY2ql43lgJdf-5bfx4vKihPXsUmPY74zRfPG3rfG8iS1B28BCTHdn1NVoilO6rx_TWvSmwwo3w\" alt=\"\"\/><\/figure>\n\n\n\n<p>Right-click on the schema and click on Set as Default Schema.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/qo4pPjG3O8_-8rJV2nAYtdIixVG6mb48HStXDTwUz-JjJ8oik3x5RLLL6gRuYtPIMUlmj4oTiSd1zBEaHESB6B73iAOEMYmYpw_PyX20RAWtQMKCamFvn5LURbpF6qMGp2VzD8HY-OWlidiLAA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, in the SQL file, add the following insert query to input some data in the table.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/NkHRYc-_ntJ4FxoVREL1SIi7EENbHuvz8Frv-uVfd0ESZB8nL3kOd32JbNT1gQpb4hPaUoGDYPjyQTlDUnjX3rZhMrOgJYu_OQkc7kuzCIGx0nPRrnoCDd6cFvUZBcaz1sD3HQ-U_SGyNyw70A\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run the select all query to verify the data creation.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/8f_72fFoYaUjAHlhWShAFVpFQq8eRFiK4qaUPLRnzdcTJhfJltJZQb6qo45__fyRU-4wCuZ4wW-GRz13glGaWpf-IifzXdV4snvPcHCehRAJEMYtsyrbu33XbbBAX_gnWlO0LaRfx8V_L0kF7g\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<h4><strong>SELECT Operation<\/strong><\/h4>\n\n\n\n<p>Navigate back to the code base and add the following Select Query code to fetch the dummy data.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/CViZJmb0zlf98xzvdSlDhNn9lgHegOMJZLrXKn4JwaUgIKFwbFnAWGHtg1GJrvAJUWCjOf-08ld7AhujqmkH9f5NkG5SdsaEa-IfrLcjKcnALDP0ozKo8cus6glOjtCMNgWB2ULptEmIe2VLGQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>The final code with the query looks as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/enFsJ41s3EyTvXV99MRe6V8_f0Qc4j-uVzZUCmL0rDgbR7goN1Vt5Rx3rb87bTrzDRpb6N4ozYYCf3s87Es3IkSKdeUqRgLZLASslNZz0mA9lYjHcZ9j2_tp3ZK3QsJqHPcqJrz7EE9lknfTkg\" alt=\"\"\/><\/figure>\n\n\n\n<p>Run the command:<\/p>\n\n\n\n<p><code>node app.js<\/code><\/p>\n\n\n\n<p>On success, you will see the data as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/2LeU-kZYbDBQHHQyUGPECJgJsVScVXyRC9ydP35K-0J4xIpur4lJoiHZtRmY9EQXa13vSYZEa5GWqWAXjQVnVxuD1DANsnb6h_wc3_30nCBv-sUmccUbaT32XZ0R3PuDpsuBlQEzQ9dJ1ONO1A\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<p>Use the below code to alter the output displaying it in a better way.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/X9TaNG3k3a1yrFZK-httIWi4J6KBBrjRiI0yN-rWdRjgphjZdDgNo2Pgt39g3OZaDDHq3F13zKDZTe_CO08zj4BFENz1HFzMwvHadCvj_w3zSZEgQ-m64VUUWA3UUUxy-fcmB2yZFVHXYJsuGw\" alt=\"\"\/><\/figure>\n\n\n\n<p>The final code looks as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/Q4uUxQSeD_A-X6ZMJufwEO9G6s10VTIHdwoT8T7NkGDL2rSzpfzYq-MPD61eiCbYOtu4YRTFlj-5SeaxaWprDTjZ_NFWoNmPghjNIXWPZHdyORZXqQg3JQUE_y2quk3feCJBlGM-ftZ6jBlneA\" alt=\"\"\/><\/figure>\n\n\n\n<p>On executing the above code, you will see the output as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/xVK1Ou866Q60vwRR5CdzhVvGNU9ibU0H90FYk15YZE4GAZh1GjHiOHJQlQckhgwqIEnQCP0rSqZU1281lI6rsrHEvpoZ4ccYQ83O4gUioOPm3smglSDArCs6NVZy6IMtD-Ag8tJZZL3H6LGTZw\" alt=\"\"\/><\/figure>\n\n\n\n<h4><strong>INSERT Operation<\/strong><\/h4>\n\n\n\n<p>Now, let\u2019s execute the Insert operation. Add the following code to the code base.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/Iy_8eTG7UPm2MB55YEJco5Uw6DcrVtgSK4TyB2hTnmVZoA9qox-GJmO8gwHDjCfoYwtR3pIMy0hetfXKAfJYYEJXnIDkLV9DAn0UIomGucI2TLxALH7diGf7FBmdBTBYSH9jNvXc4TG8M3Rqqw\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, you will see the ID of the newly created user as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/OWQq22q5_oiQ69kSZtGGf7fkG6GWHQDfYbKq4ssTt0reBzGm_dqZ5HGQh4NTSlt_3Wyic5enwSXNwKx4UZP6CMtNPodgsGIpICG074BiZlejD6YAX9sEzGNw--aW-_XwnF9bSQvINN5Y5CPUpQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>You can verify the new user creation by navigating to the MySQL Workbench software.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/mfNaiJL9Llthjelmx_0GpGpdXDH2K5vkVR-aLut18nYY2EwFemeS9D-oaPwFPtLKNO7WS3mb6rU3VOIrP7wE53c0ZKgypGOoP5a-f-pe_ffuuROM-q9UWAYQbLsOQz8kl_wHvRZGNrEXGty2uw\" alt=\"\"\/><\/figure>\n\n\n\n<h4><strong>UPDATE Operation<\/strong><\/h4>\n\n\n\n<p>Use the below code adding it to the code base to perform the update operation.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/ghSwjPfDVI0oGm_TkvnRVegCQVTOzG4s4kHX5z8_mfku02f1krej49iq07I4KzzbYPSARk_WCDK00ilJPjUx61XpUfk6xrh0W8faF7rhH6UkWQuewVr_tbT7rXQXUhKuoUO7oXLr1kDHTO2qSA\" alt=\"\"\/><\/figure>\n\n\n\n<p>Executing the code, on success, you will see the message as shown below which means that one row has been updated.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/bfArfghg8Jk8R8xjSc2EBb5ygIO3_ClJ5Fs8FykXXCJdWEaEYvt200TgZMuQ9tKS9xaNtz0Dm3z3lJ_IaDIDy9U8UHgSYP1-VUGzJFnEdUPbdlKxNcF1TmdtEEsLcNReRJj1UxBf3yVDyd-r6A\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now, we can use the select query code to identify the changes. Run the below code.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/Cp5WpQRN8SCqnExEi1ZD5NT5PbERFIiagNBwJOE7170idJTj3B5wC8bXDuZfbcQuMIZaIsu7hUb4qZ7eB1H8EMhRmTSeNTo8-L2ajDXbQggp6uVJnDccZK5QVjOrEaG6F_R7lKxspRkojbiO1Q\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, you will see the updated value in the output.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/XBxu5_d03RubQD06LpfFY83hS0xW4RI8OzXIbEcFXU3N2T9sQGlr55s2AEUmKOH9cTXhWLSkjGR09HhosRqalJUfqDrC8af-tcI90jfpK0iERTDOkZcN18h3Lo-qbD_tK4R4_BUh7YbE5PywoQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can verify the same by navigating to the MySQL Workbench. Run the below query and verify the change.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/ABvr1VF53YI4p_t_c7RqipdofzdGKRHNvNGBZdkX9IrSjGe-F6NcdEtwpJ1ZfDjViA1wRLiu9jB7fo1Gy6crogf1RCYrKTAelo65TJmqbL5VpMvvtxAd_TDDp9gFzAYVrw4BS6-gKpSLQiX3Yw\" alt=\"\"\/><\/figure>\n\n\n\n<h4><strong>DELETE Operation<\/strong><\/h4>\n\n\n\n<p>Now, use the below code to perform the Delete operation. We will delete the newly added entry.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh3.googleusercontent.com\/re1YXQFs_5zBnPCXmT0kWsbs2nD_RAV5Ut7Vk_wWUXCbup9VbbVVR_24V3HvtGgJzLxbwcrGBnYhcf3L5DcTacaZB6j7cZJ53fw6otHQ6I1DEcQS3eMxj_DFYn-qnH5N74H7-9vs4ZtOEhMZAQ\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, you will see the message as shown in the image below which stated that one row has been deleted.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh5.googleusercontent.com\/uaZS3sUdiBFYf_eDqRYLKoCHHUJVjx69kC73nfisu2yQvVJk4DKJbut_thH1MlTkrw-l3EUGGv0LEf8IiQbarcLfuHEgqDBXpvPrVnGlFlwLG0irqG7GJDeRRDjMQYRnLyNCp_nMCSPY7clRGw\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can verify the same using the Select operation code.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/Cp5WpQRN8SCqnExEi1ZD5NT5PbERFIiagNBwJOE7170idJTj3B5wC8bXDuZfbcQuMIZaIsu7hUb4qZ7eB1H8EMhRmTSeNTo8-L2ajDXbQggp6uVJnDccZK5QVjOrEaG6F_R7lKxspRkojbiO1Q\" alt=\"\"\/><\/figure>\n\n\n\n<p>On success, you will see the entry deleted.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh4.googleusercontent.com\/rprXmOqVQaSVyLUnzsjToerfcJR6LW7Ge8oaina2WmJ3rZdQ0FC7CPrv-7b9RmdmH-oWk4gcEHrdfXY9s7qQwgNTOCI3VFa2AuIDoDWkzcw888MCrreeEebmOZTJvrwV9dfthd23WXhK94q6Bw\" alt=\"\"\/><\/figure>\n\n\n\n<p>We can verify the same on MySQL Workbench. Run the below select query and verify the result.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/lh6.googleusercontent.com\/b14Rln4vy_IHwmc8YvVhwjLXxct5JvZ41EGadEN6i4P8bhBAdY3kEbMArPnpv_Po12X01gVHBo-uOADYRbghfwkpKDVVkyPCxJ3UA8XamS0ob_5EOGSzBmIr466HnF8roRClygg5CofTeoHvng\" alt=\"How to connect to MySQL database and run CRUD operations in Node.js?\"\/><\/figure>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p class=\"has-text-align-justify\">In this hands-on, we had a look at how to get started with the MySQL npm package which is a Node.js client for connecting to MySQL, written in JavaScript. We saw how to use the package to connect to a MySQL database and perform the CRUD operations. We began with creating a new directory onto the local machine followed by initializing a node.js app to be able to code the way to connect to the database. Navigating to the MySQL Workbench software, we saw how to create a new connection and create a new schema. We then installed the MySQL package and inserted a code snippet to create a connection with the database. Then in the MySQL Workbench, creating a new SQL file, we created a new table and inserted some junk data in it. Moving back to the code editor, we then coded the different ways for performing the CRUD operations and parallelly tested out the results. We will come up with more such use cases in our upcoming blogs. Stay tuned to keep getting all updates about our upcoming new blogs on AWS and relevant technologies.&nbsp;<\/p>\n\n\n\n<p>Meanwhile\u2026<\/p>\n\n\n\n<p><strong>Keep Exploring -&gt; Keep Learning -&gt; Keep Mastering<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At <a href=\"https:\/\/www.workfall.com\/\">Workfall<\/a>, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you\u2019re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at<a href=\"https:\/\/www.workfall.com\/partner\/\"> workfall.com\/partner<\/a> today.<\/p>\n\n\n\n<p><\/p>\n\n\n<style type=\"text\/css\"><\/style><section id='' \n                class='helpie-faq accordions faq-toggle open-first groupSettings-546__enabled' \n                data-collection='' \n                data-pagination='0' \n                data-search='0' \n                data-pagination-enabled='0'\n                role='region'\n                aria-label='FAQ Section'\n                aria-live='polite'><h3 class=\"collection-title\">Frequently Asked Questions:<\/h3><article class=\"accordion \"><div class='helpie-faq-row'><div class='helpie-faq-col helpie-faq-col-12' ><ul><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-3150\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-3150\"\r\n                data-id=\"post-3150\" \r\n                data-item=\"hfaq-post-3150\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. Which npm package is used for MySQL integration in Node.js?<\/div><\/div><div id=\"accordion-content-post-3150\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-3150\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0The <\/span><span style=\"font-weight: 400\">mysql2<\/span><span style=\"font-weight: 400\"> or <\/span><span style=\"font-weight: 400\">sequelize<\/span><span style=\"font-weight: 400\"> package is commonly used for connecting Node.js to MySQL databases.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-3151\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-3151\"\r\n                data-id=\"post-3151\" \r\n                data-item=\"hfaq-post-3151\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. How do you establish a database connection?<\/div><\/div><div id=\"accordion-content-post-3151\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-3151\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0You can use <\/span><span style=\"font-weight: 400\">mysql.createConnection()<\/span><span style=\"font-weight: 400\"> or a connection pool with proper host, user, password, and database credentials.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-3152\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-3152\"\r\n                data-id=\"post-3152\" \r\n                data-item=\"hfaq-post-3152\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. What are CRUD operations?<\/div><\/div><div id=\"accordion-content-post-3152\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-3152\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0CRUD stands for Create, Read, Update, and Delete \u2014 the four basic operations performed on a database table.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-3153\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-3153\"\r\n                data-id=\"post-3153\" \r\n                data-item=\"hfaq-post-3153\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. How do you handle SQL injection in Node.js?<\/div><\/div><div id=\"accordion-content-post-3153\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-3153\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0Always use parameterized queries or ORM methods to avoid injection vulnerabilities.<\/span><\/p>\n<\/div><\/li><li class=\"accordion__item \"><div class=\"accordion__header \" \r\n                id=\"accordion-header-post-3154\"\r\n                role=\"button\"\r\n                aria-expanded=\"false\"\r\n                aria-controls=\"accordion-content-post-3154\"\r\n                data-id=\"post-3154\" \r\n                data-item=\"hfaq-post-3154\" \r\n                style=\"background:transparent;\" \r\n                data-tags=\"\"\r\n                tabindex=\"0\"><div class=\"accordion__title\">Q. Can MySQL operations be asynchronous in Node.js?<\/div><\/div><div id=\"accordion-content-post-3154\" \r\n                class=\"accordion__body\" \r\n                role=\"region\"\r\n                aria-labelledby=\"accordion-header-post-3154\"\r\n                style=\"background:transparent;\"><p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span><span style=\"font-weight: 400\">\u00a0Yes, using async\/await or promises ensures non-blocking database interactions.<\/span><\/p>\n<\/div><\/li><\/ul><\/div><\/div><\/article><\/section>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">8<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span> In this blog, we will have a look at getting started with the MySQL npm package which is a Node.js client for connecting to MySQL, written in JavaScript. We will see how to use the package to connect to a MySQL database and perform the CRUD operations. We will begin with creating a new directory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":931,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[292],"tags":[27,244,50,265,250,6],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to connect to MySQL database and run CRUD operations in Node.js? - The Workfall Blog<\/title>\n<meta name=\"description\" content=\"In this blog, we will have a look at getting started with the MySQL npm package which is a Node.js client for connecting to MySQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to connect to MySQL database and run CRUD operations in Node.js? - The Workfall Blog\" \/>\n<meta property=\"og:description\" content=\"In this blog, we will have a look at getting started with the MySQL npm package which is a Node.js client for connecting to MySQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/\" \/>\n<meta property=\"og:site_name\" content=\"The Workfall Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/workfall\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-04T11:01:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-10-06T07:31:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/05\/Cover-Images_Part2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@workfall\" \/>\n<meta name=\"twitter:site\" content=\"@workfall\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Workfall\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\",\"name\":\"Workfall - Hire #Kickass Coders On Demand\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/workfall\/\",\"https:\/\/www.linkedin.com\/company\/workfall\/\",\"https:\/\/facebook.com\/workfall\",\"https:\/\/twitter.com\/workfall\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"contentUrl\":\"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400\",\"width\":400,\"height\":400,\"caption\":\"Workfall - Hire #Kickass Coders On Demand\"},\"image\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#website\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/\",\"name\":\"The Workfall Blog\",\"description\":\"#Tech #Remote #Jobs\",\"publisher\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learning.workfall.com\/learning\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#primaryimage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/05\/Cover-Images_Part2.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/05\/Cover-Images_Part2.png\",\"width\":1200,\"height\":628,\"caption\":\"MySQL Database in Node.js\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#webpage\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/\",\"name\":\"How to connect to MySQL database and run CRUD operations in Node.js? - The Workfall Blog\",\"isPartOf\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#primaryimage\"},\"datePublished\":\"2022-05-04T11:01:16+00:00\",\"dateModified\":\"2025-10-06T07:31:53+00:00\",\"description\":\"In this blog, we will have a look at getting started with the MySQL npm package which is a Node.js client for connecting to MySQL.\",\"breadcrumb\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/learning.workfall.com\/learning\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to connect to MySQL database and run CRUD operations in Node.js?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#webpage\"},\"author\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\"},\"headline\":\"How to connect to MySQL database and run CRUD operations in Node.js?\",\"datePublished\":\"2022-05-04T11:01:16+00:00\",\"dateModified\":\"2025-10-06T07:31:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#webpage\"},\"wordCount\":1181,\"publisher\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/05\/Cover-Images_Part2.png\",\"keywords\":[\"Database\",\"javaScript\",\"mysql\",\"node\",\"nodeJS\",\"workfall\"],\"articleSection\":[\"Data Engineering\"],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a\",\"name\":\"Workfall\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"contentUrl\":\"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png\",\"caption\":\"Workfall\"},\"sameAs\":[\"https:\/\/www.workfall.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to connect to MySQL database and run CRUD operations in Node.js? - The Workfall Blog","description":"In this blog, we will have a look at getting started with the MySQL npm package which is a Node.js client for connecting to MySQL.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/","og_locale":"en_US","og_type":"article","og_title":"How to connect to MySQL database and run CRUD operations in Node.js? - The Workfall Blog","og_description":"In this blog, we will have a look at getting started with the MySQL npm package which is a Node.js client for connecting to MySQL.","og_url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/","og_site_name":"The Workfall Blog","article_publisher":"https:\/\/facebook.com\/workfall","article_published_time":"2022-05-04T11:01:16+00:00","article_modified_time":"2025-10-06T07:31:53+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/05\/Cover-Images_Part2.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_creator":"@workfall","twitter_site":"@workfall","twitter_misc":{"Written by":"Workfall","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization","name":"Workfall - Hire #Kickass Coders On Demand","url":"https:\/\/learning.workfall.com\/learning\/blog\/","sameAs":["https:\/\/www.instagram.com\/workfall\/","https:\/\/www.linkedin.com\/company\/workfall\/","https:\/\/facebook.com\/workfall","https:\/\/twitter.com\/workfall"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","contentUrl":"https:\/\/i1.wp.com\/18.141.20.153\/learning\/blog\/wp-content\/uploads\/2021\/10\/cropped-WF_logo.png?fit=400%2C400","width":400,"height":400,"caption":"Workfall - Hire #Kickass Coders On Demand"},"image":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#website","url":"https:\/\/learning.workfall.com\/learning\/blog\/","name":"The Workfall Blog","description":"#Tech #Remote #Jobs","publisher":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learning.workfall.com\/learning\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#primaryimage","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/05\/Cover-Images_Part2.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/05\/Cover-Images_Part2.png","width":1200,"height":628,"caption":"MySQL Database in Node.js"},{"@type":"WebPage","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#webpage","url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/","name":"How to connect to MySQL database and run CRUD operations in Node.js? - The Workfall Blog","isPartOf":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#primaryimage"},"datePublished":"2022-05-04T11:01:16+00:00","dateModified":"2025-10-06T07:31:53+00:00","description":"In this blog, we will have a look at getting started with the MySQL npm package which is a Node.js client for connecting to MySQL.","breadcrumb":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learning.workfall.com\/learning\/blog\/"},{"@type":"ListItem","position":2,"name":"How to connect to MySQL database and run CRUD operations in Node.js?"}]},{"@type":"Article","@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#article","isPartOf":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#webpage"},"author":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a"},"headline":"How to connect to MySQL database and run CRUD operations in Node.js?","datePublished":"2022-05-04T11:01:16+00:00","dateModified":"2025-10-06T07:31:53+00:00","mainEntityOfPage":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#webpage"},"wordCount":1181,"publisher":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/#organization"},"image":{"@id":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-connect-to-mysql-database-and-run-crud-operations-in-node-js\/#primaryimage"},"thumbnailUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/05\/Cover-Images_Part2.png","keywords":["Database","javaScript","mysql","node","nodeJS","workfall"],"articleSection":["Data Engineering"],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/cab8236044692bc5b27606b13167794a","name":"Workfall","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learning.workfall.com\/learning\/blog\/#\/schema\/person\/image\/","url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","contentUrl":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/09\/avatar_user_1_1693914404-96x96.png","caption":"Workfall"},"sameAs":["https:\/\/www.workfall.com"]}]}},"jetpack_featured_media_url":"https:\/\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/05\/Cover-Images_Part2.png","jetpack-related-posts":[{"id":2453,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-create-rest-api-in-spring-boot-and-perform-crud-operations-with-mysql-database\/","url_meta":{"origin":930,"position":0},"title":"How to Create Rest API in Spring Boot and Perform CRUD Operations with MySQL Database?","date":"October 3, 2023","format":false,"excerpt":"In this blog, we will cover: What are CRUD Operations?What is Spring Boot?What is MySQL Database?What is REST APIHands-OnConclusion What are CRUD Operations? CRUD represents Create, Read\/Retrieve, Update, and Delete \u2013 fundamental actions on persistent storage, aligned with HTTP methods used in web development and database management: - POST: Establishes\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"Create Rest API in Spring Boot and Perform CRUD Operations with MySQL Database","src":"https:\/\/i2.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2023\/10\/Tech-Blogs-Cover-Images_Part3.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":221,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-migrate-rds-mysql-database-to-rds-postgresql-database-using-aws-database-migration-servicedms\/","url_meta":{"origin":930,"position":1},"title":"How to migrate RDS MySQL database to RDS PostgreSQL database using AWS Database Migration Service(DMS)?","date":"October 27, 2021","format":false,"excerpt":"Do you want to migrate data from one type of DB engine (MySQL) to another type of DB engine(PostgreSQL) with little or no downtime? In today's fast-paced environment, application, and database availability must be considered 24 hours a day, seven days a week. Some of your applications may be unable\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"AWS Database Migration Service","src":"https:\/\/i1.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/10\/DMS.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":785,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-perform-an-amazon-aurora-database-deployment-that-replicates-data-and-spans-across-multiple-aws-regions\/","url_meta":{"origin":930,"position":2},"title":"How to perform an Amazon Aurora Database deployment that replicates data and spans across multiple AWS regions?","date":"November 30, 2021","format":false,"excerpt":"Amazon Web Services provides us with the flexibility of managing the data in the databases with ease. The flexibility is thus provided by an AWS Service called Amazon Aurora. An Amazon Aurora cluster contains one or more DB instances and a cluster volume that helps us manage the data for\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"Amazon Aurora Database Deployment","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/11\/CoverImages_1200x628px-8.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1121,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-show-flash-messages-in-node-js-using-the-connect-flash-package\/","url_meta":{"origin":930,"position":3},"title":"How to Show Flash Messages in Node.js using the Connect-flash Package?","date":"August 9, 2022","format":false,"excerpt":"The Node.js Connect-flash module is extremely useful for developers whenever a flash message needs to be sent. When a user is redirected to a specific webpage, the connect-flash module in Node.js allows developers to display a pop-up message. For example, in your Node.js demo application, you might want to notify\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"Flash messages in Node.js","src":"https:\/\/i2.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/08\/Cover-Images_Part2.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":614,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-do-database-migration-using-aws-database-migration-service-dms-from-on-premise-ec2-to-rds\/","url_meta":{"origin":930,"position":4},"title":"AWS DMS: Migrating On-Premise\/EC2 Databases to RDS Made Simple","date":"November 11, 2021","format":false,"excerpt":"Do you want to migrate your on-premise databases to AWS with minimal or no downtime? In today\u2019s fast-paced world,\u00a024\/7 application and database availability are the basic criteria to consider. In this case, some of your applications may not be able to afford significant downtime while migrating on-premises databases. In this\u2026","rel":"","context":"In &quot;AWS Cloud Computing&quot;","img":{"alt_text":"Database Migration using AWS Database Migration Service (DMS)","src":"https:\/\/i0.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2021\/11\/DMS.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1187,"url":"https:\/\/learning.workfall.com\/learning\/blog\/how-to-upload-single-or-multiple-images-to-a-local-directory-with-multer-in-node-js\/","url_meta":{"origin":930,"position":5},"title":"How to upload single or multiple images to a local directory with Multer in Node.js?","date":"August 30, 2022","format":false,"excerpt":"The most fundamental operation of any application is a file upload. While images can be uploaded on the front end, they must be received via an API and database on the backend. You can set up file and image uploads in one place using Multer and Express, a Node.js framework.\u2026","rel":"","context":"In &quot;Backend Development&quot;","img":{"alt_text":"Multer in Node.js","src":"https:\/\/i2.wp.com\/learning.workfall.com\/learning\/blog\/wp-content\/uploads\/2022\/08\/Cover-Images_Part2-1-4.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/930"}],"collection":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/comments?post=930"}],"version-history":[{"count":4,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/930\/revisions"}],"predecessor-version":[{"id":3155,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/posts\/930\/revisions\/3155"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media\/931"}],"wp:attachment":[{"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/media?parent=930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/categories?post=930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learning.workfall.com\/learning\/blog\/wp-json\/wp\/v2\/tags?post=930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}