[Database][MySQL][Ubuntu] MySQL 설치 for Ubuntu 20.04

seony·2022년 7월 7일
0

Ubuntu 20.04에서 apt( Advanced Package Tool )을 이용하여 MySQL을 설치하는 과정을 기록한다.

apt package update

apt package를 update하여 최신 상태로 만든다.
$ sudo apt-get update

MySQL

기존 설치 여부 확인

$ mysql -V

명령어 'mysql' 을(를) 찾을 수 없습니다. 그러나 다음을 통해 설치할 수 있습니다:

sudo apt install mysql-client-core-8.0     # version 8.0.29-0ubuntu0.20.04.3, or
sudo apt install mariadb-client-core-10.3  # version 1:10.3.34-0ubuntu0.20.04.1

설치

$ sudo apt-get install mysql-server

아래와 같이 나오면 설치가 완료된 것이다.

done!
update-alternatives: using /var/lib/mecab/dic/ipadic-utf8 to provide /var/lib/mecab/dic/debian (mecab-dictionary) in auto mode
mysql-server-8.0 (8.0.29-0ubuntu0.20.04.3) 설정하는 중입니다 ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 113168
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
mysql-server (8.0.29-0ubuntu0.20.04.3) 설정하는 중입니다 ...
Processing triggers for systemd (245.4-4ubuntu3.17) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...

그리고 버전을 확인한다.

$ mysql -V
mysql  Ver 8.0.29-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))

DB 구동

$ sudo /etc/init.d/mysql start
Starting mysql (via systemctl): mysql.service.

DB 설정

아래의 명령을 실행하여 DB 설정을 한다. 이 명령어는 DB 설정을 변경하고 싶을 때 실행하여 DB 설정을 다시 할 수 있다.
$ sudo mysql_secure_installation 

비밀번호 설정

비밀번호 복잡성 설정 여부 선택

복잡한 비밀번호 설정 여부를 확인한다. 복잡한 비밀번호로 설정할려면 YES, 아니면 NO를 명시한다.
Securing the MySQL server deployment.

Enter password for user root: 

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: No
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y

root 비밀번호 설정

New password: 

Re-enter new password: 

Anonymous User ( 익명유저 )

MySQL에서 기본적으로 제공하는 Anonymous user를 생성하는 이를 허용할지 여부 설정한다. Anonymous user를 생성하고자 하면 YES, 생성을 원하지 않는다면 NO를 입력한다.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : YES
Success.

root 접속 권한

root 계정 접속을 localhost만 허용할지, 원격에서도 접속할 수 있도록 허용할지 선택한다. root 계정에 대한 원격접속을 허용하지 않으면 YES, 허용하면 NO를 입력한다. 보안을 위해 YES를 추천한다.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : YES
Success.

TEST Database 생성 여부

TEST Database를 생성하지 않으면 YES를, 생성할려면 NO를 입력한다.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : yes
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Privilege Table Reload 여부

변경 사항을 적용하기 위해 Privilege table을 reload 여부를 결정한다. 즉시 Privilege Table을 reload 하고자하면 YES, reload 하지 않을려면 NO를 입력한다.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yes
Success.

All done! 

DB 설정 시 에러 발생 해결법

DB 설정을 시작하고 password 설정 시 아래와 같은 에러가 발생하는 경우가 있다.
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: No
Please set the password for root here.

New password: 

Re-enter new password: 
 ... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.

이럴 때에는 아래와 같이 DB에 접속한다.

$ sudo mysql

다음과 같은 query를 실행한다.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'pw123';

그리고 다시 DB 설정을 하면 정상적으로 설정할 수 있다.

DB 접속

$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.29-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2022, 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> 

DB 종료

DB 종료한다. DB를 종료하면 connect 시 에러가 발생한다.
$ sudo /etc/init.d/mysql stop
Stopping mysql (via systemctl): mysql.service.

$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
profile
Developer

0개의 댓글

관련 채용 정보