0

I've been able to successfully connect to the MySQL database with connector/j, but I'm not sure where to go from here. I want to clone the database for use with a new sub domain and I'm just wondering if anyone could just give me a hint so I know which direction to go.

Any help would be much appreciated, thanks.

3
  • what do you mean by cloning a database in java? Do you want to create another database with the same schemas and such? If that's the case java has nothing to do with it. Commented Jul 19, 2011 at 18:55
  • 1
    Is there a reason you can't just use mysqldump to do this? Commented Jul 19, 2011 at 18:56
  • Yes, that's what I mean. Also, it has quite a lot to do with Java, as that's the language I'm programming it in. Unless MySQL dump works with java, then no, I can't use it. I'm trying to automate a series of processes, and cloning the MySQL database is only a small part of it. Commented Jul 19, 2011 at 18:56

2 Answers 2

5

By far the easiest way to clone a MySQL database is by using the standard admin tools: mysqldump dbname > dbname.sql to get a SQL dump, mysqladmin create clonedb to make the new database, mysql clonedb < dbname.sql to read in the SQL to the clone database.

If you want to do it through Java code, you're kinda making life a lot harder than it needs to be.

Sign up to request clarification or add additional context in comments.

1 Comment

Java is capable of executing command line processes with Runtime.getRuntime().exec(). This will let you run the mysql command directly, or you could even put the mysql commands into a shell or batch script and execute it like that.
0

I had the same problem.

So one solution is with Runtime.getRuntime().exec()

String command = "mysqldump -u <your login> --password=<your password> <database name to clone> | mysql -u root --password=<your password> <new database name>";

Process p = Runtime.getRuntime().exec(new String[]{"bash","-c",command});

Disadvantages: You need to store your password somewhere safely. It's not system independent.

Other solution (I think better one) is to run SQL file which will create your database schema. In mysql you can easily use mysqldump to generate schema for you.

Resource resource = new ClassPathResource(defaultDatabaseSchemaFilePath);
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(resource);
DataSource dataSource = multiTenantDataSource.getDataSource(dbName);
databasePopulator.execute(dataSource); 

Disadvantages: In your SQL file in the beginning you need to add SET FOREIGN_KEY_CHECKS=0; and in the end SET FOREIGN_KEY_CHECKS=1; as databasePopulator executes commands one by one and probably there will be foreign key check problem.

Hope it will help someone in the future.

1 Comment

That command works on linux/mac. Is there any solution for windows with this combination (direct backup and restore) ? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.