mysql 查询表相关语句

1、查询表结构
desc tablename;
show full columns from tablename;
select * from information_schema.columns where table_name=’tablename’;
2、查询表相关信息
select * from information_schema.tables where table_name=’tablename’;
3、查询列编码
select table_schema,table_name,column_name,character_set_name,collation_name from information_schema.columns where table_name=’tablename’;
4、查询表编码
select table_name,table_collation from information_schema.tables where table_name=’tablename’;

使用goldengate同步oracle

一、source端

add extract extl,tranlog,begin now
ADD EXTTRAIL /u01/ogg/dirdat/lr, EXTRACT EXTL
ADD TRANDATA chf.objce_t
edit params extl
extract extl
userid ogg,password xifenfei
exttrail /u01/ogg/dirdat/lr
dynamicresolution
gettruncates
table chf.objce_t;
ADD EXTRACT pump1, EXTTRAILSOURCE /u01/ogg/dirdat/lr, BEGIN now
add rmttrail /u01/ogg/dirdat/rl extract pump1
edit params pump1
extract pump1
userid ogg, password xifenfei
rmthost 192.168.1.111, mgrport 7809
rmttrail /u01/ogg/dirdat/rl
PASSTHRU
gettruncates
table chf.objce_t;

二、target端

edit params ./GLOBALS
CHECKPOINTTABLE ogg.chkpoint
dblogin userid ogg,password xifenfei
ADD CHECKPOINTTABLE ogg.chkpoint
add replicat repl exttrail /u01/ogg/dirdat/rl,begin now,checkpointtable ogg.chkpoint
edit  params repl
replicat repl
userid ogg,password xifenfei
assumetargetdefs
reperror default,discard
discardfile /tmp/repsz.dsc,append,megabytes 100
gettruncates
map chf.objce_t, target chf.objce_t;

使用goldengate同步mysql

一、source端

add extract extl,vam,begin now
ADD EXTTRAIL F:\ogg\mysql\dirdat\rl, EXTRACT EXTL
edit params extl
extract extl
sourcedb test@localhost:3306,userid root,password xifenfei
exttrail F:\ogg\mysql\dirdat\rl
dynamicresolution
gettruncates
table test.t_1;
add EXTRACT pump1, EXTTRAILSOURCE F:\ogg\mysql\dirdat\rl, BEGIN now
add rmttrail /ogg/mysql/dirdat/rl extract pump1
edit params pump1
extract pump1
rmthost 192.168.1.111,mgrport 7808
rmttrail /ogg/mysql/dirdat/rl
PASSTHRU
gettruncates
table test.t_1;

二、target端

edit params ./GLOBALS
dblogin sourcedb test@localhost:3306,userid root,password xifenfei
CHECKPOINTTABLE ogg.chkpoint
ADD CHECKPOINTTABLE ogg.chkpoint
add replicat repl exttrail /ogg/mysql/dirdat/rl,begin now,checkpointtable ogg.chkpoint
edit params repl
replicat repl
DBOPTIONS HOST 127.0.0.1, CONNECTIONPORT 3306
TARGETDB test,userid root,password xifenfei
assumetargetdefs
reperror default,discard
discardfile /tmp/mysql.dsc,append,megabytes 100
gettruncates
map TEST.T_1, target "test.t_1";

三、说明
本实例是win 中的mysql同步到linux中的mysql
有个注意点:
1、add extract extl,vam,begin now 不像oracle中的tranlog
2、注意各个参数中schema.tablename大小写问题
1)win中不区分,全部使用小写会自动转换为大写
2)在replicate参数中,因为map是从win中的data pump中得到,所以map后面的schema.tablename需要大写
3)在replicate参数中,因为table是linux中的对应linux中的数据库名和表名(mysql是以文件形式存储,一般均为小写),所以map后面的schema.tablename需要小写+双引号,防止转为大写
3、注意mysql数据库编码
四、与oracle不同之处
1、登录
dblogin sourcedb dbname@localhost:3306,userid root,password xifenfei
2、Extract中访问mysql
sourcedb dbname@localhost:3306,userid root,password xifenfei
3、Replicat中访问mysql
DBOPTIONS HOST 127.0.0.1, CONNECTIONPORT 3306
TARGETDB dbname,userid root,password xifenfei

