前言

本文记录一下mac安装mysql的一些过程。
没有brew的小伙伴可以看看这个 Homebrew, 该网站有安装的命令(国内在镜像下安装比较快)。brew很方便也很推荐,例如装些Nginx, redis啥的也很方便。


安装

推荐使用5.7的版本,暂时还不推荐新版本。

brew install mysql@5.7

然后就会自己安装一系列东西。然后就以输出一些提示信息.


文字版本, 方便后期拷贝命令hhhm

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 /opt/homebrew,
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="/opt/homebrew/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/mysql@5.7/include"


To start mysql@5.7:
  brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql@5.7/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
==> Summary
🍺  /opt/homebrew/Cellar/mysql@5.7/5.7.36: 320 files, 233.3MB
==> `brew cleanup` has not been run in the last 30 days, running now...
Removing: /Users/andydennis/Library/Logs/Homebrew/cmake... (64B)
Pruned 0 symbolic links and 2 directories from /opt/homebrew
==> Caveats
==> 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 /opt/homebrew,
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="/opt/homebrew/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/mysql@5.7/include"

To start mysql@5.7:
  brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql@5.7/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql

设置路径

echo 'export PATH="/opt/homebrew/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc



启动

记住安装后要重开一个终端界面再启动

mysql.server start

输入以下命令会发现多了3306端口号

netstat -AaLlnW

或者用以下命令查看一下3306端口对应的进程

lsof -i :3306


关闭

关闭数据库

mysql.server stop



连接

mysql -u root

关于数据库的用法推荐查看 mysql教程-菜鸟教程。

另外不喜欢命令行操作的也可以使用vscode的插件或者JetBrains全家桶的软件连接数据库。



修改密码

记得选定mysql数据库下(use mysql;)运行以下命令,将root用户密码改为root123

alter USER root@localhost IDENTIFIED BY 'root123';

更多推荐

brew 安装mysql (Mac OS)