每天一博--第五天 dockerfile部署mysql8.0

一、准备环境

  • docker版本
 Docker version 19.03.4-ce, build 9013bf583a
  • mysql版本
 mysql-8.0.18
  • centos版本
CentOS Linux release 8.0.1905 (Core)

二、部署

2.1、创建目录

mkdir -p ~/docker/mysql/data

2.2、下载数据库文件到mysql目录下

# 下载步骤省略,不需要解压

2.3、创建Dockerfile

#centos镜像我重新打了一个包,进行了dnf的配置与内核优化
FROM centos:v2
MAINTAINER superjay09

#Create workdir and Installdir
RUN mkdir -p /usr/local/mysql/{data,tmp,binlog,log/innodb_log,innodb_ts}

#Add new file
ADD mysql-8.0.18.tar.gz /opt
ADD rpcsvc-proto-1.4.tar.gz /opt
ADD my.cnf /etc

#Update dnf source && install rely on package
RUN dnf -y update

RUN dnf -y install epel-release gcc gcc-c++ vim make \ 
autoconf openssl openssl-devel cmake ncurses  ncurses-devel \ 
libaio-devel openssl openssl-devel pcre pcre-devel \ 
zlib zlib-devel libtool libtirpc-devel net-tools

RUN dnf clean all

RUN dnf makecache

#Install rpcgen
RUN cd /opt/rpcsvc-proto-1.4 && ./configure && make && make install

#Cmake install mysql
RUN mkdir -p /opt/mysql-8.0.18/build && cd /opt/mysql-8.0.18/build

RUN cmake ..  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \ 
-DMYSQL_TCP_PORT=3306 \ 
-DDOWNLOAD_BOOST=1 \ 
-DWITH_BOOST=/opt/mysql-8.0.18 \ 
&& make \
&& make install

#Export path
ENV PATH /usr/local/mysql/bin:$PATH

#Add user for mysql
RUN useradd -s /sbin/nologin -M mysql
RUN chown -R mysql:mysql /usr/local/mysql/

#Init mysql && start mysql
RUN /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --character-set-server=utf8mb4 --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
RUN cp -r /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

VOLUME [ "/data/docker/mysql/data","/usr/local/mysql/data" ]

EXPOSE 3306

###################################################################################################
#System Optimization

RUN mv /etc/security/limits.conf /etc/security/limits.conf.bak
ADD limits.conf /etc/security

RUN /bin/cp -r /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

RUN echo "alias ls='ls --color=tty'" >> ~/.bashrc
RUN echo "alias ll='ls -lh'" >> ~/.bashrc
RUN echo "alias l='ls -lah'" >> ~/.bashrc
RUN source ~/.bashrc

###################################################################################################

WORKDIR /usr/local/mysql

CMD [ "/usr/local/mysql/bin/mysqld_safe" ]

#RUN /etc/init.d/mysqld start

三、构建镜像

需要将ADD所需要的文件全部放到Dockerfile同级目录中

docker build -t mysql:v1

四、启动镜像

docker run -itd --name mysql-1 -p 3306:3306 mysql:v1

五、后续问题

docker build之后Image特别大,需要精简容器。虽然启动之后数据库可以正常启动,
但是数据库密码还需要手动查看,没有办法在docker run的时候通过-e MYSQL_ROOT_PASSWORD
环境变量进行读取,需要改善

六、文中需要的文件

  • limits.conf
# /etc/security/limits.conf
mysql soft nproc 65536
mysql hard nproc 65536
mysql soft nofile 65536
mysql hard nofile 65536
  • my.cnf
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
# The MySQL server
[mysqld]
port = 3306
user = mysql
socket   = /usr/local/mysql/mysql.sock
pid-file = /usr/local/mysql/mysql.pid
basedir  = /usr/local/mysql
datadir  = /usr/local/mysql/data
tmpdir   =  /usr/local/mysql/tmp
open_files_limit = 60000
explicit_defaults_for_timestamp
server-id = 1
lower_case_table_names = 1
character-set-server = utf8mb4
federated
#sql_mode=STRICT_TRANS_TABLES
max_connections = 2000
max_connect_errors = 10000
interactive_timeout = 86400
wait_timeout = 86400
sync_binlog=0
back_log=100
default-storage-engine = InnoDB
log_slave_updates = 1

#*********** Logs related settings ***********
log-bin = /usr/local/mysql/binlog/mysql-bin
binlog_format= mixed
binlog_cache_size=32m
max_binlog_cache_size=64m
max_binlog_size=512m
long_query_time = 1
log_output = FILE
log-error = /usr/local/mysql/log/mysql-error.log
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/log/slow_statement.log
#log_queries_not_using_indexes
general_log = 0
general_log_file = /usr/local/mysql/log/general_statement.log
#expire-logs-days = 14
binlog_expire_logs_seconds = 1728000
relay-log = /usr/local/mysql/binlog/relay-bin
relay-log-index = /usr/local/mysql/binlog/relay-bin.index
#****** MySQL Replication New Feature*********
master-info-repository=TABLE
relay-log-info-repository=TABLE
relay-log-recovery
#*********** INNODB Specific options ***********
innodb_buffer_pool_size = 2048M
transaction-isolation=REPEATABLE-READ
innodb_buffer_pool_instances = 8
innodb_file_per_table = 1
innodb_data_home_dir = /usr/local/mysql/innodb_ts
innodb_data_file_path = ibdata1:2048M:autoextend
innodb_thread_concurrency = 8
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_log_group_home_dir = /usr/local/mysql/log/innodb_log
innodb_flush_log_at_trx_commit = 2
innodb_max_dirty_pages_pct = 70
innodb_flush_method=O_DIRECT
[mysql]
no-auto-rehash
default-character-set=gbk
prompt = (\u@\h) [\d]>\_
  • rpcsvc下载地址
https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4/rpcsvc-proto-1.4.tar.gz

你可能感兴趣的:(docker,mysql,dockerfile)