I
I
Ivan Vekov2020-11-26 03:32:00
PHP
Ivan Vekov, 2020-11-26 03:32:00

How to connect to Mysql in Minikube/Kubernetes?

Good afternoon!

I did test tasks on kubernetes/minikube:
https://github.com/ivekov/minikubeNginxPhpMysql

I have the minikube --driver=none;

following problem running:

Message: SQLSTATE[HY000] [1045] Access denied for user 'crud'@'172.17.0.6' (using password: YES)


Wherein:
mysql> SELECT db, host, user FROM mysql.db WHERE db='mainDb';
+--------+------------+------+
| db     | host       | user |
+--------+------------+------+
| mainDb | %          | root |
| mainDb | 172.17.0.6 | crud |
+--------+------------+------+
2 rows in set (0.00 sec)


I also tried to connect as root user:
$app->get('/test/', function() {
  $link = mysqli_connect("mysql", "root", "YWRtaW4=", "mainDb");

  if (!$link) {
    echo "Ошибка: Невозможно установить соединение с MySQL." . PHP_EOL;
    echo "Код ошибки errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Текст ошибки error: " . mysqli_connect_error() . PHP_EOL;
    exit;
  }

  echo "Соединение с MySQL установлено!" . PHP_EOL;
  echo "Информация о сервере: " . mysqli_get_host_info($link) . PHP_EOL;

  mysqli_close($link);
});


Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'172.17.0.6' (using password: YES) in /var/www/html/public/index.php on line 44
Error: Unable establish a connection to MySQL. Error code errno: 1045 Error text error: Access denied for user 'root'@'172.17.0.6' (using password: YES)


Someone tell me what is the problem? I've been having this issue for a week now. Maybe I'm doing something wrong? I am running myqsl as a service, in a separate pod. And the user added already through the local machine. If you need something else, then I have no idea at all how ...

=========UPD=========
Did something stupid, because the expected password did not fit. Mysql in the pod, and I connected to the local one through the terminal, and waited for changes in the one I launched through the Stateful Set.

The question then is: Why was the password not applied?

Here is mysql stateful set config:
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-ss
spec:
  selector:
    matchLabels:
      app: my-php-app
  serviceName: mysql-service
  replicas: 1
  template:
    metadata:
      labels:
        app: my-php-app
    spec:
      containers:
      - name: mysql
        image: mariadb
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-vm
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
      volumes:
      - name: mysql-vm
        persistentVolumeClaim:
          claimName: mysql-pvc
      - name: config-volume
        configMap:
          name: mysql-conf


Here is the secret:
apiVersion: v1
kind: Secret
metadata:
  creationTimestamp: null
  name: mysql-pass
type: Opaque
data:
  password: YWRtaW4=

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Karasik, 2020-11-26
@vitaly_il1

And the user was added already through the local machine

I'm not sure I understand, but maybe you have another Mysql running on your local machine? And you added on it, not in the pod?

S
sergz99, 2020-12-10
@sergz99

Good afternoon. For your project - please tell me why in the minikubeNginxPhpMysql/configs/backend/crud/deployment.yaml file you copy the app folder to the nginx folder at the end if you have already mounted it in volume? Thanks in advance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question