我正在尝试使用MySQL和Knex进行db迁移。
当我运行命令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');发布于 2017-05-29 06:32:21
如错误消息所示,您正在尝试使用无效的凭据登录,用户的名称为空字符串在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中,您可能希望这样做,以便能够使用相同的配置:
const knex = require('knex')(
require('knexfile')[process.env.NODE_ENV || 'development']
);https://stackoverflow.com/questions/44232953
复制相似问题