您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

从MYSQL到oracle的迁移以及备份_MySQL

2024/6/17 18:19:34发布24次查看
bitscn.com 这几天做榆林政协的项目,因为关系到从mysql向oracle的迁移,所以,有了一些经验。
因为项目一开始就是用mysql开发的,所以没想到会出现的问题都出现了。
一、mysql可以导入导出脚本,本来应该准备好数据库的备份的初始化数据库和初始化数据的两个初始化脚本,经由mysql导出为一个full.sql的总的初始化脚本,用过mysql的都知道,导出的数据库脚本中,包含了建库、建表、以及插入数据的语句。需要分割成两个:建库、建表的初始化脚本和插入数据的初始化脚本。于是我写了一个类,用于分割sql脚本,在类中,我查找到create table开头的后面紧跟着的字符串,就是表的名字,取出insert开头的语句,改行就是插入数据的,于是,通过文件流读取文件的每一行,如果是插入的取出来放入一个方法convertinsertsql处理后写入一个脚本文件insert_data.sql中,剩下的写入一个脚本文件create_table.sql中, 方法convertinsertsql对于插入语句做处理,因为脚本中的插入语句是批量插入的,insert into 表名(列名) values(对应值),( 对应值),( 对应值),所以需要拆分成insert into 表名(列名) values (对应值)这样的语句,所以我通过将前面values(前的值截取后,对剩下的所有对应数据,进行通过),(用正则分割成string数组,对数组进行循环,每次追加组成插入语句,最后将插入语句全部返回后写入文件流。
package com.test.file;
import java.io.bufferedreader;
import java.io.bufferedwriter;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.outputstreamwriter;
import java.util.hashmap;
import java.util.map;
/**
 * 将mysql数据库导出的脚本
 * 分割成为建库和建表的两个脚本
 * @author gaofei
 *
 */
public class myfilereader {
/**
     * @param args
     */
    public static void main(string[] args) {
       string full_sql_path=c:/documents and settings/gaofei/桌面/最终mysql脚本(12.15).sql;
       string create_sql_path=c:/documents and settings/gaofei/桌面/建立数据库初始化.sql;
       string insert_sql_path=c:/documents and settings/gaofei/桌面/数据初始化.sql;
       try {
           readfull_to_insert(full_sql_path, create_sql_path, insert_sql_path);
       } catch (ioexception e) {
           system.out.println(文件读取或写入出错);
           e.printstacktrace();
       }
//     string aa=insert into `templatetype`(`id`,`templatetypename`,`deflong`,`defdate`,`defvar`) values (0,'通用模板类型',0,null,null),(1,'首页模板类型',0,null,null),(2,'栏目模板类型',0,null,null),(3,'专题模板类型',0,null,null),(4,'内容模板类型',0,null,null),(5,'留言模板类型',0,null,null),(6,'投票模板类型',0,null,null),(7,'特殊模板类型',0,null,null);;
//     string bb=full_to_part(aa);
//     system.out.println(bb);
    }
    /**
     * 将整体的导出的mysql脚本,拆分为建库建表和插入数据的脚本
     * 将其中的批量插入数据语句转换为每条插入数据语句的脚本
     * @param full_sql_path 原始全部导出的mysql脚本
     * @param create_sql_path 拆出来的建库建表的脚本
     * @param insert_sql_path 拆出来的插入数据脚本
     * @throws ioexception
     */
    private static void readfull_to_insert(string full_sql_path,string create_sql_path,string insert_sql_path) throws ioexception{
       file fullfile=new file(full_sql_path);
       file createfile=new file(create_sql_path);
       if(!createfile.exists())
           createfile.createnewfile();
       file insertfile=new file(insert_sql_path);
       if(!insertfile.exists())
           insertfile.createnewfile();
       inputstreamreader isr=new inputstreamreader(new fileinputstream(fullfile), utf-8);
       bufferedreader br=new bufferedreader(isr);
       outputstreamwriter osw_create=new outputstreamwriter(new fileoutputstream(createfile), utf-8);
       outputstreamwriter osw_insert=new outputstreamwriter(new fileoutputstream(insertfile), utf-8);
       bufferedwriter bw_create=new bufferedwriter(osw_create);
       bufferedwriter bw_insert=new bufferedwriter(osw_insert);
       map alldata=new hashmap();
       string line=null;
       int num=17;
       while((line=br.readline())!=null){
           string lowerline=line.tolowercase();
           if(lowerline.startswith(insert)){                         //在该语句下用来判断插入数据的先后顺序
              if(lowerline.indexof(`sequenceblock`)!=-1){
                  alldata.put(1, line);
              }else if(lowerline.indexof(`operationlogtype`)!=-1){
                  alldata.put(2, line);
              }else if(lowerline.indexof(`website`)!=-1){
                  alldata.put(3, line);
              }else if(lowerline.indexof(`fucdefine`)!=-1){
                  alldata.put(4, line);
              }else if(lowerline.indexof(`role`)!=-1){
                  alldata.put(5, line);
              }else if(lowerline.indexof(`department`)!=-1){
                  alldata.put(6, line);
              }else if(lowerline.indexof(`cmsuser`)!=-1){
                  alldata.put(7, line);
              }else if(lowerline.indexof(`account`)!=-1){
                  alldata.put(8, line);
              }else if(lowerline.indexof(`accountrole`)!=-1){
                  alldata.put(9, line);
              }else if(lowerline.indexof(`flowdefine`)!=-1){
                  alldata.put(10, line);
              }else if(lowerline.indexof(`flowtask`)!=-1){
                  alldata.put(11, line);
              }else if(lowerline.indexof(`rolefucperm`)!=-1){
                  alldata.put(12, line);
              }else if(lowerline.indexof(`templategroup`)!=-1){
                  alldata.put(13, line);
              }else if(lowerline.indexof(`templatetype`)!=-1){
                  alldata.put(14, line);
              }else if(lowerline.indexof(`template`)!=-1){
                  alldata.put(15, line);
              }else if(lowerline.indexof(`contenttype`)!=-1){
                  alldata.put(16, line);
              }else{
                  alldata.put(num++, line);
              }
           }else{
              bw_create.append(line+/r/n);
           }
       }
       for (int i = 1; i            if(alldata.containskey(i)){
              bw_insert.append(full_to_part(alldata.get(i)));
           }
       }
bw_create.flush();
       bw_insert.flush();
       br.close();
       bw_create.close();
       bw_insert.close();
    }
// private static void setsequence(){
//    
// }
/**
     * 将一行批量插入的数据转换为多行插入
     * @param line
     * @return
     */
    private static string full_to_part(string line){
       stringbuffer sb=new stringbuffer();
       string lowerline=line.tolowercase();
       int firstdan=lowerline.indexof(`);
       int firstquot=lowerline.indexof(();
       string tablename=lowerline.substring(firstdan, firstquot);
       system.out.println(--------------------------开始转换插入----+tablename+---的数据----------);
       int values_position=lowerline.indexof(values)+7;
       string forward_line=line.substring(0, values_position);
string datas_line=line.substring(values_position,line.length()-1); //得到后面插入的数据
       string[] datas=datas_line.split(//)//,//();//根据),(分割为一个字符串数组
for (int i = 0; i            string data=null;
           if(datas.length==1){            //如果只有一条数据,不会被分割的,数组就会只有一条数据
              data=datas[i];
           }else{
              if(i==0)                        //如果是第一条,那么后面需要追加一个括号
                  data=datas[i]+);
              else if(i==datas.length-1)         //如果是最后一条,需要在前面加一个括号
                  data=(+datas[i];
              else                     //如果是中间的数据,前后都需要加括号
                  data=(+datas[i]+);
           }
           sb.append(forward_line);           //将insert 字段名和values先行加入
           sb.append(data+;);
           sb.append(/r/n);
       }
       sb.append(/r/n);
       return sb.tostring();
    }
}
这是通用方法,以后需要得到mysql的两个初始化脚本就通过这个方法已转换就可以。
二、oracle建表的问题没有通过这个解决,可以通过hibernate逆向生成。建表和表结构,表关系,都有了。
下面就是插入数据了:
关于mysql的insert脚本基本上和oracle的一致,但是也有不一致,对于日期,对于一些符号’都是问题。将mysql的insert语句转换成oracle的insert语句。
1、需要将’去掉,这个符号是在插入数据时的表名上括起来的,所以,将表名上的’都替换掉。
2、需要将date日期,经过函数to_date(date,”yyyy-mm-dd”)转换一下,所以我通过正则表达式来查找到所有的日期,还有一种日期,携带时分秒的日期,需要另一个正则,最后当然都是替换成为年月日就可以了。
package com.sql.convert;
import java.io.bufferedreader;
import java.io.bufferedwriter;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.outputstreamwriter;
import java.util.regex.matcher;
import java.util.regex.pattern;
public class frommysqlinserttooracleinsert {
    /**
     * 需要转换一下地方:
     * 将所有的表名以及列名中的'去掉
     * @param args
     * @throws ioexception
     */
public static void main(string[] args) throws ioexception {
       string mysql_file=c:/documents and settings/gaofei/桌面/insertdata.sql;
        string oracle_file=c:/documents and settings/gaofei/桌面/oracle_insertdata.sql;
turnsql(mysql_file, oracle_file);
}
    private static void turnsql(string mysql_file,string oracle_file) throws ioexception{
       file mysqlfile=new file(mysql_file);
       file oraclefile=new file(oracle_file);
       if(!oraclefile.exists())
           oraclefile.createnewfile();
       inputstreamreader isr=new inputstreamreader(new fileinputstream(mysqlfile), utf-8);
       outputstreamwriter osw=new outputstreamwriter(new fileoutputstream(oraclefile), utf-8);
bufferedreader br=new bufferedreader(isr);
       bufferedwriter bw=new bufferedwriter(osw);
string line=null;
       while((line=br.readline())!=null){
           bw.append(convertstring(line));
           bw.append(/r/n);
       }
       bw.flush();
       br.close();
       bw.close();
       isr.close();
       osw.close();
    }
    private static string convertstring(string line){
       line=line.replace(`, );
       pattern p=pattern.compile('//d{4}//-//d+//-//d+');
       matcher m=p.matcher(line);
       string date=null;
       while(m.find()){
           date=m.group(0);
           line=line.replace(date, to_date(+date+,'yyyy-mm-dd'));
       }
       p=pattern.compile('//d{4}//-//d+//-//d+//s//d+//://d+//://d+');
       m=p.matcher(line);
       date=null;
       while(m.find()){
           date=m.group(0);
           string newdate=date.substring(0,date.indexof( ));
           line=line.replace(date, to_date(+newdate+','yyyy-mm-dd'));
       }
       return line;
    }
}
三、看似没有问题了,直接执行脚本就没问题了,但是问题又来了:
         我是直接用控制开的sqlplus来访问oracle的,没有工具,每次执行脚本,是通过@脚本名.sql来执行的,但是请注意如果是路径太长,比如@”c:/documents and settings/gaofei/桌面/oracle_insertdata.sql”路径中间有空格需要加上””的,
执行报错。很多错:
1、关于数据中的 等这类的特殊,oracle会认为是plsql编程中自定义的变量,当然会出问题了,所以需要设置set define off,关掉自定义变量。
2、一个问题很少会有人用到,就是关于oracle的插入数据,一个字段最多插入2999个字符。太多将会插入不进的。而我的数据都是模板或新闻,很多多是上万,所以问题来了。没有想到办法。最后我是将很多的数据删除的很短,插入成功后,通过程序一条条插入的。
迁移完成了 ,下面就是关于oracle的备份问题了。
说是可以准备sql脚本,但是脚本到时候还会是insert语句,到时候又不能插入了。
所以是通过oracle的备份命令,备份出dmp文件。
刚开始是用system用户直接备份的,但是会有很多的系统表,这是不符合要求的。于是建立一个用户future ,用future用户逆向生成表结构、表关系。然后通过赋予futuredba权限,通过insert into sequenceblock select * from system.sequenceblock;将所有表中有数据的表都从system导入到future用户中。
这样表就再future用户的指定表空间中建立出来了。
通过future直接备份数据。有两种备份方式。
$exp future/ylzxdb1219@ylzxdb file=d:/future_final.dmp full=y;
以future身份导出了所有库的信息
$exp future/ylzxdb1219@ylzxdb file=d:/future_user.dmp owner=future;
以future的身份导出了future用户中所有的数据。
但是future有dba权限,所以会再备份时将系统表备份。所以推荐第二种方式。导出的只是当前用户future的所有表的备份。
导入数据:
         $imp future/ylzxdb1219@ylzxdb fromuser=future touser=future ignore=y file=d:/future_user.dmp;
         (注:例句是将数据库的备份文件放在了d:盘下的future.user.dmp文件)
本文出自 “另类的自由” 博客 bitscn.com
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product