使用 Keyring 包,您可以通过以下方式实现您的功能。
require(keyring)
require(RMySQL)
keyring::keyring_create("set_keyring_password_here") #Remember this password
keyring::key_set("dbname", keyring = "Your_set_keyring_password_here")
keyring::key_set("host", keyring = "Your_set_keyring_password_here")
keyring::key_set("port", keyring = "Your_set_keyring_password_here")
keyring::key_set("user", keyring = "Your_set_keyring_password_here")
keyring::key_set("pass", keyring = "Your_set_keyring_password_here")
keyring::key_set("unix.sock", keyring = "Your_set_keyring_password_here")
m<-MySQL() #set the driver to mysql check your database driver and edit
summary(m)
con<-dbConnect(m, dbname = keyring::key_get("dbname",
keyring = "Your_set_keyring_password_here"),
host=keyring::key_get("host",
keyring = "Your_set_keyring_password_here"),
port=as.numeric(keyring::key_get("port",
keyring = "Your_set_keyring_password_here")),
user=keyring::key_get("user",
keyring = "Your_set_keyring_password_here"),
pass=keyring::key_get("pass",
keyring = "Your_set_keyring_password_here"),
unix.sock=keyring::key_get("unix.sock",
keyring = "Your_set_keyring_password_here"),
)
keyring::keyring_lock("Your_set_keyring_password_here") #Lock keyring after using it.