Dockerfile 基于centos7镜像搭建MySQL服务

文章目录

    • Dockerfile
    • 容器运行测试
    • 报错总结

Dockerfile

mkdir mysql
cd mysql
vim Dockerfile
将软件包考入当前目录:mysql-5.6.26.tar.gz
FROM centos:7
MAINTAINER This is MySQL
#更新yum源安装软件包
RUN yum -y update && yum -y install gcc gcc-c++ make pcre-devel expat-devel perl ncurses ncurses-devel bison cmake autoconf
#解压mysql包
ADD mysql-5.6.26.tar.gz /opt/
#创建系统用户
RUN useradd -s /sbin/nologin mysql
#进入源码包,执行cmake文件,指定工作目录
WORKDIR /opt/mysql-5.6.26
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DSYSCONFIDIR=/etc \
-DMYSQL_DATADIR=/home/mysql/ \
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock   
#make编译
RUN make && make install
#复制默认配置文件到/etc目录下
RUN cp -f support-files/my-default.cnf /etc/my.cnf
RUN mkdir -p /usr/local/mysql/
RUN chown -R mysql:mysql /usr/local/mysql/
#配置mysql环境变量
ENV PATH /usr/local/mysql/bin:$PATH
#初始化数据库
RUN /usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
#暴露服务端口
EXPOSE 3306
#启动
RUN cp support-files/mysql.server /etc/init.d/mysqld
RUN chmod 755 /etc/init.d/mysqld
RUN sed -i '/^basedir=/s#basedir=#basedir=/usr/local/mysql#' /etc/init.d/mysqld  
RUN sed -i '/^datadir=/s#datadir=#basedir=/home/mysql#' /etc/init.d/mysqld
#建立软链接
RUN ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
ENTRYPOINT ["/usr/local/mysql/bin/mysqld_safe"]

容器运行测试

#容器创建
docker build -t centos7:mysql .
#容器运行
docker run -d -P centos7:mysql --privileged
#进入容器
docker exec -it 557a1908cee1 bash
#登录MySQL
mysql -u root -p 
#grant授权
grant all privileges on *.* to 'root'@'%' identified by '123456';    
grant all privileges on *.* to 'root'@'localhost' identified by '123456';   
#刷新权限
flush privileges; 
  • 在host上下载mariadb测试:
mysql -h 容器IP -P 端口号 -u 用户 -p 密码

Dockerfile 基于centos7镜像搭建MySQL服务_第1张图片

报错总结

  • 如果mysql容器启动容易闪退:
  • 虚拟内存不够,无法启动mysql
  • 解决方法:
  • 启动swap
dd if=/dev/zero of=/swapfile bs=1M count=1024
mkswap /swapfile
swapon /swapfile
  • 重启容器
docker start 557a1908cee1

你可能感兴趣的:(Docker)