DEV Community

Mahesh K
Mahesh K

Posted on • Edited on

R Language : Connect to MySQL database

When you are processing the data in R. You may find that most of the online websites and the dashboards make use of the MySQL database. By default there does not seem to be any official driver that I have noticed for the mysql. However you have plenty of libraries that allows you to connect with MySQL.

Few of them are - RMySQL, RODBC and RJDBC.

Here for this example we will make use of the RMySQL to show you how to connect with the MySQL database. You can check out the video demo- R Language How to Connect MySQL Database.

Let's start with the first step.

Install the library.

install.packages("RMySQL")
Enter fullscreen mode Exit fullscreen mode

Then we can connect to the database. Fill in the respective details like username, password etc.

library(RMySQL)

mydb = dbConnect(MySQL(), 
user='user', password='password', 
dbname='database_name', host='host')
Enter fullscreen mode Exit fullscreen mode

From this point onwards you have to run the query for your specific SQL database operations.

You can check out the specific queries in the reference manual.

If you are using RStudio, you can also check out the tabular data output based on how you process your data.

Though a lot of R developers make use of the RMysql. I'd recommend you to check out other libraries as well. And then decide which one is the right for your workflow.

I hope the information here helps you work with the mysql and mariadb database using R lang.

Top comments (0)