一、mysql版本确定
到官网确定最新可用版本: hub.docker.com/_/mysql?tab…
# Supported tags and respective `Dockerfile` links
- [`8.0.30`, `8.0`, `8`, `latest`, `8.0.30-oracle`, `8.0-oracle`, `8-oracle`, `oracle`](https://github.com/docker-library/mysql/blob/e03765d83c1fddcfb48fa47dda6171c50b563382/8.0/Dockerfile.oracle)
- [`8.0.30-debian`, `8.0-debian`, `8-debian`, `debian`](https://github.com/docker-library/mysql/blob/e03765d83c1fddcfb48fa47dda6171c50b563382/8.0/Dockerfile.debian)
- [`5.7.39`, `5.7`, `5`, `5.7.39-oracle`, `5.7-oracle`, `5-oracle`](https://github.com/docker-library/mysql/blob/e03765d83c1fddcfb48fa47dda6171c50b563382/5.7/Dockerfile.oracle)
- [`5.7.39-debian`, `5.7-debian`, `5-debian`](https://github.com/docker-library/mysql/blob/e03765d83c1fddcfb48fa47dda6171c50b563382/5.7/Dockerfile.debian)
二、版本下载
docker pull mysql:8.0.30
8.0.30: Pulling from library/mysql
051f419db9dd: Pull complete
7627573fa82a: Pull complete
a44b358d7796: Pull complete
95753aff4b95: Pull complete
a1fa3bee53f4: Pull complete
f5227e0d612c: Pull complete
b4b4368b1983: Pull complete
f26212810c32: Pull complete
d803d4215f95: Pull complete
d5358a7f7d07: Pull complete
435e8908cd69: Pull complete
Digest: sha256:b9532b1edea72b6cee12d9f5a78547bd3812ea5db842566e17f8b33291ed2921
Status: Downloaded newer image for mysql:8.0.30
docker.io/library/mysql:8.0.30
查看镜像,docker images
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0.30 43fcfca0776d 5 days ago 449MB
mysql latest ff3b5098b416 2 weeks ago 447MB
三、安装运行
3.1 创建数据目录
数据库的目录,建议放在宿主机上
#创建mysql配置目录
mkdir -p /opt/mysql8/conf
#创建mysql数据目录
mkdir -p /opt.mysql8/data
3.2 创建mysql配置文件
可以手工创建,也可以快捷方式创建 下面介绍快捷方式法
#1、启动mysql docker
docker run --name mysql8 -p 13306:3306 -e MYSQL_ROOT_PASSWORD=a123456 -d mysql:8.0.30
ab5268177aefb6680fb8e7f7c9bd24a67a80b8b1ddc93a8ff78486b4dfbbddc2
#2、查看状态 docker ps
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab5268177aef mysql:8.0.30 "docker-entrypoint.s…" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp mysql8
#3、可以进入到容器,登录mysql,验证是否启动成功
docker exec -it mysql8 /bin/bash
bash-4.4# 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.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>
说明启动成功
#4、复制配置文件到宿主机
docker cp mysql8:/etc/mysql/conf.d /opt/mysql8/conf
#5、停止删除启动的docker
docker stop mysql8
docker rm mysql8
3.3 启动运行
docker run -dit \
--name mysql8 \
-p 13306:3306 \
-e MYSQL_ROOT_PASSWORD=a123456 \
-e LANG=C.UTF-8 \
-v /opt/mysql8/conf:/etc/mysql \
-v /opt/mysql8/data:/var/lib/mysql \
mysql:8.0.30
3.4 添加新用户
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user,host,plugin from user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| root | % | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)
#添加不限制IP登录的用户
mysql> create user 'test'@'%' identified by 'a123456';
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host,plugin from user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| root | % | caching_sha2_password |
| test | % | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
6 rows in set (0.01 sec)
#授权test用户全部权限
mysql> grant all privileges on test_grant.* to 'test'@'%' with grant option;
Query OK, 0 rows affected (0.00 sec)
#查看test用户权限
mysql> show grants for 'test'@'%';
+------------------------------------------------------------------------+
| Grants for test@% |
+------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `test`@`%` |
| GRANT ALL PRIVILEGES ON `test_grant`.* TO `test`@`%` WITH GRANT OPTION |
+------------------------------------------------------------------------+
2 rows in set (0.00 sec)
#刷新权限
flush privileges;
3.4 客户端连接
使用开源软件DBeaver连接mysql客户端
#创建数据库
mysql> create database test;
Query OK, 1 row affected (0.01 sec)
#查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
#授权数据库用户
mysql> grant all on test.* TO 'test'@'%';
Query OK, 0 rows affected (0.01 sec)