๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT-Engineering/Linux

mysql Error: Access denied for user 'root'@'localhost' ์™„์ „ ํ•ด๊ฒฐ

by ๐Ÿงž‍โ™‚๏ธ 2023. 8. 23.
๋ฐ˜์‘ํ˜•

์•„๋ž˜์˜ ์ƒํ™ฉ์€ mysql_secure_installation์„ ํ•˜๋“ , mysql -u root๋ฅผ ํ•˜๋“  mysql -u root -p๋ฅผ ํ•˜๋“  ๊ณ„์†๋ฐœ์ƒํ•˜๋Š” ์š”์ƒํ•œ ๋ฌธ์ œ์˜€๋‹ค. ํ•˜์ง€๋งŒ ๋๋‚ด ์ด ์ƒํ™ฉ์„ ์™„์ „ํ•˜๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋‹ค.

~$ mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 
Error: Access denied for user 'root'@'localhost'

 

์ฐธ๊ณ ๋กœ ๋‚˜๋Š” Ubuntu18์—์„œ ์‹คํ–‰ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  mysql์˜ ์‚ญ์ œ/์žฌ์„ค์น˜๋Š” ์•„๋ž˜์˜ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์ž.

https://gentlesark.tistory.com/158

 

์šฐ๋ถ„ํˆฌ Ubuntu 18.04์—์„œ mysql5.7 ์™„์ „ ์‚ญ์ œ ๋ฐ ์žฌ์„ค์น˜

mysql5.7 ์™„์ „ ์‚ญ์ œ ๋‚ด ๊ฒฝ์šฐ ํŒจํ‚ค์ง€ ์„ค์น˜๋กœ ์ง„ํ–‰ํ–ˆ์—ˆ๊ธฐ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ น์–ด ์ž…๋ ฅ sudo apt-get remove --purge mysql* ํŒŒ์ผ๋“ค ์ „๋ถ€ ์‚ญ์ œ sudo rm -rf /etc/mysql /var/lib/mysql sudo rm -rf /var/log/mysql sudo rm -rf /var/log/mysq

gentlesark.tistory.com

 

 

STEP1. ๋จผ์ € mysql์„ ์ข…๋ฃŒ์‹œ์ผœ์ฃผ์ž.

sudo systemctl stop mysql

 

STEP2. ์•ˆ์ „๋ชจ๋“œ๋กœ mysql์„ ์‹คํ–‰ํ•ด์ค€๋‹ค.

sudo mysqld_safe --skip-grant-tables --skip-networking &

๋งŒ์•ฝ ์—ฌ๊ธฐ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์—ฐ์†์œผ๋กœ ์‹คํ–‰์‹œํ‚จ๋‹ค.

2023-08-23T07:03:25.080225Z mysqld_safe Logging to syslog.
2023-08-23T07:03:25.092021Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2023-08-23T07:03:25.101404Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

[1]+  Exit 1

 

(Optional) ์•„๋ž˜ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด, ๋‹ค์‹œ STEP2๋ฅผ ์‹คํ–‰ํ•ด์ฃผ์ž.,

sudo mkdir /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld


STEP3.  mysql์„ root๋กœ ์‹คํ–‰

mysql์„ root๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์‹คํ–‰

mysql -u root

 

STEP4. ์†Œ์ผ“ ์„ค์ • ๋Œ€์‹  ์•”ํ˜ธ ์„ค์ •

์•„์ง ๋๋‚œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์ด์ œ ์†Œ์ผ“์„ ์‚ฌ์šฉํ•˜๋ ค๋˜ root ๊ณ„์ •์„ ์•”ํ˜ธ๋ฅผ ํ™œ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ YOUR_NEW_PASSWORD๋ถ€๋ถ„์„ ๋‹น์‹ ์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ์‚ฌ์šฉํ•˜๋ผ.

USE mysql;
UPDATE user SET authentication_string=PASSWORD('YOUR_NEW_PASSWORD') WHERE User='root';
UPDATE user SET plugin='mysql_native_password' WHERE User='root';
FLUSH PRIVILEGES;

 

STEP5. mysql ์ข…๋ฃŒ

์•„๋ž˜์˜ ๋ช…๋ น์–ด๋กœ mysql์„ ๋น ์ ธ๋‚˜์˜ค์ž.

exit;

 

STEP6. ์•ˆ์ „๋ชจ๋“œ mysql ์ข…๋ฃŒ

sudo mysqladmin -u root -p shutdown

 

STEP7. ๋‹ค์‹œ mysql ์„œ๋น„์Šค ์‹คํ–‰

sudo systemctl start mysql

 

STEP8. ๋“œ๋””์–ด ์›๋ž˜ ๋ช…๋ น์–ด๋กœ ๋กœ๊ทธ์ธ ์ˆ˜ํ–‰

์•„๋ž˜ ๋ช…๋ น์–ด ์ž…๋ ฅํ•˜๊ณ , ์•„๊นŒ ์„ค์ •ํ•œ ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ผ.

mysql -u root -p

 

์•„๋งˆ ๋‚˜์™€ ๊ฐ™์€ ์ƒํ™ฉ์ด์—ˆ๋‹ค๋ฉด, ์ž˜ ํ•ด๊ฒฐ๋์„ ๊ฒƒ์ด๋‹ค. ๋„์›€์ด ๋˜์—ˆ๊ธฐ๋ฅผ ๋ฐ”๋ผ๋ฉฐ...(๋‚˜๋„ ์™œ ๊ทธ๋ ‡๊ฒŒ ๋œ๊ฑด์ง€ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ ์“ธ๋ฐ์—†์ด ๊ณ ์ƒํ–ˆ๋‹ค)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€