macOS Mojave 10.14.6
brew 安装方式
MySQL选择5.7版本
查询brew中mysql软件包:
➜ ~ brew search mysql
安装mysql5.7版本:
➜ ~ brew install mysql@5.7
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
mysql@5.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have mysql@5.7 first in your PATH run:
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc
For compilers to find mysql@5.7 you may need to set:
export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"
To have launchd start mysql@5.7 now and restart at login:
brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
/usr/local/opt/mysql@5.7/bin/mysql.server start
安装过程如下:
➜ ~ vim .zshrc
在文件最后添加:
#mysql5.7 export PATH=/usr/local/opt/mysql@5.7/bin:$PATH
然后按esc退出insert状态,并在最下方输入:wq保存退出。
最后在刷新环境变量文件:
➜ ~ source ~/.zshrc
brew安装的服务可以直接通过brew service start/stop来进行服务启停,如下:
# 启动 mysql, 并设置为开机启动 brew services start mysql # 关闭 mysql brew services stop mysql # 重启 mysql brew services restart mysql
但是,本次直接启动报错,如图:
明明之前安装了,为什么会报错呢?原因很简单,服务名不正确!
首先,查看正确的服务名:
➜ ~ brew services list
显示正确的服务名应该是‘mysql@5.7’,所以,正确的启动服务命令为:
# 启动 mysql, 并设置为开机启动 brew services start mysql@5.7 # 关闭 mysql brew services stop mysql@5.7 # 重启 mysql brew services restart mysql@5.7
如图:
主要涉及:
1.是否使用弱口令检查?
2.输入新密码;
3.是否移除匿名用户?
4.是否开启远程访问数据库连接权限?
5.是否删除`test`数据库?
6.刷新权限;
大家可以根据自己的情况进行设置。
➜ ~ 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 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 ... 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 - 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!
1.使用Navicat测试链接:
2.使用命令行:
1.将/usr/local/opt/mysql@5.7/homebrew.mxcl.mysql@5.7.plist 文件拷贝到~/Library/LaunchAgents目录下;
2.然后执行以下语句:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql@5.7.plist
注:这里的文件名以你mysql目录下的名字,或者你自己创建时起的名字为准
附:Mac OS X的启动原理:mac固件激活,初始化硬件,加载BootX引导器。BootX加载内核与内核扩展(kext)。内核启动launchd进程。launchd根据 /System/Library/LaunchAgents, /System/Library/LaunchDaemons , /Library/LaunchDaemons,/Library/LaunchAgents , ~/Library/LaunchAgents里的plist配置,启动服务守护进程。
[END]