Question

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

Posted August 25, 2019 18.2k views
MySQLPHPWordPress

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 - https://www.digitalocean.com/community/tutorials/how-to-reset-your-mysql-or-mariadb-root-password.

Steps:

sudo systemctl stop mysql

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

Output:

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 &

Output

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/mysqld.pid ended

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

Output:

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.

Output:

/run/acpid.socket
/run/dbus/system_bus_socket
/run/snapd-snap.socket
/run/snapd.socket
/run/uuidd/request
/run/user/0/systemd/private
/run/user/0/systemd/notify
/run/fail2ban/fail2ban.sock
/run/lvm/lvmpolld.socket
/run/lvm/lvmetad.socket
/run/systemd/journal/dev-log
/run/systemd/journal/socket
/run/systemd/journal/stdout
/run/systemd/journal/syslog
/run/systemd/private
/run/systemd/notify
/run/udev/control
/var/lib/lxd/unix.socket
/var/spool/postfix/private/proxywrite
/var/spool/postfix/private/relay
/var/spool/postfix/private/ifmail
/var/spool/postfix/private/retry
/var/spool/postfix/private/defer
/var/spool/postfix/private/maildrop
/var/spool/postfix/private/mailman
/var/spool/postfix/private/discard
/var/spool/postfix/private/anvil
/var/spool/postfix/private/scalemail-backend
/var/spool/postfix/private/bsmtp
/var/spool/postfix/private/bounce
/var/spool/postfix/private/verify
/var/spool/postfix/private/trace
/var/spool/postfix/private/uucp
/var/spool/postfix/private/lmtp
/var/spool/postfix/private/local
/var/spool/postfix/private/error
/var/spool/postfix/private/rewrite
/var/spool/postfix/private/proxymap
/var/spool/postfix/private/smtp
/var/spool/postfix/private/tlsmgr
/var/spool/postfix/private/scache
/var/spool/postfix/private/virtual
/var/spool/postfix/public/showq
/var/spool/postfix/public/qmgr
/var/spool/postfix/public/pickup
/var/spool/postfix/public/cleanup
/var/spool/postfix/public/flush
/var/spool/postfix/dev/log

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
           └─control
             ├─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.

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
2 answers

Hello,

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:

/var/log/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:

https://www.digitalocean.com/community/tutorials/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 http://mysqltuner.pl/ -O mysqltuner.pl
  • Then execute it:
perl mysqltuner.pl

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:

https://www.digitalocean.com/products/managed-databases-mysql/

This was mini tutorial was posted from bobbyiliev in this question in our community: https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

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

#!/bin/bash

# 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
fi

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

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

Hope that this helps!
Regards,
Alex

by Brian Boucheron
WordPress is one of the most popular open source content management systems in the world. Although it started out focused on blogging, over the years it has developed into a more flexible platform for websites in general. After almost fifteen years of development it is quite polished and robust, yet issues can still come up. If you've recently attempted to load your WordPress-powered website and instead saw a message stating "Error Establishing Database Connection", the cause is most often...