Connecting to MySQL without pw - Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I have a Wordpress website that used to work fine, but recently I received the message “Error establishing a database connection” when trying to navigate to it. Upon researching online, I realized it may be related to changing my db password.

So I am following instructions on resetting the Mysql pw from here -


sudo systemctl stop mysql

sudo mysqld_safe --skip-grant-tables --skip-networking &


2019-08-25T15:23:22.702830Z mysqld_safe Logging to syslog.
2019-08-25T15:23:22.707805Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2019-08-25T15:23:22.712056Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

I then do:

mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld

and then run sudo mysqld_safe --skip-grant-tables --skip-networking &


2019-08-25T15:33:49.404761Z mysqld_safe Logging to syslog.
2019-08-25T15:33:49.411891Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2019-08-25T15:33:49.443922Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
2019-08-25T15:33:49.992471Z mysqld_safe mysqld from pid file /var/run/mysqld/ ended

From here, I do mysql -u root, and see this


ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Troubleshooting done so far

To troubleshoot, I followed the suggestions from this post to find the sock file. I tried to do by by

sudo find / -type s to find the mysql.sock file, but do not find it in the list.



I tried to restart the server by sudo service mysql start but got this output

Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

The output for systemctl status mysql.service is

 mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: activating (start-post) (Result: exit-code) since Sun 2019-08-25 15:41:53 UTC; 27s ago
  Process: 16974 ExecStart=/usr/sbin/mysqld (code=exited, status=1/FAILURE)
  Process: 16966 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCE
 Main PID: 16974 (code=exited, status=1/FAILURE);         : 16975 (mysql-systemd-s)
    Tasks: 2
   Memory: 5.1M
      CPU: 331ms
   CGroup: /system.slice/mysql.service
             ├─16975 /bin/bash /usr/share/mysql/mysql-systemd-start post
             └─17042 sleep 1

Aug 25 15:41:53 keshinpoint systemd[1]: Starting MySQL Community Server...
Aug 25 15:41:53 keshinpoint systemd[1]: mysql.service: Main process exited, code=exited, status=1/F

Feels like a rabbit hole here. Can someone please help me with this? I would be grateful for any guidance offered, as Ive tried answers from multiple stackoverflow posts.

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.


It looks like that for some reason MySQL is not starting when you run the mysqld_safe command, that is why you get the Can't connect to local MySQL error when you try to connect.

I would recommend first checking your MySQL error log:


There you should find more information on why MySQL is not starting.

If you are not sure how to proceed even after seeing the errors, you could share the error here so I could advice you further.

Regards, Bobby

Hello, all

I will recommend you to check our article on How To Debug the WordPress Error Establishing Database Connection:

This crash is most likely due to your system running out of memory. I’d suggest that you add a swap file to give yourself a bit more of a buffer. Check out this tutorial:

How To Add Swap on Ubuntu

You will most definitely need to upgrade your droplet as you’re running out of memory and your application/website needs more resources in order to continue to operate.

What you can also do is to use the MySQLTuner script.

The MySQLTuner is a script written in Perl and allows you to quickly test your MySQL configuration and it gives you suggestions for adjustments to increase performance and stability.

According to the official GitHub page, it supports 300 indicators for MySQL/MariaDB/Percona Server in this last version.

To run the script you could do the following:

  • SSH to your Droplet
  • Download the script:
wget -O
  • Then execute it:

The script would run multiple checks against your MySQL instance, all checks done by MySQLTuner are documented here.

Also as stated in the official documentation, it is still extremely important for you to fully understand each change you make to a MySQL database server. If you don’t understand portions of the script’s output, or if you don’t understand the recommendations, you should consult a knowledgeable DBA or system administrator that you trust.

As a good practice make sure to always test your changes on staging environments before implementing them on your production database.

On the same note, if you want to have a worry-free MySQL hosting and focus on your application, I would recommend trying out the DigitalOcean Managed Databases:

This was mini tutorial was posted from bobbyiliev in this question in our community:

You can also create a simple bash script to check if MySQL is running and if not to restart it.


# Check if MySQL is running
sudo service mysql status > /dev/null 2>&1

# Restart the MySQL service if it's not running.
if [ $? != 0 ]; then
    sudo service mysql restart

Run this script every 5 minutes using a cron job like this one:

 */5 * * * * /home/user/scripts/ > /dev/null 2>&1

Hope that this helps! Regards, Alex