{"id":595,"date":"2015-04-03T17:27:51","date_gmt":"2015-04-03T17:27:51","guid":{"rendered":"http:\/\/www.eddiedillon.info\/?p=595"},"modified":"2015-04-03T17:27:51","modified_gmt":"2015-04-03T17:27:51","slug":"correct-way-to-connect-to-mysql-from-node-js-at-least-for-me","status":"publish","type":"post","link":"https:\/\/www.eddiedillon.info\/?p=595","title":{"rendered":"Correct way to connect to mySQL from Node.js (at least for me)"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/www.eddiedillon.info\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushJScript.js\"><\/script>\n<p>Here is the code I used to connect to mySQL from node.js using this library:\u00a0https:\/\/github.com\/felixge\/node-mysql\/<\/p>\n<p>I know he constantly refers to<\/p>\n<p>connection.connect();<br \/>\nand<br \/>\nconnection.end();<\/p>\n<p>But I just simply didn&#8217;t need those, I do use RedHat openshift for my project so your mileage may vary.<\/p>\n<p><pre class=\"brush: js\">var express = require(&#039;express&#039;);\r\nvar mysql   = require(&#039;mysql&#039;);\r\n\r\nvar connection = mysql.createConnection({\r\n  host     : &#039;MYSQL_DB_HOST&#039;,\r\n  user     : &#039;MYSQL_DB_USERNAME&#039;,\r\n  password : &#039;MYSQL_DB_PASSWORD&#039;,\r\n  database : &#039;dbName&#039;\r\n});\r\n\r\n\/\/then down in your app\r\n\r\napp = express.createServer();\r\napp.get(&#039;\/login&#039;, function(req, res) {\r\n            connection.query(&#039;SELECT * FROM `Users`&#039;, function (error, results, fields) {\r\n                  res.send(&#039;Results: &#039; + results.length);\r\n            });\r\n        });<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is the code I used to connect to mySQL from node.js using this library:\u00a0https:\/\/github.com\/felixge\/node-mysql\/ I know he constantly refers to connection.connect(); and connection.end(); But I just simply didn&#8217;t need those, I do use RedHat openshift for my project so your mileage may vary. var express = require(&#039;express&#039;); var mysql = require(&#039;mysql&#039;); var connection = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=\/wp\/v2\/posts\/595"}],"collection":[{"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=595"}],"version-history":[{"count":2,"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=\/wp\/v2\/posts\/595\/revisions"}],"predecessor-version":[{"id":597,"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=\/wp\/v2\/posts\/595\/revisions\/597"}],"wp:attachment":[{"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.eddiedillon.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}