mac连接mysql报错

来源:6-3 数据库操作(更新)

发烧的冬瓜

2022-05-10

运行项目一直报这个错,在网上找了一天了,各种方法都试过了,不知道哪里有问题。

配置都是照着课程写的:

const env = process.env.NODE_ENV // 环境参数 

// 配置
let MYSQL_CONFIG

if (env === 'dev') {
    MYSQL_CONFIG = {
        host: 'localhost',
        user: 'root',
        password:'qwe123cl',
        port: '3306',
        database: 'myblog',
    }
}

报错:

[nodemon] starting `node ./bin/www.js`
Ignoring invalid configuration option passed to Connection: MYSQL_CONFIG. This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection
监听在9527端口...
node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: Access denied for user ''@'localhost' (using password: NO)
    at Packet.asError (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/packets/packet.js:728:17)
    at ClientHandshake.execute (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/commands/command.js:29:26)
    at Connection.handlePacket (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/connection.js:456:32)
    at PacketParser.onPacket (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/connection.js:85:12)
    at PacketParser.executeStart (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/packet_parser.js:75:16)
    at Socket.<anonymous> (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/connection.js:92:25)
    at Socket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
Emitted 'error' event on Connection instance at:
    at Connection._notifyError (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/connection.js:236:12)
    at ClientHandshake.<anonymous> (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/connection.js:125:14)
    at ClientHandshake.emit (node:events:402:35)
    at ClientHandshake.execute (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/commands/command.js:39:14)
    at Connection.handlePacket (/Users/chenlong/Desktop/blog/node_modules/mysql2/lib/connection.js:456:32)
    [... lines matching original stack trace ...]
    at addChunk (node:internal/streams/readable:315:12) {
  code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sqlState: '28000',
  sqlMessage: "Access denied for user ''@'localhost' (using password: NO)",
  sql: undefined
}
[nodemon] app crashed - waiting for file changes before starting...

求解决!

写回答

3回答

双越

2022-05-12

你直接把这两行删掉,看可以吗?

//img.mukewang.com/szimg/627cc64e090de19607300612.jpg

0
2
发烧的冬瓜
解决了!是导入MYSQL_CONFIG的时候没有加花括号。 const mysql = require('mysql2') const {MYSQL_CONFIG} = require('../config/db') // 包住 MYSQL_CONFIG 的花括号非常重要 顺便问一下,可以require('mysql2')吗?好像用mysql还是会报一些别的错。
2022-05-13
共2条回复

双越

2022-05-11

看你报错信息

Error: Access denied for user ''@'localhost' (using password: NO)

用户名是空的,应该是 root 才对。

你 debug 一下代码,看最后连接数据库的时候,用户名是什么?

0
4
双越
回复
发烧的冬瓜
那只能慢慢继续调试,既然 MYSQL_CONF.host 是 undefined ,那你再继续打印一下 MYSQL_CONF ,看看是什么?
2022-05-12
共4条回复

双越

2022-05-11

你打印一下你代码中的 env 是什么值?是不是 'dev' ?

0
1
发烧的冬瓜
是'dev'。
2022-05-11
共1条回复

Node.js+Express+Koa2+Nest.js 开发服务端

从入门到实战,一站式掌握 Node.js+Express+Koa2

4051 学习 · 2006 问题

查看课程