简体   繁体   English

在 mysql kubernetes pod 中创建其他用户

[英]create additional users in mysql kubernetes pods

I'm trying to create additional users for MySQL pod.我正在尝试为 MySQL 吊舱创建其他用户。

i used the env variable- MYSQL_USER to create additional users.我使用环境变量MYSQL_USER创建其他用户。 after the creation of the pod.在创建 pod 之后。 I'm able to only use the second user not the first user.我只能使用第二个用户而不是第一个用户。

is there a way i can achieve this?有没有办法我可以做到这一点?

You can use the /docker-entrypoint-initdb.d您可以使用/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.此外,它将执行 /docker-entrypoint-initdb.d 中的 extensions.sh、.sql 和.sql.gz 文件。 Files will be executed in alphabetical order.文件将按字母顺序执行。 You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data.您可以通过将 SQL 转储安装到该目录中轻松填充 mysql 服务,并提供带有贡献数据的自定义图像。 SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable. SQL 文件将默认导入到 MYSQL_DATABASE 变量指定的数据库中。

You can mount your files at that path您可以将文件挂载到该路径

volumes:
      - ./<your-path-to-create-user.sql>:/docker-entrypoint-initdb.d

create-user.sql创建用户.sql

CREATE USER 'root'@'%' IDENTIFIED BY 'password';
CREATE USER 'user1'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

so after initializing the above SQL script will get executed and create the users to database,you can edit it as per need.因此,在初始化上述 SQL 脚本后,将执行并将用户创建到数据库,您可以根据需要对其进行编辑。

If you are on Kubernetes you can use the configmap如果您在Kubernetes上,您可以使用configmap

apiVersion: extensions/v1beta1
kind: Pod
metadata:
  name: mysql
spec:
  containers:
  - name: mysql
    image: mysql
    .....
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: "root"
    .....
    volumeMounts:
      - name: mysql-inituser
        mountPath: /docker-entrypoint-initdb.d
  volumes:
  - name: mysql-inituser
    configMap:
      name: users
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: users
data:
  create-user.sql: |-
    CREATE USER 'root'@'%' IDENTIFIED BY 'password';
    CREATE USER 'user1'@'%' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

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

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