test01@test01:~$ sudo apt update
test01@test01:~$ sudo apt install mysql-server
test01@test01:~$ sudo systemctl start mysql
test01@test01:~$ sudo systemctl enable mysql
Synchronizing state of mysql.service
with SysV service script
with /lib/systemd/systemd-sysv-install.
Executing:
/lib/systemd/systemd-sysv-install enable mysql
test01@test01:~$ sudo systemctl status mysql
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service;
enabled; vendor preset: enabled)
Active: active (running)
since Sun 2025-01-19 18:58:01 KST; 3min 10s ago
Main PID: 3916 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 9415)
Memory: 363.4M
CGroup: /system.slice/mysql.service
└─3916 /usr/sbin/mysqld
1월 19 18:57:55 test01 systemd[1]:
Starting MySQL Community Server...
1월 19 18:58:01 test01 systemd[1]:
Started MySQL Community Server.
※ ctrl+c 하면 밖으로 나와짐!
test01@test01:~$ sudo mysqld --skip-grant-tables
test01@test01:~$ sudo systemctl start mysql
test01@test01:~$ sudo mysql -u root
Welcome to the MySQL monitor.
Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.40-0ubuntu0.20.04.1 (Ubuntu)
Copyright (c) 2000, 2024, 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>
use mysql; 입력mysql> use mysql;
Reading table information
for completion of table and column names
You can turn off this feature
to get a quicker startup with -A
Database changed
ALTER USER 'root'@'localhost' IDENTIFIED BY '★';mysql>
ALTER USER 'root'@'localhost' DENTIFIED BY '★';
Query OK, 0 rows affected (0.02 sec)
FLUSH PRIVILEGES;mysql> FLUSH PRIVILEGES;
mysql 다른 버전의 경우
set PASSWORD for 'root'@'localhost'=PASSWORD('lululala123'); → Ver 5.7 이상
update mysql.user set password=password('lululala123') where user='root';flush privileges;
→ Ver 5.6이하
update mysql.user set authentication_string=password('lululala123') where user='root';
→ mysql.user 테이블에 password 컬럼이 없는 경우
test01@test01:~$ sudo su
root@test01:/home/test01# cd
root@test01:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.
Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.40-0ubuntu0.20.04.1 (Ubuntu)
Copyright (c) 2000, 2024,
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>
CREATE USER 'test01'@'%' IDENTIFIED BY '★';
Query OK, 0 rows affected (0.03 sec)
mysql >
GRANT ALL PRIVILEGES ON *.* TO 'test01'@'%'
WITH GRANT OPTION;
FLUSH PRIVILEGES;
mysql> exit;
비밀번호 변경하고 싶은 경우
sudo su root
mysql -u root
update user set authentication_string=password('new password') where user = 'test01';
test01@test01:~$ mysql -h localhost -u test01 -p
CREATE DATABASE coin_db;
USE coin_db;
CREATE TABLE stg_coin_daily (
stg_id BIGINT AUTO_INCREMENT PRIMARY KEY,
market VARCHAR(20) NOT NULL,
candle_date_time_utc DATETIME NOT NULL,
candle_date_time_kst DATETIME NOT NULL,
opening_price DECIMAL(20,8),
high_price DECIMAL(20,8),
low_price DECIMAL(20,8),
trade_price DECIMAL(20,8),
candle_acc_trade_price DECIMAL(28,8),
candle_acc_trade_volume DECIMAL(28,8),
prev_closing_price DECIMAL(20,8),
change_price DECIMAL(20,8),
change_rate DECIMAL(10,8),
timestamp_unix BIGINT,
insert_dt DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY uq_coin_daily(market, candle_date_time_kst)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

%21로 써야 함from sqlalchemy import create_engine, text
from sqlalchemy.exc import SQLAlchemyError
def main():
engine = create_engine(
"mysql+pymysql://test01:★@localhost:3306/coin_db"
)
try:
with engine.connect() as conn:
# 쿼리, 세미콜론/컬럼명 주의
result = conn.execute(
text("SELECT NOW() as mytime")
)
row = result.fetchone()
if row:
# row[0]로 값 접근
print(f"DB Time: {row[0]}")
else:
print("No row returned.")
except SQLAlchemyError as e:
print(f"[ERROR] {e}")
if __name__ == "__main__":
main()


-- =========================================
-- 1) dim_date
-- =========================================
CREATE TABLE `dim_date` (
`date_key` INT NOT NULL,
`full_date` DATE NOT NULL,
`year` INT NOT NULL,
`quarter` INT NOT NULL,
`month` INT NOT NULL,
`day` INT NOT NULL,
PRIMARY KEY (`date_key`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;
-- =========================================
-- 2) dim_market
-- =========================================
CREATE TABLE `dim_market` (
`market_key` INT NOT NULL AUTO_INCREMENT,
`market` VARCHAR(20) NOT NULL COMMENT '예: KRW-BTC',
`coin_name` VARCHAR(100) NULL COMMENT '예: Bitcoin',
PRIMARY KEY (`market_key`),
UNIQUE KEY (`market`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;
-- =========================================
-- 3) fact_coin_daily
-- =========================================
CREATE TABLE `fact_coin_daily` (
`date_key` INT NOT NULL COMMENT 'dim_date 조인용',
`market_key` INT NOT NULL COMMENT 'dim_market 조인용',
`opening_price` DECIMAL(20,8),
`high_price` DECIMAL(20,8),
`low_price` DECIMAL(20,8),
`close_price` DECIMAL(20,8) COMMENT 'trade_price를 close_price로 가정',
`volume` DECIMAL(28,8),
`acc_trade_price` DECIMAL(28,8),
`acc_trade_volume` DECIMAL(28,8),
`insert_dt` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'ETL 혹은 적재 시각',
PRIMARY KEY (`date_key`, `market_key`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;
-- =========================================
-- 4) fact_coin_signal
-- =========================================
CREATE TABLE `fact_coin_signal` (
`signal_date_key` INT NOT NULL COMMENT '시그널이 발생한 날짜 (dim_date)',
`market_key` INT NOT NULL COMMENT '어떤 코인/마켓인지 (dim_market)',
`signal_type` VARCHAR(50) NOT NULL COMMENT '예: UPPER_BREAKOUT, LOWER_BREAKOUT',
`breakout_price` DECIMAL(20,8) NULL COMMENT '돌파(또는 하락 돌파) 기준 가격',
`breakout_rate` DECIMAL(10,4) NULL COMMENT '돌파 % (비율)',
`d_plus_x` INT NOT NULL COMMENT 'D+X에서 X (며칠 후를 보는지)',
`return_rate` DECIMAL(10,4) NULL COMMENT '시그널 발생일로부터 D+X일 후 수익률',
`insert_dt` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '시그널 적재 시각',
PRIMARY KEY (`signal_date_key`, `market_key`, `signal_type`, `d_plus_x`)
)
ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;

1. fact_coin_daily (일봉 팩트 테이블)