skip to Main Content

I am trying to connect Mariadb with my R shinny application in Rstudio. I created a Mariadb using docker-compose.yml and now I am trying to access the mariadb container using

library(RMariaDB)
library(DBI)
library(RMySQL)

# Connect to my-db as defined in ~/.my.cnf
drv <- dbDriver("MySQL")
con <- dbConnect(RMySQL::MySQL(),
        host="mariadb://root@localhost:3306",
        port=3306,
        user="",
        password="root")

above mentioned code but its not working its giving me error

Failed to connect to database: Error: Can’t connect to local server through socket ‘/tmp/mysql.sock’

and

Failed to connect to database: Error: Plugin mariadb could not be loaded:

Does anyone help that what I am doing wrong here or how I can connect to mariadb properly.
I looked for tutorials but I couldn’t find any which explain mariadb with R. if anyone know any tutorial or link which can explain it kindly let em know. Thanks

2

Answers


    1. Install MariaDB Client Library

      sudo apt-get install libmariadb-client-lgpl-dev

    2. Specify MariaDB Client Library Path in R code –

    Libraries

    library(RMariaDB)
    
    library(DBI)
    
    library(RMySQL)
    
    
     
    

    Specify the path to the MariaDB client library

    Sys.setenv(DYLD_LIBRARY_PATH = "/path/to/mariadb/lib")
    

    Connect to my-db as defined in ~/.my.cnf

    drv <- dbDriver("MySQL")
    con <- dbConnect(RMySQL::MySQL(),
            host="mariadb://root@localhost:3306",
            port=3306,
            user="",
            password="root")
    
    1. Check RMySQL Compatibility:
      Ensure that the version of RMySQL you are using is compatible with your R version and MariaDB version.

    2. Check System Environment Variables –
      Verify that the necessary environment variables are set correctly. This may include LD_LIBRARY_PATH on

      Sys.getenv("LD_LIBRARY_PATH")
      Sys.getenv("DYLD_LIBRARY_PATH")

    3. Restart R Session

    Login or Signup to reply.
  1. When MariaDB is in a compose, its not accessed via localhost. Unix sockets aren’t shared between containers.

    host="mariadb://root@localhost:3306"
    

    Should be:

    host="mariadb://root@mariadb:3306"
    

    Assuming your compose service called it mariadb.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search