在 MacOS 上有兩種方式安裝 MySQL 服務器:通過 brew 安裝和通過安裝包安裝。
文章目錄
- 1、通過 brew 安裝 MySQL
- 1.1、安裝 MySQL
- 1.2、啟動 MySQL 服務器
- 1.3、配置 MySQL 服務器
- 1.4、MySQL 服務器管理命令
- 2、通過安裝包安裝 MySQL
- 2.1、下載安裝包
- 2.2、安裝 MySQL
- 2.3、管理 MySQL
1、通過 brew 安裝 MySQL
在 MacOS 上,我們可以通過 brew 很容易地安裝 MySQL。
1.1、安裝 MySQL
brew install mysql
1.2、啟動 MySQL 服務器
brew services start mysql
1.3、配置 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!
1.4、MySQL 服務器管理命令
brew 提供了實用的命令可以管理 MySQL 服務器。
brew services start mysql
: 啟動 MySQL 服務器,并設置為自啟動;brew services stop mysql
: 停止 MySQL 服務器,并設置為不自啟動;brew services run mysql
: 只啟動 MySQL 服務器;mysql.server start
: 啟動 MySQL 服務器;mysql.server stop
: 停止 MySQL 服務器。
2、通過安裝包安裝 MySQL
通過安裝包安裝 MySQL 的過程有友好的 UI 界面,更加的方便快捷。
2.1、下載安裝包
點擊 這里 去下載 MySQL 社區版的 dmg
文件,它包含了 MySQL 的安裝器。
2.2、安裝 MySQL
下載安裝包后,按照以下步驟進行安裝:
- 雙擊下載
dmg
文件。雙擊后,會看到 MySQL 安裝器文件,比如:mysql-8.3.0-macos14-x86_64.pkg
。雙擊 MySQL 安裝器文件; - 在「介紹」頁面,直接點擊「繼續」按鈕;
- 在「許可」頁面,直接點擊「繼續」按鈕;
- 在「安裝類型」頁面,可以直接點擊「安裝」按鈕,也可以先點擊 “自定義” 按鈕選擇要安裝的組件后再點擊「安裝」按鈕。
- 在「配置」頁面,密碼加密方式選擇
Use Strong Password Entryption
。然后,輸入 root 用戶的密碼。點擊「Finish」按鈕就完成安裝了
2.3、管理 MySQL
MySQL 偏好窗格會被默認安裝。我們可以在系統偏好窗口中找到它。MySQL 偏好窗格允許我們啟動、停止和配置 MySQL。