R and MySQL set up - mysql

So I'm trying to set up a MySQL database in R, and after reading through many tutorials and StackOverflow posts I'm still not able to setup a connection. I'm retrieving financial data for healthcare companies via the Quandl web API, and want to store some of the data in a SQL database and retrieve. Here is my code:
#Install and load Quandl package
install.packages("Quandl")
library(Quandl)
#Install and load SQL package
install.packages("RMySQL")
library(RMySQL)
#Authenicate API key
Quandl.api_key("________")
#Load top 10 biggest Healthcare compaines in Fortune 500 according to
#http://fortune.com/2015/06/20/fortune-500-biggest-healthcare-companies/
CVS <- Quandl("WIKI/CVS")
McKesson <- Quandl("WIKI/MCK")
UnitedHealthGroup <- Quandl("WIKI/UNH")
AmerisourceBergern <- Quandl("WIKI/ABC")
ExpressScriptsHolding <- Quandl("WIKI/ESRX")
CardinalHealth <- Quandl("WIKI/CAH")
Walgreens <- Quandl("WIKI/WBA")
Johnson_Johnson <- Quandl("WIKI/JNJ")
Anthem <- Quandl("WIKI/ANTM")
Aetna <- Quandl("WIKI/AET")
con <- dbConnect(MySQL(),
user = 'root',
password = 'password',
host = 'localhost',
dbname = 'healthcare')
I've installed mysql-5.7.17-winx64 and it's in the same directory that I'm working in in RStudio. When I try to run the dbConnect() command, I get an error saying:
'Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on 'localhost' (0)'
Is there something I need to do with the SQL files? What is going wrong?

Related

How can I connect to a moto mysql instance?

I created an RDS DB Instance using a mocked boto3 rds client. Here's how I set it up in my conftest.py
#pytest.fixture
def aws_credentials():
"""Mocked AWS Credentials for moto."""
os.environ["AWS_ACCESS_KEY_ID"] = "testing"
os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
os.environ["AWS_SECURITY_TOKEN"] = "testing"
os.environ["AWS_SESSION_TOKEN"] = "testing"
#pytest.fixture
def rds_client(aws_credentials, aws_region):
with mock_rds():
client = boto3.client("rds", region_name=aws_region)
yield client
Following the example here (https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.Python.html) I set up my mysql connector like this:
db_instance = rds_client.create_db_instance(DBInstanceIdentifier="TestDBInstanceIdentifier",
DBInstanceClass="db.m4.large", Engine="mysql",
MasterUsername="root", DBName="TestDBName")
print("RDS Instance-----------------------------------------------------")
print(db_instance)
host = db_instance['DBInstance']['Endpoint']['Address']
port = db_instance['DBInstance']['Endpoint']['Port']
user = db_instance['DBInstance']['MasterUsername']
dbname = db_instance['DBInstance']['DBName']
print("Starting connection")
token = rds_client.generate_db_auth_token(DBHostname=host, Port=port, DBUsername=user, Region=aws_region)
mydb = mysql.connector.connect(host=host, database=dbname, user=user, passwd=token, port=port)
However, the connector can't find the DB:
FAILED test_read_rds_db - mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL server host 'TestDBInstanceIdentifier.aaaaaaaaaa.us-east-1.rds.amazonaws.com' (8)
Has someone been able to set this up before?
Moto does not offer this functionality. It mocks the AWS API, but does not expose any RDBMS-functionality.
You could look into Localstack instead. It uses Moto in the background to mock calls to AWS, but offers features on top of that such as the ability to connect to an RDS instance.
See the docs here: https://docs.localstack.cloud/aws/rds/

How to import .DB file by connecting MYSQL in R

I have a .db file like "sakila"
library(RMySQL)
library(DBI)
mysqlconnection = dbConnect(MySQL(), user = 'root', password = '123456', dbname = 'sakila',host = 'localhost')
dbListTables(mysqlconnection)
It reports the error:
Error in .local(drv, ...) :
Failed to connect to database: Error: Unknown database 'sakila.db'
How do I fix it?
I hope to import the .db file in Mysql by using R.

Getting Unknown MySQL Error while executing queries

Configurations:
R version: 3.6.3
Aurora MySQL Cluster: 5.7
Library: RMySQL
Dbconnect Method
con <- dbConnect(
RMySQL::MySQL(),
dbname = dbName,
host = "127.0.0.1",
user = user,
port = port,
password = pass
)
Connecting to the Aurora DB using SSH:
ssh -N -L <local_port>:<amazon_rds_cluster_url>:<sql_port> -i <pem_file> ubuntu#<ip>
Query and result:
query <- "SELECT column_name FROM table_name;"
result <- dbGetQuery(con, query)
When I am calling the query directly, it is executed as intended.
When I am calling it from a method in a package, I am getting the following error:
Error in .local(conn, statement, ...) :
could not run statement: Unknown MySQL error
I am not sure why I am seeing the error.
EDIT:
method <- function(con, query) {
output <- dbGetQuery(con, query)
return(output)
}
result <- package::method(con, query)
try this, I removed some parameters from you code.
mydb = dbConnect(MySQL(), user='root', password='password', dbname='my_database', host='localhost')

Connecting MySQL with R

This is how I am trying to connect MySQL to R.
db <- dbConnect(MySQL(), user='username', password='pwd',dbname=dbx, host = 'local', port = 3306)
But I'm getting this error:
Error in .local(drv, ...) : Failed to connect to database: Error:
Plugin caching_sha2_password could not be loaded: The specified module
could not be found.
What to do?
You can create a function to retrieve the query.
library(RMySQL)
sqlQuery <- function (query) {
# creating DB connection object with RMysql package
DB <- dbConnect(MySQL(), user="user", password="password",
dbname="databaseName", host="host")
# close db connection after function call exits
on.exit(dbDisconnect(DB))
# send Query to obtain result set
rs <- dbSendQuery(DB, query)
# get elements from result sets and convert to dataframe
result <- fetch(rs, -1)
# return the dataframe
return(result)
}
And then just:
new_dataframe <- sqlQuery("SELECT * from table")
Hope that helps

RMySQL does not work as part of a script

When I run
library(RMySQL)
mydb = dbConnect(MySQL(), user = "XX", password = "XX", dbname = "XX", host = "XX")
on the R console it works,
however when I put save it as a yy.R file and run it as R CMD BATCH yy.R, or as source("yy.R") it says
Error in mysqlNewConnection(drv, ...) :
RS-DBI driver: (Failed to connect to database: Error: Can't connect to MySQL server on 'XX' (111)
)
Calls: dbConnect -> dbConnect -> mysqlNewConnection -> .Call
Execution halted
could I check if RMySQL only runs on the console? Thanks!
Perhaps you can specify the driver with RMySQL::MySQL(), this works for me in scripts.