首先我們需要安裝 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
Weve 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 dont 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;

好了,再退出重新登錄試試看。

推薦閱讀:

相关文章