一、环境和安装
macOS 版本:10.13.1
Mysql 版本: mysql Ver 14.14 Distrib 5.7.20, for osx10.13 (x86_64) using EditLine wrapper
使用 brew 安装 MySQL:
brew install mysql
brew tap homebrew/services
brew services start mysql
其他命令:
# 获取 service 列表
brew services list
# 重启 mysql 服务
brew services restart mysql
# 停止 mysql 服务
brew services stop mysql
安装参考:Install MySQL on macOS Sierra
brew services 官网:homebrew-services
二、碰到的问题
1、无法连接
描述:
使用 Navicat 或者是命令行都无法连接,均提示用户名/密码错误。
ERROR 1045 (28000) Access denied for user 'root'@'localhost' (using password: NO)
解决方法:
使用不要密码登录:
sudo mysqld_safe --skip-grant-tables &
再使用下面脚本重新设置下数据库的 root 密码:
UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass')
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;
2、日志里面出现mysql 无法重启启动的错误日志
日志详情:
Dec 6 01:36:35 --- last message repeated 1 time ---
Dec 6 01:36:35 s01 com.apple.xpc.launchd[1] (homebrew.mxcl.mysql[18960]): Service exited with abnormal code: 1
Dec 6 01:36:35 s01 com.apple.xpc.launchd[1] (homebrew.mxcl.mysql): Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
Dec 6 01:36:45 s01 com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.system): Session adoption is only allowed in user domains.
Dec 6 01:36:45 --- last message repeated 1 time ---
Dec 6 01:36:45 s01 com.apple.xpc.launchd[1] (homebrew.mxcl.mysql[19029]): Service exited with abnormal code: 1
Dec 6 01:36:45 s01 com.apple.xpc.launchd[1] (homebrew.mxcl.mysql): Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
Dec 6 01:36:55 s01 com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.system): Session adoption is only allowed in user domains.
Dec 6 01:36:55 --- last message repeated 1 time ---
Dec 6 01:36:55 s01 com.apple.xpc.launchd[1] (homebrew.mxcl.mysql[19098]): Service exited with abnormal code: 1
Dec 6 01:36:55 s01 com.apple.xpc.launchd[1] (homebrew.mxcl.mysql): Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
Dec 6 01:37:06 s01 com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.system): Session adoption is only allowed in user domains.
解决:
我查了下系统跑的有 mysql 进程(ps -e | grep mysql
),也就是说出现了重复创建 mysql 服务。
查看 macos 的自启动服务列表:
ll ~/Library/LaunchAgents
ll /Library/LaunchDaemons
ll /Library/LaunchAgents
发现有重复的 mysql 服务,删除掉重复的就是。
因为使用的是 brew services 方式运行,所以可以通过 brew services list
来获取正在运行的,把另外一个删除了就行。
三、最后说2句
推荐使用 docker 方式,之前我使用的 mssql docker 版本就好安装的多。
用了这么久,mssql docker 偶尔会出现莫名其妙的闪退,而且也没有发现日志,疑惑!!!!。