简体   繁体   中英

create additional users in mysql kubernetes pods

I'm trying to create additional users for MySQL pod.

i used the env variable- MYSQL_USER to create additional users. after the creation of the 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

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. 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.

You can mount your files at that path

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

create-user.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.

If you are on Kubernetes you can use the 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'@'%';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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