I am trying to make a connection from Mongo Atlas into R but nothing seems to work I have tried mongolite and RMongo, is there any good solution to make a connection with my atlas mongodb into R studio.
library(mongolite)
mongo<- mongo(collection = "nameofcollection", db = "nameofdb", url = "mongodb://usr:pass#cluster0-shard-00-00-h8acf.mongodb.net:27017,cluster0-shard-00-01-12ucd.mongodb.net:27017,cluster0-shard-00-02-haucd.mongodb.net:27017/dbname?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin", verbose = TRUE)
You need change nameofcollection, nameofdb and dbname for your user password and dbname of atlas.
Change cluster url for yours cluster (atlas give you a url)
1) Go to Atlas (https://cloud.mongodb.com)
2) Go to your cluster, and click on the Connect button
3) Select "Connect your application"
4) In there, you'll see a Connection String, which will allow you to see the host name/URL for the cluster
You can then use the following R code in order to connect to your Atlas MongoDB cluster:
library(mongolite)
mongo_db_user <- "myuseranme"
mongo_db_password <- "mypassword"
mongo_database <- "mydatabase"
mongo_collection <- "mycollection"
mongo_clustername <- "cluster123-abc.mongodb.net"
# the following is the connection string for an Atlas online MongoDB cluster
url_path = sprintf("mongodb+srv://%s:%s#%s/admin",
mongo_db_user, mongo_db_password, mongo_clustername)
mongo_db <- mongo(collection = mongo_collection, db = mongo_database, url = url_path, verbose = TRUE)
data <- data.frame(Date = c("2020-04-21", "2020-04-20"), Returns = c(0.05, 0.02) )
mongo_db$insert(data)
rm(mongo_db) # disconnection
I was struggling with this for a while. Make sure to run - brew install openssl in terminal to set up the ssl connection! Then it worked fine for me. Cheers
Related
I'm using the POSTGRESQL package to access a database and R times out and crashes every time when connecting ("The previous R session was terminated due to an unexpected crash"). Terminal says 'abrt-cli status' timed out.
I've tried accessing the same db through SSH and it works. Our sysadmin thinks the credentials are correct, and says R seems to be working fine for him (although he's doing completely different work that has nothing to do with accessing postgresql databases). I've tried naming the server rather than using 'localhost', just in case, and that doesn't work either. But even if the credentials were wrong, I feel like I'd get an error rather than a crash.
I realize this might have to do with our local configuration, but I'm at the end of my rope. I'd be extremely grateful for any ideas people have.
# Install the package RPostgres
knitr::opts_chunk$set(echo = TRUE)
library(DBI)
library(RPostgreSQL)
library(tidycensus)
# The user needs to input their password
input_password <- rstudioapi::askForPassword("Database password")
db <- [CENSORED, I PROMISE IT'S CORRECT :)]
host_db <- 'localhost'
db_port <- '5432'
db_user <- 'leviem'
db_password <- input_password
drv <- dbDriver("PostgreSQL")
conn <- dbConnect(drv,
dbname = db,
host = host_db,
port = db_port,
user = db_user,
password = db_password)
# List all the tables available
dbListTables(conn)
# Close the connection
dbDisconnect (conn)
I'm trying to use RStudio to connect to the a Google Cloud postgres database. My R code is below.
From the documentation I'm relatively sure I have the correct setup.
# Load the DBI library
library(DBI)
library(RPostgres)
# Helper for getting new connection to Cloud SQL
getSqlConnection <- function(){
con <- dbConnect(
RPostgres::Postgres(),
dbname = 'my_dbname',
host ='my_host',
port = 5432,
user ='username',
password = 'my_password' )
return(con)
}
conn <- getSqlConnection()
res <- dbListTables(conn)
print(res)
When I run the code above I'm getting the error
Error: FATAL: unsupported frontend protocol 1234.5679: server supports 2.0 to 3.0
I was hoping someone might have some advice on the error or how to setup a Rstudio connection to a Google Cloud postgres DB?
Thanks in advance!
I Have my own AWS DocumentDB and I'm trying to connect to it in R using Mongolite Package
I tried to do this with mongolite ssl_options
with this code:
mong <- mongo(collection = "test", db = "test"
,url ='*******************.docdb.amazonaws.com:27017'
,verbose = TRUE
,options = ssl_options(ca= 'rds-combined-ca-bundle.pem',weak_cert_validation = T)
)
But I get this Error :
> Error: No suitable servers found (`serverSelectionTryOnce` set):
> [socket timeout calling ismaster on
> '***********************-central-1.docdb.amazonaws.com:27017']
so i need someone how can solve this problem.
You can connect to Amazon DocumentDB using TLS and the Mongolite package (https://jeroen.github.io/mongolite/index.html) using the following example connection string:
j <- mongo(url = "mongodb://<yourUsername>:<yourPassword>#docdb-2019-02-21-02-57-28.cluster-ccuszbx3pn5e.us-east-1.docdb.amazonaws.com:27017/?ssl=true", options = ssl_options(weak_cert_validation = T, key = "rds-combined-ca-bundle.pem"))
The error you are seeing typically occurs when 1/the URL for the host (Amazon DocumentDB cluster) in the connection string is incorrect or does not match that of the cluster you are trying to connect to or 2/your client machine that you are issuing the connection from is in a different region or VPC than your Amazon DocumentDB cluster.
For additional troubleshooting: https://docs.aws.amazon.com/documentdb/latest/developerguide/troubleshooting.html
I'm writing a R package for the first time. Its called aactr and is hosted on my GitHub: https://github.com/jasonbaik94/aactr
Right now, the package only has one function, aact_connect:
aact_connect <- function(user, password) {
drv <- DBI::dbDriver('PostgreSQL')
con <- DBI::dbConnect(drv,
dbname="aact",
host="aact-db.ctti-clinicaltrials.org",
port=5432,
user=user,
password=password)
}
My concern is that users of my package wouldn't like to input their username and password in their R script for privacy reasons.
What would be a nice workaround to ensure user privacy?
One thought I had: When the user types aact_connect(), a window pops up where the user can input username and password and press Enter, after which the connection would be made. Also, for those without a username or password, I'd put a parameter, init_connection = TRUE, after which this sign up page would load: https://aact.ctti-clinicaltrials.org/users/sign_up
Any others suggestions are greatly welcome!
There are many ways to securely handle database credentials in scripts. See few examples below:
Use configuration files such as yaml saves securely on user's machines for R to read into needed variables. See #Spacedman's answer.
config.yaml
db:
host : localhost
port : 5432
name : mypgdb
user : pg_useR
pwd : ***
R
library(yaml)
config = yaml.load_file("/path/to/config.yml")
dbConnect(drv, host = config$db$host, port = config$db$port,
dbname = config$db$name,
user = config$db$user, password = config$db$pwd)
Use environmental variables that are linked to user or system profiles:
db_creds <- Sys.getenv(c("DB_HOST", "DB_PORT", "DB_NAME", "DB_USER", "DB_PWD"))
con <- DBI::dbConnect(drv,
dbname = db_creds[['DB_NAME']],
host = db_creds[['DB_HOST']],
port = db_creds[['DB_PORT,']],
user = db_creds[['DB_USER']],
password = db_creds[['DB_PWD']])
Use DSNs that maintains connection parameters but requires an ODBC connection which can be run with the generalized odbc package (part of same DBI family as RPostgreSQL). Postgres maintains up-to-date odbc drivers for most operating systems. See R-bloggers post.
odbc.ini
[myPG_DSN]
Driver = PostgreSQL Unicode
Database = mypg_db
Servername = localhost
UserName = pg_useR
Password = ***
R
db <- dbConnect(odbc::odbc(), "myPG_DSN")
I am making the move from RSQLite to RMySQL and I am confused by the user and password fields. FWIW, I'm running Windows 7, R 2.12.2, MySQL 5.5 (all 64 bit), and RMySQL 0.7-5.
I installed RMySQL as prescribed in this previous SO question, and as far as I know it works (i.e., I can load the package with library(RMySQL)). But when I try to run the tutorial from the R data import guide, I get a "failed to connect to database..." error. This is the code from the tutorial from the guide:
library(RMySQL) # will load DBI as well
## open a connection to a MySQL database
con <- dbConnect(dbDriver("MySQL"), user = "root", password = "root", dbname = "pookas")
## list the tables in the database
dbListTables(con)
## load a data frame into the database, deleting any existing copy
data(USArrests)
dbWriteTable(con, "arrests", USArrests, overwrite = TRUE)
dbListTables(con)
## get the whole table
dbReadTable(con, "arrests")
## Select from the loaded table
dbGetQuery(con, paste("select row_names, Murder from arrests",
"where Rape > 30 order by Murder"))
dbRemoveTable(con, "arrests")
dbDisconnect(con)
On the second line I get the following error:
> con <- dbConnect(dbDriver("MySQL"), user = "richard", password = "root", dbname = "pookas")
Error in mysqlNewConnection(drv, ...) :
RS-DBI driver: (Failed to connect to database: Error: Access denied for user 'richard'#'localhost' (using password: NO)
)
I have tried with and without user and password and with admin as user. I have also tried using a dbname that I made before with the command line and with one that doesn't exist.
Any tips? Is there a good reference here? Thanks!
That is most likely a setup issue on the server side. Make sure that networked access is enabled.
Also, a local test with the command-line client is not equivalent as that typically uses sockets. The mysql server logs may be helpful.
First try to connect to MySQL server using MySQL Workbench or command line mysql using the same parameter. If it connects then R should also be able to connect.
Typically this issue comes when MySQL server doesn't allow connections from remote machines.
As people have told you, you can try to connect to the host with other application as mysql workbench. How odd! When I have tried in RStudio to connect to my db with your code without indicate the host in the command I haven't been able to connect.
I have needed to indicate the host ( host = 'localhost' ) in the command.