背景

目前大部分网站或者应用使用MySQL版本还是5.7,作者本机上也一直使用该版本进行测试,上次升级Mac系统后发现MySQL无法启动所以准备重装一下。在官网(https://downloads.mysql.com/archives/community/)逛了一圈有点懵逼了,MySQL Community为了让众多开发者升级到8.0真是费尽了心思,mysql-5.7.31-macos10.14-x86_64是支持Mac的最后一个版本,从mysql-5.7.32到5.7.42不再提供Mac系统安装包。为了避免繁琐的版本选择,决定采用brew进行安装,主版本号确定为5.7后直接干就完了。

Homebrew安装MySQL 5.7

需要注意的是如果没有使用魔法,brew更新和下载软件时可能比较慢。为了加速下载本人就直接使用魔法了,命令行下:

export http_proxy=http://127.0.0.1:1087;export https_proxy=http://127.0.0.1:1087;export ALL_PROXY=socks5://127.0.0.1:1080

命令行下使用魔法是没有提示的,为了保证没有问题可以自己测试下:

curl cip.cc
IP	: 199.180.118.112
地址	: 美国  宾夕法尼亚州  威尔克斯-巴里
运营商	: volumedrive.com

数据二	: 美国宾夕法尼亚州拉克万纳县克拉克斯萨密特自治区 | VolumeDrive股份有限公司

数据三	: 美国加利福尼亚
URL	: http://www.cip.cc/199.180.118.112

可以看到魔法是生效的直接安装:

brew install mysql@5.7

==> Downloading https://formulae.brew.sh/api/formula.jws.json
################################################################################################## 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
################################################################################################## 100.0%
Warning: mysql@5.7 has been deprecated because it is not supported upstream!
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/5.7/manifests/5.7.43
################################################################################################## 100.0%
.......省略.............省略.............省略.............省略.............省略...........
==> Installing dependencies for mysql@5.7: ca-certificates, openssl@3, openssl@1.1, abseil, jsoncpp and protobuf
==> Installing mysql@5.7 dependency: ca-certificates
==> Pouring mysql@5.7--5.7.43.ventura.bottle.tar.gz
==> /usr/local/Cellar/mysql@5.7/5.7.43/bin/mysqld --initialize-insecure --user=bilpam --basedir=/usr/local/Cellar/mysql@5.7/5.7.43 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> Caveats
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 /usr/local,
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="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

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

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

To start mysql@5.7 now and restart at login:
  brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/mysql@5.7/bin/mysqld_safe --datadir\=/usr/local/var/mysql
==> Summary
🍺  /usr/local/Cellar/mysql@5.7/5.7.43: 321 files, 234.6MB
==> 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`).
==> Upgrading 25 dependents of upgraded formulae:
Disable this behaviour by setting HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
apr-util 1.6.1_4 -> 1.6.3_1, cairo 1.16.0_5 -> 1.18.0, curl 7.86.0 -> 8.3.0, gnutls 3.7.8 -> 3.8.1, ffmpeg 5.1.2 -> 6.0_1, freetds 1.3.13 -> 1.4.2, gobject-introspection 1.74.0 -> 1.78.1, httpd 2.4.54_1 -> 2.4.57_1, krb5 1.20 -> 1.21.2, libevent 2.1.12 -> 2.1.12_1, libpq 15.0 -> 16.0, libssh2 1.10.0 -> 1.11.0_1, macvim 9.0.472_1 -> 9.0.1897, node@16 16.18.1 -> 16.20.2, openldap 2.6.3 -> 2.6.6, p11-kit 0.24.1_1 -> 0.25.0, python@3.10 3.10.8 -> 3.10.13, python@3.9 3.9.15 -> 3.9.18, qt 6.4.0 -> 6.5.2, rtmpdump 2.4+20151223_1 -> 2.4-20151223_3, php@8.0 8.0.25 -> 8.0.30, ruby 3.1.2_1 -> 3.2.2_1, srt 1.5.1 -> 1.5.3, unbound 1.17.0 -> 1.18.0_1, wget 1.21.3 -> 1.21.4
==> Downloading https://ghcr.io/v2/homebrew/core/apr-util/manifests/1.6.3_1
.......省略.............省略.............省略.............省略.............省略..........

配置MySQL环境变量

安装完之后我们需要配置MySQL的环境变量,正常情况下将环境变量追加到用户目录下的.bash_profile文件即可:

cd ~
vi .bash_profile
PATH=/usr/local/opt/mysql@5.7/bin:$PATH                #/usr/local/opt/mysql@5.7/是你的安装目录

按下Esc输入:wq,保存并退出vi编辑器,输入source .bash_profile使配置立即生效。

启动并初始化MySQL

命令行下执行如下命令启动MySQL服务

mysql.server start

如果使用sudo启动可能会出现如下问题,需要注意一下:

sudo mysql.server start

Starting MySQL
.Logging to '/usr/local/var/mysql/uexfdeMacBook-Pro.local.err'.
 ERROR! The server quit without updating PID file (/usr/local/var/mysql/uexfdeMacBook-Pro.local.pid).

初始化MySQL

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: 50

# 确认使用该密码吗,选择 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登录,我选的是yes,即使本地也需要保证安全吧
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

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

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

 

声明
1.本网站名称: 优易先锋资源网
2.本站永久网址:https://res.uexf.com
3.本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长support@uexf.com
4.本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责
5.本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6.本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新