In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to back up and restore the MySQL database in Java. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Backup
The above page mentions that there are three forms of mysqldump commands, as follows:
Shell > mysqldump [options] db_name [tbl_name..] shell > mysqldump [options]-- databases db_name.. shell > mysqldump [options]-- all-databases
This command has a lot of options. For details, you can see the link above. I only need to back up one database here, and the command is relatively simple. If you can use the following command in the command line window, you can use the following command to backup:
Mysqldump-umysql-pmysql-- add-drop-database-- skip-lock-tables-B tjfinal > d:/backup.sql
If the-- add-drop-database parameter is not added and the user is not a root user, the following error may be reported:
Mysqldump: Got error: 1044: "Access denied for user 'mysql'@'localhost' to database' tjfinal'" when using LOCK TABLES
Java sample code:
/ * backup database * @ throws Exception * / public static void backup () throws Exception {String savePath = BACKUP_DIR + "backup-" + DateTimeTool.getDateByTime () + ".sql"; String [] execCMD = new String [] {"mysqldump", "- u" + DB_USER, "- p" + DB_PWD, DB_NAME, "- r" + savePath, "--skip-lock-tables"} Process process = Runtime.getRuntime () .exec (execCMD); int processComplete = process.waitFor (); if (processComplete = = 0) {System.out.println ("backup succeeded.");} else {throw new RuntimeException ("backup database failed.");}} restore
To restore the database, you need to use the source command of MySQL, which is used to read a file and execute the SQL statement. If you use the command line tool, you can log in to the MySQL client and execute the source command directly:
MariaDB [tjfinal] > source file_name-- or MariaDB [tjfinal] >\. File_name
Of course, it's OK if you don't log in to the client, then you need to use the-execute (or-e) option (option) of the mysql client command, as follows:
-- execute=statement-- or-e statement
The-- execute (or-e) option indicates that you want to log in to the client and then execute the SQL command to exit, and the parameter statement of this option is the SQL command to be executed. For a description of the mysql command options, please see here: http://dev.mysql.com/doc/refman/5.1/en/mysql-command-options.html#option_mysql_execute.
So you can restore the database using the following command without logging in to the MySQL client:
Mysql-umysql-pmysql-e source dburet back up. SQL source d:/backup.sql-or mysql-umysql-pmysql-execute= "source d:/backup.sql"
Java sample code:
/ * * restore database * @ param sql SQL file to be returned * @ throws Exception * / public static void restore (String sql) throws Exception {String targetFile = BACKUP_DIR + sql; / / SQL file path String [] execCMD = new String [] {"mysql", DB_NAME, "- u" + DB_USER, "- p" + DB_PWD, "- e source", targetFile}; Process process = Runtime.getRuntime (). Exec (execCMD) Int processComplete = process.waitFor (); if (processComplete = = 0) {System.out.println ("restore successful.");} else {throw new RuntimeException ("restore database failed.");} on how to back up and restore MySQL database in Java to share here, I hope the above content can be helpful 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.