Am I Getting “Can’t Connect to Local MySQL Server Through Socket ‘/tmp/mysql.sock’” on Tiny Core Linux
Recently, while working on a lightweight LAMP setup on Tiny Core Linux, I ran into a frustrating error while trying to start MariaDB. I assumed a simple mysql start would fire up the database, but instead, I was met with a rather cryptic error message:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2 "No such file or directory")
After a lot of debugging, checking configurations, and experimenting, I finally cracked the issue and learned a lot in the process.
Here’s a detailed breakdown of what went wrong, why, and how I fixed it (plus some bonus functionality to make things smoother in the future).
The First Command I Tried
Like many others, I tried starting MariaDB with:
mysql start
But instead of launching the server, I got hit with:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2 "No such file or directory")
What the Problem Here
The error essentially means:
The MySQL client tried to connect to the MariaDB server using a Unix socket at /tmp/mysql.sock, but that socket doesn’t exist.
This happens because:
- The MariaDB server isn’t running, so the socket file never got created.
- Or the server is configured to create the socket in a different location.
- Or I simply tried to start the client (
mysql) instead of the server (mysqld).
My my.cnf Configuration
Here’s the important part of my MariaDB config file:
[client]
socket = /tmp/mysql.sock
[mysqld]
socket = /tmp/mysql.sock
So both the server and client are supposed to use /tmp/mysql.sock. But since the server wasn’t even running, the socket didn’t exist.
Correct Way to Start MariaDB on Tiny Core Linux
This was the real problem. I wasn’t actually starting the server.
Here’s how I correctly started MariaDB:
sudo mysqld --defaults-file=/etc/my.cnf &
Alternatively, if MariaDB was installed with startup scripts:
sudo /usr/local/etc/init.d/mysqld start
Reminder: mysql is the client, not the server. To start the actual service, use mysqld.
Once I did that, /tmp/mysql.sock was created, and the client connected successfully.
Adding a Reusable Startup Script
To streamline things, I wrote a small shell script to handle startup:
#!/bin/sh
# File: /usr/local/bin/start_mysql.sh
if ! pgrep -x "mysqld" > /dev/null
then
echo "Starting MariaDB server..."
/usr/local/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql &
else
echo "MariaDB is already running."
fi
Make it executable:
chmod +x /usr/local/bin/start_mysql.sh
Now I can just run:
sudo /usr/local/bin/start_mysql.sh
Even better, I created an alias for convenience:
alias mysqlstart='sudo /usr/local/bin/start_mysql.sh'
I added that to my .bashrc so it’s always available.
Extra Checks:
Here’s how I verified the server is up and running:
Check the process:
ps aux | grep mysqld
Confirm the socket file exists:
ls -l /tmp/mysql.sock
If it’s missing, locate it:
sudo find / -type s -name "mysql.sock" 2>/dev/null
If the Server Is Running But Socket Is Missing
In one test, the server was running fine but /tmp/mysql.sock still wasn’t being created. This was due to /tmp being reset on reboot or improper permissions.
Change the socket location in my.cnf:
[mysqld]
socket = /var/run/mysqld/mysqld.sock
Then, create the directory and set permissions:
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
Now restart MariaDB and you’re good to go.
Summary
| Problem | Solution |
|---|---|
Ran mysql start instead of starting server | Use mysqld, not mysql |
Socket file /tmp/mysql.sock missing | Server isn’t running or misconfigured |
| Server starts but socket missing | Check permissions or change socket path |
| Want faster setup | Use custom script and alias |
Final Thought
Setting up LAMP on Tiny Core Linux is an awesome project, but it requires understanding the difference between the client (mysql) and the server (mysqld). Most of the time, the error messages are accurate they just assume you know how the architecture works.