首先我们需要安装 Homebrew 包管理器,在开始之前请确保你的Mac已经安装了 Xcode,当然了,如果你没有安装Xcode,Homebrew会在安装过程中提示(强迫)你安装Xcode Command Line Tools。
# 安装 homebrew,是的你没看错,在终端里粘贴以下命令即可
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安装会毫无乐趣的自动进行,中间会提醒你输入系统密码。
讲真,homebrew 是一款非常良心的包管理器,它会在你绝望的时候通过一条命令搞定很多事情我就不再详细描述了。
安装MYSQL之前,我们先看一下由homebrew管理的MYSQL包是什么版本。
# 执行以下命令,查看MYSQL包的情况
➜ ~ brew info mysql
mysql: stable 5.7.19 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
Conflicts with:
mariadb (because mysql, mariadb, and percona install the same binaries.)
mariadb-connector-c (because both install plugins)
mysql-cluster (because mysql, mariadb, and percona install the same binaries.)
mysql-connector-c (because both install MySQL client libraries)
percona-server (because mysql, mariadb, and percona install the same binaries.)
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mysql.rb
==> Dependencies
Build: cmake ✘
Required: openssl ✘
==> Requirements
Required: macOS >= 10.7 ✔
==> Options
--with-archive-storage-engine
Compile with the ARCHIVE storage engine enabled
--with-blackhole-storage-engine
Compile with the BLACKHOLE storage engine enabled
--with-debug
Build with debug support
--with-embedded
Build the embedded server
--with-local-infile
Build with local infile loading support
--with-test
Build with unit tests
==> 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 To have launchd start mysql now and restart at login: brew services start mysql Or, if you don't want/need a background service you can just run:
mysql.server start
从以上一大坨信息中,我们大致可以明白以下几点:
1. MYSQL的版本是 5.7.19
2. 它存在一些冲突包,没有办法和他们共存
3. 安装后默认是没有密码的,如果你想安全一点,可以执行 mysql_secure_installation 来进行引导安全设置
4. 你可以将MYSQL设置为开机启动, brew services start mysql
5. 你也可以每次都手动启动或关闭MYSQL mysql.server start/stop/restart
接下来我们安装 MYSQL
brew install mysql
# 是的你没有看错,啰嗦了半天其实只需要在终端里执行这一条命令
等待安装完毕,我们将 MYSQL 服务设置为开机启动
brew services start mysql
重启 MYSQL 服务
brew services restart mysql
关闭 MYSQL 服务
brew services stop mysql
查看 MYSQL 服务状态
brew services list
另外,由于默认安装没有设置密码,所以你可以直接登录,然后设置 root 密码
# 登录
mysql -uroot
# 修改root密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'your-password' WITH GRANT OPTION;
flush privileges;
好了,再退出重新登录试试看。