How to connect R with MySQL or how to install RMySQL package?

I am new to R and I am trying to connect R to MySQL. I installed mysql-5.5.22-winx64 and R-2.12.0 for 64 bit. I already installed MYSQL_HOME environment path(C:\Program Files\MySQL\MySQL Server 5.5) and tried to follow these steps:

  • Install the latest RTools here
  • install mysql or mysql header and library files
  • create or edit the file C:\Program Files\R\R-2.12.1\etc\Renviron.site and add a line like MYSQL_HOME=C:/mysql (path to your mysql files)
  • copy libmysql.lib from mysql/lib to mysql/lib/opt to match the dependencies.
  • copy libmysql.dll to C:\Program Files\R\R-2.12.1\bin or to the windows/system32 directory .
  • run install.packages('RMySQL',type='source') and wait for the compilation to complete. but still I get this error: -

     *> install.packages('RMySQL',type='source') --- Please select a CRAN mirror for use in this session --- trying URL 'http://ftp.iitm.ac.in/cran/src/contrib/RMySQL_0.9-3.tar.gz' Content type 'application/x-gzip' length 165363 bytes (161 Kb) opened URL downloaded 161 Kb * installing *source* package 'RMySQL' ... ERROR: configuration failed for package 'RMySQL' * removing 'C:/PROGRA~1/R/R-212~1.0/library/RMySQL' * restoring previous 'C:/PROGRA~1/R/R-212~1.0/library/RMySQL' The downloaded packages are in 'C:\Users\sharad\AppData\Local\Temp\RtmpdQHwCb\downloaded_packages' Warning message: In install.packages("RMySQL", type = "source") : installation of package 'RMySQL' had non-zero exit status* 

Please suggest me how can I solve this problem?

Finally, I got a solution: - You can see the solution in detail on the blog .

I worked the last 2 days on installing R with the RMySQL package, finally got a solution for this, here are the steps for installing the RMySQL package: -

  • DOWNLOAD THE SOFTWARE FROM THE FOLLOWING LINKS:

      * a. R2.13.2: Download R from http://cran.stat.sfu.ca/index.html b. RTools 214: Download RTools from http://cran.cict.fr c. RMySQL 0.8-0.tar.gz: Download RMySQL from http://biostat.mc.vanderbilt.edu/wiki/main/RMySQL/RMySQL_0.8-0.tar.gz d. MySQL Server 5.0: download it from http://dev.mysql.com e. RSTUDIO (optional): download it from http://rstudio.org* 
  • INSTALL THE FOLLOWING ENVIRONMENTAL VARIABLES

      * a. MYSQL_HOME : <drive>/path to MySQL installation folder eg MYSQL_HOME= C:\Program Files\MySQL\MySQL Server 5.5\ b. R_HOME: <drive>/path to R installation eg R_HOME=C:\Program Files\R\R-2.13.2\ c. PATH: Modify path to accommodate the above variables. * 

    Ensure that the following paths are included in the Windows PATH variable: \ Rtools \ 2.14 \ Bin \ Rtools \ 2.14 \ MinGW \ Bin \ Rtools \ 2.14 \ MinGW64 \ BIN

  • CREATE FOLDERS AND COPIES

      * a. OPT: Create a folder OPT under C:\Program Files\MySQL\MySQL Server 5.5\lib and copy MYSQLLIB.LIB the above path. Also copy libmysql.dll to <drive>\<path>\R\R-2.14.0\bin\(64 bit) Or <Drive>\<path>\R\R-2.14.0\bin\i386\ (32 bit) and to C:\Windows\System32. b. Renviron.site: create or edit a file <DRIVE>\<path>\R\R-2.14.0\etc\Renviron.site and add a line: MYSQL_HOME ="C:/Program Files/MySQL/MySQL Server 5.5/" NB: USE FORWARD SLASH AND DOUBLE QUOTES HERE c. libMySQL.dll: Copy this file to C:\Program Files\R\R-2.13.2\bin\i386 as well as C:\Program Files\R\R-2.13.2\bin* 
    • RUN COMMANDS

      but. Install.Packages: Launch the R GUI by clicking on the R icon on the desktop or in the Start menu. Type INSTALL.PACKAGES ("RMySQL", type = "Sources"). This will download the necessary software from the repositories.

      b. Command line: copy the downloaded zip file (in step 4.a.) and paste it into the installation folder R. Open the "Start" menu and open the "Command line". Go to the installation folder R and enter R CMD INSTALL RMySQL_0.8-0.tar.gz

      * TEAM:

      Library (RMySQL) drv = dbDriver ("MySQL") con = dbConnect (drv, host = "localhost", dbname = "test", user = "root", pass = "root") album = dbGetQuery (con, statement = "select * from t_master") album *

+8
mysql installation r rmysql
source share
7 answers

This is not a direct answer, but you may find it useful:

  • Use the more modern version of R (currently 2.15)

  • On Windows platforms, I would rather use the RODBC + Windows MySQL driver if you are not in an environment with heterogeneous platforms (such as Linux and Windows), where the code is heavily shared between team members. And even then, the choice between using RMySQL and RODBC in the same script depending on the platform it runs is a simple if() {...} else {...}

Note that I am not saying that there are no successful stories with what you are trying to do, but IMHO, you will work earlier and work with the above.

0
source share

Now you can skip all the complicated steps and just install.packages("RMySQL")

+9
source share

I ran into this last weekend in a Mac OSX hackathon - it took me a solid 4 hours to put it all together, despite having a few reference materials (mentioned at the end). I did not find an easy walk, so I decided to publish it while it is fresh in my mind.

