os x 10.10.5下用brew安裝mysql
homebrew是mac下一個很好的包管理器,通過它安裝一些軟體很方便,而且便於管理。雖然MySQL官方提供了mac的安裝包,但我還是習慣於用brew安裝mysql。
Taps
因為brew是從github下載的軟體包,所以可能會很慢,我這裡是用的ss+proxychains-ng強制brew使用socks5代理的。關於proxychains-ng的具體用法我這裡最不贅述了。
準備工作
首先brew serach mysql
,搜尋到很多mysql相關的包:
automysqlbackup mysql mysql-sandbox
groonga-normalizer -mysql mysql++ mysql-search-replace
homebrew/versions/mysql51 mysql-cluster mysqltuner
homebrew/versions/mysql55 mysql-connector-c
homebrew/versions/mysql56 mysql-connector-c++
homebrew/php/php53-mysqlnd_ms homebrew/php/php56-mysqlnd_ms
homebrew/php/php54-mysqlnd_ms Caskroom/cask/mysqlworkbench
homebrew/php/php55-mysqlnd_ms Caskroom/cask/navicat-for-mysql
因為我需要做C語言連線mysql,所以只需要安裝mysql
。
安裝mysql
直接執行brew install mysql
,如果需要使用proxychains-ng,就用proxychains4 brew install mysql
,接下來就等mysql安裝好了,可是事情並沒有那麼順利。
安裝過程中提示錯誤:
Error: The 'brew link' step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink share/man/man8/mysqld.8
/usr/local/share/man/man8 is not writable.
You can try again using:
brew link mysql
當我使用brew link mysql
建立mysql軟連線的時候出現同樣的錯誤。看錯誤的提示大概就是/usr/local/share/man/man8 is not writable
這個目錄不可寫,這樣的話就簡單了。
執行brew doctor
發現
Warning: Some directories in /usr/local/share/man aren't writable.
This can happen if you "sudo make install" software that isn't managed
by Homebrew. If a brew tries to add locale information to one of these
directories, then the install will fail during the link step.
You should probably `sudo chown -R $(whoami)` them:
/usr/local/share/man/man8
接著執行sudo chown -R $(whoami) /usr/local/share/man/man8
,完美解決問題。
再brew link mysql
成功,mysql相關的各種命令都已經連結到了/usr/local/bin
目錄下。
需要注意的是,brew最後輸出的一段話:
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
To connect run:
mysql -uroot
To have launchd start mysql at login:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
mysql.server start
安全設定嚮導
需要注意的是,安裝好得mysql是沒有root密碼的,需要自己設定root密碼。
首先啟動mysql,mysql.server start
,然後設定資料庫訪問許可權,
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: y <-是否使用VALIDATE PASSWORD PLUGIN,這個看自己了
There 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 file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1(這裡我選了1,即MEDIUM,密碼需要有大小寫字母、數字、特殊符號)
Please set the password for root here.
New password: (輸入你的密碼)
Re-enter new password: (再次輸入你的密碼)
Estimated strength of the password: 100
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) : (是否禁止root遠端登入,根據自己的需求選擇Y/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) : (是否刪除test資料庫,直接回車)
... skipping.
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) : (是否重新載入許可權表,直接回車)
... skipping.
All done! ```
如果想設定mysql開機啟動:
To have launchd start mysql at login:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
如果想自己手動啟動
Or, if you don’t want/need launchctl, you can just run:
mysql.server start