欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Docker 搭建mysql

Docker 搭建mysql

2024/10/26 0:25:38 来源:https://blog.csdn.net/m0_65775063/article/details/143105509  浏览:    关键词:Docker 搭建mysql

拉取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=3306datadir = /usr/local/docker/mysql/datainit_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 - GPLCopyright (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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com