Mysql安装之Docker mysql

来自linux中国网wiki
跳到导航 跳到搜索

Run the application using Docker Compose

文件目录

#mkdir -p /home/data/docker-compose-mysql/mysql
#mkdir -p /home/data/docker-compose-mysql/data


mkdir -p /data/apps/docker-compose-mysql/mysql
mkdir -p /data/apps/docker-compose-mysql/data
docker-compose-mysql]# tree .
.
├── data
├── docker-compose.yml
└── mysql
    └── docker-my.cnf

mysql/docker-my.cnf

[mysqld]

default-storage-engine=INNODB

character-set-server=utf8mb4
server-id = 100
#default-time_zone = '+8:00'
explicit_defaults_for_timestamp=true
log-bin=mysql-bin

max_connections = 10667
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 1024
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 128M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 256M

thread_cache_size = 64

query_cache_type = 1
query_cache_size = 64M
query_cache_limit = 2M

ft_min_word_len = 4

binlog_format = row
#binlog_format = mixed
expire_logs_days = 7

slow_query_log = 1
long_query_time = 2
slow_query_log_file = /var/lib/mysql/mysql-slow.log
general_log = 1
general_log_file = /var/lib/mysql/mysql.log


[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4

docker-compose.yml

version: '3.1'  
services:
  mysql:
    # network_mode: "bridge"
    image: evan886/mysql5.7:v1
    #image: mysql:5.7.26
    hostname: dev-hello-mysql
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./mysql/docker-my.cnf:/etc/mysql/conf.d/docker-my.cnf
      #- "./init:/docker-entrypoint-initdb.d/"
#      - ./mysql/timezone:/etc/timezone
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: 4339pAss3335EOV
      MYSQL_USER: 'test'
      MYSQL_PASS: 'yourpassword'
      TZ: Asia/Shanghai
    restart: always


#note  这个密码不能位数太多呢 不然好多问题

usage

#本机访问 mysql 
yum install mysql -y  #client  可能是 mariadb-client
mysql -uroot -p -h127.0.0.1

dokcer build

#自己利用dockerfile build 并push 上 hub.docker
#有空要加上自己的特性 最好改为 alpine 
https://github.com/evan886/mysql/tree/master/docker-library/mysql/5.7

cd /data/builddocker/mysql/5.7
/data/builddocker/mysql/5.7# ls
docker-entrypoint.sh  Dockerfile

docker build --tag evan886/mysql5.7:v1 -f Dockerfile .


这样的tag会比较规范
REPOSITORY          TAG

jenkins/jenkins     2.176.4-alpine 

要先登录 
docker login
evan886  evan2240881

docker push evan886/mysql5.7:v1

学习初始化 有表 插入等等的 docker-compose部署MySQL

详解利用Dockerfile构建mysql镜像并实现数据的初始化及权限设置

上机有是 2020的 下面的是以前的 有空要优化一下 打包不要用 centos作 容器os

my dockerfile

cat Dockerfile 
#名称:
#
# time
FROM centos
MAINTAINER evan [email protected]
WORKDIR /root/
RUN yum -y  install wget
RUN  wget http://repo.mysql.com/mysql-community-release-el7-7.noarch.rpm
RUN rpm -ivh /root/mysql-community-release-el7-7.noarch.rpm
RUN yum -y install  mysql-server
RUN mysql_install_db --user=mysql
ENV MYSQL_USER test
ENV MYSQL_PASS mypassword
#支持中文
ENV LC_ALL  en_US.UTF-8
ADD build_table.sh  /root/build_table.sh
RUN chmod u+x /root/build_table.sh
EXPOSE 3306
ADD  run.sh  /root/run.sh
RUN  chmod u+x /root/run.sh
CMD  /root/run.sh


cat  build_table.sh 
#!/bin/bash
mysqld_safe &
sleep 3
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASS' WITH GRANT OPTION; "
mysql -e "create database evans"
mysql -e "create table evans.name(name char(20) not null,evan int not null) DEFAULT CHARSET=utf8"
mysql -e "insert into evans.evan values('韦子虚',36),('丽莉',33)"


cat run.sh 
#!/bin/bash
mysqld_safe





docker build -t evan886/centos-mysql:v1  .
 
Successfully built 3c3879c54e3b
Successfully tagged evan886/centos-mysql:v1




 [[email protected] mysql]# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
evan886/centos-mysql   v1                  3c3879c54e3b        8 minutes ago       867MB



docker push evan886/centos-mysql:v1

#测试 不加-d
  docker run -p 3309:3306 --name mysql -e MYSQL_ROOT_PASSWORD=mypassword --privileged=true mysql

  docker run -p 3309:3306 --name mysql -e MYSQL_ROOT_PASSWORD=mypassword  -d --privileged=true mysql


如果想拉我的镜像
docker pull evan886/centos-mysql:v1

测试

#我的母机是192.168.50.206
mysql  -uroot -p -h 192.168.50.206  -P3309
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.


完整测试

docker pull evan886/centos-mysql:v1

[email protected]:~/mysql# docker  images 
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
evan886/centos-mysql   v1                  3c3879c54e3b        17 hours ago        867MB

#运行 加-v 会有问题
docker run -p 3309:3306 --name mysql -e MYSQL_ROOT_PASSWORD=mypassword -v /data/mysql:/var/lib/mysql  -d --privileged=true 3c3879c54e3b

[email protected]:~/mysql# docker  ps  -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
34ce2a368d7d        3c3879c54e3b        "/bin/sh -c /root/ru…"   4 minutes ago       Exited (0) 4 minutes ago                       mysql
[email protected]:~/mysql#  docker  logs 34ce2a368d7d
180411 03:00:34 mysqld_safe Logging to '/var/log/mysqld.log'.
180411 03:00:34 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
180411 03:00:35 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended


用官方的

 docker pull mysql:5.7
#config and run 
docker run -p 3307:3306 --restart 策略名称 -v 宿主机数据目录:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysql密码 -d 镜像ID

docker run -p 3307:3306 --restart always -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=2240800 -d 75576f90a779

注意

运行启动
映射mysql外网访问端口: 3307
docker run -p 3307:3306 --restart 策略名称 -e MYSQL_ROOT_PASSWORD=mysql密码 -d 镜像ID
docker run -p 3307:3306 --restart always -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=224099 -d 75576f90a779

Docker容器自启动策略
no 不自动重启容器. (默认value)
always 在容器已经stop掉或Docker stoped/restarted的时候才重启容器
on-failure 容器发生error而退出(容器退出状态不为0)重启容器
unless-stopped 在容器已经stop掉或Docker stoped/restarted的时候才重启容器

将MySQL的数据存到宿主机上

docker run -p 3307:3306 --restart 策略名称 -v 宿主机数据目录:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysql密码 -d 镜像ID

docker run -p 3307:3306 --restart always -v /home/mysql-db-backup:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=rootroot -d 5709795eeffa


使用本地的MySQL配置文件

docker run -p 3307:3306 --restart 策略名称 -v 宿主机数据目录:/var/lib/mysql -v 宿主机数据库配置文件目录:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=mysql密码 -d 镜像ID

docker run -p 33306:3306 --restart always -v /data/mysql0:/var/lib/mysql -v /data/mysql0_conf:/etc/mysql/connf.d -e MYSQL_ROOT_PASSWORD=rootroot -d 5709795eeffa


err

# err  你现在不能用套接字建立连接因为它不见了,所以可以建立一个TCP/IP连接
 mysql -uroot -p2240881 -P3307 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
您在 /var/spool/mail/root 中有新邮件
[[email protected] ~]# mysql -uroot -p2240881 -P3307  -h 127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.

https://blog.csdn.net/hjf161105/article/details/78850658 解决Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) "


f4ba7ff24ae9: Waiting ERROR: error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/e9/e9c354083de75cbcde66071c228fca267d0e946fb41a5399f58265e5a858fcb4/data?verify=1596806434-OrTgZox94HL0JXxYMfUx6j5RT8I%3D: net/http: TLS handshake timeout

请见 DockerHub镜像加速

see also

docker-library/mysql 5.6/Dockerfile

MySQL 官方Docker镜像的使用

Docker部署安装MySQL5.7

MySQL 5.6 Linux Debian/Ubuntu源码编译安装 LNMP之MySQL

docker 安装mysql 8

Docker运行Mysql实例

Docker部署(二):MySQL数据库

修改 Docker-MySQL 容器的 默认用户加密规则

阿里云ECS-使用docker官方镜像安装mysql服务

宿主机连接docker中的mysql