Zhanhailiang Date: 2014.11.16
This article explains how to rely on the MySQL package on the node. JS platform for MySQL access.
1. Installation:
[ root@ ~/ Wade/ nodejs/ nodebeginner/ mysql_test" # NPM Install MySQL
2. mysql-based package for MySQL connection and query:
[Root@~/Wade/Nodejs/Nodebeginner/mysql_test]# cat Mysqltest.jsvar mysql = require(' MySQL '); var connection = Mysql.createconnection({Host:' localhost ', User:' ****** ', Password:' ****** '}); Connection.connect(); Connection.query(' SELECT * from Test.test ', function(err, rows, fields) { if (Err) {throw err; }Console.log(rows); Console.log( Fields);}); Connection.end(); // register the end processor without worrying about query async connection Close
3. Implementation steps:
It is no different from other languages-first configure the parameters to connect the db to connect, how to connect, with which account connection, the connection is successful after the corresponding SQL operation, and finally close the connection.
4. Complete code:
Https://github.com/billfeller/nodebeginner/tree/master/mysql_test
5. Official documents:
Https://www.npmjs.org/package/mysql
node. js MySQL Package usage instructions