我创建了一个简单的Sails.JS测试应用
sails new test-project
我只想测试与我的NAS上运行的MariaDB的连接是否正常工作,以及我是否可以使用Sails.JS中的蓝图来解决它。
我创建了一个简单的数据库:
和testuser:
我配置 config / datastores.js 如下:
module.exports.datastores = {
default: {
adapter: 'sails-mysql',
url: 'mysql://asd:asd@192.168.178.11:3306/asd'
},
};
我的 config / models.js 如下所示:
module.exports.models = {
migrate: 'safe',
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'number', autoIncrement: true, },
},
dataEncryptionKeys: {
default: 'XYZ='
},
cascadeOnDestroy: true
};
完成所有操作后,我创建了一个“API”文件:
sails generate api asd
创建了我的模型文件,反过来我完成了以下内容:
/**
* Asd.js
*
* @description :: A model definition. Represents a database table/collection/etc.
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models
*/
module.exports = {
attributes: {
name: {
type: 'string'
}
}
};
我现在的期望是我可以使用Postman使用REST API查看连接是否有效。 所以我解除了项目并简单地发出了一个GET请求
http://localhost:1337/asd/1
产生以下错误
AdapterError: Unexpected error from database adapter: `select` failed ("badConnection"). A connection either could not be obtained or there was an error using the connection.
Additional data:
{ error:
{ Error: ER_HOST_NOT_PRIVILEGED: Host 'Markus-Desktop.fritz.box' is not allowed to connect to this MariaDB server
at Handshake.Sequence._packetToError (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)
at Handshake.ErrorPacket (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\protocol\sequences\Handshake.js:101:18)
at Protocol._parsePacket (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\protocol\Protocol.js:280:23)
at Parser.write (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\protocol\Parser.js:73:12)
at Protocol.write (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.
<anonymous> (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\Connection.js:96:28)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:594:20)
--------------------
at Protocol._enqueue (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at PoolConnection.connect (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\Connection.js:123:18)
at Pool.getConnection (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\mysql\lib\Pool.js:45:23)
at Object.getConnection (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\machinepack-mysql\machines\get-connection.js:78:25)
at wrapper (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\@sailshq\lodash\lib\index.js:3250:19)
at Deferred.parley.now.flaverr.name [as _handleExec] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\machine\lib\private\help-build-machine.js:1068:17)
at Deferred.exec (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\parley\lib\private\Deferred.js:679:10)
at Deferred.switch (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\machine\lib\private\help-build-machine.js:1306:16)
at spawnConnection (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails-mysql\helpers\private\connection\spawn-connection.js:29:10)
at Object.spawnOrLeaseConnection (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails-mysql\helpers\private\connection\spawn-or-lease-connection.js:23:3)
at Object.select (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails-mysql\helpers\select.js:122:24)
at wrapper (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\@sailshq\lodash\lib\index.js:3250:19)
at Deferred.parley.now.flaverr.name [as _handleExec] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\machine\lib\private\help-build-machine.js:1068:17)
at Deferred.exec (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\parley\lib\private\Deferred.js:679:10)
at Deferred.switch (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\machine\lib\private\help-build-machine.js:1306:16)
at Object.find (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails-mysql\lib\adapter.js:185:16)
at _getPopulatedRecords (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\waterline\lib\waterline\utils\query\help-find.js:145:21)
at helpFind (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\waterline\lib\waterline\utils\query\help-find.js:582:6)
at _afterPotentiallyRunningBeforeLC (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\waterline\lib\waterline\methods\find-one.js:265:9)
at _maybeRunBeforeLC (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\waterline\lib\waterline\methods\find-one.js:239:16)
at Deferred._.extend._WLModel [as _handleExec] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\waterline\lib\waterline\methods\find-one.js:241:9)
at Deferred.exec (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\parley\lib\private\Deferred.js:679:10)
at Object.findOneRecord [as asd/findone] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\hooks\blueprints\actions\findOne.js:37:4)
at C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\router\bind.js:247:46
at routeTargetFnWrapper (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\router\bind.js:391:9)
at C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\router\bind.js:454:14
at Layer.handle [as handle_request] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\layer.js:95:5)
code: 'ER_HOST_NOT_PRIVILEGED',
errno: 1130,
sqlState: undefined,
fatal: true },
meta:
{ host: 'localhost',
port: 3306,
schema: true,
adapter: 'sails-mysql',
url: 'mysql://asd:asd@192.168.178.11:3306/asd',
identity: 'default' } }
at Object.findOneRecord [as asd/findone] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\hooks\blueprints\actions\findOne.js:36:4)
at C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\router\bind.js:247:46
at routeTargetFnWrapper (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\router\bind.js:391:9)
at C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\router\bind.js:454:14
at Layer.handle [as handle_request] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:281:22
at param (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:354:14)
at param (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:410:3)
at next (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:275:10)
at next (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\route.js:127:14)
at Hook.expressMiddleware (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\hooks\i18n\index.js:205:14)
at Hook.wrapper [as expressMiddleware] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\@sailshq\lodash\lib\index.js:3250:19)
at addLocalizationMethod (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\hooks\i18n\index.js:147:35)
at routeTargetFnWrapper (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\sails\lib\router\bind.js:391:9)
at Layer.handle [as handle_request] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:281:22
at param (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:354:14)
at param (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:410:3)
at next (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\index.js:275:10)
at next (C:\Users\Markus\Programming\NodeJS\test-project\node_modules\express\lib\router\route.js:127:14)
我可以使用asd-User使用PHPMyAdmin登录,但Sails-JS的连接失败。
[EDIT-1] 我使用PHPMyAdmin重新创建了包含权限的用户,这是它执行的命令
CREATE USER 'asd'@'%' IDENTIFIED VIA mysql_native_password USING '***';
GRANT USAGE ON *.* TO 'asd'@'%' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT ALL PRIVILEGES ON `asd`.* TO 'asd'@'%' WITH GRANT OPTION;
[/ EDIT-1]
[EDIT-2] 我尝试配置我的sails应用程序连接到数据库和主机名的IP。两者都不起作用,与上述相同。
CREATE USER 'asd'@'Markus-Desktop.fritz.box' IDENTIFIED VIA mysql_native_password USING '***';
GRANT USAGE ON *.* TO 'asd'@'Markus-Desktop.fritz.box' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT ALL PRIVILEGES ON `asd`.* TO 'asd'@'Markus-Desktop.fritz.box' WITH GRANT OPTION;
CREATE USER 'asd'@'192.168.178.34' IDENTIFIED VIA mysql_native_password USING '***';
GRANT USAGE ON *.* TO 'asd'@'192.168.178.34' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT ALL PRIVILEGES ON `asd`.* TO 'asd'@'192.168.178.34' WITH GRANT OPTION;
[/ EDIT-2]
答案 0 :(得分:0)
我的NAS上的MariaDB实例在端口3307上运行,而不是在配置的3306上运行。注意到在读取数据库实例的my.cnf时。