通过Homebrew安装MySql

通过Homebrew来安装mysql:

使用命令查询下brew所支持的版本:

> brew search mysql

选择mysql@5.7版本,比较稳定:

> brew install mysql@5.7

执行成功后,返回提示信息:

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

mysql@5.7 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 mysql@5.7 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/mysql@5.7/include"

For pkg-config to find mysql@5.7 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql@5.7/lib/pkgconfig"


To restart mysql@5.7 after an upgrade:
  brew services restart mysql@5.7
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql@5.7/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
==> Summary
🍺  /opt/homebrew/Cellar/mysql@5.7/5.7.41: 321 files, 232.8MB
==> Running `brew cleanup mysql@5.7`...
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 mysql@5.7 first in your PATH, run:
  echo'export PATH="/opt/homebrew/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/mysql@5.7/lib"export CPPFLAGS="-I/opt/homebrew/opt/mysql@5.7/include"

For pkg-config to find mysql@5.7 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql@5.7/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%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password.check_user_name    | ON     |
| validate_password.dictionary_file    |        |
| validate_password.length             | 8      |
| validate_password.mixed_case_count   | 1      |
| validate_password.number_count       | 1      |
| validate_password.policy             | MEDIUM |
| validate_password.special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set, 1 warning (0.00 sec)
选项默认值参数描述
validate_password_check_user_nameON设置为ON的时候表示能将密码设置成当前用户名。
validate_password_length8密码的最小长度,也就是说密码长度必须大于或等于8
validate_password_mixed_case_count1如果密码策略是中等或更强的,validate_password要求密码具有的小写和大写字符的最小数量。对于给定的这个值密码必须有那么多小写字符和那么多大写字符。
validate_password_number_count1密码必须包含的数字个数
validate_password_policyMEDIUM密码强度检验等级,可以使用数值0、1、2或相应的符号值LOW、MEDIUM、STRONG来指定。0/LOW:只检查长度。1/MEDIUM:检查长度、数字、大小写、特殊字符。2/STRONG:检查长度、数字、大小写、特殊字符、字典文件。
validate_password_special_char_count1密码必须包含的特殊字符个数

可以通过语句直接修改对应选项:

mysql> set GLOBAL validate_password_length=4;

完工了~

滚动至顶部