国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

mysql docker-entrypoint.sh分析

levy9527 / 3442人閱讀

摘要:其中從參數(shù)第個位置取字符,如為,則取字符,若條件為真,通過命令重置參數(shù),添加前綴,即經(jīng)過處理后變?yōu)椤?/p>

Docker Hub中有很多好用的Docker鏡像,但鏡像到底如何工作、能做什么、怎么做值得我們研究,如下所示為MySQL官方鏡像的docker-entrypoint.sh腳本分析:

#!/bin/bash
set -eo pipefail
shopt -s nullglob

################################################################
# 若啟動命令時附加了參數(shù),則在參數(shù)前添加mysqld,如$0 -f test,則經(jīng)過此代碼處理后,
# $@參數(shù)變mysqld -f test。其中${1:0:1}從$1參數(shù)第0個位置取1字符,如$1為-f,則
# 取"-"字符,若條件為真,通過set命令重置$@參數(shù),添加mysqld前綴,即經(jīng)過處理后$1變
# 為mysqld。
################################################################
# if command starts with an option, prepend mysqld
if [ "${1:0:1}" = "-" ]; then
    set -- mysqld "$@"
fi

# 解析參數(shù),是否是獲取幫助信息參數(shù),并設(shè)置wantHelp值
#####################################################
# skip setup if they want an option that stops mysqld
wantHelp=
for arg; do
    case "$arg" in
        -"?"|--help|--print-defaults|-V|--version)
            wantHelp=1
            break
            ;;
    esac
done

#############################
# 從文件中讀取變量值
#############################
# usage: file_env VAR [DEFAULT]
#    ie: file_env "XYZ_DB_PASSWORD" "example"
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
#  "$XYZ_DB_PASSWORD" from a file, especially for Docker"s secrets feature)
file_env() {
    local var="$1"
    local fileVar="${var}_FILE"
    local def="${2:-}"
    if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
        echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
        exit 1
    fi
    local val="$def"
    if [ "${!var:-}" ]; then
        val="${!var}"
    elif [ "${!fileVar:-}" ]; then
        val="$(< "${!fileVar}")"
    fi
    export "$var"="$val"
    unset "$fileVar"
}

###########################################################################
# 運行mysqld --help --verbose --help 2>&1 >/dev/null命令,
# 此命令會檢查配置文件,若配置文件沒問題,則成功,不成功則輸出錯誤信息,及if中添
# 加!取不成功。
###########################################################################
_check_config() {
        toRun=( "$@" --verbose --help )
        if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
                cat >&2 <<-EOM
                        ERROR: mysqld failed while attempting to check config
                        command was: "${toRun[*]}"
                        $errors
                EOM
                exit 1
        fi
}

# 1. $1參數(shù)為mysqld 以及 wanthelp 參數(shù)為空 以及root用戶,執(zhí)行此代碼;
# 2. _check_config檢查配置文件是否正確
# 3. 獲取DATADIR目錄,執(zhí)行mysqld --verbose --help --log-bin-index=/tmp/tmp.4SyApJWeIo| 
#         awk "$1 == """datadir""" { print $2; exit }"
# 4. 創(chuàng)建并修改目錄權(quán)限
# 5. 執(zhí)行exec gosu mysql docker-entrypoint.sh "$@",即重新以mysql用戶再次調(diào)用腳
#    本
# allow the container to be started with `--user`
if [ "$1" = "mysqld" -a -z "$wantHelp" -a "$(id -u)" = "0" ]; then
        _check_config "$@"
        DATADIR="$(_get_config "datadir" "$@")"
        mkdir -p "$DATADIR"
        chown -R mysql:mysql "$DATADIR"
        exec gosu mysql "$BASH_SOURCE" "$@"
fi

# 1. $1參數(shù)為mysqld 以及 wanthelp 參數(shù)為空,執(zhí)行此代碼,及exec gosu會執(zhí)行此代碼;
if [ "$1" = "mysqld" -a -z "$wantHelp" ]; then

# 2. 仍然檢查配置文件以及獲取datadir目錄
    # still need to check config, container may have started with --user
    _check_config "$@"
    # Get config
    DATADIR="$(_get_config "datadir" "$@")"

