reactアプリケーションでデータベースを扱いたく、いろいろ調べた結果、
現在、
heroku上でnode.js(express)を使って、herokuのMySQL(ClearDB)を操作できるAPIを作る という方法を取ることにしました。
(言葉の使い方を間違えていたらご指摘ただけますと幸いです)
ローカル環境のMySQLと接続すると、エラーが出ないのですが、
mysql.createConnectionの設定をherokuのものにして、queryを実行すると、deniedされます。エラー番号を見ると'create database'の権限がない 的な参考は出てくるのですが、「じゃあどうすればいいのか」がわかりません...。
ご助力お願いいたします><
表示されるエラーです↓ 見辛くなってしまいすみません。
% node index.js Example app listening! /Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Parser.js:437 throw err; // Rethrow non-MySQL errors ^ Error: ER_DBACCESS_DENIED_ERROR: Access denied for user '△△△△△'@'%' to database 'react-node-mysql' at Query.Sequence._packetToError (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14) at Query.ErrorPacket (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:79:18) at Protocol._parsePacket (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Protocol.js:291:23) at Parser._parsePacket (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Parser.js:433:10) at Parser.write (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Parser.js:43:10) at Protocol.write (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Protocol.js:38:16) at Socket.<anonymous> (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/Connection.js:88:28) at Socket.<anonymous> (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/Connection.js:526:10) at Socket.emit (events.js:311:20) at addChunk (_stream_readable.js:294:12) -------------------- at Protocol._enqueue (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Protocol.js:144:48) at Connection.query (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/Connection.js:198:25) at Object.<anonymous> (/Users/pc-name/Work Space/react-node-mysql/index.js:22:12) at Module._compile (internal/modules/cjs/loader.js:1158:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10) at Module.load (internal/modules/cjs/loader.js:1002:32) at Function.Module._load (internal/modules/cjs/loader.js:901:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47 Emitted 'error' event on Connection instance at: at Connection._handleProtocolError (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/Connection.js:423:8) at Protocol.emit (events.js:311:20) at Protocol._delegateError (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Protocol.js:398:10) at Query.<anonymous> (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Protocol.js:153:12) at Query.emit (events.js:311:20) at Query.Sequence.end (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:78:12) at Query.ErrorPacket (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:92:8) at Protocol._parsePacket (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Protocol.js:291:23) at Parser._parsePacket (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Parser.js:433:10) at Parser.write (/Users/pc-name/Work Space/react-node-mysql/node_modules/mysql/lib/protocol/Parser.js:43:10) { code: 'ER_DBACCESS_DENIED_ERROR', errno: 1044, sqlMessage: "Access denied for user '△△△△△'@'%' to database 'react-node-mysql'", sqlState: '42000', index: 0, sql: 'create database if not exists `react-node-mysql`;' }
以下のファイルがあります。
index.js↓
index.js
1const express = require('express') 2const app = express() 3const mysql = require('mysql') 4 5const databaseName = 'react-node-mysql' 6const connection = mysql.createConnection({ 7 host: "○○○○.net", 8 user: "△△△△△", 9 password: "xxxxx", 10 database: "heroku_9999" 11}); 12//上記設定は間違いがないはずです...。heroku configをターミナルで実行すると、以下のような情報が出てきて、それをもとに上記の設定をしています。 13//mysql://△△△△△:xxxxx@○○○○.net/heroku_9999?reconnect=true 14 15//試しに以下の情報でローカルのSQLに接続すると、エラーが出ない。 16// const connection = mysql.createConnection({ 17// host: "localhost", 18// user: "root", 19// password: "", 20// database: "test" 21// }); 22 23connection.query('create database if not exists ??;', databaseName) 24connection.query('use ??', databaseName) 25connection.query('create table if not exists todo_list(id int auto_increment, name varchar(255), isDone boolean, index(id))') 26 27//-------------------GET---------------------------------- 28app.get('/', (req, res) => { 29 connection.query('show databases', (error, results, fields) => { 30 if (error) { console.log(error) } 31 res.send(results + 'aaa') 32 }) 33}) 34 35 36app.listen(process.env.PORT || 3002, () => console.log('Example app listening!'))
package.json↓
package.json
1{ 2 "name": "react-node-mysql", 3 "version": "1.0.0", 4 "description": "", 5 "main": "index.js", 6 "scripts": { 7 "start": "node index.js", 8 "test": "echo \"Error: no test specified\" && exit 1" 9 }, 10 "author": "", 11 "license": "ISC", 12 "dependencies": { 13 "express": "^4.17.1", 14 "mysql": "^2.18.1" 15 } 16} 17
回答1件
あなたの回答
tips
プレビュー