macOS M1 源码安装 MySQL8 版本

你好,我是猿java。

最近,因为换电脑,需要在本地安装 MySQL 的测试环境版本,电脑是 macOS M1芯片,因为中间折腾了一番,所以特意把安装的过程记录如下:

操作系统

img.png

MySQL源码下载

MySQL官方下载地址

将文件下载到本地Mac的目录,如下:

1
2
3
4
5
6
7
8
weiki@WeikideMacBook-Pro custsoftware % pwd
/Users/weiki/custsoftware
weiki@WeikideMacBook-Pro custsoftware % ll
total 360704
drwxr-xr-x 3 weiki staff 96 9 27 14:37 ./
drwxr-x---+ 56 weiki staff 1792 9 27 14:42 ../
-rw-r--r--@ 1 weiki staff 175929717 9 27 14:17 mysql-8.0.30-macos12-arm64.tar.gz
weiki@WeikideMacBook-Pro custsoftware %

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 解压文件
weiki@WeikideMacBook-Pro custsoftware % tar zxvf mysql-8.0.30-macos12-arm64.tar.gz

# 查看,目录下新增 一个 mysql-8.0.30-macos12-arm64目录
weiki@WeikideMacBook-Pro custsoftware % ll
total 360704
drwxr-xr-x 4 weiki staff 128 9 27 14:50 ./
drwxr-x---+ 56 weiki staff 1792 9 27 14:42 ../
drwxr-xr-x 11 weiki staff 352 9 27 14:50 mysql-8.0.30-macos12-arm64/
-rw-r--r--@ 1 weiki staff 175929717 9 27 14:17 mysql-8.0.30-macos12-arm64.tar.gz

# 将解压目录移动到 /usr/local/mysql
weiki@WeikideMacBook-Pro custsoftware % mv mysql-8.0.30-macos12-arm64 /usr/local/mysq

# 增加 my-default.cnf 配置
weiki@WeikideMacBook-Pro cd /usr/local/mysql/

weiki@WeikideMacBook-Pro vim weiki@WeikideMacBook-Pro
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /usr/local/mysql/data/mysql.sock
log-error = /usr/local/mysql/data/mysqld.log

[client]
socket=/usr/local/mysql/data/mysql.sock

为什么要放到 /usr/local/mysql

在 mysql/support-files/mysql.server 脚本里有如下描述

1
2
3
4
5
6
7
8
9
10
11
# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
# [mysqld]
# basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
# and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
# below.

如果不把mysql安装到 /usr/local/mysql目录下,就需要做上述脚本描述的工作,因此,为了减少这些配置,所以把mysql安装到 /usr/local/mysql目录下

初始化&启动

1
2
3
4
5
6
7
8
9
10
11
12
13
weiki@WeikideMacBook-Pro cd /usr/local/mysql
weiki@WeikideMacBook-Pro mysql % sudo bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2022-09-27T06:43:30.004016Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.30) initializing of server in progress as process 85541
2022-09-27T06:43:30.008139Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
2022-09-27T06:43:30.014427Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-09-27T06:43:30.062437Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-09-27T06:43:30.633461Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: XW?s4SjtJM*g
2022-09-27T06:43:30.949707Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.30).
weiki@WeikideMacBook-Pro sudo support-files/mysql.server start
# 看到这些信息就代表 mysql启动成功
Starting MySQL
.Logging to '/usr/local/mysql/data/WeikideMacBook-Pro.local.err'.
SUCCESS!

查看mysql进程

1
2
3
4
weiki@WeikideMacBook-Pro mysql % ps -ef|grep mysql
0 85876 1 0 2:43下午 ttys003 0:00.01 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/WeikideMacBook-Pro.local.pid
74 85958 85876 0 2:43下午 ttys003 0:00.38 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=WeikideMacBook-Pro.local.err --pid-file=/usr/local/mysql/data/WeikideMacBook-Pro.local.pid
501 86041 65061 0 2:43下午 ttys003 0:00.00 grep mysql

连接mysql

连接mysql时需要输入一个秘钥,这个秘钥是在上面的初始化步骤生成的一个随机秘钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
weiki@WeikideMacBook-Pro mysql % /usr/local/mysql/bin/mysql -uroot -P3306 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.30

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

修改密码

mac本地mysql的密码,自己设置一个好记的就ok了

1
2
3
4
5
6
mysql> alter user 'root'@'localhost' identified by '123456' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

启动&重启&停止&状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#启动
sudo /usr/local/mysql/support-files/mysql.server start

# 重启
weiki@WeikideMacBook-Pro mysql % sudo /usr/local/mysql/support-files/mysql.server restart
Password:
Shutting down MySQL
.. SUCCESS!
Starting MySQL
. SUCCESS!
# 查看状态
weiki@WeikideMacBook-Pro mysql % sudo /usr/local/mysql/support-files/mysql.server status
SUCCESS! MySQL running (9568)

# 停止
weiki@WeikideMacBook-Pro sudo /usr/local/mysql/support-files/mysql.server stop

配置全局变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
weiki@WeikideMacBook-Pro vim ~/.bash_profile
# 增加这些内容
export MYSQL=/usr/local/mysql
export PATH=$PATH:$MYSQL/bin

# 让配置生效
weiki@WeikideMacBook-Pro source ~/.bash_profile

# 全局变量生效
weiki@WeikideMacBook-Pro mysql % mysql -uroot -P3306 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

学习交流

如果你觉得文章有帮助,请帮忙转发给更多的好友,或关注公众号:猿java,持续输出硬核文章。

drawing