原创

Linux系统:用docker安装mysql。

Docker的镜像仓库:https://hub.docker.com/

1.在Docker的镜像仓库 下载mysql


指定版本下载, 本人选择的版本 5.7

mysql5.7下载完成
检查docker当前所有的镜像:sude docker images

mysql镜像已存在
2.根据下载的docker image创建docker container

sudo docker run -p 3307:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/log:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
参数说明
-p 3307:3306;将容器的3306端口映射到主机的3307端口   --name  把mysql容器命名 叫mysql

-v 目录挂载:

-v /mydata/mysql/log:/var/log/mysql 将docker的实例配置文件夹挂载到主机

-v /mydata/mysql/log:/var/lib/mysql 将docker的日志文件夹载到主机

配置文件位置在etc/mysql   挂载到本机/etc/mysql文件夹下

-v /mydata/mysql/conf:/etc/mysql 将docker的配置文件夹挂载到主机   
-e MYSQL_ROOT_PASSWORD=root 初始化root用的密码



容器启动成功


查看docker 正在运行中的容器 docker ps

mysql容器已经启动成功

进入镜像mysql 容器内部   docker exec -it mysql  /bin/bash

查看mysql 装在 linux镜像容器哪里 : whereis mysql

解决mysql编码问题(可自行跳过)

创建mysql配置文件

vi /mydata/mysql/conf/my.cnf
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve


重启mysql容器  docker restart mysql



回到mysql 容器 修改成功

[root@VM-0-14-centos conf]# docker exec -it mysql /bin/bash
root@14877ff0aa56:/# cd /etc
root@14877ff0aa56:/etc# ls
X11                     dpkg         init.d         logcheck       os-release  rc3.d        shadow-
adduser.conf            environment  inputrc        login.defs     pam.conf    rc4.d        shells
alternatives            fstab        issue          logrotate.d    pam.d       rc5.d        skel
apt                     gai.conf     issue.net      machine-id     passwd      rc6.d        ssl
bash.bashrc             group        kernel         mecabrc        passwd-     rcS.d        subgid
bindresvport.blacklist  group-       ld.so.cache    mke2fs.conf    perl        resolv.conf  subuid
cron.daily              gshadow      ld.so.conf     motd           profile     rmt          systemd
debconf.conf            gshadow-     ld.so.conf.d   mtab           profile.d   securetty    terminfo
debian_version          host.conf    ldap           mysql          rc0.d       security     timezone
default                 hostname     libaudit.conf  nsswitch.conf  rc1.d       selinux      update-motd.d
deluser.conf            hosts        localtime      opt            rc2.d       shadow       xattr.conf
root@14877ff0aa56:/etc# cd mysql
root@14877ff0aa56:/etc/mysql# ls
my.cnf
root@14877ff0aa56:/etc/mysql# cat my.cof
cat: my.cof: No such file or directory
root@14877ff0aa56:/etc/mysql# cat my.cnf
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
root@14877ff0aa56:/etc/mysql#
开发者手册
  • 咖煮
  • 2021-12-13 22:53:01.318

评论区