Back up and restore a MySQL database using Java

Source: Internet
Author: User
First, let's talk about basic operations. This is not related to java. It only talks about mysql operations, but also about Windows operations, which are roughly the same in linux. 1. If mysqldump is used, you

First, let's talk about basic operations. This is not related to java. It only talks about mysql operations, but also about Windows operations, which are roughly the same in linux. 1. If mysqldump is used, you

First, let's talk about basic operations. This is not related to java. It only talks about mysql operations, but also about Windows operations, which are roughly the same in linux.

1. If mysqldump is used, you need to install the mysql server. If mysql clients are to be installed in linux, the server clients must be installed because we test on the local machine, mysql must be installed because it is operated on windows. If the installation is complete, no installation is required.

2. Set the path environment variable, that is, set the mysql installation directory/bin to the path environment variable.

3. Make sure that you can access the remote Mysql Server by entering the following command in cmd: mysql-uroot-p123456-hlocalhost, where-u is followed by the user name and-p is followed by the password, -h is followed by the IP address or host name of your mysql server. If it is a local machine, it doesn't matter if you do not write it. You can also write localhost. Of course, you can also write an ip address. We recommend that you create a database locally, create several tables and insert data. If you back up remote mysql, you must specify the IP address of the remote server.

4. if you want to back up remote MYSQL, you must find a way to connect to the mysql server in step 3. You can use Baidu to figure out how to remotely access the mysql server. If it is local, you only need to ensure that MYSQL is started, the username and password are correct.

5. If Step 3 is correct, open cmd and enter the following command mysqldump-uroot-p123456-hlocalhost dataname> d: \ mysql. in bak, "-u" is followed by the user name, "-p" is followed by the password, and "-h" is followed by your mysql remote server or local machine (this can be left unspecified on the local machine) the IP address dataname is the name of the database on your mysql server. d: \ mysql. bak is the full file path and can be specified at will. > Indicates backing up data to a file, while <indicates restoring the file to a database. If the data on the server is very important during testing, be careful to use the <symbol (it is a restoration command ).

6. After Step 5 is successfully executed, a file d: \ mysql. bak will be obtained, which is the backup file.

7. The recovery command is to change> in the command in step 5 to <.

II. The usage of mysqldump is mentioned above. The following describes how to use java for backup and restoration.

1. Make sure that your environment is correct, that is, the above commands can be successfully executed.

2. write one. bat file, such as creating a mysqlbak under the root directory of drive D. bat content: mysqldump-uroot-p123456-hlocalhost dataname> d: \ mysql. bak. This command is not explained. Run it first to check whether the backup file can be obtained correctly.

3. Make sure that the. bat file you just created can run correctly. Then, call the. bat file in java to obtain the backup file. The call method is as follows:

Java code

// Some methods to back up the. bat file for java.
Process p = runtime.getruntime(cmd.exe c ("d: \ mysqlbak. bat ");
P. waitFor ();

S = new callback (new FileInputStream ("d: \ xuchdata. bak "));

While (s. hasNext ())
{
System. out. println (s. next ());
}

System. out. println (p. exitValue ());

4. If Step 3 is correctly executed, the backup file is obtained

5. The first four steps are backup. The restoration idea is similar to this. You must first have a backup file and use java to execute the. bat file you wrote to restore the database.

In the end, the MySQL command is used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.