在Mac下进行MySQL环境搭建的两种方法

文 / @UTHEME

Mac下进行MySQL环境搭建的两种方法

对于使用Mac OS的开发者而言,MySQL环境的搭建是必不可少的。在这篇文章中,我们将会介绍两种非常简单的方法来搭建MySQL环境。

方法一:用dmg镜像安装

1. 下载MySQL安装包并安装

首先,我们需要从MySQL官网下载Mac版安装包,并按照常规步骤进行安装。安装过程中会生成一个临时密码,并在安装完成后提示,我们需要记住这个密码。

2. 登陆MySQL

通过终端命令行登陆MySQL,输入之前生成的临时密码即可登陆。如果出现提示“bash:mysql:command not found”,则需要执行以下两个命令进行软连接:

cd /usr/local/bin
ln -fs /usr/local/mysql-8.0.11-macos10.13-x86_64/bin/mysql mysql

3. 修改密码

在MySQL8.0.4之前,我们只需要执行“SETPASSWORD=PASSWORD(‘新密码’)”即可修改密码;但是在新版本中,密码认证方式进行了改变。所以,具体步骤可以参考https://blog.csdn.net/yi247630676/article/details/80352655。

方法二:用Homebrew进行安装

众所周知,Mac可以利用homebrew进行安装管理,使用起来也十分方便。如果没有安装homebrew,可以在https://brew.sh/处进行下载和安装。下面是MySQL的安装方式:

brew install mysql
# 安装指定版本
brew install mysql@1.1.1

接下来只需要等待就可以了。出现安装完成的文字后,我们已经很清楚我们需要做什么了。

4. 初始化MySQL

输入“mysql_secure_installation”命令,按照说明进行初始化操作。在初始化过程中,会有很多问题出现,以下是具体的问题及解释:

Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: Y(回复y需要密码8位以上,回复n则不做限制)

The password validation component is not available. Proceeding with the further steps without the component.
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(是否移除匿名用户。考虑安全我选了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) : Y(是否允许远程连mysql的root。我用做本地调试,不是远程服务器,所以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.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y(是否y了删除test数据库,我选了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(选y, 重新加载权限列表)

Success.

All done!

5. 启动MySQL

现在,我们可以通过以下命令进行MySQL的登陆和启动:

mysql -uroot -p (登陆MySQL)
brew services start mysql@5.7 (启动mysql)
brew services stop mysql@5.7 (停止mysql)
mysql.server start (启动mysql,无后台服务)

总结

以上就是两种在Mac下进行MySQL环境搭建的简单方法了。这里还有一个推荐,学习MySQL可以观看《mysql视频教程》。

添加UTHEME为好友
扫码添加UTHEME微信为好友
· 分享WordPress相关技术文章,主题上新与优惠动态早知道。
· 微信端最大WordPress社群,限时免费入群。