Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

macOS安装/卸载MySQL 8.0.16 #121

Open Qingquan-Li opened 5 years ago

Qingquan-Li commented 5 years ago

环境:


一、下载安装 MySQL 8.0.16

参考:

下载 .dmg 安装包,进行包安装,安装过程中全部选择默认即可。

注意: MySQL 8.0 默认的密码机制是 caching_sha2_password (Strong),这一步不建议选择默认,建议选回 mysql_native_password (Legacy)。

附: 也可以使用包管理器 Homebrew 安装 MySQL (不建议):

$ brew install mysql


二、启动 MySQL 8.0.16

$ cd /usr/local/mysql
$ sudo ./bin/mysqld_safe

把 mysql 的安装路径加入到 shell 中

为了在终端(这里是 iTerm2 )使用 mysql 命令,而不用每次都 cd 进入 mysql 的安装路径,建议把 mysql 的安装路径加入到 shell 中(注意:这里的 shell 是 oh-my-zsh 而不是默认的 bash ):

前往文件夹: ~/.zshrc 添加 MySQL 路径(或 vim ~/.zshrc ):

export PATH="/usr/local/mysql/bin:$PATH"

执行以下命令测试是否添加路径成功

$ echo $PATH

查看 mysql 版本:

$ mysql -V

附:使用 alias 添加别名指向相应的 mysql 路径

除了“把 mysql 的安装路径加入到 shell 中”,还可以使用 alias 的方式,添加别名指向相应的 mysql 路径(不建议)。

在 shell 的资源文件中添加别名,以便更容易从命令行访问常用程序,如 mysql 和 mysqladmin ( bash 或 zsh ):

$ alias mysql=/usr/local/mysql/bin/mysql
$ alias mysqladmin=/usr/local/mysql/bin/mysqladmin


三、连接 MySQL 8.0.16

参考:https://dev.mysql.com/doc/refman/8.0/en/connecting-disconnecting.html

语法:
$ mysql -h host -u user -p
在运行着 mysql 的用户名为 root 的本机电脑上连接 mysql:
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>


四、卸载 MySQL 8.0.16

参考:

打开「系统偏好设置」=> 停止 MySQL 服务 => 卸载 MySQL => 再逐条执行以下命令(因为上一步卸载 MySQL 时已经自动删除了一些文件,所以执行下面命令时可能会提示找不到文件)。

To uninstall MySQL and completely remove it (including all databases) from your Mac do the following: