The issue is ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2 “No such file or directory”) error . This type of error occurred most of enterprise grade Linux Distributions the fix provides the solve this errors.

Pre Steps after occurring the MariaDB installations

Step-I (Install the MariaDB)

On Fedora

# dnf install mariadb

On CentOS / RHEL / OL / SL

# yum install mariadb mariadb-server
Step-II (Runnig MySQL Command)

Firstly Check the mysql command for testing purpose.

# mysql
 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2 "No such file or directory")
Step-III (Checking the Mariadb Service)

After that check the mariadb services

Also Read  How to Install NRDP Addon on Nagios Core on CentOS 7 / Ubuntu 20.04
# systemctl status mariadb
 ● mariadb.service - MariaDB 10.0 database server
 Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
 Active: inactive (dead)

How to fix this?

Step-I (Start the Mariadb service)

then start the mariadb services.

# systemctl start mariadb
Step-II (Auto enable the Service)

Finally Service started automatically.  Add the service as systemd system and service managers are on.

# systemctl enable mariadb
 Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
Step-III (Verify services are stared or not)
# systemctl status mariadb
● mariadb.service - MariaDB 10.0 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2015-08-19 21:46:13 IST; 8s ago
Process: 18042 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
Process: 17903 ExecStartPost=/usr/libexec/mysql-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 17710 ExecStartPre=/usr/libexec/mysql-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Process: 17680 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
Main PID: 17901 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─17901 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─18012 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/m...
Aug 19 21:46:12 localhost.localdomain mysql-prepare-db-dir[17710]: The latest information about MariaDB is available at http://mariadb.org/.
Aug 19 21:46:12 localhost.localdomain mysql-prepare-db-dir[17710]: You can find additional information about the MySQL part at:
Aug 19 21:46:12 localhost.localdomain mysql-prepare-db-dir[17710]: http://dev.mysql.com
Aug 19 21:46:12 localhost.localdomain mysql-prepare-db-dir[17710]: Support MariaDB development by buying support/new features from MariaDB
Aug 19 21:46:12 localhost.localdomain mysql-prepare-db-dir[17710]: Corporation Ab. You can contact us about this at sales@mariadb.com.
Aug 19 21:46:12 localhost.localdomain mysql-prepare-db-dir[17710]: Alternatively consider joining our community based development effort:
Aug 19 21:46:12 localhost.localdomain mysql-prepare-db-dir[17710]: http://mariadb.com/kb/en/contributing-to-the-mariadb-project/
Aug 19 21:46:12 localhost.localdomain mysqld_safe[17901]: 150819 21:46:12 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Aug 19 21:46:12 localhost.localdomain mysqld_safe[17901]: 150819 21:46:12 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Aug 19 21:46:13 localhost.localdomain systemd[1]: Started MariaDB 10.0 database server.

Congratulations now you have fixed the cannot connect to local MySQL server through socket error on CentOS and RHEL.

Also Read  WordPress Linux Server Hardening Guide