質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

解決済

2回答

1506閲覧

Node.jsアプリケーションとMySQLの接続エラー

minalucky

総合スコア1

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

0グッド

0クリップ

投稿2021/01/15 05:36

前提・実現したいこと

プロゲートで、プログラミングの勉強をしています。
node.jsとMySQLの接続をしようとしているのですが、エラーが出てしまいます。
ローカルホストにアクセスしたときに、EJSで書いた文字はアクセスして表示することができるのですが、MySQLのデータがコマンドプロンプトに表示ができないです。

発生している問題・エラーメッセージ

PS C:\Users\yasut\Desktop\list-app> node app.js
error connecting: Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
at Handshake.Sequence._packetToError (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
at Protocol._parsePacket (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:309:12)
--------------------
at Protocol._enqueue (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\yasut\Desktop\list-app\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\Users\yasut\Desktop\list-app\app.js:13:12)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
undefined

該当のソースコード

「app.js」
const express = require('express');
const mysql = require('mysql');

const app = express();

const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '******',
database: 'list_app'
});

connection.connect((err) => {
if (err) {
console.log('error connecting: ' + err.stack);
return;
}
console.log('success');
});

app.get('/', (req, res) => {
connection.query(
'SELECT * FROM users',
(error, results) => {
console.log(results);
res.render('hello.ejs');
}
);
});

app.listen(3000);

「MySQL」
PS C:\Users\yasut\Desktop\list-app> npm install mysql
npm WARN list-app@1.0.0 No description
npm WARN list-app@1.0.0 No repository field.

added 9 packages from 14 contributors and audited 74 packages in 2.193s
found 0 vulnerabilities

PS C:\Users\yasut\Desktop\list-app> mysql --user=root --password
Enter password: ****************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| shop_app |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)

mysql> CREATE DATABASE list_app;
Query OK, 1 row affected (0.09 sec)

mysql> USE list_app;
Database changed
mysql> SHOW tables;
Empty set (0.00 sec)

mysql> CREATE TABLE users (id INT AUTO_INCREMENT, name TEXT, PRIMARY KEY (id)) DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 1 warning (1.88 sec)

mysql> SHOW tables;
+--------------------+
| Tables_in_list_app |
+--------------------+
| users |
+--------------------+
1 row in set (0.07 sec)

mysql> DESCRIBE users;
+-------+------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | text | YES | | NULL | |
+-------+------+------+-----+---------+----------------+
2 rows in set (0.10 sec)

mysql> SELECT * FROM users;
Empty set (0.00 sec)

mysql> INSERT INTO users(name) VALUES ('にんじゃわんこ');
Query OK, 1 row affected (0.11 sec)

mysql> SELECT * FROM users;
+----+----------------+
| id | name |
+----+----------------+
| 1 | にんじゃわんこ |
+----+----------------+
1 row in set (0.00 sec)

「localhost:3000/」にアクセスすると、MySQLには「undefined」と表示されますが、実際には下記のように表示されるようです。

success
[
RowDataPaclcet{ id: 1, name: 'ninja wanko' },
RowDataPaclcet{ id: 2, name: 'Ken the ninja' }
]

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

const mysql = require('mysql');

npmのmysqlは1年前から更新が止まっています。
mysql - npm

これからはmysql2を使うようにしましょう。
mysql2 - npm

投稿2021/01/16 06:09

technocore

総合スコア7225

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

これじゃね? エラーメッセージを適切なところで区切って、ネット検索すればわかる。
Node.jsでMySQL 8.0へ接続しようとする時に発生するエラー - Qiita

投稿2021/01/15 06:00

編集2021/01/15 06:01
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

minalucky

2021/01/16 05:46

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; のコマンドを実行し、無事に解決しました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問