Skip to contents

RMariaDB is a database interface and MariaDB driver for R. This version is aimed at full compliance with the DBI specification, as the successor to the RMySQL package.

Goals and non-goals

RMariaDB aims to:

  • implement the DBI specification for MariaDB and MySQL, and to keep proving it by running the DBItest suite as part of its own tests
  • take over from RMySQL as the R interface to MySQL and MariaDB
  • speak to both MariaDB and MySQL servers, which differ in SQL dialect and in other details
  • map MariaDB types to R types that do not lose information: 64-bit integers via bit64, binary columns via blob, and date-time columns with an explicit connection time zone
  • support parameterised queries through dbBind() and the params argument, so that values never have to be pasted into the SQL string

It is explicitly not trying to:

  • define what the database interface looks like: the generics, their semantics and the specification are DBI’s, this package supplies the MariaDB implementation
  • translate R code into SQL: queries are handed to dbSendQuery() and friends as SQL strings
  • carry forward the RMySQL extensions that DBI does not define: dbApply(), dbMoreResults() and dbNextResults() were removed as finished experiments
  • bundle a client library: the package is built against MariaDB Connector/C or libmysqlclient, which has to be installed on the system
  • store your credentials: connection parameters come from dbConnect() or a MariaDB option file, and passwords are better kept in something like keyring

Hello World

library(DBI)
# Connect to my-db as defined in ~/.my.cnf
con <- dbConnect(RMariaDB::MariaDB(), group = "my-db")

dbListTables(con)
dbWriteTable(con, "mtcars", mtcars)
dbListTables(con)

dbListFields(con, "mtcars")
dbReadTable(con, "mtcars")

# You can fetch all results:
res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
dbFetch(res)
dbClearResult(res)

# Or a chunk at a time
res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
while(!dbHasCompleted(res)){
  chunk <- dbFetch(res, n = 5)
  print(nrow(chunk))
}
# Clear the result
dbClearResult(res)

# Disconnect from the database
dbDisconnect(con)

Installation

Binary packages for OS X or Windows can be installed directly from CRAN:

install.packages("RMariaDB")

The development version from github:

# install.packages("pak")
pak::pak("r-dbi/DBI")
pak::pak("r-dbi/RMariaDB")

Discussions associated with DBI and related database packages take place on R-SIG-DB. The website Databases using R describes the tools and best practices in this ecosystem.

Installation from source on Linux or OS X currently requires MariaDB Connector/C, preferably in version 2.3.4/3.0.3 or later. With older versions, character and blob columns do not work reliably. Alternatively, Oracle’s libmysqlclient can be used.

Connector/C

On recent Debian or Ubuntu install libmariadb-dev.

sudo apt-get install -y libmariadb-dev

On Fedora, CentOS or RHEL we need mariadb-devel:

sudo yum install mariadb-devel

On OS X use mariadb-connector-c from Homebrew:

brew install mariadb-connector-c

MySQL client library

On recent Debian or Ubuntu install libmysqlclient-dev.

sudo apt-get install -y libmysqlclient-dev

On Fedora, CentOS or RHEL we need mysql-devel:

sudo yum install mysql-devel

Follow instructions to enable the MySQL yum repository if the above command attempts to install MariaDB files.

On OS X use mysql-connector-c from Homebrew:

brew install mysql-connector-c++

MariaDB configuration file

Instead of specifying a username and password in calls to dbConnect(), it’s better to set up a MariaDB configuration file that names the databases that you connect to most commonly. This file should live in ~/.my.cnf and look like:

[database_name]
option1=value1
option2=value2

If you want to run the examples, you’ll need to set the proper options in the [rs-dbi] group of any MariaDB option file, such as /etc/my.cnf or the .my.cnf file in your home directory. For a default single user install of MariaDB, the following code should work:

[rs-dbi]
database="test"
user="root"
password=""

Acknowledgements

Many thanks to Christoph M. Friedrich, John Heuer, Kurt Hornik, Torsten Hothorn, Saikat Debroy, Matthew Kelly, Brian D. Ripley, Mikhail Kondrin, Jake Luciani, Jens Nieschulze, Deepayan Sarkar, Louis Springer, Duncan Temple Lang, Luis Torgo, Arend P. van der Veen, Felix Weninger, J. T. Lindgren, Crespin Miller, and Michal Okonlewski, Seth Falcon and Paul Gilbert for comments, suggestions, bug reports, and patches to the original RMySQL package, and to all contributors (of code and discussions) to this package.