1. 下载
到 https://dev.mysql.com/downloads/mysql/ 下载相应版本的压缩包,如 mysql-8.0.19-macos10.15-x86_64.tar.gz。
2. 安装配置
解压下载的文件 mysql-8.0.19-macos10.15-x86_64.tar.gz
到安装目录 $MYSQL_HOME
,如 /path/to/mysql/mysql-8.0.19-macos10.15-x86_64
。目录结构应该如下:
/path/to/mysql/mysql-8.0.19-macos10.15-x86_64/
|--bin/
| |--mysqld < server
| |--mysqld-debug < server with debug
| |--mysql < client
| |--mysqladmin < admin tool
| |--...
|--docs/
|--include/
|--lib/
|--man/
|--share/
|--support-files/
|--LICENSE
|--README
创建软链接方便日后的升级维护:
$ ln -s /path/to/mysql/mysql-8.0.19-macos10.15-x86_64 /path/to/mysql/mysql-latest
添加系统环境变量以便在终端可以直接使用 mysql 的命令:
# 编辑文件 `~/.profile`
$ vi ~/.profile
# 添加如下 3 行配置到文件 `~/.profile` 内
export MYSQL_HOME=/path/to/mysql/mysql-latest
export MYSQL_DATA=/path/to/mysql/mysql-data
PATH=$PATH:$MYSQL_HOME/bin
# 使上述添加的配置立即生效
$ source ~/.profile
3. 数据库初始化
执行如下命令初始化 mysql:
$ mysqld --initialize --console --datadir=$MYSQL_DATA
2020-02-12T06:49:32.947848Z 0 [System] [MY-013169] [Server] /path/to/mysql/mysql-8.0.19-macos10.15-x86_64/bin/mysqld (mysqld 8.0.19) initializing of server in progress as process 16486
2020-02-12T06:49:32.971647Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /path/to/mysql/mysql-latest/ is case insensitive
2020-02-12T06:49:37.667706Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: E;Lur9R4gxjv
--datadir
参数指定了 mysql 的数据目录,不指定时默认为$MYSQL_HOME/data
。初始化成功后终端会输出为 root 账号创建的默认密码,用于后续登陆。
4. 启动数据库
执行如下命令直接在终端启动:
$ mysqld --datadir=$MYSQL_DATA
2020-02-12T06:57:36.655331Z 0 [System] [MY-010116] [Server] /Volumes/RJ/green/mysql/mysql-8.0.19-macos10.15-x86_64/bin/mysqld (mysqld 8.0.19) starting as process 16676
2020-02-12T06:57:36.684761Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /Volumes/RJ/data/mysql-8/ is case insensitive
2020-02-12T06:57:38.021921Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-02-12T06:57:38.085620Z 0 [System] [MY-010931] [Server] /Volumes/RJ/green/mysql/mysql-8.0.19-macos10.15-x86_64/bin/mysqld: ready for connections. Version: '8.0.19' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server - GPL.
2020-02-12T06:57:38.248663Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/tmp/mysqlx.sock' bind-address: '::' port: 33060
启动后的默认连接端口为 3306。
--datadir
参数指定了 mysql 的数据目录,不指定时默认为$MYSQL_HOME/data
。
要停止 mysql 服务,在终端执行如下命令:
$ mysqladmin -u root -p shutdown
Enter password: ***
5. 修改 root 账号密码
$ mysql -u root -p # 使用 root 账号登陆 mysql 服务器
Enter password: ***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19
Copyright (c) 2000, 2020, 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> alter user 'root'@'localhost' identified by 'new-root-password'; # 修改为新的密码
Query OK, 0 rows affected (0.02 sec)
mysql> \q
Bye
6. 账号创建及维护
# 创建只能在 localhost 连接服务器的 tester 账号
mysql> create user 'tester'@'localhost' identified by 'password';
# 创建可通过任何 example.com 连接服务器的 tester 账号
mysql> create user 'tester'@'%.example.com' identified by 'password';
# 为本地 tester 账号赋予所有权限
mysql> grant all on *.* to 'tester'@'localhost' with grant option;
# 让本地 tester 账号只可以管理 testdb 数据库
mysql> grant all on testdb.* to 'tester'@'localhost';
# 让本地 tester 账号对 testdb 数据库某个 user 表只有特定的权限
mysql> grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP on testdb.user to 'tester'@'localhost'
# 查看账号的权限
mysql> show grants for 'tester'@'localhost';
# 反向删除通过 grant 命令赋予的权限
mysql> revoke all on *.* from 'tester'@'localhost';
mysql> revoke INSERT,UPDATE,DELETE on testdb.user from 'tester'@'localhost';
# 删除账号
mysql> drop user 'tester'@'localhost';
7. 数据库创建及维护
# 显示可用的数据库编码
SHOW CHARACTER SET;
SHOW CHARACTER SET like 'utf8%';
# 创建数据库
create database if not exists testdb character set utf8;
show create database testdb;
# 删除数据库
drop database testdb;
参考
- MySQL 官方安装文档:Installing MySQL on macOS