Cant connect to MySQL with localhost

Posted December 14, 2018 15.9k views
MySQLApacheUbuntu 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

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.

Submit an Answer
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—-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.