Access MySQL Single-Instance Kubernetes Deployment

10/16/2021

I was following the Run a Single-Instance Stateful Application tutorial of Kubernetes (I changed the MySQL docker image's tag to 8), and it seems the server is running correctly: enter image description here

But when I try to connect the server as the tutorial suggesting:

kubectl run -it --rm --image=mysql:8 --restart=Never mysql-client -- mysql -h mysql -ppassword

I get the following error:

ERROR 1045 (28000): Access denied for user 'root'@'10.1.0.99' (using password: YES) pod "mysql-client" deleted


I already looked at those questions:

But changing the mountPath or port didn't work.

-- Roy Yosef
docker
kubernetes
mysql

1 Answer

10/16/2021

Default behavior of root account can only be connected to from inside the container. Here's an updated version of the example that allows you to connect from remote:

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:8.0.26
        name: mysql
        env:
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          value: password
        - name: MYSQL_ROOT_HOST
          value: "%"
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        emptyDir: {}
        # Following the original example, comment the emptyDir and uncomment the following if you have StorageClass installed.
        # persistentVolumeClaim:
        #  claimName: mysql-pv-claim

No change to the client connect except for the image tag:

kubectl run -it --rm --image=mysql:8.0.26 --restart=Never mysql-client -- mysql -h mysql -ppassword

Test with show databases;:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
-- gohm'c
Source: StackOverflow