Database/RDBMS

[MySQL 8 접속 에러] - caching_sha2_password

귀찮은 개발자 2024. 2. 9. 20:27
더보기

2022 년도에 작성된 글 입니다.

Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]

 

PHP 을 사용하다 데이터베이스 연결문제가 발생했다. 아이디 비밀번호 맞는데 위와 같은 에러 메세지가 출력되었다. 

접속 안되는 이유

MySQL 8 부터는 더 강화된 인증 방식으로 caching_sha2_password 를 도입했는데 PHP 의 MySQL Driver 에서는 지원하지 않아 발생하는 문제였다. 

 

해결 방법

caching_sha2_password 를 지원하게 한다.

PHP7.4 이상부터는 개선 되었다고 하니 아래 문서 참고
1. https://www.php.net/manual/en/mysqli.requirements.php

 

PHP: Requirements - Manual

Requirements In order to have these functions available, you must compile PHP with support for the mysqli extension. MySQL 8 When running a PHP version before 7.1.16, or PHP 7.2 before 7.2.4, set MySQL 8 Server's default password plugin to mysql_native_pas

www.php.net

2. https://www.php.net/manual/en/mysql-xdevapi.installation.php 

 

PHP: Installation - Manual

Installation This » PECL extension is not bundled with PHP. An example installation procedure on Ubuntu 18.04 with PHP 7.2: // Dependencies $ apt install build-essential libprotobuf-dev libboost-dev openssl protobuf-compiler liblz4-tool zstd // PHP with

www.php.net

Password Type 을 mysql_native_password 으로 변경

이와 같은 방식으로 Password Type 을 변경하면 되지만 실무에서는 강력한 비밀번호 매커니즘을 사용하는 것이 바람직하다. 

https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html

 

MySQL :: MySQL 8.0 Reference Manual :: 3.5 Changes in MySQL 8.0

3.5 Changes in MySQL 8.0 Before upgrading to MySQL 8.0, review the changes described in this section to identify those that apply to your current MySQL installation and applications. Perform any recommended actions. Changes marked as Incompatible change a

dev.mysql.com

그렇다면 개인 프로젝트를 할 때 간단히 조치하는 방법은 무엇이 있는가? 

Docker 으로 MySQL 을 설치할 때 커맨드 옵션을 추가해주면 된다. 이는 도커 허브의 MySQL 에서 권장하는 방법이다. 

# Use root/example as user/password credentials
version: '3.1'

services:

  db:
    image: mysql
    # NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
    # (this is just an example, not intended to be a production configuration)
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example