无法从命令行 mac 访问 mysql

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

can't access mysql from command line mac

mysqlterminal

提问by Michael Insalaco

mysql on os x 10.6 is located in /usr/local/mysql/bin/mysql

os x 10.6 上的 mysql 位于 /usr/local/mysql/bin/mysql

I get command not found when I type mysql --versionin the terminal. Is this because the socket path is wrong? if so how do I fix it?

当我mysql --version在终端中输入时,我找不到命令。这是因为套接字路径错误吗?如果是这样,我该如何解决?

thanks

谢谢

回答by sascha

Just do the following in your terminal:

只需在您的终端中执行以下操作:

echo $PATH

echo $PATH

If your given path is not in that string, you have to add it like this: export PATH=$PATH:/usr/local/or export PATH=$PATH:/usr/local/mysql/bin

如果给定的路径不在该字符串中,则必须像这样添加它:export PATH=$PATH:/usr/local/export PATH=$PATH:/usr/local/mysql/bin

回答by tyan

I'm using OS X 10.10, open the shell, type

我使用的是 OS X 10.10,打开外壳,输入

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

it works temporary.if you use Command+T to open a new tab ,mysql command will not work anymore.

它是临时的。如果您使用 Command+T 打开一个新选项卡,mysql 命令将不再起作用。

We need to create a .bash_profile file to make it work each time you open a new tab.

我们需要创建一个 .bash_profile 文件以使其在每次打开新选项卡时都能正常工作。

nano ~/.bash_profile

add the following line to the file.

将以下行添加到文件中。

# 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

Save the file, then open a new shell tab, it works like a charm..

保存文件,然后打开一个新的 shell 选项卡,它就像一个魅力..

by the way, why not try https://github.com/dbcli/mycli

顺便说一句,为什么不试试https://github.com/dbcli/mycli

pip install -U mycli

it's a tool way better than the mysqlcli.. A command line client for MySQL that can do auto-completion and syntax highlighting

它是一种比 mysqlcli 更好的工具方式.. MySQL 的命令行客户端,可以执行自动完成和语法突出显示

回答by Tom Auger

On OSX 10.11, you can sudo nano /etc/pathsand add the path(s) you want here, one per line. Way simpler than figuring which of ~/.bashrc, /etc/profile, '~/.bash_profile` etc... you should add to. Besides, why export and append $PATH to itself when you can just go and modify PATH directly...?

在 OSX 10.11 上,您可以sudo nano /etc/paths在此处添加所需的路径,每行一个。比确定哪个~/.bashrc, /etc/profile, '~/.bash_profile` 等更简单...你应该添加。此外,当您可以直接去修改 PATH 时,为什么要导出并附加 $PATH 到自身......?

回答by Amit Kumar

I've tried all the solutions from the answers but couldn't get mysqlcommand to work from the terminal, always getting the message

我已经尝试了答案中的所有解决方案,但无法mysql从终端获得工作命令,总是收到消息

bash: command not found

The solution is to change the .bash_profile, and add the mysql path to .bash_profile

解决办法是更改.bash_profile,将mysql路径添加到.bash_profile

To do so follow these steps: 1. Open a new Terminal window or make sure you are in the home directory 2. Open .bash_profile using

为此,请执行以下步骤: 1. 打开一个新的终端窗口或确保您在主目录中 2. 打开 .bash_profile 使用

nano .bash_profile

3. Add the following command to add the mysql path

3.添加如下命令添加mysql路径

PATH="/usr/local/mysql/bin:${PATH}"
export PATH

4. Press Ctrl+X, then press y and press enter.

4. 按Ctrl+X,然后按 y 并按 Enter。

The following is how my .bash_profile looks like enter image description here

以下是我的 .bash_profile 的样子 在此处输入图片说明

回答by lowlow20

I think this is the more simpler approach:

我认为这是更简单的方法:

  1. Install mySQL-Shell package from mySQL site
  2. Run mysqlsh (should be added to your path by default after install)
  3. Connect to your database server like so: MySQL JS > \connect --mysql [username]@[endpoint/server]:3306
  4. Switch to SQL Mode by typing "\sql" in your prompt
  5. The console should print out the following to let you know you are good to go:
  1. 从 mySQL 站点安装 mySQL-Shell 包
  2. 运行mysqlsh(安装后应该默认添加到你的路径中)
  3. 像这样连接到您的数据库服务器: MySQL JS > \connect --mysql [username]@[endpoint/server]:3306
  4. 通过在提示中键入“\sql”切换到 SQL 模式
  5. 控制台应打印出以下内容,让您知道一切顺利:

Switching to SQL mode... Commands end with ;

切换到 SQL 模式...命令以 ; 结尾

Go forth and do great things! :)

走出去,做伟大的事情!:)

回答by user12974639

On mac, open the terminal and type:

在 Mac 上,打开终端并输入:

cd /usr/local/mysql/bin

cd /usr/local/mysql/bin

then type:

然后输入:

./mysql -u root -p

./mysql -u root -p

It will ask you for the mysql root password. Enter your password and use mysql database in the terminal.

它会询问您的 mysql 根密码。输入您的密码并在终端中使用 mysql 数据库。

回答by jon jon

adding this code to my .profile worked for me: :/usr/local/mysql/bin

将此代码添加到我的 .profile 对我有用::/usr/local/mysql/bin

Thanks.

谢谢。

P.S This .profile is located in your user/ path. Its a hidden file so you will have to get to it either by a command in Terminal or using an html editor.

PS 此 .profile 位于您的用户/路径中。它是一个隐藏文件,因此您必须通过终端中的命令或使用 html 编辑器来访问它。