goldedgate 初始化数据

一、mysql数据库初始化
1、source端配置

ADD EXTRACT EINI1, SOURCEISTABLE
EDIT PARAMS EINI1
EXTRACT EINI1
sourcedb test@localhost:3306,USERID root, PASSWORD xifenfei
RMTHOST 192.168.1.111, MGRPORT 7808
RMTTASK REPLICAT, GROUP RINI1
TABLE "test.web_statistics";

3、target端配置

ADD REPLICAT RINI1, SPECIALRUN
EDIT PARAMS RINI1
REPLICAT RINI1
ASSUMETARGETDEFS
DBOPTIONS HOST 127.0.0.1, CONNECTIONPORT 3306
TARGETDB test,userid root,password xifenfei
DISCARDFILE ./dirrpt/RINI2.dsc, PURGE
MAP "test.web_statistics", TARGET "test.web_statistics";

3、开启复制
start extract eini1
二、oracle数据库
1、source端配置

ADD EXTRACT EINI1, SOURCEISTABLE
EDIT PARAMS EINI1
EXTRACT EINI1
SETENV (NLS_LANG =AMERICAN_AMERICA.UTF8)
USERID ogg, PASSWORD "xifenfei"
RMTHOST 192.168.1.111, MGRPORT 7809
RMTTASK REPLICAT, GROUP RINI1
TABLE chf.init_obj;

2、target端配置

ADD REPLICAT RINI1, SPECIALRUN
EDIT PARAMS RINI1
REPLICAT RINI1
ASSUMETARGETDEFS
SETENV (NLS_LANG =AMERICAN_AMERICA.UTF8)
USERID ogg, PASSWORD xifenfei
DISCARDFILE ./dirrpt/RINI1.dsc, PURGE
MAP chf.init_obj, TARGET chf.init_obj;

3、开启复制
start extract eini1

ora-nnnnn 错误记录

一、ORA-00257解决
原因:空间不足错误
解决:检查数据文件和日志文件的存储空间,发现问题,进行添加相应数据文件或者对归档日志重新设置归档路径
注意:在10g及其以上版本中,可能是闪回日志文件(系统默认2g)不足
解决方案一:关闭闪回日志的功能
alter database flashback off;
解决方案二:增大闪回日志文件
alter system set DB_RECOVERY_FILE_DEST_SIZE=10g
解决方案三:修改闪回日志文件到一个大目录中
ALTER SYSTEM SET db_recovery_file_dest=’/u01/app/oracle/flash_recovery_area’
二、ORA-16038: 日志 3 序列号 5035 无法归档
ORA-19809: 超出了恢复文件数的限制
ORA-00312: 联机日志 3 线程 1:
……REDO03.LOG
当启动数据库时,有类此上述错误
原因:日志组3的数据文件损坏或丢失
解决:
1、查看alter文件
2、查询v$log视图,确定给日志文件是否归档
3、如果已经归档
用CLEAR命令重建该日志文件
alter database clear logfile group 3;
4、如果没有归档需要先归档
alter database clear unarchived logfile group 3;
5、启动数据库
alter database open;
三、ORA-12162: TNS:net service name is incorrectly specified
新装数据库,使用sqlplus连接,出现上述错误
原因:环境变量ORACLE_SID没有设置
解决vi .profile
export ORACLE_SID=orcl
四、TNS-12555: TNS: permission denied error solution
现象:安装11g r2 的grid中
TNS-12555: TNS: permission denied
TNS-12560: TNS: protocol adapter error
TNS-00525: Insufficient privilege for operation
Linux Error: 1: Operation not permitted
解决:
chmod 777 / var / tmp / .oracle
五、TNS-12547: TNS:lost contact
现象:TNS-12547: TNS:lost contact
TNS-12560: TNS:protocol adapter error
TNS-00517: Lost contact
Linux Error: 104: Connection reset by peer
解决:在/etc/hosts中添加
127.0.0.1 localhost

查询oracle中主外键列

一、查询表主键列

