14

I just installed MySQL 5.5.27 on WinXP. When I open a command prompt (Start -> Run, and type "cmd"), I can access MySQL by running "mysql -u root -p". However, when I open a Cygwin terminal and try the same thing, I get this error

$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql.sock' (2)

Indeed, there is no "/var/run/mysql.sock" file.

Dave A
  • 2,780
  • 9
  • 41
  • 60
  • possible duplicate of [Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)](http://stackoverflow.com/questions/4448467/cant-connect-to-local-mysql-server-through-socket-var-lib-mysql-mysql-sock) – John Carter Aug 10 '12 at 00:38

6 Answers6

26

If you specify the host on the command line, this issue should go away:

mysql -u root -p -h 127.0.0.1

You can also create a my.ini that mysql will use:

echo [client] >c:\my.ini
echo user=root >>c:\my.ini
echo host=127.0.0.1 >>c:\my.ini

Then you can just type:

mysql -p

You can even add the password:

echo password="abracadabra" >>c:\my.ini

Then, just type:

mysql

and you're in!

See also https://serverfault.com/questions/337818/how-to-force-mysql-to-connect-by-tcp-instead-of-a-unix-socket

Community
  • 1
  • 1
Ross Smith II
  • 11,799
  • 1
  • 38
  • 43
  • I like your solution because I don't have to specify "-h 127.0.0.1" every time, but when I tried this, I still got the same error. I even changed the file name to /cygdrive/c/my.ini but no dice. – Dave A Aug 20 '12 at 13:09
  • 6
    The Cygwin version of the `mysql` command looks in different locations for the `my.cnf` file. Specifically, it looks in the following locations: `/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf`. So, in the answer above, change `c:\my.ini` to `~/.my.cnf` and it should work fine. – Ross Smith II Aug 21 '12 at 23:05
  • 1
    When I changed the file name to "my.cnf" it works great. Thanks, - – Dave A Aug 27 '12 at 14:57
  • 1
    FYI, I'm using mysql version 5.5.16 and had to change "hostname" to "host" in the .my.cnf file, like so: host=127.0.0.1 – Erin Heyming Sep 24 '12 at 18:12
  • Erin, you're right, I don't know how I got that wrong. I fixed the answer. Thanks! – Ross Smith II Sep 25 '12 at 00:43
7

Try adding this to your command:

-h 127.0.0.1

The problem is that the mysql client default host is localhost, and it treats localhost specially, using a unix socket, which is accessed via that file, but your server may not be configured to listen on the unix socket.

However, if you access the same server via the loopback IP 127.0.0.1 it will use a TCP socket instead of the unix socket and (assuming the server is online) it should work.

John Carter
  • 53,924
  • 26
  • 111
  • 144
1

Just to save few keystorkes,

Add following alias to your ~/.bashrc file.

alias mysql='mysql -u root -h 127.0.0.1'

After adding this, You can just type "mysql" in your terminal & there you go right inside mysql.

Vishal
  • 39
  • 3
0

As most of people mentioned here - one of the solutions will be to use aliases. Don't like them to be honest because they are preventing me learning some really nice Linux commands :) It's just a joke. But the best way for you, as I think, will be to locate a ~/.bashrc file located in your home directory and put there:

alias mysql="mysql -h 127.0.0.1"

Don't forget that you have to restart your session in order for this solution to work or you may type bash command at the same terminal session - it will reload all your bash settings. Good luck!

P.S. I suggest you to close all other terminal windows before editing .bashrc because you may just got a read-only file. I had such issue under Win7x64

0

I successfully installed MySQL in Cygwin on my PC according to Rafael Hart. I created a database and performed some queries and everything worked great. The next day when I tried logging into MySQL, I got the error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql.sock' (111 "Connection refused")

Apparently, when you shutdown your PC, the services also get shutdown and do not restart on boot. To fix the problem, I typed the following command to restart the mysqld service:

$ mysqld_safe &

Then everything started working.

-1

Here's how to run MYSQL from cygwin

Go here:
https://cygwin.rafaelhart.com/setting-up-mysql-on-cygwin/

To begin MySQL setup run the following:

mysql_install_db

Run mysql - you'll get a firewall alert from windows if you have it active. mysqld_safe & Immediately following that, it would be wise to run the following:

mysql_secure_installation
devpro
  • 16,184
  • 3
  • 27
  • 38