Dockerfile创建Mysql5.6及Mysql5.7

一、Mysql-5.7的Dockerfile建立

[root@docker mysql-5.7]# ll
total 47704
-rw-r--r-- 1 root root     1247 Nov 30 17:15 Dockerfile
-rw-r--r-- 1 root root      560 Nov 30 16:43 my.cnf
-rw-r--r-- 1 root root 48833145 Nov 30 14:51 mysql-boost-5.7.20.tar.gz
-rw-r--r-- 1 root root       36 Nov 30 17:14 run.sh
[root@docker mysql-5.7]# cat run.sh 
#!/bin/bash
systemctl enable mysqld

[root@docker mysql-5.7]# vi my.cnf 

[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

[root@docker mysql-5.7]# vi Dockerfile
FROM centos:7
MAINTAINER this is mysql-5.7 image <chu>
RUN yum -y update
RUN yum -y install ncurses ncurses-devel bison cmake make gcc gcc-c++
RUN groupadd mysql
RUN useradd -M -s /sbin/nologin mysql -g mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src
WORKDIR /usr/local/src/mysql-5.7.20
RUN cmake \
 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
 -DSYSCONFDIR=/etc \
 -DSYSTEMD_PID_DIR=/usr/local/mysql \
 -DDEFAULT_CHARSET=utf8  \
 -DDEFAULT_COLLATION=utf8_general_ci \
 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
 -DMYSQL_DATADIR=/usr/local/mysql/data \
 -DWITH_BOOST=boost \
 -DWITH_SYSTEMD=1 && make -j6 && make install
RUN chown -R mysql:mysql /usr/local/mysql
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc/my.cnf
RUN chown mysql:mysql /etc/my.cnf
ENV PATH $PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
RUN /usr/local/mysql/bin/mysqld \
 --initialize-insecure \
 --user=mysql \
 --basedir=/usr/local/mysql \
 --datadir=/usr/local/mysql/data
EXPOSE 3306
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
ADD run.sh /run.sh
RUN sh /run.sh
CMD ["/usr/sbin/init"]

[root@docker mysql-5.7]# docker run -d -P --name mysql --privileged mysql5.7:new		# 不降权处理
33ac32f7ee22ea8068a71b2473909182e2caab45caececf2897416f5fedfef50
[root@docker mysql-5.7]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                        PORTS                                         NAMES
33ac32f7ee22        mysql5.7:new          "/usr/sbin/init"         5 minutes ago       Up 5 seconds                  0.0.0.0:32769->3306/tcp                       mysql

[root@docker mysql-5.7]# docker exec -it mysql /bin/bash
[root@33ac32f7ee22 mysql-5.7.20]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant all privileges on *.* to 'root'@'%' identified by 'qwe123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;

在这里插入图片描述

  • 另一台机器检测
[root@localhost ~]# yum -y install mariadb
[root@localhost ~]# mysql -h 10.0.0.101 -u root -P 32769 -pqwe123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

在这里插入图片描述

二、Mysql-5.6的Dockerfile建立

[root@docker mysql-5.6]# ll
total 31444
-rw-r--r-- 1 root root     1219 Nov 30 18:55 Dockerfile
-rw-r--r-- 1 root root 32192348 Nov 27 14:48 mysql-5.6.36.tar.gz

[root@docker mysql-5.6]# vi Dockerfile 

From centos:7
MAINTAINER this mysql-5.6.36 image <chu>
RUN yum -y install gcc gcc-c++ zlib-devel pcre-devel perl \
make cmake bison ncurses ncurses-devel autoconf
RUN useradd -M -s /sbin/nologin mysql
ADD mysql-5.6.36.tar.gz /opt/
WORKDIR /opt/mysql-5.6.36/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j6 && make install
RUN cp -p /opt/mysql-5.6.36/support-files/my-default.cnf /etc/my.cnf
RUN cp -p /opt/mysql-5.6.36/support-files/mysql.server /etc/rc.d/init.d/mysqld
RUN chmod +x /etc/rc.d/init.d/mysqld
RUN chkconfig --add mysqld
RUN chown -R mysql:mysql /usr/local/mysql/
ENV PATH /usr/local/mysql/bin:/usr/local/mysql:$PATH
WORKDIR /usr/local/mysql/scripts/
RUN ./mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld_safe"]

[root@docker mysql-5.6]# docker build -t mysql5.6:new .
[root@docker mysql-5.6]# docker run -d -P --name mysql5.6 mysql5.6:new
7fc3b3e0886deaa0925cfd7957bbd9830c6c8993ad0eff5b5fab0ad4b5b652c5
[root@docker mysql-5.6]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                         NAMES
7fc3b3e0886d        mysql5.6:new          "/usr/local/mysql/bi…"   4 seconds ago       Up 4 seconds        0.0.0.0:32770->3306/tcp                       mysql5.6

[root@7fc3b3e0886d scripts]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant all privileges on *.* to 'root'@'%' identified by 'asd123';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

在这里插入图片描述

  • 另一台机器检测
[root@localhost ~]# mysql -h 10.0.0.101 -u root -P 32770 -pasd123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.36 Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值