日期:2014-05-17  浏览次数:21003 次

求mysql导出数据库的方法
在网上找的这种方法提示错误啊。

mysqldump -u 用户名 -p 数据库名 > 导出的文件名


mysql> mysqldump -u root -p shoppingbook > shoppingbook.sql;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
dump -u root -p shoppingbook > shoppingbook.sql' at line 1

我用的是GUI工具是MySQL Tools for 5.0。还有我怎么也找不到我数据库文件的位置啊?在mysql server文件夹下只有3个文件夹bin、lib和share。找不到我建的数据表啊?

------解决方案--------------------
我的
Java code
/**
      * 数据库备份实现方法
      * 
      * 
      */
 @Override
 public String backupMysql() {
  //备份文件存放路径   
  String sql =""; 
  //数据库名称 //读取XML得到数据库名称
  String databaseName = ""; 
     //  服务URL
  String address = ""; 
     //  备份文件存放路径
  String sqlpath = "";    
  //读取XML得到数据库用户名称
  String username = "";
  //读取XML得到数据库用户密码名称 
  String password = "";
  //备份服务地址
  address = "localhost"; 
  //数据库服务路径
  String url="";
    //获得服务发布的绝对路径
  String path = System.getProperty("user.dir"); 
  path = path.substring(0,path.lastIndexOf(“\\"))+"\\webapps\\BIMS\\";
  //读取applicationDataSource.xml文件 取出 数据库名,
  File f = new File(path+ "/WEB-INF/classes/configs/applicationDataSource.xml");
  SAXReader reader = new SAXReader();
  Document doc = null;
  try {
   doc = reader.read(f);
  } catch (DocumentException e1) {   
   log.debug(e1.getMessage());
  }
  //获得所有node节点集合
   Node node = doc.selectSingleNode("//*[@name='url']");
        Element e = (Element)node;
        url=e.attributeValue("value");
        
        node = doc.selectSingleNode("//*[@name='username']");
        e = (Element)node;
        username=e.attributeValue("value");

        node = doc.selectSingleNode("//*[@name='password']");
        e = (Element)node;
        password=e.attributeValue("value");  
  
  // 得到MYSQL的用户名密码后调用 mysql 的 cmd:
  String[] realp=path.split(":");
  //备份文件存放路径 目录名自己可以写
  sql = realp[0]+":"+"\\MySQlBimsBuckup\\";   
  String[] urls=url.split("/");
  //数据库名称 
  databaseName = urls[urls.length-1]; 
     //备份文件存放路径   
  sqlpath = sql;    
  address = "localhost";
  //文件路径
  File backupath = new File(sqlpath); 
  if (!backupath.exists()) {
   backupath.mkdir();
  }
  String time = StringUtil.dateToStr(new Date(), "yyyy-MM-dd");
  String filename = time + ".sql";
  StringBuffer sb = new StringBuffer(); 
  //拼接命令
  sb.append("mysqldump ");
  sb.append("--opt ");
  sb.append("-h ");
  sb.append(address);
  sb.append(" ");
  sb.append("--user=");
  sb.append(username);
  sb.append(" ");
  sb.append("--password=");
  sb.append(password);
  sb.append(" ");
  sb.append("--lock-all-tables=true ");
  sb.append("--result-file=");
  sb.append(sqlpath);
  sb.append(filename);
  sb.append(" ");
  sb.append("--default-character-set=utf8 ");
  sb.append(databaseName); 
  Runtime cmd = Runtime.getRuntime();
  try {
   Process p = cmd.exec("cmd /c " + sb.toString());
   InputStreamReader isr = new InputStreamReader(p.getInputStream());
   BufferedReader br = new BufferedReader(isr);
   FileOutputStream fos = new FileOutputStream(sqlpath + filename);
   OutputStreamWriter osw = new OutputStreamWriter(fos);
   BufferedWriter bw = new BufferedWriter(osw);
   String line = null;
   while ((line = br.readLine()) != null) {
    bw.write(line);
    bw.flush();
   }
   br.close();
   bw.close();
  //System.out.println("调用命令:" + sb.toString());
  } catch (IOException e1) {
   log.debug("",e1);//没做处理
  }