1

I tried to run all images regarding Spring Boot Microservices on Kubernetes.

When I run kubectl apply -f k8s,

I get this issue for order service, payment service, auth service and lastly product service.

Here is the issue shown below.

Caused by: java.sql.SQLException: Access denied for user 'springmicroserviceuser'@'172.17.0.6' (using password: YES)

How can I define a command to create user and password in MySQL deployment yaml file.

Here is the file shown below.

# Persistent Volume
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  hostPath:
    #    path: "/mnt/data"  # - for Unix/Linux
    path: "/run/desktop/mnt/host/c/temp/testfiles"
    type: DirectoryOrCreate
---

# Persistent Volume Claim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  resources:
    requests:
      storage: 1Gi
  storageClassName: manual
  accessModes:
    - ReadWriteOnce

# StatefulSet - MySql
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:8.0
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: ippavlova_1990
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql-initdb
              mountPath: /docker-entrypoint-initdb.d
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-initdb
          configMap:
            name: mysql-initdb-cm
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pvc

# Headless Service
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  clusterIP: None
  selector:
    app: mysql
  ports:
    - port: 3306

# Config
---

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-initdb-cm
data:
  init.sql: |
        CREATE USER 'springmicroserviceuser'@'localhost' IDENTIFIED BY 

'111111';
    CREATE DATABASE IF NOT EXISTS orderdb;
    CREATE DATABASE IF NOT EXISTS paymentdb;
    CREATE DATABASE IF NOT EXISTS productdb;
    GRANT ALL PRIVILEGES ON `orderdb`.* TO 'springmicroserviceuser'@'%' WITH GRANT OPTION;
    GRANT ALL PRIVILEGES ON `paymentdb`.* TO 'springmicroserviceuser'@'%' WITH GRANT OPTION;
    GRANT ALL PRIVILEGES ON `productdb`.* TO 'springmicroserviceuser'@'%' WITH GRANT OPTION;

Here is the screenshot of one pod.

Image

Here is the repo : Link

Jonas
  • 121,568
  • 97
  • 310
  • 388
S.N
  • 2,157
  • 3
  • 29
  • 78

1 Answers1

0

Create the user for the IP address for which you are getting this error using below command on the docker-mysql terminal.

CREATE USER 'springmicroserviceuser'@'172.17.0.6' IDENTIFIED BY 'root';


GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.23.0.6' WITH GRANT OPTION;


docker exec -it a5572eb07b8fe8e216d9addc63c1b8f50473638393c39d79e44e2ca7ffd6781e mysql -uroot -p (to get the docker-mysql terminal)[![This picture is for docker-mysql CLI][1]][1]
flush privileges;

Refer to this SO link1 ,link2 for more information.

Fariya Rahmat
  • 2,123
  • 3
  • 11
  • 172.17.0.6 is the dynamic part. When I run it, this part is always changed. How can I fix it? – S.N Feb 03 '23 at 19:31
  • You can use a [service discovery](https://www.densify.com/kubernetes-autoscaling/kubernetes-service-discovery#:~:text=What%20is%20Service%20Discovery%3F,applications%20are%20designed%20using%20microservices) tool like Kubernetes to locate the IP address of the MySQL database if its IP address is dynamic. – Fariya Rahmat Feb 07 '23 at 11:09
  • I already defined service discovery in k8 folder as you can see. – S.N Feb 07 '23 at 11:14