在 macOS 上安裝 MySQL 有多種方法,以下是兩種常見的安裝方式:通過 Homebrew 安裝和通過安裝包安裝。以下是詳細的步驟:
一、通過 Homebrew 安裝 MySQL
Homebrew 是 macOS 的包管理器,使用它安裝 MySQL 非常方便。
1.安裝 Homebrew(如果尚未安裝)
打開終端,運行以下命令安裝 Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2.安裝 MySQL
在終端中運行以下命令安裝 MySQL:
brew install mysql
3.啟動 MySQL 服務
啟動 MySQL 服務,并設置為開機自啟:
brew services start mysql
4.配置 MySQL
運行以下命令配置 MySQL 的安全性:
mysql_secure_installation
在配置過程中,你可以設置 root 用戶的密碼,配置一些選項以增強 MySQL 服務器的安全性。你會看到如下類似的輸出:
Securing the MySQL server deployment.Connecting to MySQL using a blank password.VALIDATE PASSWORD COMPONENT 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 component?Press y|Y for Yes, any other key for No: YThere are three levels of password validation policy:LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary filePlease enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.New password:Re-enter new password:Estimated strength of the password: 25
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
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) : Y
Success.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.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!
5.管理 MySQL 服務
Homebrew 提供了實用的命令來管理 MySQL 服務:
? 啟動 MySQL 服務并設置為自啟動:
brew services start mysql
? 停止 MySQL 服務并設置為不自啟動:
brew services stop mysql
? 只啟動 MySQL 服務:
brew services run mysql
? 使用mysql.server
命令啟動或停止 MySQL 服務:
mysql.server startmysql.server stop
二、通過安裝包安裝 MySQL
通過安裝包安裝 MySQL 的過程有友好的 UI 界面,更加方便快捷。
1.下載安裝包
訪問MySQL 官方下載頁面,選擇適合 macOS 的 DMG 安裝包。例如,下載mysql-8.0.26-macos10.15-x86_64.pkg
。
2.安裝 MySQL
? 雙擊下載的.dmg
文件,將其掛載。
? 雙擊其中的.pkg
安裝包文件啟動安裝向導。
? 按照向導提示完成安裝,包括同意許可協議、選擇安裝位置(通常不可更改)等。
? 在安裝過程中,系統會提示你設置 MySQL 的 root 用戶密碼,請務必記住。
? 安裝完成后,通常會在“系統偏好設置”中添加一個 MySQL 的圖標,用于啟動和停止 MySQL 服務。
3.配置 MySQL
? 配置環境變量(可選):
為了方便在終端中使用 MySQL 命令,可以將 MySQL 的bin
目錄(通常位于/usr/local/mysql/bin
)添加到 PATH 環境變量中。編輯~/.bash_profile
或~/.zshrc
文件,添加以下內容:
export PATH="/usr/local/mysql/bin:$PATH"
然后運行以下命令使配置生效:
source ~/.bash_profile
或者:
source ~/.zshrc
? 運行安全腳本:
安裝完成后,建議運行安全腳本:
mysql_secure_installation
這將引導你完成一些安全設置,如設置 root 密碼、移除匿名用戶、禁止遠程 root 登錄等。
三、驗證安裝
? 打開終端,運行以下命令驗證 MySQL 是否安裝成功:
mysql -u root -p
輸入你在安裝過程中設置的 root 用戶密碼。
? 在 MySQL 命令行中,運行以下命令查看數據庫列表和 MySQL 版本:
SHOW DATABASES;SELECT VERSION();
四、常見問題
1.無法啟動 MySQL 服務
? 如果 MySQL 服務無法啟動,可以嘗試以下命令手動啟動:
sudo /usr/local/mysql/support-files/mysql.server start
? 檢查 MySQL 的日志文件,通常位于/usr/local/mysql/data/
目錄下,查看具體的錯誤信息。
2.忘記 root 密碼
? 如果你忘記了 root 用戶的密碼,可以通過以下步驟重置密碼:
? 停止 MySQL 服務:
sudo /usr/local/mysql/support-files/mysql.server stop```? 啟動 MySQL 服務,跳過權限表:```shsudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &```? 登錄 MySQL:```shmysql -u root```? 重置 root 密碼:```sqlFLUSH PRIVILEGES;ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';```? 退出 MySQL 并重啟服務:```shexitsudo /usr/local/mysql/support-files/mysql.server restart```五、總結通過上述步驟,你可以在 macOS 上成功安裝并配置 MySQL。使用 Homebrew 安裝 MySQL 是最簡單和推薦的方式,因為它提供了方便的命令行工具來管理 MySQL 服務。如果你更喜歡圖形化界面,可以通過安裝包安裝 MySQL。安裝完成后,建議運行安全腳本`mysql_secure_installation`,以增強 MySQL 服務器的安全性。