MySQL
Plugin
MySQL is a NodeJS plugin. You must download NodeJS recommend version fron official site (https://nodejs.org/en/) and install it with
npm install mysql
OK. We installed MySQL. In our index.js write this:
<syntaxhighlight lang="javascript">
var mysql = require('mysql');
</syntaxhighlight>
So. Functions for working with MySQL:
createConnection
Create information for connection but do not open it. <syntaxhighlight lang="javascript"> var mysqlc = mysql.createConnection({
host:'127.0.0.1',// host of server user:'kostya_nad',// MySQL user password:'MyStrongPassword',// MySQL password database:'gmgta'// MySQL database
}); </syntaxhighlight>
connect
Open connection to database <syntaxhighlight lang="javascript"> mysqlc.connect(function(e) { if(e) { console.log("Error connecting to the database with error "+e); } else { console.log('Database connected!') } }); </syntaxhighlight>
end
Close connection to database
<syntaxhighlight lang="javascript"> mysqlc.end(); </syntaxhighlight>
query
Send a MySQL query to database <syntaxhighlight lang="javascript"> mysqlc.query("SELECT * FROM `users` WHERE `nickname`=", [], function(e, r) { if(e) { console.log('Error on connection ... '); throw e; } else { console.log('Password is '+r[0].passcode); } }); </syntaxhighlight>