node.js(koa2)+MySQL简易入门

安装Mysql

具体的安装MySQL省略。。。node

  1. 配置密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password'; 
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your password';
复制代码

使用mysql模块或者node.js的orm时须要在mysql命令行中配置一下密码,才能够正常启动node.js链接mysql

配置MySQL相关

1.安装mysql引擎sql

npm install mysql --save
复制代码

mysql模块是node.js操做MySQL的驱动(引擎),能够在node.js环境下对MySQL数据库进行建表,增、删、改、查等操做。数据库

2.建立MySQL数据库npm

CREATE DATABASE login_test DEFAULT CHARSET utf8;
复制代码

使用node.js链接MySQL

  1. 引入mysql模块
const mysql = require('mysql');
复制代码
  1. 建立connection实例

使用createConnection方法,该方法接受一个OBject参数,提供链接数据库的主机,用户名,密码,数据库名,建立一个connection实例链接对象。数组

const connection = mysql.createConnection({
 host: 'localhost',
 user: 'root',
 password: 'password',
 database: 'login_test'
});
复制代码
  1. 调用链接方法,链接数据库

调用connection中的connect方法链接数据库,该方法接受一个含有err的函数,提供了错误处理办法。bash

connection.connect(function (err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});

复制代码
  1. 关闭数据库链接 提供了enddestroy两种方法,区别是destroy会立刻中止数据库链接,而end方法会在处理完数据库请求后中止链接。
connection.end(function (err) {
  if (err) {
    return console.log('error:' + err.message);
  }
  console.log('Close the database connection.');
});
复制代码

完整的数据库链接函数

const mysql = require('mysql');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'login_test'
});

connection.connect(function (err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});

connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

connection.end(function (err) {
  if (err) {
    return console.log('error:' + err.message);
  }
  console.log('Close the database connection.');
});

复制代码

MySQL链接池

  1. 建立链接池

经过connectioncreatePool方法能够建立链接池。oop

const pool = mysql.createPool({
   connectionLimit: 10,
   host: 'localhost',
   user: 'root',
   password: 'password', 
   database: 'login_test'
});
复制代码

这样将建立一个具备10个链接的链接池。须要注意的是,链接池的建立是惰性的,若是仅仅使用2个链接,将仅创立2个链接。ui

2.使用链接池

首先,链接池是这样的调用顺序pool.getConnection() -> connection.query() -> connection.release()

pool.getConnection(function (err, connection) {
 if (err) throw err; // not connected!
 // Use the connection
 connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
   // When done with the connection, release it.
   connection.release();
   // Handle error after the release.
   if (error) throw error;
   console.log('The solution is: ', results[0].solution);
   // Don't use the connection here, it has been returned to the pool. }); }); 复制代码

使用完链接后,能够经过connection.release方法释放链接,能够被其余请求使用。

使用connection.destroy方法,能够摧毁当前链接,链接池能够在须要时,建立一个新的链接。

使用end方法能够关闭链接池的全部链接

pool.end(function (err) {
   // all connections in the pool have ended
 });
复制代码

须要注意的是,执行pool.end会调用每个链接的connction.end方法,已经在event loop执行栈中的任务会继续执行,未进入的将不会执行。

完整例子

const mysql = require('mysql');

const pool = mysql.createPool({
 connectionLimit: 10,
 host: 'localhost',
 user: 'root',
 password: 'password',
 database: 'login_test'
});

pool.getConnection(function (err, connection) {
 if (err) throw err; // not connected!
 // Use the connection
 connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
   // When done with the connection, release it.
   connection.release();
   // Handle error after the release.
   if (error) throw error;
   console.log('The solution is: ', results[0].solution);
   // Don't use the connection here, it has been returned to the pool. }); }); setTimeout(function () { pool.end(function (err) { // all connections in the pool have ended }); }, 1000) 复制代码

在MySQL中建立表

query方法

mysql模块提供了一个query方法,能够经过实例化后的Connection,Pool,PoolNamespace调用。query方法有两种使用方法:

  1. .query(sqlString, callback)
  2. .query(sqlString, values, callback)

values为sql语句中占位符的替换变量,须要以Array的形式提供,若是为多个变量则须要以Array中嵌套Array的方式使用。(在MySQL写入中有具体例子)

callback回调函数有三个参数:

error:错误信息

results:查询结果

fields:结果字段信息

使用mysql建立表

const mysql = require('mysql');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'login_test'
});

// connect to the MySQL server
connection.connect(function (err) {
  if (err) {
    return console.error('error: ' + err.message);
  }
});

let createTodos = `create table if not exists user_table(
                          id int primary key AUTO_INCREMENT,
                          name varchar(255) NOT NULL,
                          password varchar(255) NOT NULL,
                          email varchar(255) DEFAULT '',
                          create_time datetime NOT NULL,
                          update_time datetime DEFAULT NOW()
                      ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;`;
connection.query(createTodos, function (err, results, fields) {
  if (err) {
    console.log(err.message);
  }
});

connection.end(function (err) {
  if (err) {
    return console.log(err.message);
  }
});

复制代码

这里使用了一个简单的user表,若是实际项目中,应该将ProfileAuthorization分离

MySQL命令行中输入 use login_test;show tables;查看建好的数据表

在MySQL中写入数据

单行写入

const mysql = require('mysql');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'login_test'
});

