且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Kubernetes中将另一个用户添加到MySQL

更新时间:2023-02-01 16:58:28

将创建用户脚本"安装到容器的/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.