天天看點

在AWS上建立MySQL資料庫并在RStudio裡連接配接與操作資料庫

今天參加了一個SQL的Workshop,本來早上九點實在是對我有點早想着不來的,不過發現并沒有用過AWS,就還是來看一看。這裡是這次Workshop的Github連接配接:Learning Analytics Curriculum & Teaching

詳情還是參見Github,這裡僅為簡單羅列重點内容,有問題可以私信!

用R連接配接的好處就是可以直接在R裡面操作資料庫并進行資料分析,不需要到處.csv的中間檔案,然後再加載進R的工作空間,也算是效率上的小提升吧!

擁有一個Amazon Web Services(AWS)賬号

注冊位址:https://aws.amazon.com/

*注:需要信用卡,但是以下操作皆免費,也許需要科學上網。

在AWS上建立一個MySQL的執行個體

  1. 登入你的AWS賬号
  2. 找到

    Services

    → \rightarrow →

    Database

    → \rightarrow →

    RDS

  3. 點選

    Create Database

  4. Choose a database creation method

    下選擇

    Standard Create

  5. Engine options

    裡選擇

    MySQL

  6. Templates

    選擇

    Free tier

  7. Settings

    中設定

    DB instance identifier

    ,如

    sqltest

  8. Settings

    下的

    Credential settings

    中設定

    username

    password

  9. 将Under

    Connectivity

    中的

    Additional connectivity configuration

    裡的

    Publicly accessible

    勾選為

    Yes

  10. Additional configuration

    裡設定

    database name

    ,如

    testdb

  11. 不要勾選

    Automatic backups

  12. 點選

    Create database

設定Security Group

  1. Security Groups

    裡點選

    Inbound

    然後點選

    Edit

  2. 點選

    Add Rule

    選擇

    SQL/Aurora

    修改

    Source

    My IP

用R連接配接AWS上的資料庫

  • DBI: A database interface definition for communication between R and relational database management systems. All classes in this package are virtual and need to be extended by the various R/DBMS implementations.
  • RMySQL: Legacy ‘DBI’ interface to ‘MySQL’ / ‘MariaDB’ based on old code ported from S-PLUS. A modern ‘MySQL’ client based on ‘Rcpp’ is available from the ‘RMariaDB’ package.
library(DBI)
library(RMySQL)

db_user <- 'admin'
db_password <- 'your_password'
db_name <- 'testdb'
# 這個是RDS=>Databases=>Connectivity&security裡面的Endpoint
db_host <- 'sqltest.c24zvetb1ohx.us-east-2.rds.amazonaws.com'
db_port <- 3306

mydb <- dbConnect(MySQL(), user = db_user, password = db_password, dbname = db_name, host = db_host, port = db_port)

summary(mydb)
           

在R裡面操作資料庫