I'm not sure about compatibility with Windows, but hopefully these instructions will make it easier for you as well.

I tried to get R and MySQL to communicate in a local environment (maybe a server environment might need changes). I use XAMPP (although I did not use RMySQL to connect), but in the end I was able to use the PHP page to write the R file, execute this file and write R to the MySQL table. As far as I know, this only works for MacOSX ...

All the software used was in the form of dmg, so no binary settings are needed.

  • Download R and run some basic commands to make sure you have a job.

  • In R, you need to install RODBC (if you don't already have one). Enter this in console R.

 install.packages("RODBC") 

This installs RODBC, but with OS Mavericks some files are no longer included, so you get an error

ODBC headers sql.h and sqlext.h not found

and you need to get sql.h and sqlext.h files in the right place.

To make this the easiest way, make sure you have installed homebrew (simple instructions). Then use this code in the terminal to complete the installation.

After that, you again enter the console R

 install.packages("RODBC") 
  1. Find MySQL for an appropriate ODBC installation . I am running Mac OSX 10.6, so I downloaded dmg and installed it. It took care of itself.

  2. Now comes the hard part. Apparently, Mac OX took out the ODBC administrator after a recent OS version, so you need to download ODBC Manager ( http://www.odbcmanager.net/ ). It is also a dmg file, so just drag it to the utilities folder.

I had difficulty installing 5.3.6 dmg install (crash), so instead I installed 5.2.7.

  1. Open ODBC Manager. You need to configure DSN, so click on the System DSN tab and click on add.

  2. A pop-up window will appear asking you to select a driver. I had a "MySQL ODBC 5.2 Driver" based on my installation of MySQL ODBC. Click OK. If you do not see the driver, you need to confirm that ODBC MySQL is installed.

  3. In the next pop-up window, make the data source name (DSN) what you want, but remember that this is the name you should use to call from R. In the underlying area of ​​keywords (keywords will be in quotation marks and the value will be in round brackets), ADD

    "database" (with the value of your database name)

    "server" (for a local environment, DO NOT use localhost - use the local IP address 127.0.0.1 instead. *** This was the key for me)

    "uid" (database user id)

    "pwd" (database password)

    "socket" (not sure if this was necessary, but after a few lessons it stayed in my configuration and everything works, so maybe you need it. You can find your socket location in my.cnf - do a spotlight search The location of the socket file is under the CLIENT)

    Here my configuration looked like this:

    DSN ("test" - it was the top)

    database ("TVs")

    socket ("/Applications/XAMPP/xamppfiles/var/mysql.sock")

    uid ("root")

    pwd ("")

    server ("127.0.0.1")

  4. In R, follow below - I believe that these last 3 steps should be done every time you start R and before you make a MySQL query.

    Library (RODBC)

  5. Make sure you enable MySQL and Apache from the XAMPP control panel.

  6. Then do

    odbcConnect ("test") - notice how I used my DSN in double quotes. Change as necessary.

This should help you. You can read other tutorials about creating MySQL queries in R.

I hacked it together out of a lot of great posts about stack overflows (thanks everyone!), Random other sites / messaging history and Joseph Adler's “R In A Nutshell” book, but let me know if I missed something or it is unclear.

Good luck

+4
source share

This worked for me: using 32-bit Windows XP SP3, R Studio, Rv2.15.3

Follow the instructions: http://biostat.mc.vanderbilt.edu/wiki/Main/RMySQL

However, if you downloaded MySQL version 5.6, you will need to copy libmysql.lib , for example, along this path: D:\Program Files\MySQL\MySQL Server 5.6\lib and create a new folder, as in: D:\Program Files\MySQL\MySQL Server 5.6\lib\opt , and paste the above libmysql.lib into opt . Then the Renviron.site file will have the following: MYSQL_HOME=D:/Program Files/MySQL/MySQL Server 5.6 . Pay attention to the slash.

Finally, follow the instructions from the install.packages line, as indicated in the link above. I had to do some tests to get a final solution.

0
source share

IF THIS IS AN ERROR IN R

 library(RMySQL) #----------- Loading required package: DBI Error : .onLoad failed in loadNamespace() for 'RMySQL', details call: i$Location error: $ operator is invalid for atomic vectors Error: package or namespace load failed for 'RMySQL' 

THEN FROM OUTSIDE IN R

 set MYSQL_HOME=F:/Program Files/MySQL/MySQL Server 5.6 

AND BACK INSIDE:

 library(RMySQL) #Loading required package: DBI #MYSQL_HOME defined as F:/Program Files/MySQL/MySQL Server 5.6 

OR ONLY FROM INSIDE R

 Sys.setenv(MYSQL_HOME = "F:/Program Files/MySQL/MySQL Server 5.6") library(RMySQL) #Loading required package: DBI #MYSQL_HOME defined as F:/Program Files/MySQL/MySQL Server 5.6 
0
source share

Follow directions here:

http://www.ahschulz.de/2013/07/23/installing-rmysql-under-windows/

I followed him and it worked like a charm. :)

0
source share

I also spent several hours trying to make this work on Windows 10, getting errors. The fixes I found for RMySQL were dirty and complex, but RODBC led the solution more painlessly and elegantly. I had R, RStudio, MySQL Server, MySQL Workbench, and it took the following additional steps to work:

Now you can connect, run the request and disconnect:

 library(RODBC) cursor <- odbcConnect("mysql_odbc", uid="root", pwd="HaysPuffyWalton5") out <- sqlQuery(cursor, "SELECT * FROM emp WHERE deptno = 10"); close(cursor); 
0
source share

All Articles