MySQL import and export the entire database as a file

Source: Internet
Author: User
MySQL import and export the entire database as a file
  1. When using, be sure to note whether it is in the import state, if it is an import operation, you must first back up the database, otherwise the data will be emptied.
  2. /*
  3. Export
  4. SELECT * into OUTFILE ' c:/name.txt '
  5. Fields TERMINATED by ', ' optionally enclosed by ' \ '
  6. LINES TERMINATED by ' \ n '
  7. from zones;
  8. Import
  9. LOAD DATA INFILE ' c:/name.txt ' into TABLE zones
  10. Fields TERMINATED by ', ' optionally enclosed by ' \ '
  11. LINES TERMINATED by ' \ n ';
  12. */
  13. Set_time_limit (0);
  14. Define (HOST, ' 127.0.0.1 ');
  15. Define (USER, ' root ');
  16. Define (PWD, ");
  17. Define (DBNAME, ' test ');
  18. Define (DIR, ' e:/www/mysql_data/');
  19. 0 for import 1 for export 2 for repair
  20. Define (operation,1);
  21. Mysql_connect (HOST,USER,PWD) or
  22. Die ("Could Not connect:". Mysql_error ());
  23. mysql_select_db (DBNAME) or
  24. Die ("Could not select DB:". Mysql_error ());
  25. $result = mysql_query ("Show Tables");
  26. if (!is_dir (dir)) {
  27. Die (' Folder does not exist ');
  28. }
  29. if (operation==0) {
  30. Prerequisite: You need to import the table structure previously
  31. Export command: Mysqldump-u root-p-D test>test.sql
  32. while ($row = Mysql_fetch_array ($result, mysql_num)) {
  33. $fileName =dir. $row [0]. ". TXT ";
  34. if (file_exists ($fileName)) {
  35. Delete truncate
  36. $QUERYSQL = ' TRUNCATE TABLE '. $row [0]. ' `';
  37. mysql_query ($QUERYSQL) or
  38. Die ("Could not truncate TABLE:". $querySql. Mysql_error ());
  39. $QUERYSQL = "LOAD DATA INFILE '". $fileName. "'
  40. Into TABLE ' ". $row [0]." `
  41. Fields TERMINATED by ', ' optionally enclosed by ' \ '
  42. LINES TERMINATED by ' \ n ' ";
  43. mysql_query ($QUERYSQL) or
  44. Die (' Could not insert into table: '. $querySql. Mysql_error ());
  45. echo ' insert into table '. $row [0]. ' Success.
    ';
  46. }else{
  47. Echo ' not insert into table '. $row [0]. '
    ';
  48. }
  49. }
  50. echo "The task is finished";
  51. }else if (operation==1) {
  52. if (is_writable (DIR)) {
  53. while ($row = Mysql_fetch_array ($result, mysql_num)) {
  54. $fileName =dir. $row [0]. ". TXT ";
  55. if (file_exists ($fileName)) {
  56. Unlink ($fileName);
  57. }
  58. $QUERYSQL = "SELECT * into OUTFILE '". $fileName. "'
  59. Fields TERMINATED by ', ' optionally enclosed by ' \ '
  60. LINES TERMINATED by ' \ n '
  61. From '. $row [0]. " `";
  62. mysql_query ($QUERYSQL) or
  63. Die ("Could not dump Table:".) $querySql. Mysql_error ());
  64. echo ' Dump table '. $row [0]. ' Success.
    ';
  65. }
  66. echo "The task is finished";
  67. }else{
  68. Echo ' Folder '. DIR. ' Can not writeable ';
  69. }
  70. }else if (operation==2) {
  71. while ($row = Mysql_fetch_array ($result, mysql_num)) {
  72. $fileName =dir. $row [0]. ". TXT ";
  73. if (file_exists ($fileName)) {
  74. Unlink ($fileName);
  75. }
  76. $QUERYSQL = "REPAIR TABLE ' $row [0] '";
  77. mysql_query ($QUERYSQL) or
  78. Die ("Could not repair table:". $querySql. Mysql_error ());
  79. echo ' Repair table '. $row [0]. ' Success.
    ';
  80. }
  81. }
  82. Mysql_free_result ($result);
  83. Mysql_close ();
  84. ?>
Copy Code
  • 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.