错误:对用户"@'localhost"的访问被拒绝(使用密码:否) [英] Error: Access denied for user ''@'localhost' (using password: NO)

查看:335
本文介绍了错误:对用户"@'localhost"的访问被拒绝(使用密码:否)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MySQL和Knex进行数据库迁移.

I'm trying out db migrations with MySQL and Knex.

运行命令knex migrate:latest时,我得到

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

我尝试在代码库上添加密码(分别为"123"和否"),尽管最让我困惑的是,即使我的数据库文件中有user: "root",该错误也会给出一个空字符串,如下所示:用户...

I've tried adding a password on the codebase (to '123' and 'NO'), though what confuses me most is that even as I have user: "root" in my database file, the error gives an empty string as the user...

我分享了我想象中的相关文件:

I share what I imagine are the pertinent files:

//mysql_db.js

// mysql_db.js

const knex = require('knex')({
  client: 'mysql',
  connection: {
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'SQL_Data',
  },
});

module.exports = knex;

//knexfile.js

// knexfile.js

const path = require('path');

module.exports = {
    development: {
      client: 'mysql',
      connection: {
      filename: '/server/SQL/mysql_db',
    },
    migrations: {
      directory: path.join(__dirname, '/server/SQL/migrations'),
    },
    seeds: {
      directory: path.join(__dirname, '/server/SQL/seeds'),
    },
  },
};

//knex.js

const environment = proces.env.NODE_ENV || 'development';
const config = require('../../knexfile.js')[environment];
module.exports = require(knex)('config');

//迁移定义"

exports.up = (knex, Promise) => knex.schema.createTable('sql_table', ((table) => {
  table.increments();
  table.string('name').notNullable();
  table.string('email').notNullable();
  table.string('description').notNullable();
  table.string('url').otNullable();
}));

exports.down = (knex, Promise) => knex.schema.dropTable('sql_table');

推荐答案

错误消息说,您正在尝试使用无效的凭据用户登录,该用户的名称为空字符串在数据库中不存在.

As error message say you are trying to login with invalid credentials user whose name is empty string doesn't exist in DB.

这意味着您的配置错误.您的node-mysql驱动程序配置中存在一些奇怪的段,该段试图引用其他文件,该文件会导出初始化的knex实例

This means your configuration is wrong. you have some strange segment in your node-mysql driver configuration, which tries to refer other file, which exports initialized knex instance

  client: 'mysql',
  connection: {
    filename: '/server/SQL/mysql_db'
  }

那是完全错误的. knexfile的正确格式与用于创建knex实例的格式几乎相同,除了knexfile还支持根据NODE_ENV环境变量选择配置文件.

That is just plain wrong. Correct format for knexfile is pretty much the same that is used to create knex instance, except that knexfile supports also selecting the profile according to NODE_ENV environment variable.

const path = require('path');

module.exports = {
  development: {
    client: 'mysql',
    connection: {
      host: 'localhost',
      user: 'root',
      password: '',
      database: 'SQL_Data',
    },
    migrations: {
      directory: path.join(__dirname, '/server/SQL/migrations'),
    },
    seeds: {
      directory: path.join(__dirname, '/server/SQL/seeds'),
    },
  },
};

在您的mysql_db中,您可能想要执行以下操作来初始化knex以: 能够使用相同的配置:

In your mysql_db you might like to do something like this to init knex to be able to use the same config:

const knex = require('knex')(
  require('knexfile')[process.env.NODE_ENV || 'development']
);

这篇关于错误:对用户"@'localhost"的访问被拒绝(使用密码:否)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