# 3. 若mysql數(shù)據(jù)庫未創(chuàng)建,則執(zhí)行本段邏輯
    if [ ! -d "$DATADIR/mysql" ]; then
# 4. 檢查是否設(shè)置變量,如root密碼、允許root密碼為空亦或者隨機密碼
        file_env "MYSQL_ROOT_PASSWORD"
        if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
            echo >&2 "error: database is uninitialized and password option is not specified "
            echo >&2 "  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD"
            exit 1
        fi
# 5. 創(chuàng)建目錄
        mkdir -p "$DATADIR"

# 6. 執(zhí)行mysqld命令初始化數(shù)據(jù)庫
        echo "Initializing database"
        "$@" --initialize-insecure
        echo "Database initialized"

# 7. command -v mysql_ssl_rsa_setup檢查命令是否可執(zhí)行,以及是否存在
# server-key.pem文件,若不存在,則生成證書
        if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then
            # https://github.com/mysql/mysql-server/blob/23032807537d8dd8ee4ec1c4d40f0633cd4e12f9/packaging/deb-in/extra/mysql-systemd-start#L81-L84
            echo "Initializing certificates"
            mysql_ssl_rsa_setup --datadir="$DATADIR"
            echo "Certificates initialized"
        fi

# 8. 獲取socket值并啟動mysql
        SOCKET="$(_get_config "socket" "$@")"
        "$@" --skip-networking --socket="${SOCKET}" &
        pid="$!"

# 9. 設(shè)置mysql變量(列表形式),而后可以${mysql[@]}調(diào)用
        mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )

# 10. 運行30次,驗證mysql是否已經(jīng)啟動完畢
        for i in {30..0}; do
            if echo "SELECT 1" | "${mysql[@]}" &> /dev/null; then
                break
            fi
            echo "MySQL init process in progress..."
            sleep 1
        done
# 11. 若i為0值,則表明mysql啟動失敗
        if [ "$i" = 0 ]; then
            echo >&2 "MySQL init process failed."
            exit 1
        fi

# 11. 解決時區(qū)bug
        if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
            # sed is for https://bugs.mysql.com/bug.php?id=20545
            mysql_tzinfo_to_sql /usr/share/zoneinfo | 
                         sed "s/Local time zone must be set--see zic manual page/FCTY/" | "${mysql[@]}" mysql
        fi

# 12. 生成root隨機密碼
        if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
            export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
            echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
        fi

