Remote client ์—์„œ MariaDB ์›๊ฒฉ ์ ‘์† ๋ฐฉ๋ฒ•

2022. 9. 5. 22:38ใ†๐ŸŽฏ OpenSource/MariaDB

Intro

MariaDB์— ์›๊ฒฉ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ์˜ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜๊ณ , ์‚ฌ์šฉ์ž ๊ณ„์ •์— ๋”ฐ๋ฅธ ๊ถŒํ•œ ๋ถ€์—ฌ๋ฅผ ํ•˜์—ฌ ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•˜๋„๋ก ํ•œ๋‹ค.

 

MariaDB-Client, MariaDB-Server ์„ค์น˜

MariaDB client์— ํ•ด๋‹นํ•˜๋Š” WEB ์„œ๋ฒ„์— MariaDB-Client ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๊ณ ,
DB ์„œ๋ฒ„์— Mariadb-Server ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
MariaDB ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜ ๋งํฌ์—์„œ yum repo๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ๊ฐ ์„œ๋ฒ„์— ์ถ”๊ฐ€ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
https://mariadb.org/download/?t=repo-config

## web ์„œ๋ฒ„
[root@wglee-web ~]# yum search all MariaDB-client
[root@wglee-web ~]# yum install MariaDB-client

## db ์„œ๋ฒ„
[root@wglee-db ~]# yum install mariadb-server

 

MariaDB-Server setup

db ์„œ๋ฒ„์—์„œ ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋กœ root ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
ํ•ด๋‹น ๋ช…๋ น์–ด๋Š” shell script ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•˜๋ฉฐ, ๋‹ค์Œ ํ•ญ๋ชฉ๋“ค์„ ์ดˆ๊ธฐ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
-> root password ์ดˆ๊ธฐํ™”
-> ์›๊ฒฉ์ง€์—์„œ root๋กœ ์ ‘๊ทผ ๋ชปํ•˜๋„๋ก ์ œํ•œ
-> MariaDB ์„ค์น˜์‹œ์— ํ…Œ์ŠคํŠธ ์œ„ํ•ด ์ƒ์„ฑ๋œ anonymous ์‚ฌ์šฉ์ž ๊ณ„์ • ์ œ๊ฑฐ
-> anonymous ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” test ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ œ๊ฑฐ

[root@wglee-db ~]#  mysql_secure_installation

๊ทธ ๋‹ค์Œ, root password ์ž…๋ ฅ ์—†์ด mysql์— ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋„๋ก my.cnf ์— ์„ค์ •ํ•œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ๋Œ€์—ญ์—์„œ ๋“ค์–ด์˜ค๋Š” connection์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก 0.0.0.0์œผ๋กœ ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค.
( my.cnf๋Š” mariaDB์˜ ๊ณต์‹ ์„ค์ • ํŒŒ์ผ์ด๋‹ค.)

[root@wglee-db ~]# cat /etc/my.cnf | grep -v '^$\|^#'
[mysqld]
...
# ์ถ”๊ฐ€
bind-address = 0.0.0.0
...
# ์ถ”๊ฐ€
[client]
user=root
password="[ํŒจ์Šค์›Œ๋“œ]"

 

MariaDB-Server - Create Database, User

์›๊ฒฉ ์„œ๋ฒ„์—์„œ ์ ‘๊ทผํ•  ์œ ์ €์™€, ํ•ด๋‹น ์œ ์ €์—๊ฒŒ ์ ‘๊ทผ ํ—ˆ์šฉ์„ ํ•  ์ž„์˜์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

MariaDB [(none)]> CREATE DATABASE wgleeDB;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> CREATE USER  'wglee'@'localhost' IDENTIFIED BY '[ํŒจ์Šค์›Œ๋“œ]';
Query OK, 0 rows affected (0.00 sec)

 

MariaDB-Server - Grant privileges to user

์›๊ฒฉ์—์„œ ์ ‘๊ทผํ•  ์‚ฌ์šฉ์ž wglee์— ๋Œ€ํ•œ ๊ถŒํ•œ์„ ์„ค์ •ํ•œ๋‹ค.
์ ‘๊ทผ ํ—ˆ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ์›๊ฒฉ์ง€ ์ฃผ์†Œ ๋“ฑ์„ ํ—ˆ์šฉ ๋ฒ”์œ„์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

## No1. wglee ๊ณ„์ •์ด wgleeDB ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์›๊ฒฉ์ง€IP์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ—ˆ์šฉ
MariaDB [(none)]> GRANT ALL ON wgleeDB.* to 'wglee'@'๊ณต์ธIP' IDENTIFIED BY '[ํŒจ์Šค์›Œ๋“œ]' WITH GRANT OPTIO
N;
Query OK, 0 rows affected 

## No2. wglee ๊ณ„์ •์ด wgleeDB ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ชจ๋“  ์›๊ฒฉ์ง€ IP์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ—ˆ์šฉ
MariaDB [(none)]> GRANT ALL ON wgleeDB.* to 'wglee'@'%' IDENTIFIED BY '[ํŒจ์Šค์›Œ๋“œ]' WITH GRANT OPTION;

## No3. wglee ๊ณ„์ •์ด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— .0/24 ๋Œ€์—ญ์˜ ์›๊ฒฉ์ง€ IP์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ—ˆ์šฉ
MariaDB [(none)]> GRANT ALL ON *.* to 'wglee'@'๊ณต์ธIP.%' IDENTIFIED BY '[ํŒจ์Šค์›Œ๋“œ]' WITH GRANT OPTION;

๋งˆ์ง€๋ง‰์—๋Š” flush privileges๋ฅผ ํ•ด์„œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•œ๋‹ค.

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

 

MariaDB-Client - Test Connection

client์ธ web ์„œ๋ฒ„์—์„œ wglee ๊ณ„์ •์œผ๋กœ ์ ‘์†์„ ํ…Œ์ŠคํŠธ ํ•œ๋‹ค.
์ด๋•Œ client์™€ server ์‚ฌ์ด์— ๋ฐฉํ™”๋ฒฝ์ด ํ—ˆ์šฉ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
(์ง€๊ธˆ์€ mariadb-server๊ฐ€ 3306์œผ๋กœ ๋Œ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— server์—์„œ inbound 3306 ๋กœ ํ—ˆ์šฉํ•ด์คŒ)
-h ์˜ต์…˜์œผ๋กœ wglee-db ์„œ๋ฒ„์˜ ๊ณต์ธ ์•„์ดํ”ผ๋ฅผ host๋กœ ์ง€์ •ํ•œ๋‹ค.
์•ž์„œ ์„ค์ •ํ•œ wglee ๊ณ„์ •์˜ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์›๊ฒฉ์ง€์˜ MariaDB์— ์ ‘๊ทผํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

[root@wglee-web ~]# mysql -u wglee -h DB์„œ๋ฒ„๊ณต์ธIP -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server

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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wgleeDB            |
+--------------------+
2 rows in set (0.001 sec)

MariaDB [(none)]> use wgleeDB
Database changed

https://webdock.io/en/docs/how-guides/database-guides/how-enable-remote-access-your-mariadbmysql-database