Connect to MySQL Using Unix Socket File

We can connect to the mysqld server by using Unix Socket file or by using TCP/IP. A connection using Unix socket file is faster than TCP/IP but socket file used when connecting to a server on the same computer. Hostname and port can be skip when we use a Unix socket file.

Locate a Unix Soket File:

[ec2-user@ip-172-xx-xx-x ~]$ mysql -u root -p -h 127.0.0.1 -e "select @@socket"

Enter password:

+---------------------------+

| @@socket                  |

+---------------------------+

| /var/lib/mysql/mysql.sock |

 

Or

[ec2-user@ip-172-xx-xx-x ~]$ cat /etc/my.cnf|grep socket

socket=/var/lib/mysql/mysql.sock

 

Check Connection using Unix Socket File:

[ec2-user@ip-172-xx-xx-x ~]$ mysql -u root -p -S /var/lib/mysql/mysql.sock

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 13

Server version: 8.0.34 MySQL Community Server - GPL

 

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>