Create MySQL database using Ant

By xngo on June 18, 2019

To create MySQL database using Ant, do the followings:

  1. Save the XML code below in a file, e.g. create_database.xml.
  2. Change mydatabasename from the XML code below to the database name that you want.
  3. Run the following from Command prompt:
ant -f create_database.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="create_db" name="Create a database in mysql using Ant">
 
  <!-- The following properties need to be changed according to your environment. -->
  <property name="mysqladmin" value="C:\wamp\bin\mysql\mysql5.1.36\bin\mysqladmin.exe"/>
  <property name="mysql.username" value="root"/>
  <property name="mysql.pwd" value="mypassword"/>
  <property name="mysql.dbname" value="mydatabasename"/>
 
  <!-- mysqladmin is used to create a database. -->
  <target name="create_db">
    <exec executable="${mysqladmin}">
      <arg value="--user=${mysql.username}"/>
      <arg value="--password=${mysql.pwd}"/>
      <arg value="CREATE"/>
      <arg value="${mysql.dbname}"/>
    </exec>
  </target>
 
</project>

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.