select column_name
  from dba_cons_columns
 where constraint_name in (select constraint_name
                             from dba_constraints
                            where table_name = upper('tablename')
                              and constraint_type = 'P');

二、查询表外键列

select column_name
  from dba_cons_columns
 where constraint_name in (select constraint_name
                             from dba_constraints
                            where table_name = upper('tablename')
                              and constraint_type = 'R');

三、查询表中列

select column_name
  from dba_tab_columns
 where table_name = upper('tablename');

四、查询表之间的主外键关系

select b.table_name  主键表名,
       b.column_name 主键列名,
       a.table_name  外键表名,
       a.column_name 外键列名
  from (select a.constraint_name,
               b.table_name,
               b.column_name,
               a.r_constraint_name
          from dba_constraints a, dba_cons_columns b
         WHERE a.constraint_type = 'R'
           and a.constraint_name = b.constraint_name) a,
       (select distinct a.r_constraint_name, b.table_name, b.column_name
          from dba_constraints a, dba_cons_columns b
         WHERE a.constraint_type = 'R'
           and a.r_constraint_name = b.constraint_name) b
 where a.r_constraint_name = b.r_constraint_name;

goldengate通用配置

一、理论
source

ADD EXTRACT <ext>, TRANLOG, BEGIN <time>, [, THREADS]
ADD EXTTRAIL <local_trail>, EXTRACT <ext>
EDIT PARAMS <ext>
-- Identify the Extract group:
EXTRACT <ext>
-- Specify database login information as needed for the database:
[SOURCEDB <dsn_1>,][USERID <user>[, PASSWORD <pw>]]
-- Specify the local trail that this Extract writes to:
EXTTRAIL <local_trail>
-- Specify tables to be captured:
TABLE <owner>.<table>;
ADD EXTRACT <pump_1>, EXTTRAILSOURCE <local_trail>, BEGIN <time>
ADD RMTTRAIL <remote_trail_1>, EXTRACT <pump_1>
EDIT PARAMS <pump_1>
-- Identify the data pump group:
EXTRACT <pump_1>
-- Specify database login information as needed for the database:
[SOURCEDB <dsn_1>,][USERID <user>[, PASSWORD <pw>]]
-- Specify the name or IP address of the first target system:
RMTHOST <target_1>, MGRPORT <portnumber>
-- Specify the remote trail on the first target system:
RMTTRAIL <remote_trail_1>
-- Allow mapping, filtering, conversion or pass data through as-is:
[PASSTHRU | NOPASSTHRU]
-- Specify tables to be captured:
TABLE <owner>.<table>;

target

edit params ./GLOBALS
CHECKPOINTTABLE <owner>.<tablename>
ADD REPLICAT <rep_1>, EXTTRAIL <remote_trail_1>, BEGIN <time> checkpointtable <owner>.<tablename>
edit params <rep_1>
-- Identify the Replicat group:
REPLICAT <rep_1>
-- State whether or not source and target definitions are identical:
SOURCEDEFS <full_pathname> | ASSUMETARGETDEFS
-- Specify database login information as needed for the database:
[TARGETDB <dsn_3>,] [USERID <user id>[, PASSWORD <pw>]]
-- Specify error handling rules:
REPERROR (<error>, <response>)
-- Specify tables for delivery:
MAP <owner>.<table>, TARGET <owner>.<table>[, DEF <template name>];

二、配置实例
source

add extract extl,tranlog,begin now
ADD EXTTRAIL /u01/ogg/dirdat/rl, EXTRACT EXTL
ADD TRANDATA chf.objce_t
edit params extl
extract extl
SETENV (NLS_LANG = "AMERICAN_AMERICA.ZHS16GBK")
userid ogg,password xifenfei
exttrail /u01/ogg/dirdat/rl
dynamicresolution
gettruncates
table chf.objce_t;
ADD EXTRACT pump1, EXTTRAILSOURCE /u01/ogg/dirdat/rl, BEGIN now
add rmttrail /u01/ogg/dirdat/rl extract pump1
edit params pump1
extract pump1
SETENV (NLS_LANG = "AMERICAN_AMERICA.ZHS16GBK")
userid ogg, password xifenfei
rmthost 192.168.1.111, mgrport 7809
rmttrail /u01/ogg/dirdat/rl
PASSTHRU
gettruncates
table chf.objce_t;

