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

I had to recently install a new WSL2 instance of ubuntu 22.04 and this is what worked for me. This works from installing WSL 2 fresh to getting mysql -u root working. As far as i could find this is the best and most up-to-date way of getting the latest WSL and mysql working. I just did it with WSL2 Ubuntu 22.04.2 LTS and mysql 8.0.34.

update ubuntu, install mysql, start mysql server:

sudo apt update && sudo apt upgrade
sudo apt install mysql-server
sudo /etc/init.d/mysql start

at this point, sudo mysql will let you log into mysql but if you run mysql -u root you may get the WSL: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) error. If not, skip this step.

This is what you do to get around the sock error:

sudo chmod 777 -R /var/run/mysqld

However, when you try to run mysql -u root you'll get ERROR 1698 (28000): Access denied for user 'root'@'localhost'. You need to update the mysql root password. this can be accomplished manually by going to mysql console:

sudo mysql

and then in mysql console running these lines to update your password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'INSERT_PASSWORD_HERE';
FLUSH PRIVILEGES;

then, exit out of the mysql console and when you type:

mysql -u root -p

and your password you'll get into mysql console and you're good! This will now allow you to run sudo mysql_secure_installation (if you want) without getting the access denied or sock errors.

NOTE on if sudo mysql giving access denied too: you probably FUBARed your mysql root user already by trying to do something from a previous answer on another post. It is technically possible to get it back with grant tables but I spent 4 hours trying to and it was faster to just redo my WSL instance and follow the guide above than to figure out how to get that working. Up to you.