I have a MariaDB Docker container that I want to access automatically with the help of a bash script.
我有一个MariaDB Docker容器,我希望借助bash脚本自动访问它。
I'm using Docker compose:
我正在使用Docker撰写:
version: '3'
services:
drupal:
image: jonasvbogaert/php-docker:${IMAGE_VERSION}
container_name: drupalenv
ports:
- 8080:80
volumes:
- /var/www/html/
restart: always
environment:
DRUPAL_SITE_NAME: Drupal
DRUPAL_USER: admin
DRUPAL_PASS: admin
mariadb:
image: mariadb:latest
container_name: mariadbenv
restart: always
ports:
- 3036:3036
depends_on:
- drupal
environment:
MYSQL_ROOT_PASSWORD: ""
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
MYSQL_DATABASE: drupal`
The first command is to dive in the container (works fine):
第一个命令是潜入容器(工作正常):
docker exec -it mariadbenv bash
But the second one:
但第二个:
mysql
outputs the following error:
输出以下错误:
ERROR 1045 (28000): Access denied for user 'jonasvb'@'localhost' (using password: NO) the input device is not a TTY
ERROR 1045(28000):用户'jonasvb'@'localhost'拒绝访问(使用密码:NO)输入设备不是TTY
When I enter "mysql" myself, it works.
当我自己输入“mysql”时,它有效。
This is the script I use:
这是我使用的脚本:
function main () {
getUserInput
}
function fireDocker () {
if [ $DRUPAL_VERSION = "7" ]; then
IMAGE_VERSION="drupal7"
export IMAGE_VERSION
docker-compose up -d
mountDump
else
IMAGE_VERSION="drupal8"
export IMAGE_VERSION
docker-compose up -d
mountDump
fi
}
function getUserInput () {
echo "Enter Drupal version (7 or 8)"
read DRUPAL_VERSION # Read
fireDocker $DRUPAL_VERSION
}
function mountDump(){
docker exec -it mariadbenv bash
mysql
}
main
EDIT
编辑
When I execute the first command without -t flag. I have this:
当我执行第一个没有-t标志的命令时。我有这个:
And it stays like this.
它就像这样。
1 个解决方案
#1
1
You can run mysql commands in container using
您可以使用容器在容器中运行mysql命令
docker exec -i some_mysql_container mysql --user=root --password=root <<< "select database();"
Here the password and username should match with the one that is being used in the container.
这里的密码和用户名应该与容器中使用的密码和用户名相匹配。
A better approach in your cases would be to place all the dumps in the host and then map that host directory inside container at /docker-entrypoint-initdb.d
. For a better understanding how all dumps are imported in container you may look at official entrypoint.sh
of mariadb:latest
在您的情况下,更好的方法是将所有转储放在主机中,然后将该主机目录映射到容器中的/docker-entrypoint-initdb.d。为了更好地理解如何在容器中导入所有转储,您可以查看mariadb:latest的官方entrypoint.sh
L170 onwards:
L170起:
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
This enables users to place everything inside /docker-entrypoint-initdb.d/
and then
这使用户可以将所有内容放在/docker-entrypoint-initdb.d/中
- run bash scripts from there
- 从那里运行bash脚本
- run sql scripts
- 运行sql脚本
- restore archived databases
- 恢复存档的数据库
#1
1
You can run mysql commands in container using
您可以使用容器在容器中运行mysql命令
docker exec -i some_mysql_container mysql --user=root --password=root <<< "select database();"
Here the password and username should match with the one that is being used in the container.
这里的密码和用户名应该与容器中使用的密码和用户名相匹配。
A better approach in your cases would be to place all the dumps in the host and then map that host directory inside container at /docker-entrypoint-initdb.d
. For a better understanding how all dumps are imported in container you may look at official entrypoint.sh
of mariadb:latest
在您的情况下,更好的方法是将所有转储放在主机中,然后将该主机目录映射到容器中的/docker-entrypoint-initdb.d。为了更好地理解如何在容器中导入所有转储,您可以查看mariadb:latest的官方entrypoint.sh
L170 onwards:
L170起:
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
This enables users to place everything inside /docker-entrypoint-initdb.d/
and then
这使用户可以将所有内容放在/docker-entrypoint-initdb.d/中
- run bash scripts from there
- 从那里运行bash脚本
- run sql scripts
- 运行sql脚本
- restore archived databases
- 恢复存档的数据库