target

edit params ./GLOBALS
CHECKPOINTTABLE ogg.chkpoint
ADD CHECKPOINTTABLE ogg.chkpoint
add replicat repl exttrail /u01/ogg/dirdat/rl,begin now,checkpointtable ogg.chkpoint
edit repl
replicat repl
SETENV (NLS_LANG = "AMERICAN_AMERICA.ZHS16GBK")
userid ogg,password xifenfei
assumetargetdefs
reperror default,discard
discardfile /tmp/repsz.dsc,append,megabytes 100
gettruncates
map chf.objce_t, target chf.objce_t; 

ORACLE GOLDENGATE安装配置

Oracle GoldenGate软件基于数据库日志结构变化,通过解析源端在线日志或归档日志获得数据增量,再将这些变化应用到目标数据库,从而实现源库和目标库的数据同步。下面通过一个简单的示例,详细介绍利用GoldenGate实现Oracle数据库之间的同步。
1. 安装
1.1 下载介质
GoldenGate的安装介质可以从Oracle的官网上下载。
http://www.oracle.com/technetwork/middleware/goldengate/overview/index.html
1.2 配置GoldenGate用户
下载完成后将其拷贝到源和目标的相应位置解压完成后,即可以开始进行配置。
# su – oracle
$ mkdir /u01/ggate
$ cd /u01/ggate
$ tar xvf …….tar
注意,如果使用Oracle 11g的数据库,需要创建一个link文件。
$ ln -s /u01/app/oracle/product/11.2.0/db_1/lib/libnnz11.so –
/u01/app/oracle/product/11.2.0/db_1/lib/libnnz10.so
$ vi ~/.bash_profile
添加如下的内容:
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/u01/ggate
export GGATE=/u01/app/oracle/ggate
1.3 创建目录
使用ggsci工具,创建必要的目录。
$ cd /u01/app/oracle/ggate
$ ./ggsci
Oracle GoldenGate Command Interpreter for Oracle
Version 11.1.1.0.0 Build 078
Linux, x86, 32bit (optimized), Oracle 10 on Jul 28 2010 13:24:18
Copyright (C) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
GGSCI (gridcontrol) 1> create subdirs
至此,GoldenGate基本的安装完成。
Note. 此部分需要在源端和目标端完成。
2. 源数据库配置
GoldenGate主要通过抓取源端数据库重做日志进行分析,将获取的数据应用到目标端,实现数据同步。因此,为了让GoldenGate能够正常工作,源数据库需要进行一定配置。
2.1 归档模式、附加日志、强制日志

--查看
select db.LOG_MODE, db.SUPPLEMENTAL_LOG_DATA_MIN, db.FORCE_LOGGING
  from v$database db
--修改
--1)archivelog
shutdown immediate
startup mount
alter database archivelog;
alter database open;
--2)force logging
alter database force logging;
--3)supplemental log data
alter database add supplemental log data;

2.2 关闭数据库的recyblebin
alter system set recyclebin=off scope=spfile;
如果数据库是10g,需要关闭recyclebin并重启;或者手工purge recyclebin。
2.3 配置复制的DDL支持

create user ggate identified by ggate default tablespace users temporary tablespace temp;
grant connect,resource,unlimited tablespace to ggate;
grant execute on utl_file to ggate;
@$GGATE/marker_setup.sql;
@$GGATE/ddl_setup.sql;
@$GGATE/role_setup.sql;
grant GGS_GGSUSER_ROLE to ggate;
@$GGATE/ddl_enable.sql;

2.4 创建源端和目标端的测试用户

--source
create user sender identified by oracle default tablespace users temporary tablespace temp;
grant connect,resource,unlimited tablespace to sender;
--destination
create user receiver identified by oracle default tablespace users temporary tablespace temp;
grant connect,resource,unlimited tablespace to receiver;

3. 配置manager
在源端和目标端分别执行下面的步骤。
3.1 创建manager
[ggate@gridcontrol gg]$ ./ggsci
GGSCI (gridcontrol) 1> info all
Program Status Group Lag Time Since Chkpt
MANAGER STOPPED
GGSCI (gridcontrol) 2> edit params mgr
PORT 7809
ggate (gridcontrol) 3> start manager
Manager started.
4. 配置源端复制队列
GGSCI (gridcontrol) 1> add extract ext1, tranlog, begin now
EXTRACT added.
GGSCI (gridcontrol) 2> add exttrail /u01/ggate/dirdat/lt, extract ext1
EXTTRAIL added.
GGSCI (gridcontrol) 3> edit params ext1
extract ext1
userid ggate@source, password oracle
rmthost centos4, mgrport 7809
rmttrail /u01/ggate/dirdat/lt
ddl include mapped objname sender.*;
table sender.*;
GGSCI (gridcontrol) 6> info all
Program Status Group Lag Time Since Chkpt
MANAGER STOPPED
EXTRACT STOPPED EXT1 00:00:00 00:10:55
5. 配置目标端同步队列
5.1 在目标端添加checkpoint表
[oracle@centos4 ggate]$ ./ggsci
GGSCI (centos4) 1> edit params ./GLOBAL –添加下列内容
GGSCHEMA ggate
CHECKPOINTTABLE ggate.checkpoint
GGSCI (centos4) 2> dblogin userid ggate password ggate
Successfully logged into database.
GGSCI (centos4) 3> add checkpointtable ggate.checkpoint
Successfully created checkpoint table GGATE.CHECKPOINT.
5.2 创建同步队列
GGSCI (centos4) 4> add replicat rep1, exttrail /u01/ggate/dirdat/lt, checkpointtable ggate.checkpoint
REPLICAT added.
GGSCI (centos4) 5> edit params rep1
replicat rep1
ASSUMETARGETDEFS
userid ggate,password ggate
discardfile /u01/ggate/dirdat/rep1_discard.txt, append, megabytes 10
DDL
map sender.*, target receiver.*;
6. 开启同步
GGSCI (gridcontrol) 14> start extract ext1
GGSCI (gridcontrol) 15> info all
Program Status Group Lag Time Since Chkpt
MANAGER RUNNING
EXTRACT RUNNING EXT1 00:00:00 00:00:05
GGSCI (centos4) 7> start replicat rep1
GGSCI (centos4) 8> info all
Program Status Group Lag Time Since Chkpt
MANAGER RUNNING
REPLICAT RUNNING REP1 00:00:00 00:00:00

linux命令补充

find命令
当前路径下及所有子目录下查找文件名是netmgr的文件
# find . -name netmgr
在/usr及所有子目录下查找名字是vi的文件
# find /usr -name vi
在当前目录及所有子目录下查找文件名包含net的文件
# find ./ -name “net*”
在当前目录及所有子目录查找整个文件名或路径(包含路径)包含特定字符串rdbms 的文件或者路径
# find ./ -print |grep rdbms
查找当前目录及所有目录下的文件的文件内容中含有特定字符串
比如我们要查找当前目录下哪个文件的内容里含有good这个单词:
# find ./ -print|xargs grep -i good
查找1天内创建的文件。
find . -mtime -1 -print
查找大于10000字节的文件:
find . -size +10000c -print
sar命令
sar -u 2 5
每2秒显示一次cpu情况,显示5次
sar -w 5 5
5秒钟显示一次显示交换(swapping)活动,显示5次
iostat命令
iostat 3
iostat显示物理磁盘I/O情况,表明3秒钟显示一次。
nohup命令
nohup run_sql.ksh > logfile.lst 2>&1 &
上面这条命令中不光nohup,还有其他内容,让我们来了解一下各部分的含义:
nohup
将这个任务提交,让其持续运行,甚至你断开终端会话。
run_sql.ksh
指定想在后台中运行的Unix脚本
>logfile.lst
指定存放输出的文件名
2>&1
将标准错误信息输入到标准输出设备上。2代表标准错误信息。1代表标准输出设备。
&
tail命令
tail -f命令不断的显示输出文件中的新的行
tail -100命令显示最后100行
杀掉所有的oracle数据库进程
ps -ef|grep “ora_”|grep -v grep|awk ‘{print $2}’|xargs -i kill -9 {}

