58

I can successfully connect to MySQL from a DOS prompt, but when I try to connect from cygwin, it just hangs.

$/cygdrive/c/Program\ Files/MySQL/MySQL\ Server\ 5.1/bin/mysql -u root -p

What's wrong?

MCS
  • 22,113
  • 20
  • 62
  • 76
  • I just came across this issue. Still there after 9 years. MySQL Ver 14.14 Distrib 5.7.18, for Win64 (x86_64). Cygwin 2.8.0(0.309/5/3) x86_64. – Taz May 16 '17 at 13:52
  • yep, just had the firewall dev shame me ... it is still around – snitch182 Oct 04 '19 at 09:16

14 Answers14

76

I just came across this, and when I read someone's mention of it being a windows/DOS command that you run in cygwin I did a which mysql and that gave me:

$ which mysql
/cygdrive/c/Program Files/MySQL/MySQL Server 5.5/bin/mysql

So I ran the cygwin Setup.exe searched for "mysql" and installed the latest "mysql client". Now which mysql looks like:

$ which mysql
/usr/bin/mysql

And the MySQL command works in cygwin :)

Though it's an old question, it would be nice to have the actual answer here, as people (like myself) might still stumble across it.

If your attempts to run the MySQL client from Cygwin return the following error:

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

Then you can fix it by adding the explicit -h 127.0.0.1 options to the command line, as in:

$ mysql -u root -p -h 127.0.0.1

Updates based on comments:

To avoid specifying -h 127.0.0.1 on the command line every time you connect, you can add:

[client]
host=127.0.0.1

to the file /etc/my.cnf

On some installations of Cygwin, specifying the host with -h might not be enough. If so, try also specifying:

--protocol=tcp

or add

protocol=tcp

to the config file.

Svend Hansen
  • 3,277
  • 3
  • 31
  • 50
  • 1
    I kept trying to use the socket, 127.0.0.1 did the trick! Thanks. – Screenack Jul 13 '12 at 20:21
  • 10
    To save putting `-h 127.0.0.1` each time you connect, you can create a MySQL config file at `/etc/my.cnf`, and put; `[client]\n host=127.0.0.1`. The MySQL client will use the network by default then. – sixones Oct 02 '12 at 15:42
  • 2
    As far as MySQL is concerned, `localhost` != `127.0.0.1`. The former indicates that a connection should be made via UNIX socket, which the native Windows server obviously doesn't support. The latter forces a network connection, hence the difference. – Yaakov Jul 16 '13 at 00:56
  • If you are trying to look where to put my.cnf : create a new directory under "C:\cygwin64\etc\". Name it "mysql". Put "my.cnf" there. The content should be [client], and, on a new line, host=127.0.0.1 – trogne Oct 30 '15 at 14:28
  • On my Cygwin, `-h localhost` is not enough, I also have to specify `--protocol=tcp` to avoid the socket error. – Kenney Nov 17 '15 at 20:53
  • Or just add protocol=tcp in my.cnf file. – trogne Nov 22 '15 at 17:13
  • This doesn't solve the problem, it circumvents it by using Cygwin's own MySQL instead of XAMPP's. Surely this would have some side-effects if you're using XAMPP as a server stack and have everything setup with its own MySQL binaries. – Hashim Aziz Jun 26 '19 at 19:44
19

Assuming that you have a native Windows build of MySQL, there is a terminal emulation incompatibility between DOS (command prompt) windows and bash. The prompt for mysql isn't showing up.

