0.環境
macOS Mojave 10.14.6
brew 安裝方式
MySQL選擇5.7版本
一、安裝mysql5.7
查詢brew中mysql軟件包:
? ~ brew search mysql
安裝mysql5.7版本:
? ~ brew install mysql@5.7
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
mysql@5.7 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 mysql@5.7 first in your PATH run:
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc
For compilers to find mysql@5.7 you may need to set:
export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"
To have launchd start mysql@5.7 now and restart at login:
brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
/usr/local/opt/mysql@5.7/bin/mysql.server start
安裝過程如下:
二、設置環境變量
? ~ vim .zshrc
在文件最后添加:
#mysql5.7 export PATH=/usr/local/opt/mysql@5.7/bin:$PATH
然后按esc退出insert狀態,并在最下方輸入:wq保存退出。
最后在刷新環境變量文件:
? ~ source ~/.zshrc
三、啟動mysql5.7
brew安裝的服務可以直接通過brew service start/stop來進行服務啟停,如下:
# 啟動 mysql, 并設置為開機啟動 brew services start mysql # 關閉 mysql brew services stop mysql # 重啟 mysql brew services restart mysql
但是,本次直接啟動報錯,如圖:
明明之前安裝了,為什么會報錯呢?原因很簡單,服務名不正確!
首先,查看正確的服務名:
? ~ brew services list
顯示正確的服務名應該是‘mysql@5.7’,所以,正確的啟動服務命令為:
# 啟動 mysql, 并設置為開機啟動 brew services start mysql@5.7 # 關閉 mysql brew services stop mysql@5.7 # 重啟 mysql brew services restart mysql@5.7
如圖:
四、執行`mysql_secure_installation`進行安全加固
主要涉及:
1.是否使用弱口令檢查?
2.輸入新密碼;
3.是否移除匿名用戶?
4.是否開啟遠程訪問數據庫連接權限?
5.是否刪除`test`數據庫?
6.刷新權限;
大家可以根據自己的情況進行設置。
? ~ 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? 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. 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.使用Navicat測試鏈接:
2.使用命令行:
六、設置開機啟動
1.將/usr/local/opt/mysql@5.7/homebrew.mxcl.mysql@5.7.plist 文件拷貝到~/Library/LaunchAgents目錄下;
2.然后執行以下語句:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql@5.7.plist
注:這里的文件名以你mysql目錄下的名字,或者你自己創建時起的名字為準
附:Mac OS X的啟動原理:mac固件激活,初始化硬件,加載BootX引導器。BootX加載內核與內核擴展(kext)。內核啟動launchd進程。launchd根據 /System/Library/LaunchAgents, /System/Library/LaunchDaemons , /Library/LaunchDaemons,/Library/LaunchAgents , ~/Library/LaunchAgents里的plist配置,啟動服務守護進程。
[END]