简体   繁体   中英

Running MYSQL command inside a shell script not working

I am trying to run a shell script with commands to run the Django Server.

#!/bin/bash

docker run -e MYSQL_ROOT_PASSWORD=root --name db_name -d mariadb

docker exec -it container_name mysql -u root -proot -e "create database db_name CHARACTER SET UTF8;  CREATE USER invuser@XXX.17.0.3 IDENTIFIED BY 'root1234$'; GRANT ALL PRIVILEGES ON db_name .* TO invuser@XXX.17.0.3 IDENTIFIED BY 'root1234$';"

#mysql -u root -proot -e "create database db_name CHARACTER SET UTF8;  CREATE USER invuser@XXX.17.0.3 IDENTIFIED BY 'root1234$'; GRANT ALL PRIVILEGES ON db_name .* TO invuser@XXX.17.0.3 IDENTIFIED BY 'root1234$'"


echo "Docker image name will be $1"

python3 manage.py makemigrations
python3 manage.py migrate

docker build . -t $1
docker run -d -p 8000:8000 $1

In this script, When I am trying to run:

docker exec -it container_name mysql -u root -proot -e "create database db_name CHARACTER SET UTF8;  CREATE USER invuser@XXX.17.0.3 IDENTIFIED BY 'root1234$'; GRANT ALL PRIVILEGES ON db_name .* TO invuser@XXX.17.0.3 IDENTIFIED BY 'root1234$';"

I am getting this error:

ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2)

OR when I run:

mysql -u root -proot -e "create database db_name CHARACTER SET UTF8;  CREATE USER invuser@XXX.17.0.3 IDENTIFIED BY 'root1234$'; GRANT ALL PRIVILEGES ON db_name .* TO invuser@XXX.17.0.3 IDENTIFIED BY 'root1234$'"

I am getting this error:

ERROR 2002 (HY000): Can't connect to MySQL server on 'XXX.17.0.2' (115)

But when I run this command out of the script, manually, It is working as expected and creating the database too and user too.

Can someone please suggest a better way to implement this via script? I am using Centos 7 btw.

Thanks

The docker exec is being executed before the container is fully initialized.

Its better to use the full range of container arguments to initialize the database and avoid the need to docker exec :

docker run -d --name db_name \
    -e MARIADB_DATABASE=db_name \
    -e MARIADB_USER=invuser \
    -e MARIADB_PASSWORD='root1234$' \
    mariadb

If you want a invuser@XXX.17.0.3 user strictly you can put RENAME USER invuser@'%' TO invuser@'XXX.17.0.3' in a /docker-entrypoint-initdb.d/rename.sql sql file passed in by volume.

To wait until the server is started up:

cid=db_name
waiting=${DOCKER_LIBRARY_START_TIMEOUT:-10}
echo "waiting to start..."
while [ $waiting -gt 0 ]
do
    (( waiting-- ))
    sleep 1
    if ! docker exec -i $cid mysql -h localhost --protocol tcp -P 3306 -e 'select 1' 2>&1 | fgrep "Can't connect" > /dev/null
    then
        break
    fi
done
if [ $waiting -eq 0 ]
then
    echo 'timeout'
    exit 1
fi

This will loop until a TCP connection is available.

source: mariadb container test script .

Hopefully MDEV-25434 HEALTHCHECK will get included sometime to make this process simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM