Friday, January 2, 2015

RMySQL

  1. Introduction
    1. RMySQL is a database interface and MySQL driver for R
    2. The latest version will always be available here: http://cran.r-project.org/web/packages/RMySQL/index.html
    3. Binaries No Longer Supported
    4. It too difficult to keep binary versions of RMySQL up to date with the latest R version and MySQL version
    5. Always compiling RMySQL from the latest source packages is recommended.
  2. Installation (officer)
    1. Download Rtools from here: http://www.murdoch-sutherland.com/Rtools/
      1. making sure to install the correct version for your R version
      2. Be sure that you place the Rtools bin path in your default path so that R can invoke the compilers
    2. Install a MySQL client library from http://www.mysql.com or http://dev.mysql.com
      1. If you already installed a MySQL server, you may want to re-run the install to ensure that you also installed client header and library files
      2. Note that Xampp doesn't include these
    3. Edit or create the file Renviron.site and add the variable MYSQL_HOME which contains the location of your MySQL install
      1. The file typically isn't created when installing R, so you may need to create it yourself
      2. You will want to place it under the /etc directory in your R Home area
      3. If you don't know where that is, you can issue R.home() at your R prompt
      4. You will be adding a variable named MYSQL_HOME in variable=value syntax
      5. Here's an example: 
        • Location of Renviron.site: C:/PROGRA~1/R/R-2.11~1.0/etc/Renviron.site 
        • Content is: MYSQL_HOME=C:/PROGRA~1/MySQL/MYSQLS~1.0/
    4. Restart R and execute install.packages('RMySQL',type='source') at the R prompt
  3. Installation (personal)

    본 장에서는 Windows에서 RMySQL를 사용하여 원격 MySQL를 연동하는 방법을 설명
    1. Windows 정보
      1. Windows 7
    2. 사전 준비
      1. Rtools 설치
      2. MySQL Server가 설치된 원격 서버
    3. 설명
      1. Connector/C (libmysqlclient) 설치
      2. libmysql.dll 파일 복사
        • Connector/C 설치 위치가 'C:\Program Files\MySQL\MySQL Connector C 6.1' 라고 가정
        • lib 폴더에 opt 폴더를 생성 (32 bit)
        • lib 폴더의 libmysql.lib 를 opt 폴더로 복사 (32 bit)
        • lib 폴더에서 libmysql.dll 를 bin 폴더로 복사 (64 bit)
      3. R 관련 설정
        • R 설치 위치가 'D:\Program Files\R\R-3.1.0' 라고 가정
        • etc 폴더에 Renviron.site 파일 생성
        • 다음 내용 추가: MYSQL_HOME=C:/Program Files/MySQL/MySQL Connector C 6.1
      4. R 시작
        • 다음 명령어 실행: Sys.getenv('MYSQL_HOME')
        • 반환 값: C:/Program Files/MySQL/MySQL Connector C 6.1
      5. RMySQL 사용
        • install.packages('RMySQL',type='source')
        • library(RMySQL)
        • con <- dbConnect ( MySQL () , user ="계정", password ="비번", dbname ="데이타베이스", host ="IP 주소")
        • dbGetQuery (con , " select * from 테이블명 ")

  4. Reference

    1. http://biostat.mc.vanderbilt.edu/wiki/Main/RMySQL
    2. http://cran.r-project.org/web/packages/RMySQL/index.html

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.