MySQL 是世界上最受欢迎的开源数据库。凭借其可靠性、易用性和性能,MySQL 已成为 Web 应用程序的数据库优先选择。
其实很多时候,我们不需要单独在电脑上安装mysql了,笔者的环境为MacOS,安装Docker for Mac客户端。
一、查看可用的 MySQL 版本
访问 MySQL 镜像库地址:https://hub.docker.com/_/mysql
可以通过 Sort by 查看其他版本的 MySQL,默认是最新版本 mysql:latest
- 默认的选择Description标签,需要切换其它版本,点击Tags便签。
此外,我们还可以用 docker search mysql 命令来查看可用版本:
docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9073 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3206 [OK]
mysql/mysql-serverOptimized MySQL Server Docker images. Create… 673 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 67
centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK]
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 60
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
bitnami/mysql Bitnami MySQL Docker Image 35 [OK]
tutum/mysql Base docker image to run a MySQL database se… 34
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 28 [OK]
prom/mysqld-exporter 26 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 24
centos/mysql-56-centos7 MySQL 5.6 SQL database server 18
circleci/mysql MySQL is a widely used, open-source relation… 16
mysql/mysql-router MySQL Router provides transparent routing be… 14
二、拉取 MySQL 镜像
$ docker pull mysql:latest
#拉取8版本的镜像
$ docker pull mysql:8
三、查看本地镜像
使用以下命令来查看是否已安装了 mysql:
docker images
四、运行容器
% docker run -itd --name docker-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v ~/Docker/db/mysql/8.0.9:/var/lib/mysql mysql
参数解析:
-itd:i是交互式操作,t是一个终端,d指的是在后台运行
-p : 设置端口,
--name : 取名 ,
-e MYSQL_ROOT_PASSWORD=123456 : -e 指定环境变量, 设置 账号为 root ,密码为 123456.
-v ~/mysql/data:/var/lib/mysql 是将Docker的/var/lib/mysql 文件夹映射到本机的~/Docker/db/mysql/8.0.9
查看进程如下:
% docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9488be5b71d mysql "docker-entrypoint.s…" 7 hours ago Up 7 hours 0.0.0.0:3306->3306/tcp, 33060/tcp docker-mysql
如果停掉容器,
$ docker stop Container ID or Names
下次启用已经停掉的容器,用下面的命令即可
$ docker start Container ID or Names
比如笔者启动容器docker-mysql
% docker start docker-mysql
五、进入容器
% docker exec -it docker-mysql bash
进入容器后,Terminal打头的格式为root@ContainerId
, 比如笔者的
root@0d4a5de80d4d:/#
六、操作数据库
1、登录MySQL
mysql -u root -p
root@e9488be5b71d:/# 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.19 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.
2、显示数据库
$ show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.05 sec)
3、创建数据库用户zgpeace
创建数据库用户zgpeace,并把所有权限授权与zgpeace, 退出root用户。
root@e9488be5b71d:/# mysql -u zgpeace -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.19 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
用新创建的用户zgpeace登录。
root@e9488be5b71d:/# mysql -u zgpeace -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.19 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
七、实操
参考链接:Docker安装MySQL 8 for Mac(图文详解)