请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

mac连接mysql报错

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

配置都是照着课程写的:

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

3回答

双越 2022-05-12 16:33:20

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

https://img1.sycdn.imooc.com//szimg/627cc64e090de19607300612.jpg

0 回复 有任何疑惑可以回复我~
  • 提问者 发烧的冬瓜 #1
    删了也没用,还是报错:Error: Access denied for user ''@'localhost' (using password: NO)
    回复 有任何疑惑可以回复我~ 2022-05-12 18:38:39
  • 提问者 发烧的冬瓜 #2
    解决了!是导入MYSQL_CONFIG的时候没有加花括号。
    const mysql = require('mysql2')
    const {MYSQL_CONFIG} = require('../config/db') // 包住 MYSQL_CONFIG 的花括号非常重要
    
    顺便问一下,可以require('mysql2')吗?好像用mysql还是会报一些别的错。
    回复 有任何疑惑可以回复我~ 2022-05-13 17:51:48
双越 2022-05-11 17:31:18

看你报错信息

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

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

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

0 回复 有任何疑惑可以回复我~
  • 提问者 发烧的冬瓜 #1
    具体怎么debug呀?这方面没接触过,mysql在mac上具体怎么用我看视频里也没怎么讲。
    回复 有任何疑惑可以回复我~ 2022-05-11 17:37:26
  • 双越 回复 提问者 发烧的冬瓜 #2
    最简单的 debug ,就是你在连接 mysql 的地方 console.log 一下 MYSQL_CONF.host ,打印看看是什么值。
    回复 有任何疑惑可以回复我~ 2022-05-12 08:11:05
  • 提问者 发烧的冬瓜 回复 双越 #3
    ```
    // 开始连接
    con.connect()
    
    console.log(`MYSQL_CONFIG.host: ${MYSQL_CONFIG.host}`)
    
    ```
    试了试,输出是`MYSQL_CONF.host: undefined`,不知道哪里出问题了o(╥﹏╥)o
    回复 有任何疑惑可以回复我~ 2022-05-12 11:06:48
双越 2022-05-11 08:47:23

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

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信