AWS RDS로 데이터 관리
소개
AWS에서 관계형 데이터베이스를 처리하는 방법은 다음과 같습니다.
AWS RDS 무료 프레임
간단 작성 - MySQL -
EC2에서의 작업(2020.05.02 추가)
mysql-client 설치
$ sudo apt install mysql-client-core-5.7
EC2 -> RDS에 연결
$ mysql -h RDSのエンドポイント -u RDSで設定したユーザ -p RDSで設定したパスワード
mysql 측에서의 작업(2020.05.02 추가)
사용자 데이터베이스 작성
mysql> CREATE USER 'mysql'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> SET PASSWORD FOR 'mysql'@'%'='MyPassw0rd';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DATABASE awsfree;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT ALL ON awsfree.* TO 'mysql'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE staff (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
staff_name VARCHAR(255) NOT NULL
) ENGINE=InnoDB DEFAULT charset = utf8;
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO staff (staff_name) VALUES ('尼存 太郎');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO staff (staff_name) VALUES ('尼存 花子');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM staff;
+----+---------------+
| id | staff_name |
+----+---------------+
| 1 | 尼存 太郎 |
| 2 | 尼存 花子 |
+----+---------------+
2 rows in set (0.00 sec)
동작 확인(2020.05.02 추가)
pymysql 설치
$ pip install pymysql
python -> mysql 연결 테스트
rds_config.pyrds_host = 'RDSのエンドポイント'
db_user = 'mysql'
db_password = 'MyPassw0rd'
db_name = 'awsfree'
rds_test.pyimport pymysql
import rds_config as RDS
con = pymysql.connect(
host=RDS.rds_host,
user=RDS.db_user,
password=RDS.db_password,
db=RDS.db_name,
cursorclass=pymysql.cursors.DictCursor
)
try:
with con.cursor() as cur:
sql = 'SELECT * FROM staff'
cur.execute(sql)
result = cur.fetchall()
print(result)
except:
print('Error !!!!')
staff 추가sql = 'INSERT INTO staff (staff_name) VALUES (%s)'
cur.execute(sql, ('尼存 次郎'))
con.commit()
연결 테스트$ python rds_test.py
실행 결과[{'id': 1, 'staff_name': '尼存 太郎'}, {'id': 2, 'staff_name': '尼存 花子'}]
결론
$ sudo apt install mysql-client-core-5.7
$ mysql -h RDSのエンドポイント -u RDSで設定したユーザ -p RDSで設定したパスワード
사용자 데이터베이스 작성
mysql> CREATE USER 'mysql'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> SET PASSWORD FOR 'mysql'@'%'='MyPassw0rd';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DATABASE awsfree;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT ALL ON awsfree.* TO 'mysql'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE staff (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
staff_name VARCHAR(255) NOT NULL
) ENGINE=InnoDB DEFAULT charset = utf8;
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO staff (staff_name) VALUES ('尼存 太郎');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO staff (staff_name) VALUES ('尼存 花子');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM staff;
+----+---------------+
| id | staff_name |
+----+---------------+
| 1 | 尼存 太郎 |
| 2 | 尼存 花子 |
+----+---------------+
2 rows in set (0.00 sec)
동작 확인(2020.05.02 추가)
pymysql 설치
$ pip install pymysql
python -> mysql 연결 테스트
rds_config.pyrds_host = 'RDSのエンドポイント'
db_user = 'mysql'
db_password = 'MyPassw0rd'
db_name = 'awsfree'
rds_test.pyimport pymysql
import rds_config as RDS
con = pymysql.connect(
host=RDS.rds_host,
user=RDS.db_user,
password=RDS.db_password,
db=RDS.db_name,
cursorclass=pymysql.cursors.DictCursor
)
try:
with con.cursor() as cur:
sql = 'SELECT * FROM staff'
cur.execute(sql)
result = cur.fetchall()
print(result)
except:
print('Error !!!!')
staff 추가sql = 'INSERT INTO staff (staff_name) VALUES (%s)'
cur.execute(sql, ('尼存 次郎'))
con.commit()
연결 테스트$ python rds_test.py
실행 결과[{'id': 1, 'staff_name': '尼存 太郎'}, {'id': 2, 'staff_name': '尼存 花子'}]
결론
$ pip install pymysql
rds_host = 'RDSのエンドポイント'
db_user = 'mysql'
db_password = 'MyPassw0rd'
db_name = 'awsfree'
import pymysql
import rds_config as RDS
con = pymysql.connect(
host=RDS.rds_host,
user=RDS.db_user,
password=RDS.db_password,
db=RDS.db_name,
cursorclass=pymysql.cursors.DictCursor
)
try:
with con.cursor() as cur:
sql = 'SELECT * FROM staff'
cur.execute(sql)
result = cur.fetchall()
print(result)
except:
print('Error !!!!')
sql = 'INSERT INTO staff (staff_name) VALUES (%s)'
cur.execute(sql, ('尼存 次郎'))
con.commit()
$ python rds_test.py
[{'id': 1, 'staff_name': '尼存 太郎'}, {'id': 2, 'staff_name': '尼存 花子'}]
Reference
이 문제에 관하여(AWS RDS로 데이터 관리), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/sireline/items/da965ff75ed3855758b1텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)