Error: Access denied for user ''@'localhost' (using password: NO)
Error: Access denied for user ''@'localhost' (using password: NO)
我正在尝试使用 MySQL 和 Knex 进行数据库迁移。
当我 运行 命令 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');
// "migration definition"
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');
由于错误消息说您正在尝试使用无效的凭据登录,数据库中不存在名称为空字符串的用户。
这意味着您的配置有误。您的 node-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']
);
我正在尝试使用 MySQL 和 Knex 进行数据库迁移。
当我 运行 命令 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');
// "migration definition"
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');
由于错误消息说您正在尝试使用无效的凭据登录,数据库中不存在名称为空字符串的用户。
这意味着您的配置有误。您的 node-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']
);