0%

Mac Docker安装MySQL

需要快速安装一个MySQL数据库,还是使用Docker方便快捷。

首先创建本地目录/usr/local/mysql-docker,并设置权限。

1
$ sudo chmod 777 mysql-docker

这样存放Docker上面MySQL的数据文件,哪怕重启以后,数据不会丢失。

运行下面命令,就可以在Docker下面成功安装 MySQL 5.7。

1
docker run --name mysql --env MYSQL_ROOT_HOST=172.17.%.% --env MYSQL_ROOT_PASSWORD=123456 -v /usr/local/mysql-docker:/var/lib/mysql -p 3306:3306 -d mysql/mysql-server:5.7

如果没有把本地路径 添加到Docker的文件共享目录里,可能会报如下错误:

1
2
3
4
The path /usr/local/mysql-docker
is not shared from OS X and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> File Sharing.
See https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.

进入Docker -> Preferences... -> File Sharing,把路径添加进去即可。

进入Docker容器

1
$ docker exec -it 5fd4bb2b6c67 bash

进入数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bash-4.2# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.29 MySQL Community Server (GPL)

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>