Solve: Can’t connect to local MySQL server through socket /tmp/mysql.sock
A frequent error message received when using the mysql command line utility is: Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ While this error message can be frustrating, the solution is simple.
When connecting to a MySQL server located on the local system, the mysql client connects thorugh a local file called a socket instead of connecting to the localhost loopback address 127.0.0.1. For the mysql client, the default location of this socket file is /tmp/mysql.sock. However, for a variety of reasons, many MySQL installations place this socket file somewhere else like /var/lib/mysql/mysql.sock.
While it is possible to make this work by specifying the socket file directly in the mysql client command
mysql --socket=/var/lib/mysql/mysql.sock ...
it is painful to type this in every time. If you must do so this way (because you don’t have permissions to the file in the solution below), you could create an alias in your shell to make this work (like alias mysql=”mysql –socket=/var/lib/mysql/mysql.sock” depending on your shell).
To make your life easier, you can make a simple change to the MySQL configuration file /etc/my.cnf that will permanently set the socket file used by the mysql client. After making a backup copy of /etc/my.cnf, open it in your favorite editor. The file is divided into sections such as
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
[mysql.server]
user=mysql
basedir=/usr/local/mysql
If there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:
[client]
socket=/var/lib/mysql/mysql.sock
If there is already a [client] section in the my.cnf file, add or edit the socket line as appropriate. You won’t need to restart your server or any other processes. Subsequent uses of the mysql client will use the proper socket file.









