简体   繁体   English

mysql源命令在docker容器中什么都不做

[英]mysql source command do nothing inside docker container

Description 描述

I'm running docker container with mysql in it and I want to run python script after mysql started, which will apply dump on it. 我正在运行带有mysql的docker容器,我想在mysql启动后运行python脚本,这将在其上应用dump。

Here is a snippet of Dockerfile : 这是Dockerfile的片段:

FROM mysql:5.6

RUN apt-get update && \
    apt-get install -y python

ADD apply_dump.py /usr/local/bin/apply_dump.py
ADD starter.sh /usr/local/bin/starter.sh

CMD ["/usr/local/bin/starter.sh"]

starter.sh : starter.sh

nohup python '/usr/local/bin/apply_dump.py' &
mysqld

apply_dump.py : apply_dump.py

import os
import urllib
import gzip
import shutil
import subprocess
import time
import logging
import sys

# wait for mysql server
time.sleep(5)
print "Start dumping"

dumpName = "ggg.sql"
dumpGzFile = dumpName + ".gz"
dumpSqlFile = dumpName + ".sql"

print "Loading dump {}...".format(dumpGzFile)
urllib.urlretrieve('ftp://ftpUser:ftpPassword@ftpHost/' + dumpGzFile, '/tmp/' + dumpGzFile)

print "Extracting dump..."
with gzip.open('/tmp/' + dumpGzFile, 'rb') as f_in:
    with open('/tmp/' + dumpSqlFile, 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

        print "Dropping database..."
        subprocess.call(["mysql", "-u", "root", "-proot", "-e", "drop database if exists test_db"])

        print "Creating database..."
        subprocess.call(["mysql", "-u", "root", "-proot", "-e", "create schema test_db"])

        print "Applying dump..."
        subprocess.call(["mysql", "--user=root", "--password=root", "test_db", "-e" "source /tmp/{}".format(dumpSqlFile)])
        print "Done"

content of ggg.sql.gz is pretty simple: ggg.sql.gz的内容非常简单:

CREATE TABLE test_table (id INT NOT NULL,PRIMARY KEY (id));

Problem 问题

Database created, but table is not. 数据库已创建,但表不是。 If I'll go to container and will run this script manually, table will be created. 如果我将转到容器并将手动运行此脚本,将创建表。 If I'll replace source command with direct sql create statement that will work as well. 如果我用直接的sql create语句替换source命令也可以。 But in reality dump file will be pretty big and only source command will cope with this (or not only it?). 但实际上转储文件会非常大,只有source命令才能应对这种情况(或者不仅仅是它?)。 Am I doing something wrong? 难道我做错了什么?

Thanks in advance. 提前致谢。

Try passing your source SQL file into the MySQL command like this, instead of using the -e flag: 尝试将源SQL文件传递到MySQL命令,而不是使用-e标志:

subprocess.call(["mysql", "--user=root", "--password=root", "test_db", "<", "/tmp/%s" % dumpSqlFile])

This will call import your SQL file using the widely used syntax: 这将使用广泛使用的语法调用导入您的SQL文件:

mysql --user=root --password=root test_db < /tmp/source.sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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