Docker 搭建mysql

拉取mysql镜像

docker pull mysql

bash 复制代码
# 拉取镜像
[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

bash 复制代码
[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如下

bash 复制代码
[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 指定数据库密码

bash 复制代码
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

sql 复制代码
-- 修改访问主机以及密码
ALTER USER 'root'@'%' IDENTIFIED BY 'root123456';
 
-- 刷新权限
FLUSH PRIVILEGES;
bash 复制代码
# 正在运行的容器
[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)

外部连接mysql

相关推荐
蜗牛、Z16 小时前
Android 日常开发Adb常用命令附档
android·adb
invicinble1 天前
对于Mysql深入理解
数据库·mysql
lightningyang1 天前
DVWA 在 openEuler 系统的安装部署
docker·渗透测试·openeuler·dvwa·天枢一体化虚拟仿真靶场平台
霖霖总总1 天前
[小技巧56]深入理解 MySQL 聚簇索引与非聚簇索引:原理、差异与实践
数据库·mysql
伐尘1 天前
【MySQL】间隙锁 与 排他锁 的区别
数据库·mysql
快乐非自愿1 天前
【面试题】MySQL 的索引类型有哪些?
数据库·mysql·面试
Ha_To1 天前
2026.1.28 docker安装
运维·docker·容器
霖霖总总1 天前
[小技巧55]深入解析数据库日志机制:逻辑日志、物理日志与物理逻辑日志在 MySQL InnoDB 中的实现
数据库·mysql
北极熊~~1 天前
emqx持久化 Docker 容器中生成的数据并打包新镜像,迁移至新机器运行
运维·docker·容器
tomiatomia1 天前
MinIO docker 部署使用的几个坑
运维·docker·容器