Anonymous said on November 19, 2008
Should I be able to connect to MySQL over my LAN? I am running MAMP on Tiger, and can connect using MAMP’s built in tools, but preferred the look of Sequel Pro, which I can use to connect with MySQL on the local machine using LocalHost, root user, and password, port 8889 and Socket: /Applications/MAMP/tmp/mysql/mysql.sock. However when I try to connect to that Mac from another on the LAN, which works to MAMP fine via http://192.168.0.4:8888/ It says Can’t connect to local MySQL server through socket ‘/Applications/MAMP/tmp/mysql/mysql.sock’ (2). I’m confused. MAMP’s using port 8888, and MySQL’s using port 8889. How can I get pass both in the URL? Or am I all wrong here? Any suggestions?
arun said on January 6, 2009
I found the following in the MySQL documentation:
1. Log in as the root user. (“Start” => “Switch User”)
2. Open a terminal window (the icon that looks like a monitor behind a clamshell).
3. Type “cd /bin” enter (no quotes). :: usually not nessecary ::
4. Type “mysql_install_db –user=mysql
This is from: http://www.novell.com/coolsolutions/feature/595...
obaid said on January 29, 2009
can this type of error be generated by something like:
mysql_fetch_assoc();
is missing
do help please…!!!
gogster said on February 15, 2009
I am on client side. I can’t change serwer settings. Can send php scripts only! It would be usefull if it will be my own serwer, but it isnt.. So your’s advice is useless..
The reason fo client is bad serwer name in “mysql_connect” function. For example database is:
“http://abc.def.ghi.com/mysql5/”
so you should use it properly like this:
mysql_connect(“abc.def.ghi.com”,…);
insted of not properly like this:
mysql_connect(“http://abc.def.ghi.com/mysql5/”,…);
gogster said on February 15, 2009
of course in this problem command:
mysql_connect(“localhost”,…);
not work
David C. Rankin, J.D.,P.E. said on May 13, 2009
Excellent nugget of information:
It came in real handy when the geniouses at opensuse broke an update of mysql to mysql-5.1.34-29.1 by putting the mysql.sock and mysqld.pid in /var/run/mysql instead of /var/lib/mysql as normal. Great quick fix was found here. In addition to /etc/my.cnf, the init script at /usr/sbin/rcmysql needed to be fixed as well.
Thanks
Steven Sheffey said on June 30, 2009
Hi guys, I to have this problem and I’m new to mySQL and PHP. Where are you guys locating all these directories such as: /var/lib/mysql or /usr/local/mysql/data ?
JEME said on June 30, 2009
Good day qmchenry,
I try with de line:
mysql –socket=/var/lib/mysql/mysql.sock …
but now the terminal list this error:
ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: YES)
would you can help me
NikoJr said on September 25, 2009
Man u have saved my life hehehe… very useful.
Thanku very much
Anonymous said on October 17, 2009
Wouldn’t the link be removed at server reboot and the problem appear again??
I had the same problem but it is because my.cnf was not in the default locations
CLIENT:
Default options are read from the following files in the given order:
/etc/my.cnf ~/.my.cnf /usr/local/mysql/etc/my.cnf
So even when I added the [client] section I got the same error, so I did a link
#ln -s /my/cnf/file_location/my.cnf /etc/my.cnf
And it worked great.
Anonymous said on January 11, 2010
i am getting this message
Warning: mysql_pconnect() [function.mysql-pconnect]: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) in /home/content/07/5479507/html/kb/install/install_step3.php on line 61
Could not connect to the server: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
what should i do?
Anonymous said on January 15, 2010
Please make sure that your disk having enough space..!
Anonymous said on January 28, 2010
Another way to give the path for socket file is this:
connection=MySQLdb.connect(user=”root”,passwd=”",db=”python”,unix_socket=”/opt/lampp/var/mysql/mysql.sock”)
This did wonders for me and I am really very thankful to this: http://www.oesf.org/forum/index.php?showtopic=24885.
Hope this will help many others as it did for me.
Albert said on February 15, 2010
Fantastic!
I had the same problem and that was the solution.
Thanks a lot!
Anonymous said on February 22, 2010
Excellent explanation. But I still have a question. I have a different scenario. My mySQL database is on a different server with a hosting company and I am connecting from my client in my home. I am getting the same error. Is there something that can be done from my end to fix this error? Thanks anyways.
Anonymous said on February 22, 2010
Continuing to my earlier post I am not receiving the above error while connecting to the database but when I am running first ever query in my web page. Can somebody suggest another explanation?
Anonymous said on March 15, 2010
Yes…this solution looks very simple. Now, where the heck is the /etc/my.cnf file?! I can’t find it anywhere. I’m so frustrated right now. I have a hard time when I spend sooo much time just trying to get software working so I can actually get some work done!
Thanks for the help.
Benjamin See said on April 8, 2010
I would like to point out that, when performing tasks in an automated environment, such as setting up your mysql during kickstart installation, it important to note that if you execute mysql-related commands immediately after you start the mysql service, it is likely that your commands failed due to the mysql service “still in the progress” of starting up.
And the error message that you get would be similar that mysql.sock is not found.
The solution is simple. Allow for some delay between starting your mysql service, and performing your first mysql tasks, so that when the first mysql command is issued, the service is started and running properly.
Pat said on June 2, 2010
Just want to add my humble thank you to the list. I was updating our php version in preparation for another software update when things went south. Could not figure out for the life of me why apache could not connect to mysql. Manual connection …. no problem…. apache -> php -> mysql …. problem
mysql.sock …. running amok. symbolic link solved the problem!
What a relief!
cheers!
muzie said on June 8, 2010
thanks brother!
Marco said on August 29, 2010
Usually there’s a hickup in your my.ini that will get you this message at the first mysql server restart after upgrading or changing your configuration.
Locate the .err file in your mysql/datadir and see what option is deprecated this time.
Deepak Frazer said on September 21, 2010
it worked for me on CentOS. mysql Server version: 5.0.77
Thanks dude
Heri Anto マスター said on October 14, 2010
pkill mysqld
su mysql
mysql_install_db
chown -R mysql.mysql /var/lib/mysql/
mysqld_safe &
Heri Anto マスター said on October 14, 2010
try this work for me
pkill mysqld
su mysql
mysql_install_db
chown -R mysql.mysql /var/lib/mysql/
mysqld_safe &
cave_person_2 said on November 10, 2010
Good lord, can SOMEONE say how to do it as if, and I’m just giving you an example, as if not everyone was computerically inclined? I think I must be one of the last 4 in thje USA and we’re all sorry. I really have no idea what you’re talking about, what these things mean and where all these icons with clamshells could be found, etc? So theres another user for my system? Where? And then again, please explain it as if I weren’t a techie too if you can. I would be forever greatful….
Arik_harel said on December 9, 2010
For xampp this command worke for me:
ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock
CJ said on December 17, 2010
Hello. I’m a real newbie to all this so would be grateful for answers in words of one syllable and sorry if this is obvious to everyone else, but most comments are going over my head. I think if I can get past this bit, then I should be OK to do everything I want to do, I’m just stuck here, trying to get a connection.
I’m getting the ‘Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)’ error message.
I’ve set up my first MySQL database and its hosted on a remote web server so I don’t have access to config files. I’m on the client side, trying to connect so I can create some dynamic pages for my website.
My web hosting company are being less than helpful in helping me to sort this out. What sort of questions should I be asking them and then if they say I’m using the wrong socket, what php script would I need to specify (I would need it in full)?
Sorry, this must be really basic stuff for everyone else but it’s causing me a lot of grief and if I can just get through this I can actually do some work….
Thanks in advance for any suggestions.
CJ
Anonym said on December 21, 2010
BTW also this occurs when your site under ddos attack.
In this situation mysqld will load high cpu usage too.
Yahya Nikandish said on February 16, 2011
yahya.nikandish@yahoo.com
computerically inclined? said on March 10, 2011
Calm down, maybe you are on the wrong message board. This is a technical message board. Sounds like you are looking for the Fisher Price Computer Users Group.
Gopinathkarangula said on June 6, 2011
Hi to all for me this problem occurred twice, so i just thought and understood the problem, when ever you are changing some config file related to mysql, better stop the service, edit you config, and start the service, then mysqld problem you will never face..
Chandan Singh Pachwal said on July 25, 2011
try runing it as
/data/(folder in which mysql is installed)/mysql/bin/mysql
Chandan Singh Pachwal said on July 25, 2011
try runing it as
/data/(folder in which mysql is installed)/mysql/bin/mysql
rana said on September 24, 2011
When I upload my php and MySQL website of web hosting sites then i am getting this message.
Warning: mysql_pconnect() [function.mysql-pconnect]: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /srv/disk2/833826/www/mycprogamsolution.co.cc/Connections/check_mag.php on line 9
Fatal error: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /srv/disk2/833826/www/mycprogamsolution.co.cc/Connections/check_mag.php on line 9
what should i do?
Roy M J said on December 2, 2011
Hi i am having the same error, How should my connnection code look like? Currently its
mysql_connect(“localhost:/tmp/mysql5.sock”,”dbo377327247″,”password”);
But the error says
Can’t connect to local MySQL server through socket ‘/tmp/mysql5.sock’ (2)
How should i link the socket to this code?
Thanks
susu said on January 2, 2012
fantastic illustration ,, but i couldn’t change the ‘my.cnf’ cz it’s read only.. how can i change it’s permission???
thx alot ;D