const sql = `INSERT INTO user_table (name,password,create_time) 
             VALUES ('test','123','2019-08-25 00:00:00');
`

pool.getConnection(function (err, connection) {
  if (err) throw err;
  connection.query(sql, function (error, results, fields) {
    connection.release();
    if (error) throw error;
    console.log(results)
  });
});
复制代码

MySQL命令行查看结果select * from user_table;

使用占位符?动态写入

const mysql = require('mysql');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'login_test'
});

const values = ['test', '123', '2019-08-25 00:00:00']
const sql = `INSERT INTO user_table (name,password,create_time) 
             VALUES ( ? , ? , ? );
`

pool.getConnection(function (err, connection) {
  if (err) throw err;
  connection.query(sql, values, function (error, results, fields) {
    connection.release();
    if (error) throw error;
    console.log(results)
  });
});
复制代码

这里经过query语句的第二种使用方法(三个参数)来实现动态生成SQL语句而后插入。

一次插入多行

const mysql = require('mysql');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'login_test'
});

const values = [[
  ['test1', '1', '2019-08-25 00:00:00'],
  ['test2', '2', '2019-08-25 00:00:00'],
  ['test3', '3', '2019-08-25 00:00:00']
]]
const sql = `INSERT INTO user_table (name,password,create_time) 
             VALUES ?
`;

pool.getConnection(function (err, connection) {
  if (err) throw err;
  connection.query(sql, values, function (error, results, fields) {
    connection.release();
    if (error) throw error;
    console.log(results)
  });
});
复制代码

将SQL语句中(?,?,?)换成?values换成三层嵌套的数组形式。

MySQL查询

简单查询

const mysql = require('mysql');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'login_test'
});


const sql = `SELECT * from user_table`;

pool.getConnection(function (err, connection) {
  if (err) throw err;
  connection.query(sql, function (error, results, fields) {
    connection.release();
    if (error) throw error;
    console.log(results)
  });
});
复制代码

传值查询

const mysql = require('mysql');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'login_test'
});

const values = `test`;
const sql = `SELECT * from user_table WHERE name=?`;

pool.getConnection(function (err, connection) {
  if (err) throw err;
  connection.query(sql, values, function (error, results, fields) {
    connection.release();
    if (error) throw error;
    console.log(results)
  });
});
复制代码

注意:这个?占位符号的写法与escape方法执行逻辑是一致的,一样能够抵御简单SQL注入。

MySQL更新

const mysql = require('mysql');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'login_test'
});

const values = ['test_pass', 'test1'];
const sql = `UPDATE user_table SET password=?  WHERE name=?`;

pool.getConnection(function (err, connection) {
  if (err) throw err;
  connection.query(sql, values, function (error, results, fields) {
    connection.release();
    if (error) throw error;
    console.log(results)
  });
});
复制代码

依然使用?占位符还有value的Array参数。

MySQL删除数据

删除单个查询结果

const mysql = require('mysql');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'login_test'
});

const values = ['test', '4'];
const sql = `DELETE from user_table WHERE name=? AND id=?`;

pool.getConnection(function (err, connection) {
  if (err) throw err;
  connection.query(sql, values, function (error, results, fields) {
    connection.release();
    if (error) throw error;
    console.log(results)
  });
});
复制代码
相关文章
相关标签/搜索