Mysql安装之Docker mysql
目录
Run the application using Docker Compose
文件目录
mkdir -p /home/data/docker-compose-mysql/mysql mkdir -p /home/data/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 = 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: 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 # - ./mysql/timezone:/etc/timezone ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: pass3335 TZ: Asia/Shanghai restart: always #note 这个密码不能位数太多呢 不然好多问题
usage
#本机访问 mysql yum install mysql -y #client mysql -uroot -p -h127.0.0.1
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 [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
测试
#我的母机是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 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
用官方的
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 中有新邮件 [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) "
see also
docker-library/mysql 5.6/Dockerfile