nodejs链接mysql 中的问题

首先你得对mysql ,有个大概的认识。

  比如说:如何安装,使用基本的语法,测试安装是否能成功,以及成功之后简单的对于数据库的,操作(增删改查)。。。

下面是业务场景:在爬虫过程中,租后需要将信息输出到mysql ,。

遇到的问题就是报错,说我语法错误(1)

之前不是报这个错,之前是什么COM_QUIT(2)的错误,为什么呢,因为我在函数中去调用了mysql ,(

connection.connect();
connection.end();

)后面搜索谷歌,有人说可以把这个两个删除,造成(2)报错原因是因为每次都都去调用,我重新看了一下语法。。发现有个连接池的东西。后面删了就出现了你现在所看到的1错误。

 那么:

这个模块不是一个接一个地创建和管理连接,而是使用mysql.createPool(config)提供内置的连接池。
可以集中连接以简化共享单个连接或管理多个连接。
当你完成一个连接时,只需调用connection.release(),连接将返回到池中,准备再次被其他使用。
var mysql = require('mysql');
var pool  = mysql.createPool({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret',
  database : 'my_db'
});

pool.getConnection(function(err, connection) {
  // Use the connection
  connection.query('SELECT something FROM sometable', function (error, results, fields) {
    // And done with the connection.
    connection.release();

    // Handle error after the release.
    if (error) throw error;

    // Don't use the connection here, it has been returned to the pool.
  });
});

特别要注意语法。

 
 
原文地址:https://www.cnblogs.com/zzty/p/8093059.html