Question

Cant connect to MySQL with localhost

Hi, I’m new to Digital Ocean,

recently set up a droplet from scratch following tutorials and encountered an error when connecting to a database, so I tried a droplet using one click app, selecting MySQL on 18.04. Im encountering exactly the same problem. I’ve tried numerous solutions after searching google and this community for the past few hours but nothing has fixed it.

Running a connection test returns the error ‘Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)’, I’ve checked socket location and port is correct in /etc/mysql/mysql.conf.d/mysqld.cnf which it is. Further scripts keep returning ‘can’t connect to localhost’, all database info is correct and connection works in terminal. I’ve started, restarted, re-installed mysql driver, now its driving me crazy!

Can anyone advise?

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I wonder if you’re attempting to connect remotely and need to set up remote user and privileges? I set up a $5 Droplet using the MySQL One-click app just now. After logging into the Droplet command line via ssh I tested:

  • local direction connection

Testing both sudo mysql and mysql -u root -p commands allowed me to connect to MySQL on the localhost.

  • localhost connection

Testing mysql -u root -h localhost -p also worked.

  • remote connection

I followed the instructions in this tutorial (for Ubuntu 18.04) and my remote user was able to connect and USE the test database I had granted them privileges to

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-remote-database-to-optimize-site-performance-with-mysql-on-ubuntu-18-04#step-1-—-configuring-mysql-to-listen-for-remote-connections

Hope this helps.