从 cygwin 连接到 mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/329866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:27:08  来源:igfitidea点击:

connecting to mysql from cygwin

mysqlcygwin

提问by MCS

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

我可以从 DOS 提示符成功连接到 MySQL,但是当我尝试从 cygwin 连接时,它只是挂起。

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

What's wrong?

怎么了?

采纳答案by Ken Gentle

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 mysqlisn't showing up.

假设您有 MySQL 的本机 Windows 版本,DOS(命令提示符)窗口和bash. 提示mysql没有出现。

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 CYGWINsytem properties or in bash, but I've never taken the time to work this one out.

CYGWIN系统属性或 中可能有一种解决方法bash,但我从未花时间解决这个问题。

回答by Svend Hansen

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 mysqland that gave me:

我刚刚遇到了这个,当我读到有人提到它是你在 cygwin 中运行的 Windows/DOS 命令时,我做了一个which mysql,这给了我:

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

So I ran the cygwin Setup.exesearched for "mysql" and installed the latest "mysql client". Now which mysqllooks like:

所以我运行cygwinSetup.exe搜索“mysql”并安装了最新的“mysql客户端”。现在which mysql看起来像:

$ which mysql
/usr/bin/mysql

And the MySQL command works in cygwin :)

并且 MySQL 命令适用于 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:

如果您尝试从 Cygwin 运行 MySQL 客户端会返回以下错误:

$ 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.1options to the command line, as in:

然后您可以通过向-h 127.0.0.1命令行添加显式选项来修复它,如下所示:

$ mysql -u root -p -h 127.0.0.1

Updates based on comments:

基于评论的更新:

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

为避免-h 127.0.0.1每次连接时都在命令行指定,可以添加:

[client]
host=127.0.0.1

to the file /etc/my.cnf

到文件 /etc/my.cnf

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

在 Cygwin 的某些安装中,指定主机-h可能还不够。如果是这样,请尝试同时指定:

--protocol=tcp

or add

或添加

protocol=tcp

to the config file.

到配置文件。

回答by Donal Lafferty

Other answers lack the following keydetail:

其他答案缺少以下关键细节:

Cygwin has two shells:

Cygwin 有两个外壳:

  1. Default: c:\cygwin\bin\mintty.exe
  2. Basic: c:\cygwin\Cygwin.bat(which launches c:\cygwin\bin\bash.exe)
  1. 默认: c:\cygwin\bin\mintty.exe
  2. 基本:(c:\cygwin\Cygwin.bat启动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)

Win32 MySQL 可以正确写入 #2,但不能正确写入#1,因为Win32 MySQL 无法正确探测 stdin(感谢 @PeterNore)

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

想知道您是否使用 Win32 MySQL?使用which,例如

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

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

奖励Cygwin 克服路径问题的指南感谢 @Dustin

回答by DustinB

回答by Clive Stellings

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

从 cmd.exe 可执行文件运行 bash,然后 mysql 将在 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.
  1. 在桌面上为 cmd.exe 创建快捷方式。
  2. 打开快捷方式的属性并将启动目录更改为 cygwin bin 目录(通常为 C:\cygwin\bin)。
  3. 将“/c bash.exe”添加到目标参数中的命令末尾。

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.

这将在 windows cmd.exe 环境下运行 bash,当您尝试运行 mysql 时,它将按照您的预期执行。这适用于 Windows 7,但尚未在任何其他版本中进行测试。

回答by Steve Qian

  1. Put cygwin bin directory in path env variable.
  2. Use command window by running cmd
  3. Run bash -l in cmd window
  1. 将 cygwin bin 目录放在路径 env 变量中。
  2. 通过运行 cmd 使用命令窗口
  3. 在 cmd 窗口中运行 bash -l

Then MySQL can be run without problem.

然后MySQL可以毫无问题地运行。

回答by Benoit Philippon

Svend Hansen's answer is the right one:

Svend Hansen 的回答是正确的:

  • 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"

  • 安装 windows mysql 服务器文件(例如来自 mysql-5.5.25-win32.msi)

  • 使用 cygwin 安装程序 (setup.exe) 安装 Cygwin mysql 客户端

  • 在 cygwin 窗口中使用 cygwin 客户端“mysql -u[user] -p[Password] -h[host]”连接到你的服务器,在我的例子中是“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.

想了下问题贴出来的时候,cygwin setup没有提供mysql组件,现在解决了。

回答by tryexceptcontinue

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.

我为此创建了一个让我满意的半修复程序。我在 cmd.exe 中运行 cygwin.bat,然后输入 mysql - 一切正常。

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.

简单的解决方案?下载Console2,在设置下,您可以将其指向 cygwin shell。重启Console2,运行mysql,出现输出。

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

无论如何,这都是有利的,因为 Console2 具有比 Mintty 更强大的界面/自定义。我真的很喜欢透明度和颜色映射选项。

回答by user2814726

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
  1. 只需从 C:\Program Files\MySQL\MySQL Server 5.5\bin 复制你的 mysql.exe
  2. 将此 mysql.exe 粘贴到 C:\cygwin\usr\local\bin
  3. 现在运行哪个mysql,它会

回答by Bakudan

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

Althoug Svend Hansen 的回答有一些要点,另一件事是环境变量中的 PATH - 如果mysql路径cygwin之前

which mysql

will show

将会呈现

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

otherwise it will show the cygwin client.

否则它将显示 cygwin 客户端。

As reference Wikipedia says:

正如参考维基百科所说:

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

某些程序可能会在安装期间将其目录添加到PATH 变量内容的前面,以加快搜索过程和/或覆盖操作系统命令。