To confirm this, type a command and return - it will probably work, but the prompt and the echo of the command (what you're typing) is getting lost.

There may be a workaround in either the CYGWIN sytem properties or in bash, but I've never taken the time to work this one out.

Ken Gentle
  • 13,277
  • 2
  • 41
  • 49
9

Other answers lack the following key detail:

Cygwin has two shells:

  1. Default: c:\cygwin\bin\mintty.exe
  2. Basic: c:\cygwin\Cygwin.bat (which launches c:\cygwin\bin\bash.exe)

The Win32 MySQL can write properly to #2, but not #1, because Win32 MySQL cannot probe stdin properly (thanks @PeterNore)

Want to know if you're using Win32 MySQL? Use which, e.g.

$ which mysql
/cygdrive/c/Program Files/MySQL/MySQL Server 5.1/bin/mysql

Bonus: Cygwin guide to overcoming path problems (thanks @Dustin)

Community
  • 1
  • 1
Donal Lafferty
  • 5,807
  • 7
  • 43
  • 60
5

I posted a solution/workaround here:

enter key sometimes not recognized in windows apps under cygwin

Community
  • 1
  • 1
DustinB
  • 11,037
  • 5
  • 46
  • 54
5

Run bash from the cmd.exe executable and then mysql will work inside bash.

  1. Create a shortcut for cmd.exe on your desktop.
  2. Open up the properties for the shortcut and change the startup directory to the cygwin bin directory (usually C:\cygwin\bin).
  3. Add "/c bash.exe" to the end of the command in the target parameter.

This will run bash under the windows cmd.exe environment and when you attempt to run mysql it will execute as you would expect. This is working under windows 7 but has not been tested in any other version.

Johan Bezem
  • 2,582
  • 1
  • 20
  • 47
  • (-1 retracted because user is new to SO). The OP can start bash under windows; your solution doesn't add any information to the regular bash-shortcut provided by a standard Cygwin installation. And you do not answer his question: What is wrong with his way of calling MySQL? – Johan Bezem Jan 05 '12 at 14:07
  • Changing the properties of the cmd terminal window (esp fonts) will make it as great as just starting the cygwin terminal. This should be the answer to this question. – Josiah Jun 22 '17 at 04:14
  • google has now google-ized your answer so you probably won't get any plus points for it since no-one will visit SO for it anymore, except I did, so maybe I'm wrong. – Adam Aug 23 '17 at 13:34
2
  1. Put cygwin bin directory in path env variable.
  2. Use command window by running cmd
  3. Run bash -l in cmd window

Then MySQL can be run without problem.

Darren
  • 68,902
  • 24
  • 138
  • 144
Steve Qian
  • 622
  • 6
  • 2
1

Svend Hansen's answer is the right one:

  • Install windows mysql server files (from mysql-5.5.25-win32.msi for example)

  • Install Cygwin mysql client with cygwin installer (setup.exe)

  • Connect to your server in a cygwin window using cygwin client "mysql -u[user] -p[Password] -h[host]", in my case "mysql -uroot -pXXXX -h127.0.0.1"

I think that when the question was posted, the cygwin setup did not provide mysql components, which is solved now.

1

Althoug Svend Hansen answer has some points, another thing is the PATH in Environment variables - if the path to mysql is before that of cygwin

which mysql

will show

/cygdrive/c/Program Files/MySQL/MySQL Server 5.5/bin/mysql

otherwise it will show the cygwin client.

As reference Wikipedia says:

Some programs may add their directory to the front of the PATH variable's content during installation, to speed up the search process and/or override OS commands.

Community
  • 1
  • 1
Bakudan
  • 19,134
  • 9
  • 53
  • 73
1
  1. Download Cygwin
  2. Install mysql client app

  3. create an alias in .bashrc file

    alias mysql='mysql -h 127.0.0.1'

  4. execute source .bashrc

Now you can connect to mysql

mysql -u user -p

0

I have created a semi-fix for this that satisfies me. I ran cygwin.bat in cmd.exe, then typing mysql in- everything worked fine.

I realized right there that the problem was mintty.

Easy solution? Download Console2, and under settings you can point it to the cygwin shell. Restart Console2, run mysql and the output appears.

This is advantageous anyways, because Console2 has a more robust interface/customization than Mintty. I really like the transparency and color mapping options.

tryexceptcontinue
  • 1,587
  • 15
  • 13
0

Do This:

  1. just copy ur mysql.exe from C:\Program Files\MySQL\MySQL Server 5.5\bin
  2. paste this mysql.exe in C:\cygwin\usr\local\bin
  3. now run which mysql, It will
Bakudan
  • 19,134
  • 9
  • 53
  • 73
0

Disclaimer: The following solved this issue for me under MinTTY on MinGW/MSYS. From research, I believe this same root cause affects Cygwin as well.

Answer is posted here: https://stackoverflow.com/a/23164362/1034436

In a nutshell, you'll need to prepend your mysql command with winpty's console.exe (or have aliases that does so). This solution worked with native Windows MySQL executables and not a special cygwin/mingw build. You do, however, have to compile winpty, but that was simple and painless, and worked as per their documentation for me.

Note: This also solved my issue with several other native Windows console applications, namely Python and Mercurial with OpenSSH.

Community
  • 1
  • 1
martian111
  • 595
  • 1
  • 6
  • 21
0

Reinstall cygwin and during reinstallation search for mysql in packages, install the mysql client and then it would work fine.

Praveen Kishor
  • 2,413
  • 1
  • 23
  • 28
0

Found this question today 2018-03-18 looking for some answers to

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysql.sock' (2 "No such file or directory")

The file /etc/my.conf references config files in /etc/my.cnf.d I added this to /etc/my.cnf.d/client.cnf:

[client]
host=127.0.0.1
protocol=tcp

After that I was able to access the local windows MySQL instance from a cygwin terminal using mysql -u root -p

Ken Ingram
  • 1,538
  • 5
  • 27
  • 52