如何在 Python 中附加到 Docker 容器然后执行 MySQL 命令?

时间:2021-01-20 22:01:02

标签: python bash docker docker-compose

我正在尝试通过 Python 脚本配置一些内容来实例化我的 Docker 容器。

#/bin/bash

# Permissions - These need sorting
chmod -R 777 example.co.uk

# Instance the container and build the OS
docker-compose up --build -d

# Initiate the Laravel App
docker exec app php artisan key:generate
docker exec app php artisan config:clear

# Add the Mysql user
docker exec mysql bash

我现在遇到的问题是 bash 返回容器 CLI 的新终端实例。我需要以某种方式附加到它并运行 mysql -u root -p,然后附加到它以输入密码并执行一些 SQL 命令。

运行 mysql -u root -p 会返回 mysql command not found,因为我不在返回的 bash CLI 中。然后我需要运行的 SQL 命令是:

GRANT ALL ON laravel.* TO 'laraveluser'@'%' IDENTIFIED BY 'somesecurepassword';
FLUSH PRIVILEGES;

我已经进行了大量研究,但我遇到的问题是我不知道我在寻找什么。任何帮助将不胜感激。

0 个答案:

没有答案