PHP cannot connect to MySQL on sql305.infinityfree.com

Website URL

https://bantay.site.je

Error Message

My website is hosted on InfinityFree account if0_43250058, domain bantay.site.je. PHP cannot connect to my MySQL database on sql305.infinityfree.com. It returns SQLSTATE[HY000] [2002] No such file or directory, but the database and tables are visible in phpMyAdmin. Can you check the MySQL connection from my hosting account and advise how to make the app’s storage directories writable by PHP?

Other Information

The database and its tables are visible in phpMyAdmin. The website homepage loads, but the API health endpoint fails. The error shown is SQLSTATE[HY000] [2002] No such file or directory. Please check the MySQL connection from this hosting account and advise how to make the backend storage directories writable by PHP.

Welcome to the InfinityFree community forum! Good to have you here.

  • Most people answering here are volunteers. They’re often the fastest source of help. Admin (the only staff member) checks in about once a day for issues on our end.
  • Volunteers can’t see your account or website. Share what you actually see, like error messages and what happens when you try something. That’s more useful than your theory of what’s wrong.
  • AI tools often get InfinityFree wrong. If a volunteer’s advice differs from what an AI told you, please try the volunteer’s suggestion first.
  • Solving an issue is a joint effort. Try the suggestions you get and report back what happened.

Beep boop, I’m a bot! I dug through the forum to get you started. I sometimes get things wrong though, so do with this what you will until a human has a look.

The No such file or directory error usually means the app is connecting with localhost as the database host, not sql305. Our article on common MySQL connection errors says that message appears when localhost is the hostname, because the script then looks for a database on the same server as your site, and that doesn’t work here.

Link to the article:

phpMyAdmin showing your tables doesn’t rule this out. It uses the right host on its own, while your app uses whatever its config says. If your app reads its settings from a .env file or a similar config, check that the file really has the host you expect. A default of localhost can be used when a value is missing or misnamed.

Could you post the lines of your connection code or config that set the host, database name and username, with the password removed? Also say whether the API health endpoint reads them from a .env file or has them written in the code.

I couldn’t find anything on the forum or in the knowledge base about making storage directories writable for PHP. If you still need that after the connection works, tell me which framework or app this is and the exact error you get when it tries to write.

Not usually, always. There is exactly one reason why you can get that error and it’s because the database connection is using localhost as the database hostname. It’s impossible for it to be anything else.

If you’ve already checked your configuration file, please also check that:

  • Your configuration file is correctly being read.
  • The parameters are correctly being passed to the code where the database connection is being setup.
  • The database connection is built correctly, e.g. that your PDO DSN is correct, or that you’re providing the right parameters in the right order to mysqli_connect.

PHP can read and write any files and directories within the htdocs folder of your website.

If your domain is not your account’s primary domain, i.e. it’s assigned to it’s own directory instead of the main htdocs folder, it can also access files one level above. So if your domain is assigned to the htdocs folder, it can only access the htdocs folder. If it’s assigned to bantay.site.je/htdocs, it can access anything within the bantay.site.je folder.

As long as your app’s storage directory is in a supported location, your app should be able to access it.