Jonas Van Bogaert
Jonas Van Bogaert

Reputation: 339

Run "mysql" command from bash script

I have a MariaDB Docker container that I want to access automatically with the help of a bash script.

I'm using Docker compose:

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

When I enter "mysql" myself, it works.

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: enter image description here

And it stays like this.

Upvotes: 0

Views: 1775

Answers (1)

Ayushya
Ayushya

Reputation: 10407

You can run mysql commands in container using

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

L170 onwards:

    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

  • run bash scripts from there
  • run sql scripts
  • restore archived databases

Upvotes: 1

Related Questions