在 osx 的命令行中使用 Mysql - 命令未找到?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26554818/
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 13:09:12  来源:igfitidea点击:

Using Mysql in the command line in osx - command not found?

mysqlmacos

提问by user1574598

I'm trying to get mysql up and running on my mac osx 10.9.5. I've installed the latest version14.145.6.21community server. I've gone to system preferences and started the mysql server, then launched terminal and typed this:

我正在尝试在我的 mac osx 10.9.5 上启动并运行 mysql。我已经安装了最新版本的14.145.6.21社区服务器。我已经转到系统首选项并启动了 mysql 服务器,然后启动了终端并输入了以下内容:

/usr/local/mysql/bin/mysql --versionwhich returns the version, but when I type any type of mysql command I get command not found. I've also tried sudo mysql_secure_installation, mysql -u root --password=password.

/usr/local/mysql/bin/mysql --version它返回版本,但是当我输入任何类型的 mysql 命令时,我得到command not found. 我也试过了sudo mysql_secure_installationmysql -u root --password=password

I do have web hosting with mysql etc installed, but I want to be able to get to grips with it in the command line first.

我确实安装了 mysql 等虚拟主机,但我希望能够首先在命令行中掌握它。

回答by kishanio

So there are few places where terminal looks for commands. This places are stored in your $PATHvariable. Think of it as a global variable where terminal iterates over to look up for any command. This are usually binaries look how /bin folder is usually referenced.

所以终端查找命令的地方很少。这些地方存储在您的$PATH变量中。将其视为终端迭代以查找任何命令的全局变量。这通常是二进制文件,看看通常如何引用 /bin 文件夹。

/binfolder has lots of executable files inside it. Turns out this are command. This different folder locations are stored inside one Global variable i.e. $PATHseparated by :

/bin文件夹里面有很多可执行文件。原来这是命令。这个不同的文件夹位置存储在一个全局变量中,即$PATH:

Now usually programs upon installation takes care of updating PATH& telling your terminal that hey i can be all commands inside my binfolder.

现在通常安装时的程序会负责更新PATH并告诉您的终端,嘿,我可以成为我bin文件夹中的所有命令。

Turns out MySql doesn't do it upon install so we manually have to do it.

事实证明,MySql 在安装时不会这样做,所以我们必须手动进行。

We do it by following command,

我们通过以下命令来做到这一点,

export PATH=$PATH:/usr/local/mysql/bin

If you break it down, exportis self explanatory. Think of it as an assignment. So exporta variable PATHwith value old $PATHconcat with new bini.e. /usr/local/mysql/bin

如果你分解它,export是不言自明的。把它想象成一项任务。所以export一个PATH值为 old $PATHconcat 与 new binie的变量/usr/local/mysql/bin

This way after executing it all the commands inside /usr/local/mysql/binare available to us.

这样,在执行它之后/usr/local/mysql/bin,我们可以使用里面的所有命令。

There is a small catch here. Think of one terminal window as one instance of program and maybe something like $PATHis class variable ( maybe ). Note this is pure assumption. So upon close we lose the new assignment. And if we reopen terminal we won't have access to our command again because last when we exported, it was stored in primary memory which is volatile.

这里有一个小问题。将一个终端窗口视为程序的一个实例,并且可能类似于$PATH类变量(可能)。请注意,这是纯粹的假设。所以在关闭时,我们失去了新的任务。如果我们重新打开终端,我们将无法再次访问我们的命令,因为上次导出时,它存储在易失性的主内存中。

Now we need to have our mysql binaries exported every-time we use terminal. So we have to persist concat in our path.

现在我们需要在每次使用终端时导出我们的 mysql 二进制文件。所以我们必须在我们的路径中坚持 concat 。

You might be aware that our terminal using something called dotfilesto load configuration on terminal initialisation. I like to think of it's as sets of thing passed to constructer every-time a new instance of terminal is created ( Again an assumption but close to what it might be doing ). So yes by now you get the point what we are going todo.

您可能知道我们的终端使用称为dotfiles在终端初始化时加载配置的东西。我喜欢将其视为每次创建新终端实例时传递给构造函数的一组事物(再次假设,但接近它可能正在做的事情)。所以是的,现在你明白我们要做什么了。

