Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

MAC安装Mysql

Mac通过Homebrew安装Mysql

安装准备

安装Homebrew , 参考 https://brew.sh/

1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

已经安装好的直接更新一下

1
brew update

先看下mysql信息

1
brew info mysql

输出如下

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
28
29
mysql: stable 8.0.29 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/8.0/en/
Conflicts with:
mariadb (because mysql, mariadb, and percona install the same binaries)
percona-server (because mysql, mariadb, and percona install the same binaries)
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mysql.rb
License: GPL-2.0-only with Universal-FOSS-exception-1.0
==> Dependencies
Build: cmake ✘, pkg-config ✘
Required: icu4c ✘, libevent ✘, libfido2 ✘, lz4 ✘, openssl@1.1 ✘, protobuf ✘, zstd ✔
==> Caveats
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

To restart mysql after an upgrade:
brew services restart mysql
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
==> Analytics
install: 77,576 (30 days), 259,598 (90 days), 993,386 (365 days)
install-on-request: 77,429 (30 days), 258,929 (90 days), 989,867 (365 days)
build-error: 370 (30 days)

明确说明了和mariadb有冲突,印象中mariadb应该不支持Mac的,看下

1
brew info mariadb

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
mariadb: stable 10.8.3 (bottled)
Drop-in replacement for MySQL
https://mariadb.org/
Conflicts with:
mariadb-connector-c (because both install `mariadb_config`)
mysql (because mariadb, mysql, and percona install the same binaries)
mytop (because both install `mytop` binaries)
percona-server (because mariadb, mysql, and percona install the same binaries)
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mariadb.rb
License: GPL-2.0-only
==> Dependencies
Build: bison ✘, cmake ✘, fmt ✘, pkg-config ✘
Required: groonga ✘, openssl@1.1 ✘, pcre2 ✘, zstd ✔
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To restart mariadb after an upgrade:
brew services restart mariadb
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/mariadb/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql

是支持的,竟然二进制文件与mysql是一模一样的。

安装mysql

1
brew install mysql

安装信息就不记录了,记录一下安装成功后的提示信息

1
2
3
4
5
6
7
8
9
10
11
12
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

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

几个关注的信息,mysql的启动方式有两种,后台的和前台直接启动,后台启动直接
brew services restart mysql
前台直接启动命令行输入(没有说怎么关闭)
/opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql

安装目录

安装后 mysql执行文件在 /opt/homebrew/bin 目录下;默认的配置文件 my.cnf/opt/homebrew/etc 目录下;
数据目录在 /opt/homebrew/var/mysql 目录下(同时包含了binlog文件)。

安全设置

因为安装的mysql是没有密码的,所以需要重洗安全设置一下,执行mysql_secure_installation命令。

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
➜  ~ mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT 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 component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 0
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
... Failed! Error: Your password does not satisfy the current policy requirements

New password:

Re-enter new password:

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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) : y
Success.

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) : n

... skipping.
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
mysql -uroot -pxxxxxxxxx

其他

这里备注一下 bew services 的使用

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Usage: brew services [subcommand]

Manage background services with macOS' launchctl(1) daemon manager.

If sudo is passed, operate on /Library/LaunchDaemons (started at boot).
Otherwise, operate on ~/Library/LaunchAgents (started at login).

[sudo] brew services [list] (--json):
List information about all managed services for the current user (or root).

[sudo] brew services info (formula|--all|--json):
List all managed services for the current user (or root).

[sudo] brew services run (formula|--all):
Run the service formula without registering to launch at login (or boot).

[sudo] brew services start (formula|--all|--file=):
Start the service formula immediately and register it to launch at login
(or boot).

[sudo] brew services stop (formula|--all):
Stop the service formula immediately and unregister it from launching at
login (or boot).

[sudo] brew services kill (formula|--all):
Stop the service formula immediately but keep it registered to launch at
login (or boot).

[sudo] brew services restart (formula|--all):
Stop (if necessary) and start the service formula immediately and register
it to launch at login (or boot).

[sudo] brew services cleanup:
Remove all unused services.

--file Use the service file from this location to
start the service.
--all Run subcommand on all services.
--json Output as JSON.
-d, --debug Display any debugging information.
-q, --quiet Make some output more quiet.
-v, --verbose Make some output more verbose.
-h, --help Show this message.

使用如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  brew services list
Name Status User File
mysql none

➜ brew services start mysql
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

➜ brew services list
Name Status User File
mysql started forgkan ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

➜ brew services stop mysql
Stopping `mysql`... (might take a while)
==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)

绿色安装

绿色安装,直接下载Archive包 MySQL Community Downloads
解压后直接可以执行bin/mysqld

初始化

1
mysqld --initialize --console

创建data目录,以及创建root用户的初始密码。

启动mysql

1
mysqld --defaults-file=my.ini

重置root密码(默认密码是初始化的时候给出的)

1
mysqladmin -uroot -p password

sakila-db

下载 sakila-db

  • windows:
1
https://downloads.mysql.com/docs/sakila-db.zip
  • linux:
1
https://downloads.mysql.com/docs/sakila-db.tar.gz