使用Homebrew安装MySQL

3,877 阅读3分钟

关于 Homebrew 请参考 Homebrew 与 Cakebrew

通过 Cakebrew 安装 mysql

安装 mysql 5.7

如图,输入你要安装的软件,选择其中一个,点击上方的安装按钮,就开始安装了,由于我已经安装了 msyql@5.7 所以上面显示的是卸载

配置 msyql 的环境变量

安装完之后我们需要配置 msyql 的环境变量

  1. 切换到用户目录下cd ~
  2. 编辑 .bash_profile 配置文件 vi .bash_profile
  3. 输入i 切换到编辑模式
  4. 在path中追加安装路径 PATH=/usr/local/opt/mysql@5.7/bin:$PATH ,/usr/local/opt/mysql@5.7/是你的安装目录
  5. 按下Esc输入:wq,保存并退出vi编辑器
  6. 输入source .bash_profile使配置立即生效

启动并初始化msyql

启动mysql服务

sudo mysql.server start

初始化mysql

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?
# 上面的意思是说你是否要为root设置密码,选择 y
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
# 这里要你选一个密码强度等级,0表示LOW,只要求你的密码长度大于8,由于我只是在本地玩玩,选择0
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0

Please set the password for root here.
# 按照所选的密码强度要求 设定密码
New password:
# 再次输入密码
Re-enter new password:

Estimated strength of the password: 25
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
# 确认使用该密码吗,选择 y
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.
# 删除默认的匿名用户吗,选择 y
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登录,我选的是no。因为我只是本地玩玩不会用于生产,也不会存什么敏感数据
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数据库吗,我选择 no
Remove test database and access to it? 
(Press y|Y for Yes, any other key for No) : no

 - 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.
# 是否让配置立即生效,选择 y
Reload privilege tables now? 
(Press y|Y for Yes, any other key for No) : y

Success.
All done!

设置完成之后,就可以通过 root 和刚刚设置的密码登陆mysql了

mysql -u root -p