简体   繁体   English

如何编辑存在于 kubernetes mysql 容器中的 my.cnf 文件?

[英]How to edit my.cnf file which is present in kubernetes mysql container?

I have created mysql container inside pod by giving mount path as /var/lib/mysql.我通过将挂载路径指定为 /var/lib/mysql 在 pod 中创建了 mysql 容器。 When I try to create container with this mountPath getting below error.当我尝试使用此 mountPath 创建容器时出现以下错误。

2022-12-16T07:13:59.139528Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. 2022-12-16T07:13:59.139528Z 0 [ERROR] [MY-010457] [Server] --initialize specified 但数据目录中有文件。 Aborting.中止。

2022-12-16T07:13:59.139537Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. 2022-12-16T07:13:59.139537Z 0 [ERROR] [MY-013236] [Server] 指定的数据目录/var/lib/mysql/不可用。 You can remove all files that the server added to it.您可以删除服务器添加到其中的所有文件。

So I decided to change the data directory path in my.cnf to give it to empty folder such as /var/lib/dbmysql.所以我决定更改my.cnf中的数据目录路径,将其提供给空文件夹,例如/var/lib/dbmysql。

But I am not able to change the my.cnf file inside the mysql container.但是我无法更改 mysql 容器内的 my.cnf 文件。

Steps that I followed:我遵循的步骤:

  1. kubectl exec -i -t pod-name --container container-name -- /bin/bash kubectl exec -i -t pod-name --container 容器名称 -- /bin/bash

  2. In the bash trying to change the file using在 bash 中尝试使用更改文件

    a) bash-4.2$ cat > /etc/my.cnf a) bash-4.2$ cat > /etc/my.cnf

     bash: /etc/my.cnf: Permission denied

    b) vim /etc/my.cnf b) vim /etc/my.cnf

     bash: vim: command not found

same for nano and vi nano 和 vi 也一样

c) Also tried to install vim using apt-get as well as apk update but getting same error command not found. c) 还尝试使用 apt-get 和 apk update 安装 vim,但未找到相同的错误命令。

Please guide me !!请指导我!

One possibility is changing the volume path /var/lib/mysql , if you want to use the same path then you can try the below recommended way also.一种可能性是更改卷路径/var/lib/mysql ,如果您想使用相同的路径,那么您也可以尝试以下推荐的方式。

To run a single-instance stateful MySQL application in Kubernetes using a PV and a Deployment refer to Run a Single-Instance Stateful Application .要使用 PV 和 Deployment 在 Kubernetes 中运行单实例有状态 MySQL 应用程序,请参阅运行单实例有状态应用程序

Try by deploying everything again and also check if the MySQL image you are using doesn't have any issues and runs without PVC first.尝试再次部署所有内容,并检查您使用的 MySQL 映像是否没有任何问题并首先在没有 PVC 的情况下运行。

For example, this YAML file describes a Deployment that runs MySQL and references the PVC.例如,此 YAML 文件描述了一个运行 MySQL 并引用 PVC 的 Deployment。 The file defines a volume mount for /var/lib/mysql, and then creates a PVC that looks for a 20G volume.该文件为 /var/lib/mysql 定义了一个卷挂载,然后创建了一个寻找 20G 卷的 PVC。 This claim is satisfied by any existing volume that meets the requirements, or by a dynamic provisioner.满足要求的任何现有卷或动态供应商都会满足此要求。

Before applying, update necessary changes like an image of MySQL.在应用之前,更新必要的更改,例如 MySQL 的图像。

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

And Deployment & Service like..以及部署和服务之类的……

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM