mac安装mysql的两种方法

时间:2022-01-07 02:48:54

安装方式一:用dmg镜像安装
1、安装
下载好mysql MAC版安装包,常规步骤安装,安装过程中会出现如下提示:

2018-04-22T08:07:19.044013Z 1 [Note] A temporary password is generated for [email protected]: TfrNnt9!k1xi

其中TfrNnt9!k1xi 是初始密码,最好先记住!

2、登陆
但是在终端命令行 登陆mysql

mysql -u root -p
提示:-bash: mysql: command not found

遇上-bash: mysql: command not found的情况别着急,这个是因为/usr/local/bin目录下缺失mysql导致,只需建立软链接,即可以解决:

把mysql安装目录,比如MYSQLPATH/bin/mysql,映射到/usr/local/bin目录下:

cd /usr/local/bin

ln -fs /usr/local/mysql-8.0.11-macos10.13-x86_64/bin/mysql mysql
接下来登陆就OK啦(完事!)

3、修改密码
在MySQL8.0.4以前,执行

SET PASSWORD=PASSWORD(‘修改的密码‘);
即可修改密码。

如果mysql是8.0版本以上,这样默认是不行的。因为之前,MySQL的密码认证插件是“mysql_native_password”,而现在使用的是“caching_sha2_password”。

因为当前有很多数据库工具和链接包都不支持“caching_sha2_password”,为了方便,我暂时还是改回了“mysql_native_password”认证插件。

在MySQL中执行命令:

ALTER USER ‘root‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘新密码‘;
总结:
其实镜像安装非常简单,只是在映射mysql指令的时候需要烧点脑子。

安装方式二:用Homebrew进行安装
最近发现mac上一个神奇工具 Homebrew(包缺失管理),就像maven管理jar包的依赖一样,Homebrew帮忙管理各种工具,真是太方便。Homebrew的常用指令。

安装mysql:brew install mysql
feideMacBook-Air:~ fei$ brew install [email protected]
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/[email protected]_sierra.
Already downloaded: /Users/fei/Library/Caches/Homebrew/[email protected]_sierra.bottle.tar.gz
==> Pouring [email protected]_sierra.bottle.tar.gz
==> /usr/local/Cellar/[email protected]/5.7.23/bin/mysqld --initialize-insecure --user=fei
==> Caveats
We‘ve installed your MySQL database without a root password. To secure it run:
mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
mysql -uroot

This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have this software first in your PATH run:
echo ‘export PATH="/usr/local/opt/[email protected]/bin:$PATH"‘ >> ~/.bash_profile

For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/[email protected]/lib
CPPFLAGS: -I/usr/local/opt/[email protected]/include

To have launchd start [email protected] now and restart at login:
brew services start [email protected]
Or, if you don‘t want/need a background service you can just run:
/usr/local/opt/[email protected]/bin/mysql.server start
==> Summary
?? /usr/local/Cellar/[email protected]/5.7.23: 317 files, 234.4MB
看看输出的提示,剩下的该怎么操作其实brew提示的很清楚了!!!

对mysql进行初始化操作:mysql_secure_installation
cometdeMacBook-Pro:~ comet
$ mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
// 这个选yes的话密码长度就必须要设置为8位以上,但我只想要6位的
Press y|Y for Yes, any other key for No: N
Please set the password for root here.

New password: // 设置密码

Re-enter new password: // 再一次确认密码
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
// 移除不用密码的那个账户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.

Normally, root should only be allowed to connect from
‘localhost‘. This ensures that someone cannot guess at
the root password from the network.
//是否禁止远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

... skipping.
By default, MySQL comes with a database named ‘test‘ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

// 是否删除test库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

  • Dropping test database...
    Success.

  • Removing privileges on test database...
    Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!
OK!这样brew提示中的第一步就完成!接下来就可以尝试登陆了

登陆mysql:mysql -u root -p
启动mysql:brew services start [email protected]
停止mysql:brew services stop [email protected]