본문 바로가기

리눅스

[리눅스] MySQL에서 사용자의 패스워드를 변경하는 방법

728x90

MySQL에서 사용자의 패스워드를 변경하는 방법

MySQL 5.7(5.7.41)에서 사용자의 패스워드를 변경

MySQL 버전 확인

/usr/local/mysql/bin/mysqld --version
$ /usr/local/mysql/bin/mysqld --version
/usr/local/mysql/bin/mysqld  Ver 5.7.41 for linux-glibc2.12 on x86_64 (MySQL Community Server (GPL))

my.conf에 skip-grant-tables 옵션 추가

vim /usr/local/mysql/my.cnf
[mysqld]
...
skip-grant-tables

MySQL 서비스 실행

/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf --user=mysql &

MySQL 쉘 접속(root 계정으로 로그인)

/usr/local/mysql/bin/mysql -uroot

root 계정의 패스워드 변경

UPDATE mysql.user SET authentication_string=PASSWORD('P@ssw0rd1!') WHERE user='root' AND Host='localhost';

my.conf에 skip-grant-tables 옵션 제거MySQL 서버를 재실행합니다.

/usr/local/mysql/bin/mysqladmin -uroot -p'P@ssw0rd1!' shutdown --socket /tmp/mysql.sock
vim /usr/local/mysql/my.cnf
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf --user=mysql &
/usr/local/mysql/bin/mysql -uroot -p'P@ssw0rd1!'
$ /usr/local/mysql/bin/mysql -uroot -p'P@ssw0rd1!'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> select version();
+-----------+
| version() |
+-----------+
| 5.7.41    |
+-----------+
1 row in set (0.00 sec)

mysql>

MySQL 8.0(8.0.33)에서 사용자의 패스워드를 변경

MySQL 버전 확인

mysqld --version
$ mysqld --version
/usr/sbin/mysqld  Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)

my.conf에 skip-grant-tables 옵션 추가

vim /usr/local/mysql/my.cnf
[mysqld]
...
skip-grant-tables

MySQL 서비스 실행

mysqld_safe --defaults-file=/etc/mysql/my.cnf --user=mysql &

MySQL 쉘 접속(root 계정으로 로그인)

mysql -uroot

root 계정의 패스워드 변경

ALTER USER 'root'@'localhost' IDENTIFIED BY 'P@ssw0rd1!';

my.conf에 skip-grant-tables 옵션 제거MySQL 서버를 재실행합니다.

mysqladmin -uroot -p'P@ssw0rd1!' shutdown --socket /tmp/mysql.sock
vim /etc/mysql/my.cnf
mysqld_safe --defaults-file=/etc/mysql/my.cnf --user=mysql &
mysql -uroot -p'P@ssw0rd1!'
$ mysql -uroot -p'P@ssw0rd1!'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> select version();
+-----------+
| version() |
+-----------+
| 8.0.33    |
+-----------+
1 row in set (0.00 sec)

mysql>

 

728x90