Error: connect ECONNREFUSED 127.0.0.1:3306(避坑!!)

Error: connect ECONNREFUSED 127.0.0.1:3306

今天做koa连接数据库配置时,出现 Error: connect ECONNREFUSED 127.0.0.1:3306

报错

 Error: connect ECONNREFUSED 127.0.0.1:3306
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
      --------------------
      at Protocol._enqueue (D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\node_modules\mysql\lib\protocol\Protocol.js:144:48)
      at Protocol.handshake (D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\node_modules\mysql\lib\protocol\Protocol.js:51:23)
      at PoolConnection.connect (D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\node_modules\mysql\lib\Connection.js:116:18)
      at Pool.getConnection (D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\node_modules\mysql\lib\Pool.js:48:16)
      at Pool.query (D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\node_modules\mysql\lib\Pool.js:202:8)
      at D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\node_modules\co-mysql\index.js:23:13
      at new Promise (<anonymous>)
      at Object.o.query (D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\node_modules\co-mysql\index.js:14:13)
      at server.use (D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\mysql-co-mysql.js:21:26)
      at dispatch (D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\node_modules\koa-compose\index.js:42:32)

几次检查后,在修改访问端口等,均发现没有问题

server.use (D:\weblearning\mycode\mobileWebCode\webRecode\node\koa-\mysql-co-mysql.js:21:26)

也基本没问题

mysql-co-mysql.js

const Koa=require('koa')
const Router=require('koa-router')
const mysql=require('mysql')
const co=require('co-mysql')

let conn=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'20191217'
})

let db=co(conn);

let server=new Koa()
server.listen(8000);

server.context.db=db;
//全局声明以后,可以用ctx.db直接使用,比较方便
server.use(async ctx=>{
   let data=await ctx.db.query('SELECT * FROM item_table')
   ctx.body=data;
})

最后检查wampserver,发现开启的MySQL端口为3308(可能之前改的给忘了),将端口重新设置为3306即可

你可能感兴趣的:(前端学习中遇到小问题)