I used the following code to connect R to my own MySQL server (i.e. localhost server).
con <- dbConnect(MySQL(),user="root",password="********",dbname="try",host="localhost")
dbListTables(con) # to see what all tables the database has
#data(test) (shows error becuase its not yet in R, its still on server)
dbListFields(con, 'test') #to see what all fields the table has
rs <- dbSendQuery(con, "SELECT * FROM test") #data is still on the server
data <- fetch(rs, n = -1) #using fetch to bring data into R
Now I have to connect to someone else's MySQL server (i.e. their IP would be different, the server would be on their system/machine) to get the data from them.
So, what all details do I need and what all modification do I need to do in the code?
Thank You.
Set the correct host (remote server domain name or at a pinch, dotted IP address), user name and password as defined by the admin of the remote MySQL server.
Once that's set in con <- dbConnect(... everything should be the same.
con <- dbConnect(MySQL(),user="fred",password="********",dbname="try",host="yoursql.wherever.com")
Note you might have problems if your local network policy blocks any of the ports that MySQL uses.
Related
I am working on writing an R package to submit to Bioconductor. It needs to fetch 4 databases from a remote mysql server. My question is, what is the right way to go about this with regard to the usernames and passwords, both for the host server and for mysql? So far I have been able to fetch the data by creating an SSH tunnel locally, using:
ssh -L 3307:localhost:3306 username#mycompany.org
And then running the following in R:
library(RMySQL)
library(dbConnect)
options(warn=-1)
getData <- function(){
driver = dbDriver("MySQL");
connection = dbConnect(driver, user='**', password='**', dbname='dbname', host='127.0.0.1',port=3307);
assign("age",dbGetQuery(connection, statement= paste("select * from age")),.GlobalEnv);
assign("biblio",dbGetQuery(connection, statement= paste("select * from biblio")),.GlobalEnv);
}
This fetches me the data I need. What would be the correct way to connect to the server, fetch the data, and then close the connection without exposing my server or passwords, since I want to submit this and publish eventually?
Backstory, I would like to build shiny apps to give to some of our data collectors so they can review what has been collected. We currently house all of our data in a cloud based MySQL server. Ideally, I would like the shiny app to pull data directly from the MySQL server so it can be fully automated without any data pulls and up 24/7.
I have been trying to first just build the connection between R and MySQL using the RMySQL package and can't seem to get it working. I have set up a specific username/password for this connection that is read only(however I have also tried my regular username which has all privileges granted). This is the code I am running;
mydb=dbConnect(
MySQL(),
user='myuser',
password='mypass',
dbname='vgtg',
host='ipaddress',
port=3306,
)
Obviously the 'ipaddress' of the server has been changed for the sake of posting here but it is a generic looking address like
'192.168.1.1'
When I run the code above I get this error message;
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on
'ipaddress' (0)
I have tried looking for previous questions posted but none seem to be exactly this error message that I am receiving. It makes me think that for some reason RMySQL is looking locally for the server when it is actually a cloud based, remote, server.
Also, is there anything more I should set up server side to allow the connection? I do have a server admin to help out but I am not sure how familiar he is with R and likewise I am not particularly familiar with working with servers. He has opened port 3306 for me and is able to see my attempts to connect through the port.
Your syntax is correct with the exception of
port=3306,
You need to drop the comma. That said, the error you received is unrelated to the syntax.
Without knowing the details of your setup, it is hard to diagnose. Where does the MySQL DB reside? For example, if it is on an AWS RDS instance, then the host isn't a standard IP address, it is something like this
mydb.cm1abc2v4mod.us-west-1.rds.amazonaws.com
Assuming that the IP address you used is correct, then the problem is most likely on the server. You need to ensure that port 3306 is open to traffic. Otherwise, R will not be able to connect to the DB.
I'm new on grafana and I need to connect my database (mysql) to it.
I tried to configure the custom.ini file as follow,
I added the next section on database
#################################### Database
####################################
type = mysql
host = localhost:3306
name = grafana (db on my sql server)
user = grafana (user on my sql server)
#### If the password contains # or ; you have to wrap it with triple quotes.
Ex “”"#password;"""
password =***** (password of my sql user)
and the next section on session
#################################### Session ####################################
[session]
provider = mysql (at the beginning)
provider_config = grafana:*******#tcp(localhost:3306)/grafana
provider = mysql (at the end)
When I try to connect to the serve I get this error : “Service init failed: Migration failed err: this authentication plugin is not supported”
I’m new on grafana and I don’t know if I have to set more thinks in the custom.ini
If someone can figure out what is wrong here I will be the happiest man on the world :D
Thanks for the time you spent reading this and a big big thanks to the savior !!
I finally find the problem. The fact is that mysql 8 has some permission faeture which brings a lot of issue for connecting to grafana or read files. So I install mysql 5.7 instead and all was fine !
I will start a new project ==> Data visualization by Rstudio. My data into a server phpMyAdmin (SQL Database).
I will use just 4 table. So in this case I should do a link between my database and Rstudio. I found this question on Stackoverflow enter link description here
I tried by this solution, I started by installed the package mySQL, but it return an error:
package ‘mysql’ is not available (for R version 3.2.2)
Knowing that I'm working on PalantirCloud.
Can you please give me a suggestion how can I resolve this problem?
Thank you.
1st grand ALL PRIVILEGES of remote MySQL server to a specific(database) user.
Then run these commands in Rstudio
install.packages("RMySQL")
library("RMySQL")
mydb = dbConnect(MySQL(),user='root',password='query',dbname='bd_test', host='192.X.X.X')
Where -
User = Remote MySQL database username
Password = Remote MySQL database user's password
dbname = Remote database name
And Host = The Remote server URL/IP address
I am accessing MySQL connections from R via RMySQL.
I found a MySQL command with which to get the database/schema name
SELECT DATABASE();
So I can call this via dbGetQuery() to get it from within my R scripts.
However, I also found that calling summary(connection) automatically gets the database, among other info.
> summary(connection)
<MySQLConnection:(6746,0)>
User: root
Host: localhost
Dbname: my_database
Connection type: Localhost via UNIX socket
No resultSet available
Because of the quickness of the result, I assume the connection stores that information within R, but I don't know how to access its contents.
I tried checking its environment, and even looking in the source of RMySQL to understand it, however I don't have enough experience... yet.
So how do I get the Dbname as a variable (not printed)?
Thanks and greetings from Mx.
While, I don't quite understand why you want the database name as a variable, you can capture it as follows:
db_info <- capture.output(mysqlDescribeConnection(MySQLcon, verbose = T))
Note the use of mysqlDescribeConnection(), rather than summary.
The dbname can be accessed as the fourth element of the output vector:
db_info[4]
[1] " Dbname: my_database "
If you really have plans to use it as a variable, some string manipulation will be necessary.
db <- strsplit(db_info[4], ' ')
final_dbname <- db[[1]][4]
[1] "my_database"