简体   繁体   English

如何连接R与MySQL或如何安装RMySQL包?

[英]How to Connect R with MySQL or how to install RMySQL package?

I am new in R and i am trying to connect R with MySQL. 我是R的新手,我正在尝试将R连接到MySQL。 I have installed mysql-5.5.22-winx64 and R-2.12.0 for 64 bit. 我已经为64位安装了mysql-5.5.22-winx64R-2.12.0 I have already set MYSQL_HOME environment path(C:\\Program Files\\MySQL\\MySQL Server 5.5) and trying to follow these steps: 我已经设置了MYSQL_HOME environment path(C:\\Program Files\\MySQL\\MySQL Server 5.5)并尝试按照以下步骤操作:

  1. Install latest RTools from here 从这里安装最新的RTools
  2. install MySQL or header and library files of mysql 安装MySQL或mysql的头文件和库文件
  3. create or edit file C:\\Program Files\\R\\R-2.12.1\\etc\\Renviron.site and add line like MYSQL_HOME=C:/mysql (path to your mysql files) 创建或编辑文件C:\\Program Files\\R\\R-2.12.1\\etc\\Renviron.site并添加如MYSQL_HOME=C:/mysql (mysql文件的路径)
  4. copy libmysql.lib from mysql/lib to mysql/lib/opt to meet dependencies. libmysql.libmysql/lib复制到mysql/lib/opt以满足依赖关系。
  5. copy libmysql.dll to C:\\Program Files\\R\\R-2.12.1\\bin or to windows/system32 directory . libmysql.dll复制到C:\\Program Files\\R\\R-2.12.1\\binwindows/system32 directory
  6. run install.packages('RMySQL',type='source') and wait while compilation will end. 运行install.packages('RMySQL',type='source')并等待编译结束。 but still i am getting 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 the solution:- You can see the solution in detail on my blog . 最后我得到了解决方案: - 您可以在我的博客上详细查看解决方案。

I was working for last 2 days on Installation of R with RMySQL package, finally got the solution for that, here are the steps to install RMySQL package:- 我在使用RMySQL包安装R的最后2天工作,最后得到了解决方案,这里是安装RMySQL包的步骤: -

  1. DOWNLOAD 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* 
  2. SET THE FOLLOWING ENVIRONMENT 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. * 

    Be sure that the following paths areincluded in your Windows PATH variable: \\Rtools\\2.14\\bin \\Rtools\\2.14\\MinGW\\bin \\Rtools\\2.14\\MinGW64\\bin 确保Windows PATH变量包含以下路径: \\ Rtools \\ 2.14 \\ bin \\ Rtools \\ 2.14 \\ MinGW \\ bin \\ Rtools \\ 2.14 \\ MinGW64 \\ bin

  3. CREATE FOLDER AND COPY FILES 创建文件夹和复制文件

      * 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* 
    1. RUN COMMANDS 运行命令

      a. 一个。 Install.Packages: Run R GUI by clicking on the R icon on desktop or from Start menu. Install.Packages:通过单击桌面上的R图标或从“开始”菜单运行R GUI。 Type INSTALL.PACKAGES(“RMySQL”,type=”Sources”). 输入INSTALL.PACKAGES(“RMySQL”,type =“Sources”)。 This will download the required software from repositories. 这将从存储库下载所需的软件。

      b. Command Prompt: Copy the downloaded zip file (in step 4.a.) and paste it under R installation folder. 命令提示符:复制下载的zip文件(在步骤4.a.中)并将其粘贴到R安装文件夹下。 Go to start menu and open Command Prompt. 转到“开始”菜单并打开“命令提示符”。 Go to the R installation folder and type R CMD INSTALL RMySQL_0.8-0.tar.gz 转到R安装文件夹,然后键入R CMD INSTALL RMySQL_0.8-0.tar.gz

      *COMMANDS: *命令:

      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* library(RMySQL)drv = dbDriver(“MySQL”)con = dbConnect(drv,host =“localhost”,dbname =“test”,user =“root”,pass =“root”)album = dbGetQuery(con,statement = “select * from t_master”)专辑*

