查看“Mysql安装之Docker mysql”的源代码
←
Mysql安装之Docker mysql
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
=Run the application using Docker Compose= [[基于Alpine的mysql Docker镜像]] ==文件目录== <pre> #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</pre> == mysql/docker-my.cnf== ===5.x my.cnf === <pre> [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 </pre> ===8.x my.cnf === <pre> </pre> == docker-compose.yml== <pre> version: '3.1' services: mysql: # network_mode: "bridge" #image: evan886/mysql5.7:v1 image: evan886/mysql5.7-debian #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 #前面是host 母机 ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: 4939pAss3035EOV MYSQL_USER: 'test' MYSQL_PASS: 'yourpassword' TZ: Asia/Shanghai restart: always #note 这个密码不能位数太多呢 不然好多问题 </pre> ===mysql 8 docker-compose.yml=== <pre> cat docker-compose.yml version: '3' services: mysql: restart: always privileged: true image: mysql:8.0.22 container_name: mysqlServer3308 volumes: - ./db:/var/lib/mysql - ./conf:/etc/mysql/conf.d - ./logs:/logs command: --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --explicit_defaults_for_timestamp=true environment: MYSQL_ROOT_PASSWORD: "eaAbc123654" MYSQL_USER: "test" MYSQL_PASSWORD: "Abc123654" MYSQL_INITDB_SKIP_TZINFO: "Asia/Shanghai" ports: - 3308:3306 network_mode: "bridge" </pre> [https://blog.csdn.net/oschina_41731918/article/details/122837753 MySQL-8.0版本的docker-compose安装脚本(单实例] [https://juejin.cn/post/6844904036362092558 一张图搞定Docker 安装 MySQL8.0 ] ==usage== <pre> docker-compose up -d #本机访问 mysql yum install mysql -y #client 可能是 mariadb-client mysql -uroot -p -h127.0.0.1 </pre> =dokcer build= <pre> #自己利用dockerfile build 并push 上 hub.docker #有空要加上自己的特性 最好改为 alpine 如果不是为了体积,那还是官方的debian吧 注意 如果下载不了 点击右边的 Raw 可以方便copy https://github.com/docker-library/mysql/tree/master/5.7 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 </pre> 学习初始化 有表 插入等等的 [https://www.jianshu.com/p/ab3367b2e6b4 docker-compose部署MySQL] [https://www.jb51.net/article/115422.htm 详解利用Dockerfile构建mysql镜像并实现数据的初始化及权限设置] =上机有是 2020的 下面的是以前的 有空要优化一下 打包不要用 centos作 容器os= = my dockerfile= <pre> cat Dockerfile #名称: 当年学习docker打包用的例子而已 # # time FROM centos MAINTAINER evan evan886@gmail.com 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 [root@localhost 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 </pre> =测试= <pre> #我的母机是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.</pre> ==完整测试== <pre> docker pull evan886/centos-mysql:v1 root@debian:~/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 root@debian:~/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 root@debian:~/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 </pre> =用官方的= <pre> 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 </pre> =注意 = <pre> 运行启动 映射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的时候才重启容器 </pre> ==将MySQL的数据存到宿主机上== <pre>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</pre> ==使用本地的MySQL配置文件== <pre>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 </pre> =err= <pre> # 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 中有新邮件 [root@localhost ~]# 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) " </pre> 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= [https://github.com/docker-library/mysql/blob/f279c9cc0d20407a2c4d9465adae6b077dcf1b51/5.6/Dockerfile docker-library/mysql 5.6/Dockerfile] [https://itbilu.com/linux/docker/EyP7QP86M.html MySQL 官方Docker镜像的使用] [https://www.jianshu.com/p/5c18a4b01dcc?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation Docker部署安装MySQL5.7] [https://www.jyoryo.com/index.php/archives/89.html MySQL 5.6 Linux Debian/Ubuntu源码编译安装 LNMP之MySQL] [https://blog.csdn.net/sunyuhua_keyboard/article/details/81540012 docker 安装mysql 8] [https://blog.csdn.net/xiaolyuh123/article/details/72629300 Docker运行Mysql实例] [https://www.jianshu.com/p/7a01046b3c54 Docker部署(二):MySQL数据库] [https://www.cnblogs.com/atuotuo/p/9402132.html#_labelTop 修改 Docker-MySQL 容器的 默认用户加密规则] [https://blog.csdn.net/wxb880114/article/details/82219720 阿里云ECS-使用docker官方镜像安装mysql服务] [https://www.jianshu.com/p/0ba97a26c086 宿主机连接docker中的mysql] [[category:ops]] [[category:mysql]] [[category: container]]
返回至
Mysql安装之Docker mysql
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
我的导航
关于我
shell
python
ops
linuxchina.net
blog.linuxchina
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息