[R] How to install RMySQL package on Window

20 篇文章 1 订阅
16 篇文章 0 订阅

Installing RMySQL package under Window

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

Connecting R with MySQL can be somewhat difficult using Windows. The package RMySQL is not available as a precompiled zip-archive. It needs the installed libmysqll.dll library to be working and must therefore be compiled on your machine. Linux and Mac OSX have build-in compilers, Windows does not.

This tutorial shows how to install RMySQL under Windows and give some hints how to avoid too much extra work then updating R.

  • Step 1: Requirements

    • R (surprise! :-) )
    • RTools (compiling environment for Windows)
    • MySQL-Server (usually the community edition, INCLUDING the C-Connector)
  • Step 2: Setup and Configuring

    I assume that you already have an installed R and the MySQL-Server. This tutorial uses a 64-bit-system (which you also may have nowadays, though). The next step is to install RTools. The offered install options are fine, additional libraries like TCL/TK etc. are maybe needed if you want to compile packages who need them.

    这里写图片描述

    The next window is really arkward. Just leave the the two checkboxes checked and move on to the next step.

    这里写图片描述

    Editing the System Path is important and tells R where the compiler is located. There should be four entries. Please be aware when updating RTools: This sometimes causes double entries (can be deleted).

    这里写图片描述

  • Step 3: Tell Windows (and R) where the MySQL-Libraries are

    RMySQL needs the libmysql.dll to compile successfully. The file should be stored in lib-Folder of your MySQL installation. Usually the path should be C:\Program Files\MySQL\MySQL Server 5.5\lib. The problem is that RMySQL expects it to be stored in the bin-Folder (usually C:\Program Files\MySQL\MySQL Server 5.5\bin). So you have to copy it by hand. This step is essential for successful compiling!

    Telling R where to find the libraries can be done by three different options: Command line, environment file (.Renviron) or environment variables. Using command line options must be passed to R at every start which makes it complicated to use IDEs like Rstudio. Using the enviroment file is easier but you have to copy it into every new installation/update of R (which also requires writing rights). I suggest using environment variables as this causes no problems/extra work - espacially when updating R.

    Your environment variables are located under Control Panel → User Accunts → Change my environment variables. My environment variables look like this (the window is in German as you might have noticed ;-) ). Now you can add a new variable called MYSQL_HOME with the value C:\Program Files\MySQL\MySQL Server 5.5*. The last backslash should be added to avoid compiling errors (compare some comments below). If this does not help, pease try to use slashes instead of backslashes (C:/Program Files/MySQL/MySQL Server 5.5/).
    My installed packages are located at *C:\myRLib using the variable R_LIBS (this is optional).

    这里写图片描述

  • Step 4: Compiling

    Now restart R if you have already opened it and install RMySQL from source: install.packages(“RMySQL”, type = “source”)
    Your output should look like this. I truncated all warnings which could usually be ignored.

> install.packages("RMySQL", type = "source")
Warning in install.packages :
  package 'RMySQL' is not available (for R version 3.0.1)
trying URL 'http://ftp5.gwdg.de/pub/misc/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' ...
** Paket 'RMySQL' erfolgreich entpackt und MD5 Summen überprüft
checking for $MYSQL_HOME... C:\Program Files\MySQL\MySQL Server 5.5
** libs
gcc -m64 -I"C:/PROGRA~1/R/R-30~1.1/include" -DNDEBUG -I"C:\Program Files\MySQL\MySQL Server 5.5"/include    -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -std=gnu99 -mtune=core2 -c RS-DBI.c -o RS-DBI.o
gcc -m64 -I"C:/PROGRA~1/R/R-30~1.1/include" -DNDEBUG -I"C:\Program Files\MySQL\MySQL Server 5.5"/include    -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -std=gnu99 -mtune=core2 -c RS-MySQL.c -o RS-MySQL.o
gcc -m64 -shared -s -static-libgcc -o RMySQL.dll tmp.def RS-DBI.o RS-MySQL.o C:\Program Files\MySQL\MySQL Server 5.5/bin/libmySQL.dll -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/PROGRA~1/R/R-30~1.1/bin/x64 -lR
installing to c:/myRLib/RMySQL/libs/x64
** R
** inst
** preparing package for lazy loading
Creating a generic function for 'format' from package 'base' in package 'RMySQL'
Creating a generic function for 'print' from package 'base' in package 'RMySQL'
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
MYSQL_HOME defined as C:\Program Files\MySQL\MySQL Server 5.5
* DONE (RMySQL)

No you can load your new packages and access your local MySQL-Server.

library(RMySQL)
con <- dbConnect(MySQL(), host="127.0.0.1", port= 3306,    user="user", password = "password", dbname="db")

I hope this will help someone installing RMySQL properly.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值