1. 程式人生 > >Mac 使用brew安裝mysql詳解

Mac 使用brew安裝mysql詳解

環境

作業系統:macOS Sierra Version 10.12.1 
Homebrew:1.1.7

步驟

  1. 安裝Homebrew,詳細步驟參見Homebrew官網。
  2. brew doctor確認brew在正常工作。
  3. brew update更新包。
  4. brew install mysql 安裝mysql。
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.17.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring
mysql-5.7.17.sierra.bottle.tar.gz
==> Using the sandbox ==> /usr/local/Cellar/mysql/5.7.17/bin/mysqld --initialize-insecure --user=liangze --basedir=/usr/local/Cellar/mysql/5.7.17 --datadir=/usr/local/var/mysql --t ==> Caveats 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 now and restart at login: brew services start mysql Or, if you don't want/need a background service you can just run: mysql.server start ==> Summary
/usr/local/Cellar/mysql/5.7.17: 14,226 files, 444.4M
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

這裡順帶提下一下,在網上有很多教程說要在安裝完mysql之後執行mysql_install_db --verbose --user=whoami--basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp,我試了一下行不通,原因mysql官方已經說了,看這段:

mysql_install_db is deprecated as of MySQL 5.7.6 because its functionality has been integrated into mysqld, the MySQL server. To initialize a MySQL installation, invoke mysqld with the –initialize or –initialize-insecure option. For more information, see Section 2.10.1.1, “Initializing the Data Directory Manually Using mysqld”. mysql_install_db will be removed in a future MySQL release.

大意就是說mysql_install_db這個功能已經在MySQL 5.7.6下取消了,它的功能現在已經整合在了mysqld裡了。所以儘量不要直接在網上拿來主義,如果我們仔細看brew install mysql的安裝過程,看這句:

==> /usr/local/Cellar/mysql/5.7.17/bin/mysqld –initialize-insecure –user=liangze –basedir=/usr/local/Cellar/mysql/5.7.17 –datadir=/usr/local/var/mysql –t

說明brew已經為我們安裝好了。我們接下來需要做的brew也已經告訴我了,看這句:

We’ve installed your MySQL database without a root password. To secure it run: 
mysql_secure_installation

5. 
那麼我們就按照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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

莫慌,提示說找不到mysql.sock。原因是mysql程序還沒啟動。 
6. 啟動mysql服務

mysql.server start
  • 1

7.再執行

> mysql_secure_installation
  • 1

發現成功執行命令

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!
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

8. 
最後測試一下登陸

> mysql -u root -p
  • 1

ALL DONE!

總結

學會使用brew info 軟體名來檢視提示。以這次安裝mysql的過程為力,網上的教程有可能有出入,和實際情況不同,但如果我們仔細檢視提示,是可以靠自己完成的。

參考資料