拉取mysql镜像
docker pull mysql
# 拉取镜像
[root@eason ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
查看镜像
docker images
[root@eason conf]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 6.2.6 7614ae9453d1 2 years ago 113MB
mysql latest 3218b38490ce 2 years ago 516MB
创建mysql 映射文件
my.cfg如下
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
#服务端口号 默认3306
port=3306
datadir = /usr/local/docker/mysql/data
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
# 最大连接数
max_connections=200
# 连接失败的最大次数。防止有人从该主机试图攻击数据库系统
max_connect_errors=20
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
启动mysql容器
-e MYSQL_ROOT_PASSWORD=root123456 指定数据库密码
docker run -p 3306:3306 --name mysql --privileged=true -v /usr/local/docker/mysql/log:/var/log/mysql -v /usr/local/docker/mysql/data:/var/lib/mysql -v /usr/local/docker/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root123456 -itd 镜像id
进入容器
进入容器执行刷新权限等sql
-- 修改访问主机以及密码
ALTER USER 'root'@'%' IDENTIFIED BY 'root123456';
-- 刷新权限
FLUSH PRIVILEGES;
# 正在运行的容器
[root@eason conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d3635be7753 3218b38490ce "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
25c6f2a64187 redis:6.2.6 "docker-entrypoint.s…" 3 days ago Up 3 days 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis
# 进入容器
[root@eason conf]# docker exec -it 7d3635be7753 /bin/bash
root@7d3635be7753:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
# 登录mysql,用刚刚设置的密码
root@7d3635be7753:/# 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.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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.
# 执行sql
mysql> ALTER USER 'root'@'%' IDENTIFIED BY 'root123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
# 执行sql
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)