Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

dbConnect-MySQLDriver-method

Connect/disconnect to a MySQL DBMS


Description

These methods are straight-forward implementations of the corresponding generic functions.

Usage

## S4 method for signature 'MySQLDriver'
dbConnect(
  drv,
  dbname = NULL,
  username = NULL,
  password = NULL,
  host = NULL,
  unix.socket = NULL,
  port = 0,
  client.flag = 0,
  groups = "rs-dbi",
  default.file = NULL,
  ...
)

## S4 method for signature 'MySQLConnection'
dbConnect(drv, ...)

## S4 method for signature 'MySQLConnection'
dbDisconnect(conn, ...)

Arguments

drv

an object of class MySQLDriver, or the character string "MySQL" or an MySQLConnection.

dbname

string with the database name or NULL. If not NULL, the connection sets the default daabase to this value.

username, password

Username and password. If username omitted, defaults to the current user. If password is ommitted, only users without a password can log in.

host

string identifying the host machine running the MySQL server or NULL. If NULL or the string "localhost", a connection to the local host is assumed.

unix.socket

(optional) string of the unix socket or named pipe.

port

(optional) integer of the TCP/IP default port.

client.flag

(optional) integer setting various MySQL client flags. See the MySQL manual for details.

groups

string identifying a section in the default.file to use for setting authentication parameters (see MySQL).

default.file

string of the filename with MySQL client options. Defaults to \$HOME/.my.cnf

...

Unused, needed for compatibility with generic.

conn

an MySQLConnection object as produced by dbConnect.

Examples

## Not run: 
# Connect to a MySQL database running locally
con <- dbConnect(RMySQL::MySQL(), dbname = "mydb")
# Connect to a remote database with username and password
con <- dbConnect(RMySQL::MySQL(), host = "mydb.mycompany.com",
  user = "abc", password = "def")
# But instead of supplying the username and password in code, it's usually
# better to set up a group in your .my.cnf (usually located in your home
directory). Then it's less likely you'll inadvertently share them.
con <- dbConnect(RMySQL::MySQL(), group = "test")

# Always cleanup by disconnecting the database
dbDisconnect(con)

## End(Not run)

# All examples use the rs-dbi group by default.
if (mysqlHasDefault()) {
  con <- dbConnect(RMySQL::MySQL(), dbname = "test")
  summary(con)
  dbDisconnect(con)
}

RMySQL

Database Interface and 'MySQL' Driver for R

v0.10.21
GPL-2
Authors
Jeroen Ooms [aut, cre] (<https://orcid.org/0000-0002-4035-0289>), David James [aut], Saikat DebRoy [aut], Hadley Wickham [aut], Jeffrey Horner [aut], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.