您现在可以跳过所有复杂的步骤,只需执行install.packages("RMySQL")

I ran into this over the weekend at a hackathon on Mac OSX - took me a solid 4 hours to piece everything together despite having a few reference materials (mentioned at the end). 我周末在Mac OSX上遇到了一个黑客马拉松 - 尽管有一些参考资料(最后提到),我花了4个小时将所有东西拼凑在一起。 I didn't find an easy walk-through, so I decided to post one while it is fresh in my mind. 我没有找到一个简单的步行,所以我决定发布一个,虽然它在我心中是新鲜的。

I'm not sure of the compatibility with Windows, but hopefully these instructions will make it easier for you too. 我不确定与Windows的兼容性,但希望这些说明也会使您更容易。

I was trying to get R and MySQL to communicate in a local environment (there may need to be changes for a server environment). 我试图让R和MySQL在本地环境中进行通信(可能需要对服务器环境进行更改)。 I use XAMPP (though I didn't use RMySQL for the connection), but in the end I was able to use a PHP page to write an R file, execute that file, and have R write to a MySQL table. 我使用XAMPP(虽然我没有使用RMySQL进行连接),但最后我能够使用PHP页面编写R文件,执行该文件,并将R写入MySQL表。 To the best of my knowledge this only works for MacOSX... 据我所知,这仅适用于MacOSX ...

All software used was in dmg form so no binary installs necessary. 使用的所有软件都是dmg格式,因此不需要二进制安装。

  1. Download R and run some basic commands to make sure that you have it working. 下载R并运行一些基本命令以确保它有效。

  2. In R, you need to install RODBC (if you don't have it already). 在R中,您需要安装RODBC(如果您还没有)。 Type this into the R console. 在R控制台中键入此项。

install.packages("RODBC")

This installs RODBC, but since OS Mavericks, certain files are no longer included, so you get an error message 这会安装RODBC,但自从OS Mavericks以来,某些文件不再包含在内,因此您会收到错误消息

ODBC headers sql.h and sqlext.h not found 找不到ODBC头sql.h和sqlext.h

and you need to get the sql.h and sqlext.h files in the right place. 并且您需要在正确的位置获取sql.h和sqlext.h文件。

To do this the easiest way, make sure that you have homebrew installed (easy instructions). 要以最简单的方式执行此操作,请确保已安装自制软件 (简单说明)。 Then use this code in terminal to make the install. 然后在终端中使用此代码进行安装。

Once that's done, you enter into the R console one more time 完成后,再次进入R控制台

install.packages("RODBC")
  1. Search MySQL for the appropriate ODBC installation . MySQL中搜索适当的ODBC安装 I'm running Mac OSX 10.6 so I downloaded the dmg and installed it. 我正在运行Mac OSX 10.6,所以我下载了dmg并安装了它。 This took care of itself. 这照顾好自己。

  2. Now comes the tricky part. 现在是棘手的部分。 Apparently Mac OX took out the ODBC Administrator after a recent OS release, so you need to download ODBC Manager ( http://www.odbcmanager.net/ ). 显然,Mac OX在最近的操作系统发布后取出了ODBC管理器,因此您需要下载ODBC管理器( http://www.odbcmanager.net/ )。 It too is a dmg file so just drag and drop to your utilities folder. 它也是一个dmg文件,所以只需拖放到您的实用程序文件夹即可。

I had difficulties with the 5.3.6 dmg install (kept failing), so I installed 5.2.7 instead. 我在5.3.6 dmg安装时遇到了困难(保持失败),所以我安装了5.2.7。

  1. Open ODBC Manager. 打开ODBC管理器。 You need to configure the DSN, so click the tab "System DSN" and click "add". 您需要配置DSN,因此请单击“系统DSN”选项卡,然后单击“添加”。

  2. You'll get a popup window asking you to select a driver. 你会看到一个弹出窗口,要求你选择一个驱动程序。 Mine had "MySQL ODBC 5.2 Driver" based on my MySQL ODBC install. 我有基于MySQL ODBC安装的“MySQL ODBC 5.2 Driver”。 Click "Ok". 单击“确定”。 If you don't see the driver, then you need to confirm that the MySQL ODBC installed. 如果没有看到驱动程序,则需要确认已安装MySQL ODBC。

  3. In the next popup window, make the Data Source Name (DSN) whatever you want - but remember that this is the name you need to use to call from R. In the keyword area below (keywords will be in quotes and the value will be in parentheses), ADD 在下一个弹出窗口中,根据需要创建数据源名称(DSN) - 但请记住,这是您从R调用时需要使用的名称。在下面的关键字区域中(关键字将在引号中,值将为在括号中),ADD

    "database" (with value of your database name) “database”(包含数据库名称的值)

    "server" (for the local environment do NOT use localhost - instead use the local IP address 127.0.0.1. *** This was the KEY piece for me) “server”(对于本地环境不使用localhost - 而是使用本地IP地址127.0.0.1。***这是我的关键部分)

    "uid" (database user ID) “uid”(数据库用户ID)

    "pwd" (database password) “pwd”(数据库密码)

    "socket" (not sure if this was required, but after multiple tutorials it was left in my configuration and things work, so maybe you need it. You can find your socket location in my.cnf - do a spotlight search. The socket file location is under CLIENT) “socket”(不确定这是否是必需的,但经过多次教程之后,它留在我的配置中并且工作正常,所以也许你需要它。你可以在my.cnf找到你的套接字位置 - 做一个聚光灯搜索。套接字文件位置在CLIENT下)

    Here's what my configuration looked like: 这是我的配置:

    DSN ("test" - this was the at the top) DSN(“测试” - 这是在顶部)

    database ("televisions") 数据库(“电视”)

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

    uid ("root") uid(“root”)

    pwd ("") pwd(“”)

    server ("127.0.0.1") 服务器(“127.0.0.1”)

  4. In R, execute below - I believe these last 3 steps need to be done every time you start R and before you make a MySQL query. 在R中,执行下面 - 我相信每次启动R并在进行MySQL查询之前,都需要完成最后3个步骤。

    library(RODBC) 库(RODBC)

  5. Make sure that you've turned on MySQL and Apache from the XAMPP control panel. 确保您已从XAMPP控制面板打开MySQL和Apache。

  6. Then execute 然后执行

    odbcConnect("test") - notice how I used my DSN in the double quotes. odbcConnect(“test”) - 注意我在双引号中使用DSN的方式。 Interchange as necessary. 必要时互换。

This should get you up and running. 这应该让你开始运行。 You can read other tutorials about making MySQL queries in R. 您可以阅读有关在R中进行MySQL查询的其他教程。

I hacked this together from a lot of great posts on Stack Overflow (thanks everyone!), random other sites/email exchange histories, and the "R In A Nutshell" book by Joseph Adler, but let me know if I missed something or it's unclear. 我从Stack Overflow(感谢所有人!),随机的其他网站/电子邮件交换历史以及Joseph Adler撰写的“R In A Nutshell”一书中的很多帖子中共同攻击了这一点,但如果我错过了某些内容,请告诉我,或者是不清楚。

Good luck! 祝好运!

I also spent a few hours trying to make this work in Windows 10, getting errors. 我还花了几个小时试图在Windows 10中完成这项工作,从而获得错误。 The fixes I found for RMySQL were messy and complex but RODBC has more painlessly and elegantly lead to a solution. 我发现RMySQL的修复程序既凌乱又复杂,但RODBC更加轻松,优雅地导致了解决方案。 I had R, RStudio, MySQL Server, MySQL Workbench and the following additional steps were required to make it work: 我有R,RStudio,MySQL服务器,MySQL Workbench,并且需要以下额外步骤才能使其工作:

  1. Install the package in RStudio install.packages(RODBC) 在RStudio install.packages(RODBC)安装软件包
  2. Download and install the MySQL ODBC connector here 在此处下载并安装MySQL ODBC连接器
  3. Configure the MySQL ODBC connector. 配置MySQL ODBC连接器。 Here are some instructions. 是一些说明。 Just search from the start screen on Windows 10 for 'ODBC' and it pops up that window. 只需在Windows 10的开始屏幕上搜索“ODBC”,它就会弹出该窗口。 Get the connection parameters right and use the test button to make sure it's working. 获取正确的连接参数并使用测试按钮确保它正常工作。 It shows a list of your databases in a drop down menu once it connects. 它连接后会在下拉菜单中显示您的数据库列表。 In "Data Source Name" give it a name which will go into the R script, say 'mysql_odbc' 在“数据源名称”中给它一个名称,它将进入R脚本,比如'mysql_odbc'

Now you can connect, run a query and disconnect: 现在您可以连接,运行查询并断开连接:

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

It is not a direct answer but still you may find it helpful: 这不是一个直接的答案,但你仍然可以发现它有用:

  1. Use a more up to date version of R (currently at 2.15) 使用更新的R版本(目前为2.15)

  2. On Windows platforms I'd rather use RODBC + Windows MySQL driver, unless you are in a environment with heterogenous platforms (ie Linux and Windows) where code is heavily shared among team members. 在Windows平台上,我宁愿使用RODBC + Windows MySQL驱动程序,除非您处于具有异构平台(即Linux和Windows)的环境中,其中代码在团队成员之间大量共享。 And even then choosing between using RMySQL and RODBC in the same script depending on the platform it runs is a simple if() {...} else {...} 甚至然后在相同的脚本中选择使用RMySQL和RODBC,具体取决于它运行的平台是一个简单的if() {...} else {...}

Notice that I am not saying there are no success stories with what you are trying to do, but IMHO you'll be up and running sooner with the above. 请注意,我并不是说没有成功案例与你想要做的事情有关,但恕我直言,你将会更快地完成上述工作。

This worked for me: Using Windows XP SP3 32bit OS, R Studio, Rv2.15.3 这对我有用:使用Windows XP SP3 32位操作系统,R Studio,Rv2.15.3

Follow the instructions from: http://biostat.mc.vanderbilt.edu/wiki/Main/RMySQL 按照以下说明操作: http//biostat.mc.vanderbilt.edu/wiki/Main/RMySQL

However if you have downloaded MySQL edition 5.6, you will have to copy libmysql.lib present for example in 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 in the opt folder. 但是,如果您已下载MySQL版本5.6,则必须复制libmysql.lib ,例如在此路径中: D:\\Program Files\\MySQL\\MySQL Server 5.6\\lib并创建一个新文件夹,如: D:\\Program Files\\MySQL\\MySQL Server 5.6\\lib\\opt并将上面的libmysql.lib粘贴到opt文件夹中。 Then the Renviron.site file will have this: MYSQL_HOME=D:/Program Files/MySQL/MySQL Server 5.6 . 然后Renviron.site文件将具有: MYSQL_HOME=D:/Program Files/MySQL/MySQL Server 5.6 Please note the forward slash. 请注意正斜杠。

Finally follow instructions from install.packages line as provided in the link above. 最后按照上面链接中提供的install.packages行的说明进行操作。 I had to do a few trials to get it finally right. 我不得不做一些试验才能最终得到它。

IF THIS ERROR IN R 如果这个错误在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 R 然后从外面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 JUST FROM INSIDE R 或者只是来自内部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

Follow the guide here: 按照这里的指南:

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

I followed it, and it worked like a charm. 我跟着它,它就像一个魅力。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM