如何基于centos6在docker mysql容器上加载转储?

时间:2015-03-11 12:50:27

标签: mysql docker mysqldump

我正在尝试基于官方centos6图像创建我自己的mysql docker容器。我成功地构建和运行mysql容器。但是,我希望在容器启动时加载转储。现在这会产生错误:

" ERROR 1045(28000):拒绝用户访问'' localhost' "

请帮忙。

构建图片时我有以下配置加载:

#!/bin/bash

# Borrowed from https://github.com/docker-library/docker-mysql

set -e

if [ -z "$(ls -A /var/lib/mysql)" -a -n "$MYSQL_ROOT_PASSWORD" ]; then
        if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
                echo >&2 'error: database is uninitialized and MYSQL_ROOT_PASSWORD not set'
                echo >&2 '  Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?'
                exit 1
        fi

        scl enable mysql55 "mysql_install_db --user=mysql --datadir=/var/lib/mysql"

        # These statements _must_ be on individual lines, and _must_ end with
        # semicolons (no line breaks or comments are permitted).
        # TODO proper SQL escaping on dat root password D:
        cat > /tmp/mysql-first-time.sql <<-EOSQL
                DELETE FROM mysql.user ;
                CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
                GRANT ALL PRIVILEGES  ON *.* TO 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
                CREATE USER 'mysql'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
                GRANT ALL PRIVILEGES  ON *.* TO 'mysql'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
                CREATE USER ''@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
                GRANT ALL ON *.* TO ''@'%' IDENTIFIED BY IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
                DROP DATABASE IF EXISTS test ;
                commit;
                FLUSH PRIVILEGES ;
        EOSQL

        scl enable mysql55 "mysqld_safe --init-file=/tmp/mysql-first-time.sql &"
        sleep 5
        exec scl enable mysql55 "mysqladmin -uroot -p${MYSQL_ROOT_PASSWORD} shutdown"
fi

chown -R mysql:mysql /var/lib/mysql
exec scl enable mysql55 "mysqld_safe --datadir=/var/lib/mysql --user=mysql"

1 个答案:

答案 0 :(得分:0)

看起来你正试图在这些行中创建一个未命名的用户:

CREATE USER ''@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ; GRANT ALL ON *.* TO ''@'%' IDENTIFIED BY IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;

我不知道这在你的SQL引擎中是否有效,但我不认为它在MySQL中是合法的。