{"id":5761,"date":"2017-06-15T00:07:36","date_gmt":"2017-06-15T07:07:36","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=5761"},"modified":"2024-01-03T17:41:04","modified_gmt":"2024-01-04T00:41:04","slug":"connect","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/","title":{"rendered":"Connecting to the MySQL Server from Node.js"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to connect to the MySQL Server from a Node.js application.<\/p>\n\n\n\n<p class=\"note\">Note that this tutorial targets the MySQL 8.0 and Node.js v20.6.0 or later. Node.js v20.6.0 and newer offers built-in support for <code>.env<\/code> configuration files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Node.js driver for MySQL<\/h2>\n\n\n\n<p>First, open the Command Prompt on Windows or Terminal on Unix-like systems.<\/p>\n\n\n\n<p>Second, create a directory for storing the <code>Node.js<\/code> app and use the <code>npm init<\/code> command to create the <code>package.json<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">npm init --yes<\/code><\/span><\/pre>\n\n\n<p>Third, install the MySQL package using the following <code>npm<\/code> command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">npm install mysql<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Creating a sample database<\/h2>\n\n\n\n<p>First, connect to the MySQL server:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">mysql -h localhost -u root -p<\/code><\/span><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-database\/\">create a new database<\/a> called <code>todoapp<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> todoapp;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Creating configuration file .env<\/h2>\n\n\n\n<p>First, create a new file called <code>.env<\/code> in the project directory.<\/p>\n\n\n\n<p>Second, add the MySQL connection&#8217;s parameters to the <code>.env<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">DB_HOST=localhost\nDB_PORT=3306\nDB_USER=root\nDB_PASSWORD=\nDB_NAME=todoapp<\/code><\/span><\/pre>\n\n\n<p>You should replace the <code>DB_HOST<\/code>, <code>DB_PORT<\/code>, <code>DB_USER<\/code>, <code>DB_NAME<\/code>, and <code>DB_PASSWORD<\/code> with the actual ones.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connecting to MySQL Server from Node.js<\/h2>\n\n\n\n<p>First, create a <code>connect.js<\/code> file in the project&#8217;s directory.<\/p>\n\n\n\n<p>Next, import the <code>mysql<\/code> module in the <code>connect.js<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> mysql = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'mysql'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, create a connection to the MySQL server by calling the <code>createConnection()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> connection = mysql.createConnection({\n  <span class=\"hljs-attr\">host<\/span>: process.env.DB_HOST,\n  <span class=\"hljs-attr\">port<\/span>: process.env.DB_PORT,\n  <span class=\"hljs-attr\">user<\/span>: process.env.DB_USER,\n  <span class=\"hljs-attr\">password<\/span>: process.env.DB_PASSWORD,\n  <span class=\"hljs-attr\">database<\/span>: process.env.DB_NAME,\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that we access the MySQL parameters from the <code>.env<\/code> file via the <code>process.env<\/code>. This feature has been available since Node.js v20.6.0.<\/p>\n\n\n\n<p>After that, call the <code>connect()<\/code> method on&nbsp;the <code>connection<\/code> object to connect to the MySQL server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">connection.connect(<span class=\"hljs-function\">(<span class=\"hljs-params\">err<\/span>) =&gt;<\/span> {\r\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\r\n\r\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Connected to the MySQL server.'<\/span>);\r\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>connect()<\/code> method accepts a callback function that has the <code>err<\/code> argument that provides detailed information if any error occurs.<\/p>\n\n\n\n<p>Here&#8217;s the complete <code>connect.js<\/code> program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> mysql = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'mysql'<\/span>);\n\n<span class=\"hljs-keyword\">let<\/span> connection = mysql.createConnection({\n  <span class=\"hljs-attr\">host<\/span>: process.env.DB_HOST,\n  <span class=\"hljs-attr\">port<\/span>: process.env.DB_PORT,\n  <span class=\"hljs-attr\">user<\/span>: process.env.DB_USER,\n  <span class=\"hljs-attr\">password<\/span>: process.env.DB_PASSWORD,\n  <span class=\"hljs-attr\">database<\/span>: process.env.DB_NAME,\n});\n\nconnection.connect(<span class=\"hljs-function\">(<span class=\"hljs-params\">err<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Connected to the MySQL server.'<\/span>);\n});\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, run the <code>connect.js<\/code> program that uses MySQL&#8217;s parameters from the .env file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"> node --env-file .env connect.js<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Connected to the MySQL server.<\/code><\/span><\/pre>\n\n\n<p>To make it more convenient, you can change the start property of the package.json file to the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">...\n  <span class=\"hljs-string\">\"scripts\"<\/span>: {\n    <span class=\"hljs-string\">\"start\"<\/span>: <span class=\"hljs-string\">\" node --env-file .env connect.js\"<\/span>\n  },\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, use the npm start command to run the connect.js with information from the .env file:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">npm start<\/code><\/span><\/pre>\n\n\n<p>The output indicates that we have successfully connected to the MySQL server from the Node.js program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<p>If you connect to MySQL 8.0 or later, you are likely getting the following error message:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTTP\" data-shcb-language-slug=\"http\"><span><code class=\"hljs language-http\"><span class=\"hljs-attribute\">error<\/span>: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTTP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">http<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In MySQL 8.0, the default authentication plugin is <code>caching_sha2_password<\/code>, unlike MySQL 5.7, which uses the <code>mysql_native_password<\/code> plugin,  supported by most clients. <\/p>\n\n\n\n<p>Therefore, if you encounter compatibility issues, you must explicitly enable <code>mysql_native_password<\/code> for a given user using the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">ALTER USER <span class=\"hljs-string\">'user'<\/span> \nIDENTIFIED WITH mysql_native_password BY <span class=\"hljs-string\">'password'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Replace the user and password with the ones that you use to connect to MySQL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Closing database connection<\/h2>\n\n\n\n<p>To close a database connection gracefully, you call the <code>end()<\/code> method on the <code>connection<\/code> object.<\/p>\n\n\n\n<p>The <code>end()<\/code> method ensures that all remaining queries will be executed before the database connection is closed.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">connection.end(<span class=\"hljs-function\">(<span class=\"hljs-params\">err<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Close the database connection.'<\/span>);\n});\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To force the connection to close immediately, you can use the <code>destroy()<\/code> method. The <code>destroy()<\/code> method guarantees that no more callbacks or events will be triggered for the connection.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">connection.destroy();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the <code>destroy()<\/code> method does not take any callback argument like the <code>end()<\/code> method.<\/p>\n\n\n\n<p>The following shows the complete <code>connect.js<\/code> program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">let<\/span> mysql = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'mysql'<\/span>);\n\n<span class=\"hljs-keyword\">let<\/span> connection = mysql.createConnection({\n  <span class=\"hljs-attr\">host<\/span>: process.env.DB_HOST,\n  <span class=\"hljs-attr\">port<\/span>: process.env.DB_PORT,\n  <span class=\"hljs-attr\">user<\/span>: process.env.DB_USER,\n  <span class=\"hljs-attr\">password<\/span>: process.env.DB_PASSWORD,\n  <span class=\"hljs-attr\">database<\/span>: process.env.DB_NAME,\n});\n\nconnection.connect(<span class=\"hljs-function\">(<span class=\"hljs-params\">err<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Connected to the MySQL server.'<\/span>);\n});\n\nconnection.end(<span class=\"hljs-function\">(<span class=\"hljs-params\">err<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (err) <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-built_in\">console<\/span>.error(err.message);\n\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Close the database connection.'<\/span>);\n});\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Connect to a MySQL database from a Node.js application.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5761\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/\"\n\t\t\t\tdata-post-title=\"Connecting to the MySQL Server from Node.js\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5761\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/\"\n\t\t\t\tdata-post-title=\"Connecting to the MySQL Server from Node.js\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to connect to the MySQL database server from a node.js application using the mysql module API.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":5759,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5761","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Connecting to the MySQL Server from Node.js<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to connect to the MySQL database server from a node.js application using the mysql module API.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Connecting to the MySQL Server from Node.js\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to connect to the MySQL database server from a node.js application using the mysql module API.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-04T00:41:04+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/connect\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/connect\\\/\",\"name\":\"Connecting to the MySQL Server from Node.js\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"datePublished\":\"2017-06-15T07:07:36+00:00\",\"dateModified\":\"2024-01-04T00:41:04+00:00\",\"description\":\"This tutorial shows you how to connect to the MySQL database server from a node.js application using the mysql module API.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/connect\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/connect\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/connect\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Node.js\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-nodejs\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Connecting to the MySQL Server from Node.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Connecting to the MySQL Server from Node.js","description":"This tutorial shows you how to connect to the MySQL database server from a node.js application using the mysql module API.","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:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/","og_locale":"en_US","og_type":"article","og_title":"Connecting to the MySQL Server from Node.js","og_description":"This tutorial shows you how to connect to the MySQL database server from a node.js application using the mysql module API.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-04T00:41:04+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/","url":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/","name":"Connecting to the MySQL Server from Node.js","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"datePublished":"2017-06-15T07:07:36+00:00","dateModified":"2024-01-04T00:41:04+00:00","description":"This tutorial shows you how to connect to the MySQL database server from a node.js application using the mysql module API.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/connect\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Node.js","item":"https:\/\/www.mysqltutorial.org\/mysql-nodejs\/"},{"@type":"ListItem","position":3,"name":"Connecting to the MySQL Server from Node.js"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=5761"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5761\/revisions"}],"predecessor-version":[{"id":14083,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5761\/revisions\/14083"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5759"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=5761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}