1. 程式人生 > >[Mac 10.13.4] 使用Homebrew安裝Mysql全過程

[Mac 10.13.4] 使用Homebrew安裝Mysql全過程

環境

作業系統:macOS High Sierra Version 10.13.4

Homebrew:1.6.4

步驟

  1. 安裝Homebrew,詳細步驟參見Homebrew官網。
  2. brew doctor確認brew在正常工作。
  3. brew update更新包。
  4. brew install mysql 安裝mysql。

其中執行mysql_secure_installation 可以設定密碼

執行brew service start mysql 可以後臺啟動mysql
執行mysql.server start 前臺啟動mysql(關閉控制檯,服務停止)

按照brew的提示執行 mysql_secure_installation設定密碼,執行後會報錯:

 > mysql_secure_installation

Securing the MySQL server deployment.

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

這個錯誤是因為MySQL服務還沒啟動

6、啟動MySQL服務

mysql.server start 


在我的機器上第一次沒報錯,以後執行此命令有可能會報許可權錯誤。嘗試修改許可權但是最後還是不行,最後採用sudo啟動解決此問題。

sudo mysql.server start


7、設定密碼

mysql_secure_installation

如果報許可權錯誤執行 sudo 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

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
Please set the password for root here.
// 然後按照所選的密碼強度要求設定密碼
New password:

Re-enter new password:

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
 ... Failed! Error: Your password does not satisfy the current policy requirements

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.
// 禁止遠端root登入,我選的是不禁止。因為我的mac上的資料庫不會放到公網上,也不會存什麼敏感資料
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no

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

// 這裡刪除預設自帶的test資料庫
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!

8、連線MySQL

mysql -u root -p

最後特別注意啟動方式的問題:

執行brew service start mysql 可以後臺啟動mysql

執行mysql.server start 前臺啟動mysql

如果提示許可權問題使用sudo指令,我的筆記本上如果不加此指令各種許可權問題。