0

From powershell console, I manage to run mysql command inside my mysql docker:

docker exec mysql-container mysql -u root -psecret -e 'show databases;'

But if I want to replace my password by MYSQL_ROOT_PASSWORD, it doesn't work anymore:

docker exec mysql-container mysql -u root -p$MYSQL_ROOT_PASSWORD -e 'show databases;'
# ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

It seems powershell interprets $MYSQL_ROOT_PASSWORD and pass it as empty to the container.

Other commands I tried:

docker exec mysql-container sh -c "mysql -u root -p$MYSQL_ROOT_PASSWORD -e 'show databases;'"
# Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

docker exec mysql-container sh -c 'mysql -u root -p$MYSQL_ROOT_PASSWORD -e "show databases;"'
# ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
1

2 Answers 2

1

Try with -it as it will exit because you are not allocating tty, also add escape character before password, otherwise, it will look for environment variables from the host, not inside the container.

docker exec -it mysql-container bash -c " mysql  -u root -p"\$MYSQL_ROOT_PASSWORD" -e 'show databases;'"
Sign up to request clarification or add additional context in comments.

1 Comment

With your command I get Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO). Escaping the dollar doesn't seem to work.
0

I finally manage to get the good amount of quotes to make the command run:

docker exec mysql-container sh -c 'mysql -u root -p$MYSQL_ROOT_PASSWORD -e ''show databases;'''

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.