Node接入Mysql步骤

1、本地下载mysql服务
2、用GUI 客户端(Workbench、navicat)创建数据库,建表。
3、node 里面需要用一个驱动mysqp2连接数据库。

  1. // get the client
  2. const mysql = require('mysql2');
  3. // create the connection to database
  4. const connection = mysql.createConnection({
  5. host: 'localhost',
  6. user: 'root',
  7. database: 'test'
  8. });
  9. // simple query
  10. connection.query(
  11. 'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  12. function(err, results, fields) {
  13. console.log(results); // results contains rows returned by server
  14. console.log(fields); // fields contains extra meta data about results, if available
  15. }
  16. );
  17. // with placeholder
  18. connection.query(
  19. 'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  20. ['Page', 45],
  21. function(err, results) {
  22. console.log(results);
  23. }
  24. )