// 오류가 발생한 부분의 코드. DATASOURCE는 환경변수들을 저장한 상수 변수이다.
export const AppDataSource = new DataSource({
type: DATASOURCE.TYPE,
host: DATASOURCE.HOST,
port: DATASOURCE.PORT,
username: DATASOURCE.USERNAME,
password: DATASOURCE.PASSWORD,
database: DATASOURCE.DATABASE,
synchronize: true,
logging: true,
entities: [User],
subscribers: [],
migrations: [],
});
AppDataSource.initialize()
.then(() => {
console.log("DB 초기화 완료.");
})
.catch((e) => console.log("DB 초기화 오류", e));
DB 초기화 오류 Error: Access denied for user 'root'@'localhost'
at Packet.asError (/javascript-p4-bmtown/server/node_modules/mysql2/lib/packets/packet.js:728:17)
at ClientHandshake.execute (/javascript-p4-bmtown/server/node_modules/mysql2/lib/commands/command.js:29:26)
at PoolConnection.handlePacket (/javascript-p4-bmtown/server/node_modules/mysql2/lib/connection.js:456:32)
at PacketParser.onPacket (/javascript-p4-bmtown/server/node_modules/mysql2/lib/connection.js:85:12)
at PacketParser.executeStart (/javascript-p4-bmtown/server/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/javascript-p4-bmtown/server/node_modules/mysql2/lib/connection.js:92:25)
at Socket.emit (node:events:527:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9) {
code: 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR',
errno: 1698,
sqlState: '28000',
sqlMessage: "Access denied for user 'root'@'localhost'",
sql: undefined
}
On some systems, like Ubuntu, MySQL is using the Unix auth_socket plugin by default.
Basically it means that: db_users using it, will be "authenticated" by the system user credentials.
$ mysql -u root 혹은 mysql -u root -p
auth_socket
으로 되어 있는 부분을 볼 수 있다.mysql> USE mysql;
SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User | Host | plugin |
+------------------+-----------+-----------------------+
| root | localhost | auth_socket |
...
mysql_native_password
으로 변경하고 저장한다.mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User | Host | plugin |
+------------------+-----------+-----------------------+
| root | localhost | mysql_native_password |
...
참고자료2 : https://www.lesstif.com/dbms/mysql-2054-mysql-8-authentication-policy-89556039.html
1번의 방법으로 에러를 해결한 줄 알았는데, 마찬가지로 에러가 발생했다.
기존 에러와 차이점은 using password: YES
라는 에러 메세지가 추가되어서 출력되었다.
간단한 해결 방법은 비밀번호를 바꾸는 것이었다.
변경할-비밀번호
부분에는 기존에 사용하던 비밀번호를 그대로 입력하면 된다.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '변경할-비밀번호';
mysql> FLUSTH PRIVILEGES;