就无聊编译一下玩玩,心情不好,转移一下注意力。
自我感觉应该会遇到不少问题,Alpine这个发行版非常适合用于Docker容器的基础镜像,因为体积小。缺点就是标准库和常见的红帽系Debian系发行版不一样,所以很多的软件需要自己编译解决了。
首先MySQL Connector/ODBC依赖于MySQL Client,所以需要先编译MySQL Client,而且版本最好是一样的,避免出一些莫名其妙的问题。然后就是我选择了8.0.15版本,也没有什么特别之处,就是这个版本编译成功了。不论选择什么版本,或多或少都会出一些问题,需要更改源码来保证编译通过。
最终的Dockerfile如下:
# step1: build mysql_connector_odbc
FROM alpine:3.16 as builder
# install dependencies
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
RUN apk update && apk add cmake make g++ openssl-dev ncurses-dev unixodbc-dev
# download source code
WORKDIR /build
#RUN wget https://cdn.mysql.com/archives/mysql-8.0/mysql-boost-8.0.15.tar.gz
#RUN wget https://cdn.mysql.com/archives/mysql-connector-odbc-8.0/mysql-connector-odbc-8.0.15-src.tar.gz
#RUN tar xzf mysql-boost-8.0.15.tar.gz && tar xzf mysql-connector-odbc-8.0.15-src.tar.gz
ADD mysql-boost-8.0.15.tar.gz .
ADD mysql-connector-odbc-8.0.15-src.tar.gz .
# patch
RUN sed -i '822i cmake_policy(SET CMP0115 OLD)' mysql-8.0.15/extra/libevent/CMakeLists.txt
RUN sed -i '3028i #include <stdexcept>' mysql-8.0.15/client/mysqltest.cc
# compile libmysqlclient.so
WORKDIR /build/build_client
RUN cmake -DWITHOUT_SERVER=ON -DWITH_BOOST=/build/mysql-8.0.15/boost /build/mysql-8.0.15
RUN make && make install
# compile libmyodbc8w.so & libmyodbc8a.so
WORKDIR /build/build_connector
RUN cmake -DWITH_UNIXODBC=1 -DMYSQL_INCLUDE_DIR=/usr/local/mysql/include -DDISABLE_GUI=1 -DMYSQLCLIENT_STATIC_LINKING=ON -G "Unix Makefiles" /build/mysql-connector-odbc-8.0.15-src
RUN make && make package
RUN tar xzf mysql-connector-odbc-8.0.15-.tar.gz && mv mysql-connector-odbc-8.0.15- mysql-connector-odbc-8.0.15
# step2: install mysql_connector_odbc
FROM node:18.9.0-alpine3.16
COPY --from=builder /build/build_connector/mysql-connector-odbc-8.0.15 /opt/mysql-connector-odbc-8.0.15
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
apk update && apk add unixodbc && rm -rf /var/cache/apk/*
RUN wget https://download.microsoft.com/download/8/6/8/868e5fc4-7bfe-494d-8f9d-115cbcdb52ae/msodbcsql18_18.1.2.1-1_amd64.apk && \
wget https://download.microsoft.com/download/8/6/8/868e5fc4-7bfe-494d-8f9d-115cbcdb52ae/mssql-tools18_18.1.1.1-1_amd64.apk && \
apk add --allow-untrusted msodbcsql18_18.1.2.1-1_amd64.apk && apk add --allow-untrusted mssql-tools18_18.1.1.1-1_amd64.apk && \
rm -rf *.apk && ln -s /opt/mysql-connector-odbc-8.0.15/bin/myodbc-installer /usr/local/bin/myodbc-installer && \
myodbc-installer -d -a -n "MySQL ODBC 8.0 Unicode Driver" -t "DRIVER=/opt/mysql-connector-odbc-8.0.15/lib/libmyodbc8w.so" && \
myodbc-installer -d -a -n "MySQL ODBC 8.0 ANSI Driver" -t "DRIVER=/opt/mysql-connector-odbc-8.0.15/lib/libmyodbc8a.so"
ENTRYPOINT ["tail", "-f", "/dev/null"]