给台式机重装后,决定直接在本机建立开发环境,必不可少的就是 mysql 数据库。

安装 brew

安装过程参见


安装完毕后,可以换源加快访问速度

查询 mysql

在终端输入命令

$ brew search mysql
==> Formulae
automysqlbackup            mysql-client@5.7           mysql-search-replace
mysql                      mysql-connector-c++        mysql@5.6
mysql++                    mysql-connector-c++@1.1    mysql@5.7
mysql-client ✔             mysql-sandbox              mysqltuner
==> Casks
homebrew/cask/mysql-connector-python     homebrew/cask/navicat-for-mysql
homebrew/cask/mysql-shell                homebrew/cask/sqlpro-for-mysql
homebrew/cask/mysql-utilities

可以看到有很多种版本,这里我们使用 brew info 命令查看下 mysql 的信息

$ brew info mysql
mysql: stable 8.0.21 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/8.0/en/
Conflicts with:
  mariadb (because mysql, mariadb, and percona install the same binaries)
  percona-server (because mysql, mariadb, and percona install the same binaries)
Not installed
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git/Formula/mysql.rb
License: GPL-2.0
==> Dependencies
Build: cmake ✘
Required: openssl@1.1 ✔, protobuf ✔
==> 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

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Analytics
install: 61,328 (30 days), 204,252 (90 days), 799,732 (365 days)
install-on-request: 60,225 (30 days), 198,703 (90 days), 769,522 (365 days)
build-error: 0 (30 days)

可以看到,默认的 mysql 版本为 8,所以我们不安装这个,只安装到 mysql5.7 即可(兼容性好)。

安装 mysql5.7

执行命令

brew install mysql@5.7

如果你不慎直接安装了默认版本的 mysql 想要回退到 5.7 版本,可以通过输入以下命令实现,(注意提前备份数据)。

brew uninstall mysql
rm -rf /usr/local/var/mysql
rm /usr/local/etc/my.cnf
brew install mysql@5.7

链接可执行文件

brew link --force mysql@5.7

启动 mysql

开机默认启动,执行命令

brew services start mysql@5.7

或者一次运行

/usr/local/opt/mysql@5.7/bin/mysql.server start

初始化配置

执行命令

$ 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?

Press y|Y for Yes, any other key for No: N   // 这个选yes的话密码长度就必须要设置为8位以上,但我只想要6位的
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  //不接受root远程登录账号

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


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y  //删除text数据库
 - 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!
最后修改:2020 年 10 月 16 日
如果觉得我的文章对你有用,请随意赞赏