mysql - 无法在 Windows 中执行 mysql 命令并在 mysql docker 中使用 $MYSQL_ROOT_PASSWORD

标签 mysql powershell docker

从 powershell 控制台,我设法在 mysql docker 中运行 mysql 命令:

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

但是如果我想用 MYSQL_ROOT_PASSWORD 替换我的密码,它就不再起作用了:

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)

powershell 似乎解释了 $MYSQL_ROOT_PASSWORD 并将其作为空传递给容器。

我尝试过的其他命令:

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

最佳答案

尝试使用-it,因为它会退出,因为您没有分配tty,并在密码前添加转义字符,否则,它将从主机查找环境变量,不在容器内。

docker exec -it mysql-container bash -c " mysql  -u root -p"\$MYSQL_ROOT_PASSWORD" -e 'show databases;'"

关于mysql - 无法在 Windows 中执行 mysql 命令并在 mysql docker 中使用 $MYSQL_ROOT_PASSWORD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59102665/

相关文章:

powershell - PSExec Pass参数带有Powershell空格

powershell - 使用参数 ("By Reference"参数从 PowerShell 返回对象)?

php - 避免往返 key 查找

php - 在虚拟名册上对用户进行分组

mysql - 如何从 MySQL 中提取唯一的日期(数据库中有数据)?

php - 使用 PHP 从 SQL 查询创建一个没有重复行的 HTML 表?

powershell - Amazon Web Service PowerShell 凭据设置错误

mysql - Docker中MySQL密码的最佳实践

linux - 如何强制所有centos终端dns查询到squid代理?

linux - 无法在 Azure 上的 Ubuntu 16.04 上连接 Percona Docker 镜像