.bash_profileis one of the primary known dotfile.

.bash_profile是主要的已知之一dotfile

So in following command,

所以在下面的命令中,

echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.bash_profile

What we are doing is saving result of echoi.e. output string to ~/.bash_profile

我们正在做的是将echoie 输出字符串的结果保存到 ~/.bash_profile

So now as we noted above every-time we open terminal or instance of terminal our dotfilesare loaded. So .bash_profileis loaded respectively and exportthat we appended above is run & thus a our global $PATHgets updated and we get all the commands inside /usr/local/mysql/bin.

所以现在正如我们上面提到的,每次我们打开终端或终端实例时,我们都会dotfiles加载。So.bash_profile分别加载export,我们在上面附加的内容被运行,因此我们的全局$PATH更新,我们得到了里面的所有命令/usr/local/mysql/bin

P.s.

ps

if you are not running first command export directly but just running second in order to persist it? Than for current running instance of terminal you have to,

如果您不是直接运行第一个命令 export 而只是运行第二个命令以保持它?比当前运行的终端实例你必须,

source ~/.bash_profile

This tells our terminal to reload that particular file.

这告诉我们的终端重新加载该特定文件。

回答by Justin Kiang

That means /usr/local/mysql/bin/mysql is not in the PATH variable..

这意味着 /usr/local/mysql/bin/mysql 不在 PATH 变量中..

Either execute /usr/local/mysql/bin/mysql to get your mysql shell,

要么执行 /usr/local/mysql/bin/mysql 来获取你的 mysql shell,

or type this in your terminal:

或在您的终端中输入:

PATH=$PATH:/usr/local/mysql/bin

to add that to your PATH variable so you can just run mysql without specifying the path

将其添加到您的 PATH 变量中,这样您就可以在不指定路径的情况下运行 mysql

回答by krishnamraju kammili

for me the following commands worked:

对我来说,以下命令有效:

$ brew install mysql

$ brew services start mysql

回答by Mora

You can just modified the .bash_profileby adding the MySQL $PATHas the following:
export PATH=$PATH:/usr/local/mysql/bin.

你可以只修改.bash_profile,加入了MySQL $PATH,如下:
export PATH=$PATH:/usr/local/mysql/bin

I did the following:

我做了以下事情:

1- Open Terminal then $ nano .bash_profileor $ vim .bash_profile

1- 打开终端然后$ nano .bash_profile$ vim .bash_profile

2- Add the following PATH code to the .bash_profile

2- 将以下 PATH 代码添加到 .bash_profile

# Set architecture flags
export ARCHFLAGS="-arch x86_64"
# Ensure user-installed binaries take precedence
export PATH=/usr/local/mysql/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc 

3- Save the file.

3- 保存文件。

4- Refresh Terminal using $ source ~/.bash_profile

4- 使用刷新终端 $ source ~/.bash_profile

5- To verify, type in Terminal $ mysql --version

5- 要验证,请输入终端 $ mysql --version

6- It should print the output something like this:

6-它应该打印输出如下:

$ mysql Ver 14.14 Distrib 5.7.17, for macos10.12 (x86_64)

$ mysql Ver 14.14 Distrib 5.7.17, for macos10.12 (x86_64)

The Terminal is now configured to read the MySQL commands from $PATHwhich is placed in the .bash_profile.

终端现在配置为读取$PATH放在.bash_profile.

回答by Pravin

modify your bash profile as follows <>$vim ~/.bash_profile export PATH=/usr/local/mysql/bin:$PATH Once its saved you can type in mysql to bring mysql prompt in your terminal.

如下修改您的 bash 配置文件 <>$vim ~/.bash_profile export PATH=/usr/local/mysql/bin:$PATH 保存后,您可以输入 mysql 以在终端中显示 mysql 提示符。

回答by bisi

You have to create a symlink to your mysql installation if it is not the most recent version of mysql.

如果你的 mysql 安装不是最新版本的 mysql,你必须创建一个符号链接。

$ brew link --force [email protected]

see this post by Alex Todd

看到亚历克斯托德的这篇文章