1-路飞学城问题集

线上部署后,MySQL容器中文乱码

mysql8.0.30

客户端连接的字符集必须和MySQL服务端的字符集保持一致,我在容器中查了下,就发现不一致的情况:

mysql> show variables like "collation_%";
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | latin1_swedish_ci  |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+

collation_server的字符集是utf8mb4_general_ci,而客户端的连接对象collation_connection的字符集是latin1_swedish_ci,出现不一致情况。
如何修改就是,调整一致呗。
下面是基于docker-compose的配置。
首先在宿主机的某个文件夹内创建额外的MySQL配置文件,我这里叫做mysql.cnf,文件内容长这样:

[mysqld]
init-connect="SET collation_connection=utf8mb4_0900_ai_ci"
init_connect="SET NAMES utf8mb4"
skip-character-set-client-handshake

你也可以叫做任意的名字,但扩展名必须是.cnf,然后把这个mysql.cnf挂在到容器中去。
然后修改docker-compose.yml文件:

version: '3.8'
services:
  nginx:
    image: nginx:1.21.4
    restart: always
    ports:
      - "80:80"
    container_name: "nginx"
    volumes:
      - ./luffycityweb:/home/luffycityweb
      - ./conf/nginx/web.conf:/etc/nginx/conf.d/nginx.conf
    networks:
      - default

  mysql_master:
    image: mysql:8.0.30
    restart: always
    container_name: "mysql_master"
    networks:
      - default
    environment:
      - "MYSQL_ROOT_PASSWORD=root2022"
      - "MYSQL_USER=luffycity"
      - "MYSQL_PASSWORD=luffycity"
      - "MYSQL_DATABASE=luffycity"
      - "TZ=Asia/Shanghai"
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
    ports:
      - 3306:3306
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./logs/mysql:/logs
      - ./initdb:/docker-entrypoint-initdb.d/
      - ./conf/mysql:/etc/mysql/conf.d
networks:
  default:

重点就是./conf/mysql:/etc/mysql/conf.d,左边的./conf/mysql是相对目录,即找到docker-compose.yml文件的同级目录下的conf/mysql/mysql.cnf,然后挂在到容器中的/etc/mysql/conf.d目录下,这样容器中的MySQL就能读到我们在宿主机中的配置。这样就设置了字符集。
接下来,根据docker-compose.yml文件,重新启动吧。

# 在docker-compose.yml目录执行下面的命令:
docker-compose -f docker-compose.yml down
docker-compose -f docker-compose.yml up -d

完事之后,进入容器环境再去看下字符集设置:

mysql> show variables like "collation_%";
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_0900_ai_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.00 sec)

现在查记录也都能正常了:

mysql> select name from fg_course_info;
+----------------------------------+
| name                             |
+----------------------------------+
| 7天Typescript从入门到放弃 |
| 3天Typescript精修             |
| 3天学会Vue基础              |
| 算法与数据结构体系课   |
+----------------------------------+
20 rows in set (0.00 sec)

主要参考:

线上部署遇到问题

conda install pymysql -c conda-forge死活安装不上

阿里云Ubuntu20.04 + docker

通过容器部署到阿里云的Ubuntu20.04上面,客户端都配置好了,再配置服务器端的容器时,配置到编写dockerfile定制服务端项目的镜像时出了问题。

dockerfile文件内容我也贴一下。

FROM ubuntu:20.04
LABEL maintainer="luffycity.Edu"

ENV PYTHONUNBUFFERED 1

COPY ./Miniconda3-py38_4.10.3-Linux-x86_64.sh /opt/
COPY ./sources.list /etc/apt/sources.list
COPY ./luffycityapi /luffycityapi
COPY ./luffycityapi/requirements.txt /requirements.txt

RUN apt-get update \
    && apt-get -y install wget \
    && mkdir /root/.conda \
    && bash /opt/Miniconda3-py38_4.10.3-Linux-x86_64.sh -b \
    && rm -f /opt/Miniconda3-py38_4.10.3-Linux-x86_64.sh

