2

I have the following deployment which puts up MySQL instance:

kind: Deployment
apiVersion: apps/v1beta1
metadata:
  name: mysql
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:8
        ports:
        - containerPort: 3306
        env:
          - name: MYSQL_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-root-password
                key: password

The password is just root :

kind: Secret
apiVersion: v1
metadata:
  name: mysql-root-password
type: Opaque
data:
  password: cm9vdA==

The problem is I try to connect to the instance after port forwarding the MySQL port, following the instructions from here, but get an error:

$ kubectl port-forward mysql-824284009-rpbpk 3306
Forwarding from 127.0.0.1:3306 -> 3306
Forwarding from [::1]:3306 -> 3306

# from another terminal
$ mysql -u root -p
Enter password:  
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Connecting to the server from the pod itself works:

$ kubectl exec -it mysql-824284009-rpbpk -- /bin/bash
root@mysql-824284009-rpbpk:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
...
mysql>

I have basically the same setup like here, except I'm running the cluster in minikube instead of GCP. My local MySQL is not runnning, so I assume there is no chance of clashing.

Gabriel Ruiu
  • 2,753
  • 2
  • 19
  • 23

1 Answers1

12

The port forwarding is likely there, but you need to tell mysql client to connect using host/port and not unix socket (default)

mysql --host=localhost --protocol tcp --port=3306 -u root -p

If you don't, mysql by default uses local linux socket to connect to he server: /var/run/mysqld/mysqld.sock .. It even tells you so ;)

Update: As Gabriel checked - adding --protocol tcp had finally made it works, so I am addding it to my answer

Jarek Potiuk
  • 19,317
  • 2
  • 60
  • 61
  • Tried running `mysql --host=localhost --port=3306 -u root -p` but the error still occurs. – Gabriel Ruiu Aug 13 '17 at 14:31
  • Maybe your mysql client needs other options? Can you check the manual how to connect to host/port rather than to socket? If you got an error that you cannot connect to '/var/run/mysqld/mysqld.sock' this means that you did not even try to connect to host/port. This SO answer (https://stackoverflow.com/questions/15872543/access-remote-database-from-command-line) suggest that it something like: `mysql -u {username} -p{password} -h {remote server ip} {DB name}` – Jarek Potiuk Aug 13 '17 at 16:59
  • Yet another thing to check is if your mysql server actually listens to the 3306 port simply exec to your pod and check `netstat -an | grep 3306` and if you see no server listening to 3306 port then you probably have to configure mysql server to also listen on that port. As mentioned above - by default the connection between client and server for mysql is done via the unix socket, not via TCP/IP connection, so it's not necessarily enabled. – Jarek Potiuk Aug 13 '17 at 17:04
  • 1
    Thanks for the suggestion. The solution was to include the `--protocol tcp` argument. I looked for articles/issues on connecting with something else other than socket, and found an answer here (https://serverfault.com/questions/337818/how-to-force-mysql-to-connect-by-tcp-instead-of-a-unix-socket) which suggested using the `--protocol` argument. So running the following command works: `mysql --protocol tcp -uroot -p`. You can edit your answer to include this command and I will mark it as correct. – Gabriel Ruiu Aug 13 '17 at 17:24
  • Corrected. Glad it fixed your problem Gabriel – Jarek Potiuk Aug 13 '17 at 18:40