首先在R控制台输入:install.packages('RMySQL')
报错如下:
------------------------- ANTICONF ERROR ---------------------------
Configuration failed because libmysqlclient was not found. Try installing:
* deb: libmariadb-client-lgpl-dev (Debian, Ubuntu 16.04)
libmariadbclient-dev (Ubuntu 14.04)
* rpm: mariadb-devel | mysql-devel (Fedora, CentOS, RHEL)
* csw: mysql56_dev (Solaris)
* brew: mariadb-connector-c (OSX)
If libmysqlclient is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a libmysqlclient.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
--------------------------------------------------------------------
ERROR: configuration failed for package ‘RMySQL’
* removing ‘/private/var/root/Library/R/3.2/library/RMySQL’
* restoring previous ‘/private/var/root/Library/R/3.2/library/RMySQL’
The downloaded source packages are in
‘/private/tmp/RtmplBQwje/downloaded_packages’
Warning message:
In install.packages("RMySQL", type = "source") :
installation of package ‘RMySQL’ had non-zero exit status
方法二:http://jingyan.baidu.com/article/335530da8b2b0419cb41c338.html
brew是一个软件管理工具类似于ubuntu apt-get工具。
brew安装成功后,开始安装报错缺失的依赖包命令如下:
Mac:~ root# brew update
Mac:~ root# brew install mariadb-connector-c
再次在R控制台执行:install.packages('RMySQL')
成功,具体链接数据库简单demo如下:
> library(RMySQL)
Loading required package: DBI
Warning message:
package ‘DBI’ was built under R version 3.2.5
> conn <- dbConnect(MySQL(),dbname="rmysql",username="root",password="123456")
> user = dbGetQuery(conn,'select * from user')
> dbDisconnect(conn)
[1] TRUE
> user
id username
1 1 111
2 2 322
> conn <- dbConnect(MySQL(),dbname="rmysql",username="root",password="123456")
> user = dbGetQuery(conn,'select * from user')
> user
id username
1 1 111
2 2 322
> user = dbGetQuery(conn,'select * from user where id = 1')
> user
id username
1 1 111
>
关于数据库的创建部分请参考:http://blog.fens.me/r-mysql-rmysql/