错误:拒绝访问用户'''localhost'(使用密码:否)
我正在尝试使用MySQL和Knex进行数据库迁移。错误:拒绝访问用户'''localhost'(使用密码:否)
当我运行命令knex migrate:latest
,我得到
ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)
我试过的代码库添加密码(以“123”和“NO”),但什么我最困惑的是,正如我在我的数据库文件user: "root"
,错误给出了一个空字符串作为用户...
我分享我想象什么是相关文件:
// mysql_db.js
const knex = require('knex')({
client: 'mysql',
connection: {
host: 'localhost',
user: 'root',
password: '',
database: 'SQL_Data',
},
});
module.exports = knex;
// 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');
由于错误消息说你是试图用无效凭据登录用户名为空的用户字符串不存在t在DB中。
这意味着你的配置是错误的。你有你的节点MySQL驱动程序的配置,它试图引用其他文件,其中出口初始化knex例如
client: 'mysql',
connection: {
filename: '/server/SQL/mysql_db'
}
这就是完全错误的一些奇怪的段。 knexfile的正确格式与用于创建knex实例的格式几乎相同,只不过knexfile还支持根据NODE_ENV
环境变量选择配置文件。
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到 能够使用相同的配置:
const knex = require('knex')(
require('knexfile')[process.env.NODE_ENV || 'development']
);
完美,修复它!我这样做是因为在运行'knex init'时,就是'knexfile.js'附带的代码结构作为格式的一个例子,在连接关键字的对象内请求一个文件名键。 – jaimefps
http://perkframework.com/v1/guides/database -migrations-knex.html – Hackerman