如何使用ant转储MySQL数据库?

时间:2009-09-11 13:15:18

标签: java mysql ant mysqldump

我找不到有关如何使用ant任务转储MySQL数据库的任何信息。

我是否必须创建自己的任务才能执行此操作?

ANT script ===generate==> myDataBase.sql

4 个答案:

答案 0 :(得分:22)

创建一个运行“mysqldump”命令的目标,如下所示:

<target name="dump-database">  
    <exec executable="mysqldump" output="database-dump.sql">  
        <arg value="--user=username" />  
        <arg value="--password=password" />  
        <arg value="--host=localhost" />  
        <arg value="--port=3306" />  
        <arg value="mydatabase" />  
    </exec>  
</target>  

现在您可以通过执行 ant dump-database

来进行转储

答案 1 :(得分:8)

使用ant导入某些sql文件,这也很有用:

    <exec executable="mysql" failonerror="true" input="file.sql">
        <arg value="-u ${user}" />  
        <arg value="-p${pass}" />  
        <arg value="-h ${host}" />  
        <arg value="-P 3306" />  
        <arg value="-D ${database}" />  
    </exec>

*请注意,正确的是 -ppassword ,而不是 -p密码

或:

    <exec executable="mysql" failonerror="true" input="file.sql">
        <arg value="--user=${user}" />  
        <arg value="--password=${pass}" />  
        <arg value="--host=${host}" />  
        <arg value="--port=3306" />  
        <arg value="--database=${database}" />  
    </exec>

它也很好,因为它不使用像org.gjt.mm.mysql.Driver这样的外部libs / sql驱动程序。

答案 2 :(得分:2)

您可以使用Exec任务,它将启动您的脚本,该脚本将执行转储(或其他)所需的所有操作。

答案 3 :(得分:0)

如果你想让数据驱动,请尝试使用ant sql任务:

<macrodef name="sql-retrieve-table-schema">
    <attribute name="schema-name"/>
    <attribute name="table-name"/>
    <attribute name="connection-url"/>
    <attribute name="output-file"/>
    <sequential>
        <sql userid="username" url="@{connection-url}"  classpathref="compile.classpath"
            password="apassword" driver="com.mysql.jdbc.Driver" print="true" output="@{output-file}"
            showheaders="false" showtrailers="false">
            SHOW CREATE TABLE @{table-name};
        </sql>
    </sequential>
</macrodef>
相关问题