天天看點

通過brew來安裝mysql

作者:我是楊鵬鵬

通過brew來安裝mysql:

使用指令查詢下brew所支援的版本:

> brew search mysql           
通過brew來安裝mysql

選擇[email protected]版本,比較穩定:

> brew install [email protected]           

執行成功後,傳回提示資訊:

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

[email protected] is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/[email protected]/lib/pkgconfig"


To restart [email protected] after an upgrade:
  brew services restart [email protected]
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/[email protected]/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
==> Summary
  /opt/homebrew/Cellar/[email protected]/5.7.41: 321 files, 232.8MB
==> Running `brew cleanup [email protected]`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).           

預設是沒有密碼,需要通過「mysql_secure_installation」來設定密碼,

第一次安裝,直接執行會提示錯誤:

❯ mariadb-secure-installation
zsh: command not found: mariadb-secure-installation           

需要配置路徑,在上面的傳回資訊中已經提示了,依次執行這幾個echo就可以了:

If you need to have [email protected] first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/[email protected]/lib/pkgconfig"           

設定完成後,重新加載配置,執行

> source ~/.zshrc           

先要啟動資料庫服務:

> mysql.server start           

然後在重新設定密碼:

> mariadb-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?
 // 密碼長度y 是8位以上 n 可以是6位
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.
 // 是否删除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!           

成功~

如果在密碼設定規則中選擇錯誤,可以直接進入mysql修改:

登陸mysql

> mysql -u root           

先檢視目前的密碼驗證規則:

mysql> SHOW VARIABLES LIKE 'validate_password%';           
通過brew來安裝mysql
選項 預設值 參數描述
validate_password_check_user_name ON 設定為ON的時候表示能将密碼設定成目前使用者名。
validate_password_length 8 密碼的最小長度,也就是說密碼長度必須大于或等于8
validate_password_mixed_case_count 1 如果密碼政策是中等或更強的,validate_password要求密碼具有的小寫和大寫字元的最小數量。對于給定的這個值密碼必須有那麼多小寫字元和那麼多大寫字元。
validate_password_number_count 1 密碼必須包含的數字個數
validate_password_policy MEDIUM 密碼強度檢驗等級,可以使用數值0、1、2或相應的符号值LOW、MEDIUM、STRONG來指定。0/LOW:隻檢查長度。1/MEDIUM:檢查長度、數字、大小寫、特殊字元。2/STRONG:檢查長度、數字、大小寫、特殊字元、字典檔案。
validate_password_special_char_count 1 密碼必須包含的特殊字元個數

可以通過語句直接修改對應選項:

mysql> set GLOBAL validate_password_length=4;           

完工了~

繼續閱讀