一、安装Homebrew。
Homebrew 是Mac OS的软件包管理器,它可以自动安装软件的依赖包,非常便捷。我们将使用Homebrew来安装mysql8.0。如果还没有安装Homebrew,将以下命令粘贴至终端进行安装:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
二、安装mysql。
如果之前已经安装过Homebrew,需要先更新一下Homebrew,要不然后续安装的mysql可能还是5.x低版本,在终端执行以下命令:
brew update
等更新完成后开始安装mysql8.0:
brew install mysql
注意:如果之前已经使用Homebrew安装过mysql5.x低版本,应使用以下的升级命令替换以上的安装命令:
brew upgrade mysql
Homebrew会自动下载安装mysql的相应依赖包并最终完成mysql的下载安装或升级;
三、启动mysql。
启动mysql有以下3种方式:
- 把mysql注册为开机自动启动的系统服务,然后使用launchctl启动:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents/
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
- 使用brew services把mysql注册为开机自动启动的系统服务在后台常驻运行,此方法适用于任何使用Homebrew安装的服务:
#启动
brew services start mysql
#重启
brew services restart mysql
#停止
brew services stop mysql
#查看所有可由Homebrew管理的服务
brew services list
- 使用mysql自带的mysql.server启动,但电脑重启后mysql服务不会自动启动:
#启动
mysql.server start
#重启
mysql.server restart
#停止
mysql.server stop
mysql服务启动后即可登录mysql,root用户默认没有密码,提示输入密码时直接回车即可:
mysql -u root -p
注意:如果是从低版本升级到8.0,登录后show databases;
或者尝试修改权限时可能会出现以下错误:
The user specified as a definer ('mysql.infoschema'@'localhost') does not exist
#或者
Table 'mysql.role_edges' doesn't exist
这是因为升级后某些表结构发生了变化,需要在终端运行升级脚本:
mysql_upgrade -u root -p
四、配置mysql。
mysql的配置文件路径查找优先级为/etc/my.cnf
,/etc/mysql/my.cnf
,/usr/local/etc/my.cnf
,通过Homebrew安装的my.cnf放在/usr/local/etc/
中。
设置root登录密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
也可以通过启用validate_passwrod插件为root设置密码,在终端中运行:
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: 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
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 100
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.
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) : 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
- 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!
validate_password插件用于测试密码并提高安全性,开启此插件后,在设置或修改用户登录密码时,如果新的密码不符合密码校验策略,会报以下错误:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
可在mysql中查询validate_password的各个属性设置的值:
mysql> show variables like "validate_password%";
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | ON |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
- validate_password_check_user_name #设为 ON 时密码中不允许包含用户名
- validate_password_dictionary_file #用于密码验证的字典文件路径,仅在密码验证级别为STRONG时生效
- validate_password_length #限制密码最短长度不得少于8位
- validate_password_mixed_case_count #密码中大小写字母的最少个数
- validate_password_number_count #密码中数字的最少个数
- validate_password_policy #设置密码验证级别
- validate_password_special_char_count #密码中特殊符号的最少个数
validate_password插件提供三种级别的验证标准,由参数validate_password_policy控制:
- LOW(0):只验证密码长度,不得少于8位。
- MEDIUM(1) :除验证长度外,还需满足至少包含1个数字、1个小写字母、1个大写字母和一个特殊符号。
- STRONG(2):除以上所有规则外,新设密码不能包含密码验证字典文件中的字词,文件中的字词以换行来分隔。
插件的参数可在配置文件my.cnf中进行灵活配置:
vim /usr/local/etc/my.cnf
在[mysqld]
中添加以下的配置并重启mysql:
validate_password = on
validate_password_check_user_name = on
validate_password_dictionary_file = /usr/local/etc/validate_password_file
validate_password_length = 8
validate_password_mixed_case_count = 1
validate_password_number_count = 1
validate_password_policy = MEDIUM
validate_password_special_char_count = 1
注意:在MySQL 8.0中,caching_sha2_password是默认的身份验证插件,而不再是mysql_native_password。如果客户端不支持这个身份验证插件连接mysql服务器时就会报错,可以编辑配置文件my.cnf,更改默认的身份认证插件:
vim /usr/local/etc/my.cnf
在[mysqld]
中添加以下的配置并重启mysql:
default_authentication_plugin=mysql_native_password
如果安装后有问题可以用命令brew uninstall mysql
卸载后再使用brew install mysql
重新安装。重装之前先停止mysql服务,然后删除mysql的数据文件存放目录/usr/local/var/mysql
,不同的系统存放目录可能有差异,可在mysql中查看数据文件的存放目录:
mysql> show variables like "%datadir%";
+---------------+-----------------------+
| Variable_name | Value |
+---------------+-----------------------+
| datadir | /usr/local/var/mysql/ |
+---------------+-----------------------+
1 row in set (0.00 sec)