my.cnf in kubernetes configmap is not recognized by mysql pod

2.9k views Asked by At

my.cnf in kubernetes configmap is not recognized by mysql pod.

This is mysql.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: *****
  name: *****
spec:
  serviceName: mysql-service
  replicas: 1
  selector:
    matchLabels:
      app: *****
  template:
    metadata:
      labels:   
        app: *****
    spec:
      
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: eks.amazonaws.com/nodegroup
                operator: In
                values:
                - db-node
      containers:
      - name: mysql
        image: mysql:5.7
        args:
           - "--ignore-db-dir=lost+found"
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: password
          - name: MYSQL_USER
            value: mysql
          - name: MYSQL_DATABASE
            value: ****
        ports:
        - containerPort: 3306
          protocol: TCP
        volumeMounts:
        - name: *****
          mountPath: /var/lib/mysql
        - name: mysql-configmap
          mountPath: /etc/mysql/conf.d/mysql.cnf
          subPath: mysql.cnf
      volumes:
        - name: mysql-configmap
          configMap:
            name: mysql-configmap

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-configmap
  namespace: ****
data:
  mysql.cnf: |
    [client]
    default-character-set=utf8
  
    [mysql]
    default-character-set=utf8

MySQL Container in my.cnf

root@********:/etc/mysql/conf.d# cat mysql.cnf 
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

The file is located in the folder, but the settings I want are not applied.

mysql> show variables like 'char%';                                                                               +--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

Thank you for reading.
I think it's because my.cnf is root authority, but it's not visible in other use cases.

1

There are 1 answers

3
meaningqo On

From the official documentation of the docker image found here this could have several reasons.

Check the default mysql configuration under /etc/mysql/my.cnf if there is an includedir statement which includes your /etc/mysql/conf.d directory with your custom configuration.

It is also possible to add encoding options as arguments, meaning that if you cannot fix it via the configmap option you are using right now, you could add extra arguments to your mysql image.

Your statefulset yaml would then look like that:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: *****
  name: *****
spec:
  serviceName: mysql-service
  replicas: 1
  selector:
    matchLabels:
      app: *****
  template:
    metadata:
      labels:   
        app: *****
    spec:
      
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: eks.amazonaws.com/nodegroup
                operator: In
                values:
                - db-node
      containers:
      - name: mysql
        image: mysql:5.7
        args:
           - "--ignore-db-dir=lost+found"
           - "--character-set-server=utf8mb4"
           - "--character-set-client=utf8mb4"
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: password
          - name: MYSQL_USER
            value: mysql
          - name: MYSQL_DATABASE
            value: ****
        ports:
        - containerPort: 3306
          protocol: TCP
        volumeMounts:
        - name: *****
          mountPath: /var/lib/mysql
        - name: mysql-configmap
          mountPath: /etc/mysql/conf.d/mysql.cnf
          subPath: mysql.cnf
      volumes:
        - name: mysql-configmap
          configMap:
            name: mysql-configmap

The documentation uses --character-set-server=utf8mb4 instead of default-character-set=utf8 so you might need to try both options to find out which one is the corret one.