Cant connect to MySQL with localhost

December 14, 2018 2.6k views
MySQL Apache Ubuntu 18.04

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?

1 comment
  • It’s most likely these scripts you are running.

    Edit: might be a low'memory issue too.

1 Answer

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.

by Brian Boucheron
by Mark Drake
This guide will walk you through the process of configuring a remote MySQL database server that your website or application can connect to. It uses WordPress as a working example of something that can connect to the MySQL backend remotely, but the technique is widely applicable to any application backed by MySQL.
Have another answer? Share your knowledge.