Mac 安装mysql
安装
1. 查找可用的安装包
➜ ~ brew search mysql
==> Formulae
automysqlbackup mysql-client@5.7 mysql@5.6
mysql ✔ mysql-connector-c++ mysql@5.7
mysql++ mysql-sandbox mysqltuner
mysql-client mysql-search-replace qt-mysql
2. 安装
这里我们选择最新版本
➜ ~ brew install mysql
==> 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 restart mysql after an upgrade:
brew services restart mysql
Or, if you don't want/need a background service you can just run:
/usr/local/opt/mysql/bin/mysqld_safe --datadir=/usr/local/var/mysql
3. 启动mysql
brew services start mysql
默认账号:root,无密码
尝试登录 mysql -u root
4. 配置
根据上一步安装mysql时,输出的提示,执行命令 mysql_secure_installation
,接下去按提示一步一步来就可以了
5. 登录
mysql -u root -p密码
远程登录
用第三方客户端登录,可能会出现如下问题:Authentication plugin ‘caching_sha2_password‘ cannot be loaded
因为mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password
处理办法:
- 重新安装低版本的mysql
brew install mysql@5.7
- 修改加密规则
这里重点介绍第二种办法:
进入mysql
mysql -u root -p
查看用户的密码规则,以及对应的host
mysql> use mysql;
mysql> select user,host,plugin from user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
4 rows in set (0.00 sec)
可以看到root用户的加密规则是:caching_sha2_password
修改加密规则以及密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '替换成你的密码';
检查一下是否修改成功
mysql> select user,host,plugin from user;
在尝试用第三方客户端连接
参考文章
MySQL 连接出现 Authentication plugin ‘caching_sha2_password‘ cannot be loaded 的解决