5

I just created my first SQL database using MAMP. (It is simple- just a list of pets.) I would like to load it into R. Here is what I wrote:

install.packages("dbConnect") library(dbConnect) mypets=dbConnect(MySQL(),user="root", host="localhost1234/DatabaseGrace") 

This error is returned:

Error in mysqlNewConnection(drv, ...) : RS-DBI driver: (Failed to connect to database: Error: Unknown MySQL server host 'localhost1234/DatabaseGrace' (2)) 

Any idea what this means or how I can solve it?

4 Answers 4

8

Thank you @duffymo and @Lorenz.

Summary:

  • The database is stored on my computer, so host did need to be localhost as you suggested.

Here is what ended up working.

install.packages("RMySQL") install.packages("dbConnect") library(dbConnect) dbGrace=dbConnect(MySQL(),user="root", host="localhost", dbname="DatabaseGrace", password="root", unix.sock="/Applications/MAMP/tmp/mysql/mysql.sock") 

Thanks, all!

Sign up to request clarification or add additional context in comments.

Comments

4

I think the host name should be "localhost", without the 1234.

If you intended that to mean the port, I'd check to make sure that MySQL is indeed listening on that port. The default value is 3306; the admin would have to make a conscious decision to change that.

The usual notation is "localhost:3306" in Java. If you must include the port number, try separating it from the host name with a colon.

Can you connect to MySQL using the admin shell?

Have you GRANTed permission to localhost to connect to database named DatabaseGrace? If not, MySQL won't allow you to connect.

http://dev.mysql.com/doc/refman/5.1/en/grant.html

localhost suggests to me that the database is running on your local machine. Is that true? If not, localhost isn't the right host name. It might also mean that you have a firewall between your machine and the database which prevents you from connecting.

I get a lot of mileage out of cutting & pasting any errors I get into Google to see if anyone else has ever experienced my problem. Here's the first hit.

1 Comment

Thank you @duffymo for responding. When I remove the number, the error changes. When I am looking at the data table online, I see Server: localhost:#### »Database: DatabaseGrace. (I'm omitting the four digit number because I don't know if it should be private. It isn't 3306.) Without the four digit number, the error becomes "Error in mysqlNewConnection(drv, ...) : RS-DBI driver: (Failed to connect to database: Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)" Thanks again, all!
1

The host is the computer name. So put simply localhost. Select the correct database in a second step.

1 Comment

Thank you @Lorenz for responding. I've tried both host="localhost:DatabaseGrace" host="localhost/DatabaseGrace" host="localhost" With the last one, the error has changed to "Error in mysqlNewConnection(drv, ...) : RS-DBI driver: (Failed to connect to database: Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) )"
0

this works for me with XAMPP on MAC:

library(RMySQL) con=dbConnect(MySQL(), host='localhost', unix.sock='/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock', user='root', password='...' ) df=dbGetQuery(con,'SHOW DATABASES') print(df) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.