Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize backup and restore in Mysql Database

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces you how to achieve backup and restore in the Mysql database, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Import java.io.IOException

Import java.text.SimpleDateFormat

Import java.util.Date

Public class BackupMysql {

/ * * access the url where the MySQL server is located * /

Private String serverUrl

/ * * user name for accessing MySQL database * /

Private String username

/ * * password for accessing MySQL database * /

Private String password

Public String getServerUrl () {

Return serverUrl

}

Public void setServerUrl (String serverUrl) {

This.serverUrl = serverUrl

}

Public String getUsername () {

Return username

}

Public void setUsername (String username) {

This.username = username

}

Public String getPassword () {

Return password

}

Public void setPassword (String password) {

This.password = password

}

Public BackupMysql (String serverUrl,String username, String password) {

Super ()

This.serverUrl=serverUrl

This.username = username

This.password = password

}

Public String backup (String backupPath, String dbName) throws IOException {

String backupFile = backupPath+ dbName+ new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss") .format (new Date ()) + ".sql"

String = "mysqldump" + "--host=" + serverUrl+ "--user=" + username + "--password=" + password + "--opt" + dbName + ">" + backupFile

Java.lang.Runtime.getRuntime () .exec (cmd / c + mysql)

System.out.println ("backup successful!")

Return backupFile

}

Public void restore (String restoreFile, String dbName) throws Exception {

String mysql = "mysql" + "- h" + serverUrl+ "- u" + username + "- p" + password + "" + dbName + "<" + restoreFile

System.out.println (mysql)

Java.lang.Runtime.getRuntime () .exec (cmd / c + mysql)

System.out.println ("restore successful!")

}

/ * *

* @ param args

, /

Public static void main (String [] args) {

String serverUrl= "127.0.0.1"

String userName = "root"

String pwd = "root"

BackupMysql backup = new BackupMysql (serverUrl,userName, pwd)

Try {

Backup.restore ("d:/nationz2010-12-20-14-16-47.sql", "nationz")

} catch (Exception e) {

E.printStackTrace ()

}

}

}

On how to achieve backup and restore in the Mysql database to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report