Can't connect to local MySQL server through socket '/tmp/mysql.sock' code example

Example 1: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

sudo chown -R _mysql:mysql /usr/local/var/mysql

sudo mysql.server start

Example 2: can't connect to local mysql server through socket '/var/run/mysqld/mysqld.sock'

# Start your mysql server
/etc/init.d/mysql start

Example 3: can't connect to local mysql server through socket '/tmp/mysql.sock' (2)

Try to start the MySQL server:
mysql.server start

Example 4: can't connect to local mysql server through socket '/var/run/mysqld/mysqld.sock'

# this saved my ass
# first  you must perform a graceful shutdown of the server from the command line rather than powering off the server
shutdown -h now

# an additional method for getting it back up again when you run into this problem is to move mysql.sock

# to centos
mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak

# to ubuntu
mv /var/run/mysqld/mysqld.sock /var/run/mysqld/mysqld.sock.bak

# restart mysql
service mysqld start

Example 5: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

ln -s /private/var/mysql/mysql.sock /tmp/mysql.sock

Example 6: Can't connect to local MySQL server through socket '/tmp/mysql.sock'

sudo /usr/local/mysql/support-files/mysql.server start