1 分鐘閱讀

本文使用 Mac OS X EI Capitan 10.11.6 / Homebrew 0.9.9

前言

此為MySQL於MAC環境的安裝筆記,本文將使用homebrew安裝MySQL,並大致紀錄如何手動/自動開啟服務

使用homebrew安裝

brew install 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.

解決方式

#開啟權限
$ sudo chown -R $(whoami) /usr/local/
#重新link一次
$ brew link mysql

預設root帳號沒密碼,如需要請輸入下列指令

mysql_secure_installation

如出現以下錯誤訊息

Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

表示需要先把服務on起來mysql.server start再執行動作

然後會有以下囉唆的問題:

  1. 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: Please set the password for root here. New password: Re-enter new password: 總之就是先問你要不要安裝VALIDATE PASSWORD這個套件(可以幫你有效驗證密碼的複雜度,你設定太簡單會提醒你),不想裝可以Enter跳過就好,然後輸入兩次密碼結束第一個回合(問題)

  2. 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

  3. 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

  4. 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即可

  5. 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即可

進入sql指令模式

mysql -uroot

Service手動啟動/關閉方式

mysql.server start
mysql.server stop
mysql.server restart

利用homwbrew services開機自動啟動/關閉

# 沒加上sudo只是手動啟動
$ brew services start mysql
$ brew services stop mysql

# 加上sudo可以在開機自動啟動
$ sudo brew services start mysql
$ sudo brew services start mysql

# 查看services清單
$ brew services list

若沒有homebrew service指令,可以輸入以下指令安裝

brew tap homebrew/services

查看 mysql 相關設定值

$ mysqladmin variables -u root -p
# => 可以查到socket位置: /tmp/mysql.sock

參考

  1. brew link錯誤訊息解決方式
  2. Starting and Stopping Background Services with Homebrew

更新時間:

留言