This document is to provide steps for R & SQL integration. MySQL database is used for this setup.
Setup MySQL Server with default settings. Remember to copy root user/password for later uses.
MySQL components after MySQL components and Workbench install
Edit Windows path to add add MySQL executable
(Windows-> System Properties-> Environment Variables -> Path ->Edit)
Whitelist (Allow) port : 3306
Goto : Control Panel->All Control Panel Items->Windows Defender Firewall -> New Inbound rules
Keep TCP selected, Allow, enter port 3006 -> Finish
Goto : Control Panel->All Control Panel Items->Windows Defender Firewall -> Out Outbound rules
Keep TCP selected, Allow, enter port 3006 -> Finish
Right click on these rules and select enable to activate these rules in the list
On windows Explorer: View -> Hidden Items (checked)
Go to : C:\ProgramData\MySQL\MySQL Server 8.0 Find a line with ‘secure-file-priv=’. Update if import/export data and .SQL file location is different
Start MySQL Workbench from start menu
* Goto manage connections to setup a new database connection
* Enter database server, user and password details
* Schema view should look like this image
* Select scripts to create schema, tables and load data
or you can create schema and tables in workbench UI
* Validate schema
* Validate tables and data
Password less code - R is connected to MySQL database using RMySQL Database Interface driver. While running R Markdown, database user and password is asked.
Using provided database user and password, a connection to MySQL Database is made. Database query executed to select movie, users and rating data set
library(RMySQL)
## Loading required package: DBI
<- rstudioapi::askForPassword("Database username")
user <- rstudioapi::askForPassword("Database password")
password ='localhost'
host #user ='root'
#password='root'
= dbConnect(MySQL(), user = user, password =password, dbname = 'movie_ratings', host = host)
connection ='SELECT usr.user_name, mv.movie_name, rt.rating FROM movie_ratings.user_movie_rating umr JOIN movies mv ON mv.movieId=umr.movieId JOIN users usr ON usr.userId=umr.userId JOIN ratings rt ON rt.rating=umr.rating'
sqlQuery= dbSendQuery(connection, sqlQuery)
result = fetch(result)
data.frame head(data.frame)
## user_name movie_name rating
## 1 Albert Alladin 1
## 2 Albert The Midnight Sky 1
## 3 Albert Avengers: Infinity War 1
## 4 Andoh Alladin 1
## 5 Andoh The Midnight Sky 1
## 6 Andoh Space Sweepers 1
Login / Create account on AWS
On AWS Management Console -> select RDS -> Create Database
RDS -> Databases -> Movies > Update VPC
public available
Using provided database user and password, a connection to MySQL Database is made. Database query executed to select movie, users and rating data set
library(RMySQL)
='msds.cbs1lxtno2zh.us-east-2.rds.amazonaws.com'
host ='AppUser'
user ='!PassAppUSer'
password#Above user and password can be saved as environment variable.
#Also there is point to point security between RDS MySQL instance and local R Studio so RDS MySQL can not be misused
= dbConnect(MySQL(), user = user, password =password, dbname = 'movie_ratings', host = host)
connection ='SELECT usr.user_name, mv.movie_name, rt.rating FROM movie_ratings.user_movie_rating umr JOIN movies mv ON mv.movieId=umr.movieId JOIN users usr ON usr.userId=umr.userId JOIN ratings rt ON rt.rating=umr.rating'
sqlQuery= dbSendQuery(connection, sqlQuery)
result = fetch(result)
data.frame head(data.frame)
## user_name movie_name rating
## 1 Albert Alladin 1
## 2 Albert The Midnight Sky 1
## 3 Albert Avengers: Infinity War 1
## 4 Andoh Alladin 1
## 5 Andoh The Midnight Sky 1
## 6 Andoh Space Sweepers 1
library(RMySQL)
='35.224.102.212'
host ='AppUser'
user ='!PassAppUSer'
password#Above user and password can be saved as environment variable.
#Also there is point to point security between RDS MySQL instance and local R Studio so RDS MySQL can not be misused
= dbConnect(MySQL(), user = user, password =password, dbname = 'movie_ratings', host = host)
connection ='SELECT usr.user_name, mv.movie_name, rt.rating FROM movie_ratings.user_movie_rating umr JOIN movies mv ON mv.movieId=umr.movieId JOIN users usr ON usr.userId=umr.userId JOIN ratings rt ON rt.rating=umr.rating'
sqlQuery= dbSendQuery(connection, sqlQuery)
result = fetch(result)
data.frame head(data.frame)
## user_name movie_name rating
## 1 Albert Alladin 1
## 2 Albert The Midnight Sky 1
## 3 Albert Avengers: Infinity War 1
## 4 Andoh Alladin 1
## 5 Andoh The Midnight Sky 1
## 6 Andoh Space Sweepers 1
* Grant permissions to the user mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, LOAD, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON . TO devops_user@‘%’ with grant option;