How to connect to my database on k8s using the "command" instruction of a readinessProbe?

11/19/2020

Friends, I am trying to implement a readinessProbe here like this:

readinessProbe:  
  exec:
    command: ["mysql", "-u root", "-p $(MYSQL_ROOT_PASSWORD)", "SHOW DATABASES;"]
    initialDelaySeconds: 5
    periodSeconds: 2
    timeoutSeconds: 1

But I am getting an access denied error: ERROR 1045 (28000): Access denied for user ' root'@'localhost' (using password: YES)

When I exec inside my pod, I can connect to the database normally so I think I am doing something wrong by the execution the the connection commands. Any idea how can I solve this problem?

-- marcelo
kubernetes
mysql
yaml

2 Answers

11/19/2020

It worked for me like this:

readinessProbe:
   exec:
      command:
      - "bash" 
      - "-c"
      - "mysql --user=${MYSQL_USER} --password=${MYSQL_PASSWORD} --execute=\"SHOW DATABASES;\""

But I still don't know how to "translate" this if I want to use brackets.

-- marcelo
Source: StackOverflow

11/20/2020

When you use JSON-array syntax for command: and similar options, you specify exactly how the command is split up into "words". So when the option says

<!-- language: lang-yaml -->
command: [..., "-u root", ...]

That is a single option containing an embedded space (I might say aloud "dash u space root"). In an interactive shell it'd be the equivalent of quoting the option mysql '-u root' ....

From your error message, it looks like the mysql client tool interprets the space as part of the user name; you don't want that. Either split it yourself into two separate options, or remove the space between the option and the argument.

<!-- language: lang-yaml -->
command: [..., "-u", "root", ...]
command: [..., "-uroot", ...]
-- David Maze
Source: StackOverflow