[Ubuntu] DBMS

Alexandria·2024년 3월 1일

Ubuntu

목록 보기
5/11
post-thumbnail

1. MySQL

MySQL 패키지를 설치한 후 MySQL을 실행해본다.

비밀번호를 묻는데 root에 대한 비밀번호가 없기 때문에 그냥 Enter를 누른다.

$ sudo apt -y install mysql-server
$ sudo sudo mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.31-0ubuntu0.20.04.1 (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>

root에 대한 비밀번호를 설정해본다.

mysql_native_password BY다음에 나오는 문자열이 비밀번호이다.

mysql> ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

초기 설정을 해준다. root 비밀번호를 입력하여 진행한다.

$ mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

비밀번호에 관하여 딱히 변경할게 없기 때문에 Enter만 누른다.

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:
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

 ... skipping.

기본적으로 있는 anonymous 계정을 삭제한다.

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) : y
Success.

원격 로그인 항목이다. Enter를 누른다.

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) :

 ... skipping.

기본적으로 있는 불필요한 데이터베이스를 삭제한다.

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) :

 ... skipping.

설정된 정보를 적용한다.

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) : y
Success.

All done!

외부 접속을 원한다면 바인딩되는 주소를 변경해줘야한다.

설정파일 내 bind-address의 값을 변경한다.

$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
...
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 0.0.0.0

MySQL 서비스를 재시작한다.

$ sudo systemctl restart mysql.service

원격으로 접속하고 싶다면 권한을 확인한 후 권한이 없다면 원격에 대한 권한을 부여한다.

현재 MySQL 8 버전이기 때문에 GRANT 문법이 다를 수 있음을 유의한다.

$ sudo mysql -u root -p'root'
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 8
Server version: 8.0.31-0ubuntu0.20.04.1 (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> SELECT Host,User FROM mysql.user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| localhost | debian-sys-maint |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

원격(%)에 대한 접근을 제거하고 싶다면 DELETE 해준다.

mysql> DELETE FROM mysql.user WHERE Host='%' AND User='root';
Query OK, 1 row affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT Host,User FROM mysql.user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| localhost | debian-sys-maint |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

2. MariaDB

MariaDB 패키지를 설치한 후 초기 설정을 해준다.

처음 설치 후 root에 대한 비밀번호가 없기 때문에 그냥 Enter를 누른다.

$ sudo apt -y install mariadb-server
$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

root 패스워드를 설정한다.

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

기본적으로 있는 anonymous 계정을 삭제한다.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] Y
 ... Success!

원격 로그인 항목이다. n을 선택해도 바로 원격으로 데이터베이스에 접근할 수 없다.

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? [Y/n] Y
 ... skipping.

기본적으로 있는 불필요한 데이터베이스를 삭제한다.

By default, MariaDB 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? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

설정된 정보를 적용한다.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

외부 접속을 원한다면 바인딩되는 주소를 변경해줘야한다.

설정파일 내 bind-address의 값을 변경한다.

$ sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf
...
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 0.0.0.0

MariaDB 서비스를 재시작한다.

$ sudo systemctl restart mariadb.service

mariadb를 실행하여 접속할 수 있다.

$ sudo mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

이름은 MariaDB이지만 MySQL과 같기 때문에 mysql을 실행하여도 접속할 수 있다.

$ sudo mysql -u root -p'root'
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 43
Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

원격으로 접속하고 싶다면 권한을 확인한 후 권한이 없다면 원격에 대한 권한을 부여한다.

MariaDB [(none)]> SELECT Host,User FROM mysql.user;
+-----------+------+
| Host      | User |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.000 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> SELECT Host,User FROM mysql.user;
+-----------+------+
| Host      | User |
+-----------+------+
| %         | root |
| localhost | root |
+-----------+------+
2 rows in set (0.000 sec)

원격(%)에 대한 접근을 제거하고 싶다면 DELETE 해준다.

MariaDB [(none)]> DELETE FROM mysql.user WHERE Host='%' AND User='root';
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> SELECT Host,User FROM mysql.user;
+-----------+------+
| Host      | User |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.000 sec)
profile
IT 도서관

0개의 댓글