# 13. 若MYSQL_ROOT_HOST不為空亦或者不為localhost,則創(chuàng)建root用戶
        rootCreate=
        # default root to listen for connections from anywhere
        file_env "MYSQL_ROOT_HOST" "%"
        if [ ! -z "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != "localhost" ]; then
            # no, we don"t care if read finds a terminating character in this heredoc
            # https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
            read -r -d "" rootCreate <<-EOSQL || true
                CREATE USER "root"@"${MYSQL_ROOT_HOST}" IDENTIFIED BY "${MYSQL_ROOT_PASSWORD}" ;
                GRANT ALL ON *.* TO "root"@"${MYSQL_ROOT_HOST}" WITH GRANT OPTION ;
            EOSQL
        fi

# 14. 為"root"@"localhost"重置root密碼
#     使用$rootCreate創(chuàng)建root
        "${mysql[@]}" <<-EOSQL
            -- What"s done in this file shouldn"t be replicated
            --  or products like mysql-fabric won"t work
            SET @@SESSION.SQL_LOG_BIN=0;
            SET PASSWORD FOR "root"@"localhost"=PASSWORD("${MYSQL_ROOT_PASSWORD}") ;
            GRANT ALL ON *.* TO "root"@"localhost" WITH GRANT OPTION ;
            ${rootCreate}
            DROP DATABASE IF EXISTS test ;
            FLUSH PRIVILEGES ;
        EOSQL

# 15. 已設(shè)置root密碼,故mysql需加上root密碼
        if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
            mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
        fi

# 16. 若配置了MYSQL_DATABASE變量,則創(chuàng)建
        file_env "MYSQL_DATABASE"
        if [ "$MYSQL_DATABASE" ]; then
            echo "CREATE DATABASE IF NOT EXISTS `$MYSQL_DATABASE` ;" | "${mysql[@]}"
            mysql+=( "$MYSQL_DATABASE" )
        fi

# 17. 在數(shù)據(jù)庫內(nèi)創(chuàng)建用戶
        file_env "MYSQL_USER"
        file_env "MYSQL_PASSWORD"
        if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
            echo "CREATE USER "$MYSQL_USER"@"%" IDENTIFIED BY "$MYSQL_PASSWORD" ;" | "${mysql[@]}"

            if [ "$MYSQL_DATABASE" ]; then
                echo "GRANT ALL ON `$MYSQL_DATABASE`.* TO "$MYSQL_USER"@"%" ;" | "${mysql[@]}"
            fi

            echo "FLUSH PRIVILEGES ;" | "${mysql[@]}"
        fi

# 18. 執(zhí)行/docker-entrypoint-initdb.d目錄下面的腳本,包含shell、sql
        echo
        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

# 19. 設(shè)置root密碼是否過期
        if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
            "${mysql[@]}" <<-EOSQL
                ALTER USER "root"@"%" PASSWORD EXPIRE;
            EOSQL
        fi

# 20. kill -s TERM "$pid" 殺掉mysql進程,執(zhí)行成功則返回0,而!kill取反,即kill成
#     功后才執(zhí)行后面的!wait命令
        if ! kill -s TERM "$pid" || ! wait "$pid"; then
            echo >&2 "MySQL init process failed."
            exit 1
        fi

# 21. 初始化成功后,再次啟動
        echo
        echo "MySQL init process done. Ready for start up."
        echo
    fi
fi

# 22. 正式啟動數(shù)據(jù)庫
exec "$@"

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/33177.html

相關(guān)文章

  • mysql docker-entrypoint.sh分析

    摘要:其中從參數(shù)第個位置取字符,如為,則取字符,若條件為真,通過命令重置參數(shù),添加前綴,即經(jīng)過處理后變?yōu)椤? Docker Hub中有很多好用的Docker鏡像,但鏡像到底如何工作、能做什么、怎么做值得我們研究,如下所示為MySQL官方鏡像的docker-entrypoint.sh腳本分析: #!/bin/bash set -eo pipefail shopt -s nullglob ###...

    PiscesYE 評論0 收藏0
  • mysql docker-entrypoint.sh分析

    摘要:其中從參數(shù)第個位置取字符,如為,則取字符,若條件為真,通過命令重置參數(shù),添加前綴,即經(jīng)過處理后變?yōu)椤? Docker Hub中有很多好用的Docker鏡像,但鏡像到底如何工作、能做什么、怎么做值得我們研究,如下所示為MySQL官方鏡像的docker-entrypoint.sh腳本分析: #!/bin/bash set -eo pipefail shopt -s nullglob ###...

    Richard_Gao 評論0 收藏0
  • 容器化開發(fā)環(huán)境 - 應(yīng)用和數(shù)據(jù)庫隔離

    摘要:上一篇容器化開發(fā)環(huán)境數(shù)據(jù)庫連接和遷移分享了關(guān)于如何給容器中的應(yīng)用創(chuàng)建數(shù)據(jù)庫連接,接下來將分享一下如何將數(shù)據(jù)庫從應(yīng)用容器中分離出來。 上一篇容器化開發(fā)環(huán)境 - 數(shù)據(jù)庫連接和遷移分享了關(guān)于如何給容器中的應(yīng)用創(chuàng)建數(shù)據(jù)庫連接,接下來將分享一下如何將數(shù)據(jù)庫從應(yīng)用容器中分離出來。 為什么要進行分離?當應(yīng)用和數(shù)據(jù)庫在同一個容器中運行時,也就意味著,應(yīng)用和數(shù)據(jù)庫的生命周期捆綁在了一起,應(yīng)用和數(shù)據(jù)庫的運...

    cppowboy 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<