简体   繁体   English

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

[英]Using Mysql in the command line in osx - command not found?

I'm trying to get mysql up and running on my mac osx 10.9.5. 我想在我的mac osx 10.9.5上运行mysql并运行。 I've installed the latest version 14.14 5.6.21 community server. 我已经安装了最新版本的14.14 5.6.21社区服务器。 I've gone to system preferences and started the mysql server, then launched terminal and typed this: 我已经去了系统首选项并启动了mysql服务器,然后启动了终端并键入了这个:

/usr/local/mysql/bin/mysql --version which returns the version, but when I type any type of mysql command I get command not found . /usr/local/mysql/bin/mysql --version返回版本,但是当我键入任何类型的mysql命令时,我command not found I've also tried sudo mysql_secure_installation , mysql -u root --password=password . 我也试过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等的虚拟主机,但我希望能够首先在命令行中使用它。

So there are few places where terminal looks for commands. 所以终端寻找命令的地方很少。 This places are stored in your $PATH variable. 这些地方存储在$PATH变量中。 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. 这通常是二进制文件,看看/ bin文件夹通常是如何引用的。

/bin folder has lots of executable files inside it. /bin文件夹里面有很多可执行文件。 Turns out this are command. 原来这是命令。 This different folder locations are stored inside one Global variable ie $PATH separated by : 这个不同的文件夹位置存储在一个全局变量中,即$PATH分隔:

Now usually programs upon installation takes care of updating PATH & telling your terminal that hey i can be all commands inside my bin folder. 现在通常安装的程序负责更新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, export is self explanatory. 如果你将其分解, export是不言自明的。 Think of it as an assignment. 把它想象成一项任务。 So export a variable PATH with value old $PATH concat with new bin ie /usr/local/mysql/bin 因此,使用新的bin/usr/local/mysql/bin export值为旧$PATH concat的变量PATH

This way after executing it all the commands inside /usr/local/mysql/bin are 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 $PATH is class variable ( maybe ). 可以把一个终端窗口想象成一个程序实例,也许像$PATH的类变量(可能)。 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. 如果我们重新打开终端,我们将无法再次访问我们的命令,因为在我们导出时,它被存储在易失性的主存储器中。

Now we need to have our mysql binaries exported every-time we use terminal. 现在我们需要在每次使用终端时导出我们的mysql二进制文件。 So we have to persist concat in our path. 所以我们必须在我们的道路上坚持不懈。

You might be aware that our terminal using something called dotfiles to load configuration on terminal initialisation. 您可能知道我们的终端使用了一个名为dotfiles东西来加载终端初始化的配置。 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 ). 我喜欢把它想象成每次创建一个新的终端实例时传递给constructer的东西(同样是一个假设但接近它可能正在做的事情)。 So yes by now you get the point what we are going todo. 所以是的,现在你明白了我们要做的事情。

.bash_profile is 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 echo ie output string to ~/.bash_profile 我们正在做的是保存echo结果,即输出字符串到〜/ .bash_profile

So now as we noted above every-time we open terminal or instance of terminal our dotfiles are loaded. 所以现在正如我们上面提到的那样,每次我们打开终端或终端实例时我们dotfiles加载dotfiles So .bash_profile is loaded respectively and export that we appended above is run & thus a our global $PATH gets updated and we get all the commands inside /usr/local/mysql/bin . 因此.bash_profile分别被加载并且我们上面附加的export被运行,因此我们的全局$PATH得到更新,我们得到/usr/local/mysql/bin所有命令。

Ps 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, 比你当前运行的终端实例,

source ~/.bash_profile

This tells our terminal to reload that particular file. 这告诉我们的终端重新加载该特定文件。

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

for me the following commands worked: 对我来说,以下命令有效:

$ brew install mysql

$ brew services start mysql

You can just modified the .bash_profile by adding the MySQL $PATH as the following: 您可以通过添加MySQL $PATH来修改.bash_profile ,如下所示:
export PATH=$PATH:/usr/local/mysql/bin . export PATH=$PATH:/usr/local/mysql/bin

I did the following: 我做了以下事情:

1- Open Terminal then $ nano .bash_profile or $ 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-要验证,请键入Terminal $ 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)

The Terminal is now configured to read the MySQL commands from $PATH which is placed in the .bash_profile . 终端现在配置为从$PATH读取MySQL命令,该命令放在.bash_profile

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

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 mysql@5.6

see this post by Alex Todd 看看Alex Todd的这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM