Why can I no longer connect to the database?
Dec 21, 2006 11:13 AM
Why can I no longer connect to the database?

Here's a technical tip for those progammers out there using any form of database backed web development.

Suddenly, last week, my development server stopped connecting to my database. I tried everything to fix it. I expanded GRANTS to the Tomcat user, I added new users and tried them. I tried updating the MySQL driver and I changed connection pooling libraries from the Apache Commons DBCP to the c03p library. I even, eventually, reinstalled the database. Nothing worked. Every time I'd rerun my Maven tests or even run the WAR on my Tomcat server the connection would fail with an error; something like Connection Refused to localhost:3306. However, the mysql client worked fine as did the mysql adminstrator GUI. Only my java code and the Tomcat server seemed to have trouble connecting. Of course, if I can't build my classes and run my tests the development server is pretty much out of commission.

Definitely something weird... Let's look at this one last time.

Localhost... why does that sound important?

Doh! I had accidentally modified my /etc/hosts file while testing some unrelated functionality. In the process I had deleted the line that says:

127.0.0.1 localhost

The mysql client and the mysqladministrator gui were connecting through the socket. Only the web application tried to access the database through the URL! But localhost:3306 didn't exist as far as the development machine was concerned.

Anyway, not the most technically advanced tip to contribute the web but its a good reminder that sometimes you should check the basics first.

Hosted by 4Syndication Copyright © 2007 4Syndication RSS 2.0