ENV PATH /root/miniconda3/bin:$PATH

RUN conda install pymysql -c conda-forge \
    && conda install uwsgi -c conda-forge \
    && pip install -r /requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple \
    && pip install django-haystack==3.1.1 -i https://pypi.tuna.tsinghua.edu.cn/simple \
    && chmod -R 755 /luffycityapi

WORKDIR /luffycityapi

EXPOSE 8000

VOLUME /luffycityapi

说说解决办法吧!

  1. 设置清华源......失败!!!!
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
  1. 进行入镜像内部手动conda安装....失败
docker run -i -t luffycityapi:1.0.0 bash
然后进去之后,手动尝试上一步的动作,添加清华源,然后再conda install.....不行!

期间我也尝试了手动添加依赖,没解决

apt-get install python3-dev

安装gcc,默认安装的是9.x版本的,太高......导致失败。
最终我选择不用conda装了。
所以最终的解决办法是这样的。
思路是,使用pip安装,但有个问题,直接pip安装,下载倒是能下载,但构建失败,为了解决构建失败的问题,我选择下载指定版本的gcc4.8.5的。完事再尝试pip安装uwsgi,发现可以安装上。
那么我这里简单的罗列下安装指定版本的gcc的步骤。参考:https://blog.csdn.net/haojie_duan/article/details/124143322

1. 通过sudo vim /etc/apt/sources.list在末尾增加下面源.
echo "deb http://dk.archive.ubuntu.com/ubuntu/ xenial main" >> /etc/apt/sources.list
echo "deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe" >> /etc/apt/sources.list

2. 更新apt-get
apt-get update -y

3. 下载指定版本的gcc,注意上面几部没做的话,这一步你下载不到4.8的gcc
apt-get install gcc-4.8 -y

4. 切换gcc版本update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20

5. 测试一下,输入"gcc -v"应该能看到4.8的版本号。 这就表示行了。
root@53e707acab92:/luffycityapi# gcc -v     
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.5-4ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.5 (Ubuntu 4.8.5-4ubuntu2) 


6. 你再pip install uwsgi就应该没问题了。
7. 你可以执行下"which uwsgi"看看它在哪。
root@53e707acab92:/luffycityapi# which uwsgi
/root/miniconda3/bin/uwsgi

上面这一套命令走下来,能按照成功,但是,我还要把这写明了整理一下搞到dockerfile中。
所以最终的dockerfile长这样:

FROM ubuntu:20.04
LABEL maintainer="luffycity.Edu"

ENV PYTHONUNBUFFERED 1

COPY ./Miniconda3-py38_4.10.3-Linux-x86_64.sh /opt/
COPY ./sources.list /etc/apt/sources.list
COPY ./luffycityapi /luffycityapi
COPY ./luffycityapi/requirements.txt /requirements.txt

RUN apt-get update \
    && apt-get -y install wget \
    && mkdir /root/.conda \
    && bash /opt/Miniconda3-py38_4.10.3-Linux-x86_64.sh -b \
    && rm -f /opt/Miniconda3-py38_4.10.3-Linux-x86_64.sh

ENV PATH /root/miniconda3/bin:$PATH


#  ---- start  ----
RUN echo "deb http://dk.archive.ubuntu.com/ubuntu/ xenial main" >> /etc/apt/sources.list
RUN echo "deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe" >> /etc/apt/sources.list
RUN apt-get update -y
#RUN apt-get list --upgradable
RUN apt-get install gcc-4.8 -y
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20
RUN pip install uwsgi
#  ---- end  ----

RUN conda install pymysql -c conda-forge
RUN pip install -r /requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install django-haystack==3.1.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN chmod -R 755 /luffycityapi

WORKDIR /luffycityapi
EXPOSE 8000

然后执行docker-compose -f docker-compose.yml up -d就没啥问题了。
反正就是多尝试几个方法吧。

posted @ 2022-11-25 13:37  听雨危楼  阅读(58)  评论(0编辑  收藏  举报