Node. js mysql package instructions, node. jsmysql
Author: zhanhailiang Date: 2014.11.16
This article explains how to rely on the mysql package on the node. js platform to access mysql.
1. installation:
[root@~/wade/nodejs/nodebeginner/mysql_test]# npm install mysql
2. mysql package-based connection and query:
[Root @~ /Wade/nodejs/nodebeginner/mysql_test] # cat mysqltest. js var 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, so you don't have to worry about making connection close when the query is asynchronous.
3. Implementation steps:
It is no different from other languages. First, configure parameters to describe how to connect to the database to be connected, which account to connect to, and then execute corresponding SQL operations after the connection is successful. 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