Mac 使用终端安装和打开 mysql

我下载了 mysql dmg 文件并通过向导运行。成交。我还在系统首选项下启动了 mysql 服务器。

我这样做的目的是通过练习我的 SQL 课本。终端命令对我来说是新的,但是我认为一旦我可以真正开始,通过练习工作应该是可以的。

通过研究网络,各种博客告诉我导航到终端中的 mysql 文件夹:

/usr/local/mysql

好吧。然后就不那么清楚了,因为几乎每篇文章都有一套关于如何继续的说明。我昨天在摆弄它的时候被提示输入密码——默认的 mysql 密码是什么?

谁能告诉我如何通过终端启动并运行 mysql?

347274 次浏览

If you have your MySQL server up and running, then you just need a client to connect to it and start practicing. One is the mysql-client, which is a command-line tool, or you can use phpMyAdmin, which is a web-based tool.

(Updated for 2017)

When you installed MySQL it generated a password for the root user. You can connect using

/usr/local/mysql/bin/mysql -u root -p

and type in the generated password.

Previously, the root user in MySQL used to not have a password and could only connect from localhost. So you would connect using

/usr/local/mysql/bin/mysql -u root

open terminal and type

sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'

then close terminal and open a new terminal and type

mysql -u root -p

hit enter, and it will ask you for password

I have found this solution on https://teamtreehouse.com/community/says-mysql-command-not-found

now to set new password type

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

In the terminal, I typed:

/usr/local/mysql/bin/mysql -u root -p

I was then prompted to enter the temporary password that was given to me upon completion of the installation.

This command works for me:

./mysql -u root -p

(PS: I'm working on mac through terminal)

try with either of the 2 below commands

/usr/local/mysql/bin/mysql -uroot
-- OR --
/usr/local/Cellar/mysql/<version>/bin/mysql -uroot

  1. install homebrew via terminal

  2. brew install mysql

This command works for me:

Command:

mysql --host=localhost -uroot -proot

In MacOS, Mysql's executable file is located in /usr/local/mysql/bin/mysql and you can easily login to it with the following command:

/usr/local/mysql/bin/mysql -u USERNAME -p

But this is a very long command and very boring, so you can add mysql path to Os's Environment variable and access to it much easier.

For macOS Catalina and later

Starting with macOS Catalina, Mac devices use zsh as the default login shell and interactive shell and you have to update .zprofile file in your home directory.

echo 'export PATH="$PATH:/usr/local/mysql/bin"' >> ~/.zprofile
source ~/.zprofile
mysql -u USERNAME -p

For macOS Mojave and earlier

Although you can always switch to zsh, bash is the default shell in macOS Mojave and earlier and with bash you have to update .bash_profile file.

echo 'export PATH="$PATH:/usr/local/mysql/bin"' >> ~/.bash_profile
source ~/.bash_profile
mysql -u USERNAME -p

For mac OS Catalina :

/usr/local/mysql/bin/mysql -uroot -p

This will prompt you to enter password of mysql

You can simply type in Terminal

brew services start mysql

if you installed mysql via brew on mac

In terminal

sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'

Close that and open new terminal

mysql -u root -p

Give your password