0

EDIT: The following describes my original question, but as long as I haven't resolved the issue yet, I thought maybe I should change settings inside the created mysql POD instead of mysql config files on my local computer. But when I try to do any changes inside the mysql POD I get command not found errors!

I tried to deploy a mysql image on my local Kubernetes that is running on a Kind cluster as the following:

I tried kubectl create secret generic mysql-secret --from-literal MYSQL_KEY=11111 and created mysql-server as following:

mysql-secret Opaque 1 3d21h

This is mysql-pv.yaml file:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

I did kubectl apply -f mysql-pv.yaml and it created successfully.

This is `mysql-depl.yaml` file:
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
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: MYSQL_KEY
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None

I did kubectl apply -f mysql-depl.yaml and it created successfully.

But when I want to run mysql inside it's related pod using kubectl exec -it <mysql-pod-name> sh then mysql -p commands, it asks for the password and after entering the password(11111) I get:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

This is /etc/mysql/my.cnf content:

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

Inside /etc/mysql/conf.d/ there are two files. mysql.cnf as following:

[mysql]
socket  = /var/run/mysqld/mysqld.sock
[client]
socket  = /var/run/mysqld/mysqld.sock

And mysqldump.cnf as following:

[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

Also inside /etc/mysql/mysql.conf.d/ directory, there are two files, mysql.cnf :


#
# The MySQL database client configuration file
#
# Ref to https://dev.mysql.com/doc/refman/en/mysql-command-options.html

[mysql]
socket  = /var/run/mysqld/mysqld.sock
[client]
socket  = /var/run/mysqld/mysqld.sock

And mysqld.cnf:


#
# The MySQL database server configuration file.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket  = /var/run/mysqld/mysqld.sock
port        = 3306
datadir = /var/lib/mysql


# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
# tmpdir        = /tmp
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
#
# * Fine Tuning
#
key_buffer_size     = 16M
# max_allowed_packet    = 64M
# thread_stack      = 256K

# thread_cache_size       = -1

# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP

# max_connections        = 151

# table_open_cache       = 4000

#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
#
# Log all queries
# Be aware that this log type is a performance killer.
# general_log_file        = /var/log/mysql/query.log
# general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
# slow_query_log        = 1
# slow_query_log_file   = /var/log/mysql/mysql-slow.log
# long_query_time = 2
# log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
# server-id     = 1
# log_bin           = /var/log/mysql/mysql-bin.log
# binlog_expire_logs_seconds    = 2592000
max_binlog_size   = 100M
# binlog_do_db      = include_database_name
# binlog_ignore_db  = include_database_name

Moreover, I have the following files within /var/run/mysqld/:

mysqld.pid mysqld.sock mysqld.sock.lock mysqlx.sock mysqlx.sock.lock

best_of_man
  • 367
  • 1
  • 3
  • 12
  • I'm not reading a tutorial to guess at what you were doing because it was too much effort to write a good question. My best guess given the information in the question is you're trying to connect to your local MySQL instance instead of the Kubernetes hosted one. – Ginnungagap Dec 25 '22 at 06:47
  • @Ginnungagap: I try `kubectl exec -it sh` then inside the shell I try `mysql -p` and it asks for `password`, and after entering the password, it gives me the error message I have written in my question. – best_of_man Dec 25 '22 at 17:48
  • @Ginnungagap: Maybe I should add some environment variables like `MySQL_URI : mysql://user@localhost:3306/database` to `mysql` deployment file? – best_of_man Dec 25 '22 at 18:38
  • @Ginnungagap: I tried to fully edit my question and add all the needed information. – best_of_man Dec 27 '22 at 21:25

2 Answers2

0

I guess you forget password environment variable for mysql container. Please do a double check on this step.

  • I don't think so because I removed the previous `mysql` pod and also `mysql-secret` and recreated the secret via `kubectl create secret generic mysql-secret --from-literal MYSQL_KEY=11111` command and also updated the `mysql-depl.yaml` file and applied the configuration again. But I do get the same error. – best_of_man Dec 23 '22 at 22:20
0

This error can occur when the symlink is broken between the socket file (mysql.sock) on physical storage and /var/run/mysqld/mysqld.sock in the container.

Your physical storage is mounted to /var/lib/mysql so you'll find the mysql.sock file at /var/lib/mysql/mysql.sock in the container.

If so:

  1. exec into the mysql container to get a bash prompt

  2. run 'ln -s /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock'

  3. then try to run mysql -u -p ...etc to login

You may need to run 'flush hosts' if your mysql workbench is still unhappy.