VSCode ํน์ Web Browser ๋ฅผ ํตํด ์คํํ๊ณ , ds_study ํ๊ฒฝ์์ ์์ ํฉ๋๋ค.
Jupyter notebook ๊ณผ mysql ์ ๋ชจ๋ sql_ws ํด๋์์ ์์ํฉ๋๋ค.
mysqldump --set-gtid-purged=OFF -h"์๋ ํฌ์ธํธ" -P 3306 -u
admin -p zerobase police_station >backup_police.sql
use zerobase;
delete from police_station;
โpython.ipyb ํ์ผ์ ๋ง๋ ํ ์ค๋ฅธ์ชฝ ์๋จ์ ds_study๋ฅผ ์ ํํ๋ค.
ds_study๊ฐ ์์ ์ terminal์ conda create -n ds_study
python=3.8 ์ ์
๋ ฅํด์ค๋ค
์ฌ๊ธฐ์ ์ค๋ฅ๊ฐ ๋์ anaconda๋ฅผ ์ค์นํด์ค๋ค
๋ ์ค๋ฅ๊ฐ ๋์ ํ๊ฒฝ๋ณ์ ์ค์ ํ anaconda์์
conda create -n ds_study python=3.8๋ฅผ ์ ๋ ฅ ํ
scode์์ ์ค์ ์ ํด์ฃผ์๋ค.
Python ์ผ๋ก MySQL ์ ์ ํ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
pip install mysql-connector-python
import mysql.connector -์ค์น ํ์ธ
> Local Database ์ฐ๊ฒฐ
**local = mysql.connector.connect
(
host = "localhost",
user = "root",
password = "๋น๋ฐ๋ฒํธ"
)
AWS RDS(database -1)์ฐ๊ฒฐ
remote = mysql.connector.connect
(
host ="์๋ํฌ์ธํธ",
port = 3306,
user ="admin",
password ="๋น๋ฐ๋ฒํธ"
)close Database
remote.close()
โ์คํ ํ ๋ฐ๋ก๋ฐ๋ก ๋ซ์์ค์ผ ๋์ค์ ๋ณต์กํด์ง์ง ์๋๋ค.
Local MySQL์ zerobase์ ์ฐ๊ฒฐ
local =mysql.connector.connect
(
host ="localhost",
user ="root",
password ="๋น๋ฐ๋ฒํธ",
database ="zerobase"
)
local.close()
AWS RDS(database-1)์ zerobase์ ์ฐ๊ฒฐ
remote = mysql.connector.connect
(
host ="์๋ํฌ์ธํธ",
port = 3306,
user ="admin",
password ="๋น๋ฐ๋ฒํธ",
database ="zerobase"
)
remote.close()
-์ ์ ํ cursor๋ฅผ ๋ง๋ ํ exeute๋ก ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋ด์ฃผ๋ ์ฝ๋
ํ ์ด๋ธ ์์ฑ
remote =mysql.connector.connect(
host = "์๋ํฌ์ธํธ",
port = 3306,
user = "admin",
password ="๋น๋ฐ๋ฒํธ",
database ="zerobase"
)
cur = remote.cursor()
cur.execute("CREATE TABLE sql_file (id int, filename varchar(16))")
remote.close()
๋ฐ์ ํฐ๋ฏธ๋์์ ํ์ธ (vscode)
์๋ฒ์ ์ ์
mysql -h "์๋ํฌ์ธํธ" -P 3306 -u admin -p zerobase
show tables;
desc sql_file;
-> ์์ฑ๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
-ํ ์ด๋ธ ์ญ์
import mysql.connector
remote = mysql.connector.connect(
host = "database-1.cvs6igoc6b14.us-east-2.rds.amazonaws.com",
port =3306,
user ="admin",
password ="๋น๋ฐ๋ฒํธ",
database ="zerobase"
)
cur = remote.cursor()
cur.execute("DROP TABLE sql_file")
remote.close()
๋ฐ์ ํฐ๋ฏธ๋์์ ํ์ธ (vscode)
mysql> desc sql_file;
ERROR 1146 (42S02): Table 'zerobase.sql_file' doesn't exist
-1.test03.sql ์์ฑ
์ผ์ชฝ์ ํ์ผ ์์ฑ ํ ์ ๋ ฅ
CREATE TABLE sql_file
(
id int,
filename VARCHAR(16)
);
-2. ์คํ (python.ipynbํ์ผ์์)
remote = mysql.connector.connect(
host = "database-1.cvs6igoc6b14.us-east-2.rds.amazonaws.com",
port =3306,
user ="admin",
password ="๋น๋ฐ๋ฒํธ",
database ="zerobase"
)
cur = remote.cursor()
sql =open ("test03.sql").read() -ํ์ผ์ ์ด๊ณ ์ฝ์ด์ฃผ์ธ์
cur.execute(sql)
remote.close()
โtest03.sql ๊ฐ์ ๊ฒฝ๋ก๋ผ ์ด๋ ๊ฒ ์ ์ง๋ง ๋ค๋ฅธ ๊ฒฝ๋ก๋ผ๋ฉด ๊ฒฝ๋ก ์ ์ด์ค์ผํ๋ค.
3. ํ์ธ
sql_file์ด ๋ค์ ์๊ฒจ๋จ
-SQL File๋ด์ Query๊ฐ ์ฌ๋ฌ ๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ
1. test04.sql ์์ฑ
์ผ์ชฝ์ ํ์ผ ์์ฑ ํ ์ ๋ ฅ
INSERT INTO sql_file VALUES(1,"test01.sql")
INSERT INTO sql_file VALUES(2,"test02.sql")
INSERT INTO sql_file VALUES(3,"test03.sql")
INSERT INTO sql_file VALUES(4,"test04.sql")
2. ์คํ-(python.ipynbํ์ผ์์)
remote = mysql.connector.connect(
host = "database-1.cvs6igoc6b14.us-east-2.rds.amazonaws.com",
port =3306,
user ="admin",
password ="๋น๋ฐ๋ฒํธ",
database ="zerobase"
)
cur = remote.cursor()
sql = open("test04.sql").read()
for result_iterator in cur.execute(sql, multi=True):
if result_iterator.with_rows:
print(result_iterator.fetchall())
else:
print(result_iterator.statment)
remote.commit()
remote.close()
3. ํ์ธ
show tables;
select *from sql_file