在Kubernetes中将另一个用户添加到MySQL [英] Add another user to MySQL in Kubernetes

查看:92
本文介绍了在Kubernetes中将另一个用户添加到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的MySQL

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: abc-def-my-mysql
  namespace: abc-sk-test
  labels:
    project: abc
    ca: my
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: abc-def-my-mysql
        project: abc
        ca: my
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        args: ["--default-authentication-plugin=mysql_native_password", "--ignore-db-dir=lost+found"]
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
        - name: MYSQL_DATABASE
          value: "my_abc"
        - name: MYSQL_USER
          value: "test_user"
        - name: MYSQL_PASSWORD
          value: "12345"
        volumeMounts:
         - mountPath: /var/lib/mysql
           name: abc-def-my-mysql-storage
      volumes:
      - name: abc-def-my-mysql-storage
        persistentVolumeClaim:
         claimName: abc-def-my-mysql-pvc

我想向mysql添加另一个用户,以便真正的用户可以连接到它.而不是使用"test_user".我该如何添加其他用户,就像将其他任何环境变量添加到上述配置

I would like to add another user to mysql so real users can connect to it. Instead of using "test_user". how can I add another user, is it like adding any other environment variable to the above config

推荐答案

将创建用户脚本"安装到容器的/docker-entrypoint-initdb.d 目录中.它会在首次Pod启动时执行一次.

Mount a "create user script" into container's /docker-entrypoint-initdb.d directory. it will be executed once, at first pod start.

apiVersion: extensions/v1beta1
kind: Pod
metadata:
  name: mysql
spec:
  containers:
  - name: mysql
    image: mysql
    .....
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: "root"
    .....
    volumeMounts:
      - name: mysql-initdb
        mountPath: /docker-entrypoint-initdb.d
  volumes:
  - name: mysql-initdb
    configMap:
      name: initdb

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: initdb
data:
  initdb.sql: |-
    CREATE USER 'first_user'@'%' IDENTIFIED BY '111' ;
    CREATE USER 'second_user'@'%' IDENTIFIED BY '222' ;

测试:

kubectl exec -it <PODNAME> -- mysql -uroot -p -e 'SELECT user, host FROM mysql.user;'

    +-------------+------+
    | user        | host |
    +-------------+------+
    | first_user  | %    |
    | second_user | %    |
    | root        | %    |
    +-------------+------+

请参见初始化新实例 Mysql Docker Hub镜像:

See Initializing a fresh instance Mysql Docker Hub image:

首次启动容器时,将使用指定的名称将被创建并使用提供的名称进行初始化配置变量.此外,它将使用以下命令执行文件在以下位置找到扩展名 .sh, .sql .sql.gz /docker-entrypoint-initdb.d .文件将按字母顺序执行命令.

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order.

您可以通过安装SQL轻松填充 mysql 服务转储到该目录并提供具有贡献的自定义图像数据.默认情况下,SQL文件将导入到指定的数据库中通过MYSQL_DATABASE变量.

You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

这篇关于在Kubernetes中将另一个用户添加到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