I am trying this code to connect to the MariaDB in the Red Hat Linux remote server in R.
con <- DBI::dbConnect(odbc::odbc(),
driver = ????? ,
database = "test_db",
UID = rstudioapi::askForPassword("Database user"),
PWD = rstudioapi::askForPassword("Database password"),
host = "xxx.xx.xx",
port = ????? )
Does this above function help connect to the MariaDB in the remote server?
I am not aware of what goes under the driver argument in the above function in case of MariaDB.
Also How can I find the port number for the remote server I am using?
Please help me with this. Thanks.
Related
I am running an R application in a Docker container and would like to connect to a MySQL 8 database using the RMySQL library. I can connect to the database from the container using the mysql terminal command without any problems, but when I call dbConnect() in my R script I get an unknown SSL connection error (see below).
As I am using the same SSL certificates with the terminal command as well as in my R script, it must have something to do with the MySQL version used by RMySQL. I need to have MySQL 5.7 installed in the container, too, as some other R packages cannot be built otherwise. However, according to the thread below you can specify the directory of the MySQL version to be used when installing RMySQL:
adding RMySQL package to R fails (on Windows)?
So I set $MYSQL_HOME in /etc/R/Renviron.site and called install.packages('RMySQL',type='source').
Apparently you also need to copy some .lib and .dll files to make it work in Windows, but where do I find the equivalent files in Linux?
Database handler script:
#' #import DBI
#' #import RMySQL
connectToDatabase <- function(myproject.db_config) {
dbConfig <- read.properties(myproject.db_config)
print(dbConfig)
dbHandle <- dbConnect(
MySQL(),
dbname = dbConfig$databaseName,
host = dbConfig$host,
port = as.integer(dbConfig$port),
user = dbConfig$user,
password = dbConfig$password
)
return(dbHandle)
}
Config file passed to the read.properties() command:
host=domain.subdomain.de
port=3306
user=john.doe
password=mypassword
databaseName=my_db
ssl-ca=/staging/mysql-ssl/ca.pem
ssl-cert=/staging/mysql-ssl/client-cert.pem
ssl-key=/staging/mysql-ssl/client-key.pem
Error message in R:
Error in connection_create(host, username, password, dbname, as.integer(port), :
Failed to connect: SSL connection error: unknown error number
I found the solution by myself. The SSL certificates were not applied in the dbConnect() call. They need to be defined in a mysql.cnf file which is passed to dbConnect() in the default.file parameter:
dbHandle <- dbConnect(
MySQL(),
dbname = dbConfig$databaseName,
host = dbConfig$host,
port = as.integer(dbConfig$port),
user = dbConfig$user,
password = dbConfig$password,
default.file = dbConfig$configFile
)
Config file passed to the read.properties() command:
host=domain.subdomain.de
port=3306
user=john.doe
password=mypassword
databaseName=my_db
configFile=/staging/config/mysql.cnf
mysql.cnf:
[mysqld]
ssl-ca=/staging/mysql-ssl/ca.pem
ssl-cert=/staging/mysql-ssl/client-cert.pem
ssl-key=/staging/mysql-ssl/client-key.pem
I have two systems. my first system is ubuntu 16.04. which, work as remote.
and another is centos 6. which, run in virtually. In centos, freeswitch installed. I have to send data from freeswitch to remote system means ubuntu.
on centos, my configuration of odbc.ini as :
[freeswitch]
DRIVER = /usr/lib64/libodbcmyS.so
SERVER = <ipaddress>
USER = root
PASSWORD = <password>
PORT = 3306
DATABASE = freeswitch
OPTION = 67108864
and odbcinst.int as below:
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib64/libmyodbc.so
Setup = /usr/lib64/libodbcmyS.so
FileUsage = 1
Threading = 0
after setup above code.
I have to check odbc connection. for this, command fire as :
isql -v freeswitch
but, it show error as :
[IM004][unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed
[ISQL]ERROR: Could not SQLConnect
wrong driver description arise this type of error.
I have set proper driver description. wrongly, setup description mention in driver field.
I think you need to change the line
DRIVER = /usr/lib64/libodbcmyS.so
to
DRIVER = MySQL
Thanks.
How to connect R to mysql? Do we need to install mysql on local host separately? When I try to connect it says
"Error in .local(drv, ...) : Failed to connect to database: Error:
Can't connect to MySQL server on 'localhost' (0)".
What does it mean? I am using R on windows by the way.
You don't show your R code in the post so it is hard to "fix" what might be wrong. Instead I'll just show you some code that works.
To access a mysql database you can use the RMySQL package. For this to work your mysql server must accept remote requests and you must specify the
address (for example the IP address where the server is running)
the username
the password
the database name
Your mysql server should be set up to accept non-local requests unless it's already running locally on the machine where you run R.
library(RMySQL)
mydb = dbConnect(MySQL(), user="username", password="PASSword",
dbname="dbname", host="192.168.13.15")
then you can submit queries in standard SQL language.
indata <- dbGetQuery(mydb, "select * from students")
You do not need a sql client for the R code to work, but if you cannot get it to run within R then you should try to see if you can log onto the server using a manual client. If that is not possible your server is not setup to allow the logins.
I have recently configured MySQL on my Amazon EC2 Ubuntu machine. I am trying to connect MySQL db to RStudio running on the same machine. I am using the R package RMySQL for the same.
My R-code:
library(dbConnect)
library(RMySQL)
con = dbConnect(MySQL(), user='user1', password='pass1', dbname='db1', host='http://ec2-*********.us-west-2.compute.amazonaws.com/')
The error message:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.9/d3.min.js"></script>
Error in mysqlNewConnection(drv, ...) :
RS-DBI driver: (Failed to connect to database: Error: Unknown MySQL server host 'http://ec2-52-24-185-205.us-west-2.compute.amazonaws.com/' (2)
<!-- end snippet -->
You should be able to connect on an Ubuntu machine to a MySQL database using the following:
library("RMySQL")
mySqlCreds <- list(dbhostname = "YourHostIP",
dbname = "YourDB",
username = "YourUserName",
pass = "YourPassword",
port = 1111
)
drv <- dbDriver("MySQL")
dbConnect(drv, host=mySqlCreds$dbhostname, dbname=mySqlCreds$dbname,
user=mySqlCreds$username, password=mySqlCreds$pass, port = mySqlCreds$port)
Where of course the port is whichever port you or your DBA has specified...
Thank you all,
I finally resolved the issue. I had to configure my user credentials first in mysql.
MySQL query:
create user 'user1'#'my-ec2-public-ip';
set password for 'user1'#'my-ec2-public-ip' = PASSWORD('pass1');
grant all on *.* to 'user1'#'my-ec2-public-ip';
R-code:
library(RMySQL)
#establishing connection
con = dbConnect(MySQL(), user='user1', password='pass1', dbname='db1')
Rightly quoted by grubjesic and Drew Pierce I should not mention the host, as mysql db and rstudio are running on the same machine.
I have my unixodbc odbc.ini configure file like this:
[test]
Driver = /usr/local/lib/libmyodbc5-5.1.8.so
Description = Connector/ODBC 5.1.8 Driver DSN
SERVER = 127.0.0.1
PORT = 3306
USER = root
Password =
DATABASE = test
OPTION =
SOCKET =
And the problem is that it will not use the database as specified above, which is 'test'.
What I have to do is to manually execute a direct sql to change to database and run my query:
SQLExecDirect(stmt, "USE test", SQL_NTS);
SQLExecDirect(stmt, "SELECT * FROM mytable", SQL_NTS);
Any idea on how should I get rid of the 'USE test' which is a mysql command.
Why is unixodbc not setting 'test' as the default db since it's already specified in the conf file?
As on windows the driver manager (unixODBC in this case) only acts on the Driver tag, all other entries in the DSN are up to the driver to interpret. It doesn't notice there is a database= entry and know by magic that in this driver it should execute a "USE" command, and for another call SQLSetConnectAttr( SQL_ATTR_CURRENT_CATALOG ).
On my copy of the MySQL driver, it certainly uses the database= entry. However, I would check that 1. The copy of the driver you are using is built to use the unixODBC lib to access the shared config file (libodbcinst.so), or the driver is reading it directly, and is reading the same ini file as unixODBC. Possibly check with strace to see what ini is opened after the driver is loaded. Maybe try setting ODBCINI=/path/to/your/odbc.ini
[test]
Driver = /usr/local/lib/libmyodbc5-5.1.8.so
Description = Connector/ODBC 5.1.8 Driver DSN
SERVER = 127.0.0.1
PORT = 3306
USER = root
Password =
**DATABASE** = test
**OPTION** = **3**
**SOCKET** =
and can change options between 1,2 too