linux中oracle开机启动

#!/bin/bash
#chkconfig: 345 99 10
#description:oracle 开机自动运行
#*****************方法一(oradb文件使用)**********************
# 特点:复杂、功能完善、部署麻烦
# 一、使用root用户修改/etc/oratab 文件:
# $ vi /etc/oratab
# orcl:/u01/oracle:N (ORACLE_SID:ORACLE_HOME:N)
# 改为:
# orcl:/u01/oracle:Y 也就是将最后的N改为Y
# 二、使用Oracle用户修改$ORACLE_HOME/bin/dbstart和dbshut文件:
# ORACLE_HOME_LISTNER=$1
# 改为:
# ORACLE_HOME_LISTNER=$ORACLE_HOME
# 三、放置oradb于/etc/rc.d/init.d/
# 重命名该文件名为:oradb,复制到/etc/rc.d/init.d/中
# 四、组、权限、添加启动服务
# 修改所属组和用户
# chown oracle.oinstall /etc/rc.d/init.d/oradb
# 修改访问权限
# chmod 775 /etc/rc.d/init.d/oradb
# 添加服务到自动启动中
# chkconfig –add oradb
#------------添加关机自动关闭oracle的程序-----------
# 因为使用chkconfig配置的开机启动没有关闭系统时候关闭程序选项
# 需要人工设置K99oradb链接
# ln -s  /etc/rc.d/init.d/oradb  /etc/rc.d/rc0.d/K01oradb
#-------------------结束----------------------------
# 查看数据库服务开机启动状态
# chkconfig –list oradb
#--------------整体执行脚本(步骤四)---------------
# chown oracle.oinstall /etc/rc.d/init.d/oradb
# chmod 775 /etc/rc.d/init.d/oradb
# chkconfig –add oradb
# ln -s  /etc/rc.d/init.d/oradb  /etc/rc.d/rc0.d/K01oradb
# chkconfig –list oradb
#--------------------结束---------------------------
#*****************方法一(结束)**********************
#**************方法二(简单功能少)*******************
# 特点:简单、方便、不需要使用oradb文件
# 1、修改$ORACLE_HOME/bin下的dbstart文件,约在78行,
#    修改为ORACLE_HOME_LISTNER=$ORACLE_HOME
# 2、修改/etc/oratab文件,需要修改为正确的实例名
#    (格式为:$ORACLE_SID:$ORACLE_HOME:Y/N)
#    orcl:/u/oracle/product/10.2.0/db_1:Y
# 3、修改/etc/rc.local加入su - oracle -c 'dbstart'
#*****************方法二(结束)**********************
# description: Startup Script for Oracle Databases
# /etc/rc.d/init.d/oradb
export ORACLE_BASE=/u01
export ORACLE_HOME=/u01/oracle
export ORACLE_SID=orcl
export PATH=$PATH:$ORACLE_HOME/bin
ORA_OWNR="oracle"
# if the executables do not exist -- display error
if [ ! -f $ORACLE_HOME/bin/dbstart -o ! -d $ORACLE_HOME ]
then
echo "Oracle startup: cannot start"
exit 1
fi
# depending on parameter -- start, stop, restart
# of the instance and listener or usage display
case "$1" in
start)
# Oracle listener and instance startup
echo -n "Starting Oracle: "
su - $ORA_OWNR -c "$ORACLE_HOME/bin/dbstart"
touch /var/lock/subsys/oradb
su - $ORA_OWNR -c "$ORACLE_HOME/bin/emctl start dbconsole"
echo "OK"
;;
stop)
# Oracle listener and instance shutdown
echo -n "Shutdown Oracle: "
su - $ORA_OWNR -c "$ORACLE_HOME/bin/emctl stop dbconsole"
su - $ORA_OWNR -c "$ORACLE_HOME/bin/dbshut"
rm -f /var/lock/subsys/oradb
echo "OK"
;;
reload|restart)
$0 stop
$0 start
;;
*)
echo "Usage: `basename $0` start|stop|restart|reload"
exit 1
esac
exit 0