欢迎加入 TidyFriday !
现在加入即可享受 8 折优惠! See: https://czxa.top/posts/34814/
Last week, I bought a Ubuntu Serve to deply my blog and some shiny applications. This blog demonstrades how to connect MySQL database with R by ‘RMySQL’ package.
Here is the basic connection method, before executing it, you may need to create a database named shiny
in MySQL:
1 | -- Access MySQL |
1 | library(RMySQL) |
When I try to run above codes, I encounter following errors:
Error 1
1 | Error in .local(drv, ...) : |
To fix it, you can modify your connection code by specifying where mysqld.sock
is placed.
1 | con = dbConnect(RMySQL::MySQL(), dbname = 'shiny', username = 'root', password = '12345678', host = 'localhost', port = 3306, unix.sock = '/var/run/mysqld/mysqld.sock') |
You can find the location of mysql.sock
file by running following code in your terminal:
1 | $ cat /etc/mysql/debian.cnf |
Error 2:
1 | Error in .local(drv, ...) : |
Although I can log in MySQL at the terminal using root account, the RMySQL connection fails. At this time, you can first log in your MySQL at the terminal using root account, then reset the password (even if the reset password is the same as the original one, it also needs to be operated):
1 | $ mysql -u root -p12345678; |
Then I make a successful connection!
Following are some basic usage of RMySQL package:
1 | dbRemoveTable(con, "emaillist") |
Shiny’s official website provides some more sophisticated methods for persistent data storage in Shiny apps. For MySQL is:
1 | library(RMySQL) |
Update your browser to view this website correctly. Update my browser now