2016-03-17 59 views
4

我想dockerize mysql數據庫。我有.sh腳本準備環境,創建數據庫,用戶和其他東西。 Mysql dump restore命令不能在我的.sh腳本中工作,但是如果我打開容器shell和exec命令,它工作得很好。我想在我的.sh腳本中使用命令。爲什麼mysql備份沒有恢復到我的碼頭集裝箱?

我的腳本有什麼問題?

Dockerfile:

FROM alpine:latest 

WORKDIR /app 
COPY startup.sh /startup.sh 

COPY backup.sql /app/ 

RUN apk add --update mysql mysql-client && rm -f /var/cache/apk/* 
COPY my.cnf /etc/mysql/my.cnf 

EXPOSE 3306 

startup.sh:

#!/bin/sh 

if [ -d /app/mysql ]; then 
    echo "[i] MySQL directory already present, skipping creation" 
else 
    echo "[i] MySQL data directory not found, creating initial DBs" 

    mysql_install_db --user=root > /dev/null 

    if [ "$MYSQL_ROOT_PASSWORD" = "" ]; then 
    MYSQL_ROOT_PASSWORD=111111 
    echo "[i] MySQL root Password: $MYSQL_ROOT_PASSWORD" 
    fi 

    MYSQL_DATABASE=myDb 

    if [ ! -d "/run/mysqld" ]; then 
    mkdir -p /run/mysqld 
    fi 

    tfile=`mktemp` 
    if [ ! -f "$tfile" ]; then 
     return 1 
    fi 

    cat <<EOF> $tfile 
EOF 

    if [ "$MYSQL_DATABASE" != "" ]; then 
    echo "[i] Creating database: $MYSQL_DATABASE" 
    echo "FLUSH PRIVILEGES;" >> $tfile 
    echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;" >> $tfile 
    echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;" 

    echo "CREATE USER 'myuser'@'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile 
    echo "CREATE USER 'myuser'@'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" 

    echo "CREATE USER 'myuser'@'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile 
    echo "CREATE USER 'myuser'@'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" 

    echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost' WITH GRANT OPTION;" >> $tfile 
    echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost' WITH GRANT OPTION;" 

    echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'%' WITH GRANT OPTION;" >> $tfile 
    echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'%' WITH GRANT OPTION;" 


    echo "FLUSH PRIVILEGES;" >> $tfile 
    fi 

    /usr/bin/mysqld --user=root --bootstrap --verbose=0 < $tfile 
    rm -f $tfile 
fi 



exec /usr/bin/mysqld --user=root --console 

mysql -u root myDb < backup.sql # THIS LINE NOT WORKING! WHY ? 

回答

1

你沒有真正執行你的腳本容器啓動時。我不確定你想用這個做什麼,我不應該要求澄清,所以我會盡我所能回答,並希望這有助於。

嘗試類似:

Dockerfile

FROM alpine:latest 

WORKDIR /app 
COPY startup.sh /startup.sh 

COPY backup.sql /app/ 

RUN apk add --update mysql mysql-client && rm -f /var/cache/apk/* 
COPY my.cnf /etc/mysql/my.cnf 

EXPOSE 3306 

ENTRYPOINT ["/startup.sh"] 
CMD /bin/bash -c "/usr/bin/mysqld --user=root --console && mysql -u root myDb < backup.sql" 

然後,您可以修改你的啓動腳本是這樣的:

startup.sh

#!/bin/sh 

if [ -d /app/mysql ]; then 
    echo "[i] MySQL directory already present, skipping creation" 
else 
    echo "[i] MySQL data directory not found, creating initial DBs" 

    mysql_install_db --user=root > /dev/null 

    if [ "$MYSQL_ROOT_PASSWORD" = "" ]; then 
    MYSQL_ROOT_PASSWORD=111111 
    echo "[i] MySQL root Password: $MYSQL_ROOT_PASSWORD" 
    fi 

    MYSQL_DATABASE=myDb 

    if [ ! -d "/run/mysqld" ]; then 
    mkdir -p /run/mysqld 
    fi 

    tfile=`mktemp` 
    if [ ! -f "$tfile" ]; then 
     return 1 
    fi 

    cat <<EOF> $tfile 
EOF 

    if [ "$MYSQL_DATABASE" != "" ]; then 
    echo "[i] Creating database: $MYSQL_DATABASE" 
    echo "FLUSH PRIVILEGES;" >> $tfile 
    echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;" >> $tfile 
    echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;" 

    echo "CREATE USER 'myuser'@'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile 
    echo "CREATE USER 'myuser'@'localhost' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" 

    echo "CREATE USER 'myuser'@'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" >> $tfile 
    echo "CREATE USER 'myuser'@'%' IDENTIFIED BY '2E7A80BFD6Cwdct5q4i1r9l3';" 

    echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost' WITH GRANT OPTION;" >> $tfile 
    echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost' WITH GRANT OPTION;" 

    echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'%' WITH GRANT OPTION;" >> $tfile 
    echo "GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'%' WITH GRANT OPTION;" 


    echo "FLUSH PRIVILEGES;" >> $tfile 
    fi 

    /usr/bin/mysqld --user=root --bootstrap --verbose=0 < $tfile 
    rm -f $tfile 
fi 

exec "[email protected]" 

看看official mysql Docker image repoDockerfiledocker-entrypoint.sh的設置方式。