Connect to MySQL database through dplyr using stored credentials

I would like to access my MySQL database using dplyrwithout having to store my database passwords in text R code. Therefore, I would prefer to reference my file .my.cnf, but since src_mysql has default settings for the host, user and password, the only way I can find this is pretty elegant:

test_db <- src_mysql("test",
                     default.file=path.expand("~/.my.cnf"),
                     host=NULL,
                     user=NULL,
                     password=NULL)

Is there a less accurate way to connect to a MySQL database from dplyrthrough stored credentials?

+4
source share
1 answer

, ( 2014, my.cnf) Hadley ( my.cnf NULL), NULL .

, .Rprofile :

src_mysql_from_cnf <- function(dbname,
                      dir="~/.my.cnf",
                      host=NULL,
                      user=NULL,
                      password=NULL,
                      ...) {
    if(!(file.exists(dir)))
        stop(sprintf("No such file '%s'",dir))
    dplyr::src_mysql(
        dbname,
        default.file=path.expand(dir),
        # explicitly passing null unless otherwise specified.
        host=host,
        user=user,
        password=password,
        ...)
}

test_db <- src_mysql_from_cnf("test")
+5

Source: https://habr.com/ru/post/1615103/


All Articles