我正在尝试将我的 NestJs 应用程序连接到本地的 MySql 数据库。 加入 NestJs 文档 .
我收到此错误消息:
[Nest] 8696 - 24/08/2023 15:14:40 ERROR [ExceptionHandler] ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
这是我的数据库提供商:
import { DataSource } from 'typeorm';
export const databaseProviders = [
{
provide: 'DATA_SOURCE_LOCAL',
useFactory: async () => {
const dataSource = new DataSource({
type: 'mysql',
host: '127.0.0.1',
port: 3306,
username: 'root',
password: 'root',
database: 'myDb',
entities: [myEntities],
synchronize: true,
insecureAuth: true,
});
return dataSource.initialize();
},
},
提供商示例 (user.prividers.ts) :
import { DataSource } from 'typeorm';
import {User} from './user.entity';
import { Profile } from './profile.entity';
export const userProviders = [
{
provide: 'USER_REPOSITORY',
useFactory: (dataSource: DataSource) => dataSource.getRepository(User),
inject: ['DATA_SOURCE_LOCAL'],
}
];
随着文档的预先确定,我使用“mysql2”和“typeorm”: 我的package.json文件:
{
"name": "mysql-project",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.3.4",
"@nestjs/core": "^9.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/typeorm": "^9.0.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"mysql": "^2.18.1",
"mysql2": "^3.6.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
"typeorm": "^0.3.16"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "29.5.0",
"@types/node": "18.15.11",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "29.5.0",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "29.0.5",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.2.0",
"typescript": "^4.7.4"
},
我精确地说了一件“有趣”的事情。 每次运行应用程序时,我都会在数据库中获得一个(且只有一个)新表/实体。
我有 4 个实体(测验、问题、用户、个人资料)。 Id 表“测验”已在数据库中设置,并且我运行应用程序,它将因上述消息而崩溃,但“问题”表将被创建。
我已经尝试过这个方法了:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
flush privileges;
但是,尽管事实上它不起作用,但这似乎不是一个好的实践(就像在这个主题中)并且在每种情况下都可用(例如云数据库)。
我质疑自己在数据库提供程序中使用“mysql”类型是否需要文档中的“mysql2”(在应用程序中找不到)。
我在 Node.js 中读到了有关“mysql2”的内容,例如将其添加到 index.js 中:
const mysql = require('mysql2')
但我不知道如何将其与 NestJs 一起使用。
mysql
npm 模块仍然不支持 MySQL 8 中引入的 caching_sha2_password
身份验证。因此,解决方案是切换到支持的 mysql2
npm 模块。
对于我来说 我必须卸载mysql然后安装mysql2
` npm 卸载 mysql
` 然后
npm 安装 mysql2
它对我有用。