go-oci8 使用

Linux 下OCI 配置

  1. instant client
    这里 下载 instant client 的basic + sdk +顺便把sqlplus 也下了。
    目前能下到的最新的是这几个文件

    1
    2
    3
    -rw-r--r--.  1 root root 63352239 Jun  9 14:56 instantclient-basic-linux.x64-12.1.0.2.0.zip
    -rw-r--r--. 1 root root 667174 Jun 9 14:56 instantclient-sdk-linux.x64-12.1.0.2.0.zip
    -rw-r--r--. 1 root root 861284 Jun 10 11:18 instantclient-sqlplus-linux.x64-12.1.0.2.0.zip
  2. 把这几个文件解压出来全都放在一个文件夹里面就像这样

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    ls -l instantclient_12_1/
    total 193340
    -rwxrwxr-x. 1 root root 29404 Jul 7 2014 adrci
    -rw-rw-r--. 1 root root 440 Jul 7 2014 BASIC_README
    -rwxrwxr-x. 1 root root 43944 Jul 7 2014 genezi
    -r-xr-xr-x. 1 root root 342 Jul 7 2014 glogin.sql
    -rwxrwxr-x. 1 root root 6990875 Jul 7 2014 libclntshcore.so.12.1
    -rwxrwxr-x. 1 root root 58793741 Jul 7 2014 libclntsh.so.12.1
    -r-xr-xr-x. 1 root root 1768370 Jul 7 2014 libipc1.so
    -r-xr-xr-x. 1 root root 544150 Jul 7 2014 libmql1.so
    -r-xr-xr-x. 1 root root 6213011 Jul 7 2014 libnnz12.so
    -rwxrwxr-x. 1 root root 2576030 Jul 7 2014 libocci.so.12.1
    -rwxrwxr-x. 1 root root 109549133 Jul 7 2014 libociei.so
    -r-xr-xr-x. 1 root root 156353 Jul 7 2014 libocijdbc12.so
    -r-xr-xr-x. 1 root root 337137 Jul 7 2014 libons.so
    -rwxrwxr-x. 1 root root 118491 Jul 7 2014 liboramysql12.so
    -r-xr-xr-x. 1 root root 1564082 Jul 7 2014 libsqlplusic.so
    -r-xr-xr-x. 1 root root 1546540 Jul 7 2014 libsqlplus.so
    -r--r--r--. 1 root root 3692096 Jul 7 2014 ojdbc6.jar
    -r--r--r--. 1 root root 3698857 Jul 7 2014 ojdbc7.jar
    drwxrwxr-x. 5 root root 4096 Jul 7 2014 sdk
    -r-xr-xr-x. 1 root root 9581 Jul 7 2014 sqlplus
    -rw-rw-r--. 1 root root 444 Jul 7 2014 SQLPLUS_README
    -rwxrwxr-x. 1 root root 227410 Jul 7 2014 uidrvci
    -rw-rw-r--. 1 root root 71202 Jul 7 2014 xstreams.jar
  1. 安装

    1
    2
    3
    mv instantclient_12_1/ /usr/local
    cd /usr/local/instantclient_12_1
    ln -s libclntsh.so.12.1 libclntsh.so
  2. 配置 oci8.pc

    1
    2
    vim /usr/lib/pkgconfig/oci8.pc
    echo export PKG_CONFIG_PATH=/user/lib/pkgconfig >>/etc/profile

判断是否oci8已经安全成功

1
pkg-config --list-all | grep oci8

  1. 环境变量
    1
    2
    3
    4
    5
    6
    7
    vim ~/.bash_profile
    加入以下内容:
    export ora_home=/usr/local/instantclient_12_1
    export PATH=$PATH:$ora_home
    export LD_LIBRARY_PATH=$ora_home
    使之生效:
    source ~/.bash_profile

连接oracle 测试

  1. 简易连接
    不需要更多的配置文件

    1
    sqlplus user/pwd@ip:1521/test
  2. 通过tnsnames 连接

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    vim $ora_home/tnsnames.ora
    填入以下内容:
    test =
    (DESCRIPTION =
    (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = ip或主机名称)(PORT = 1521))
    )
    (CONNECT_DATA =
    (SERVICE_NAME = orcl)
    )
    )
    然后,
    sqlplus user/pwd@test

连接可能会出现ORA-21561 错误,下面有解决办法。

go-oci8

以上步骤完成后:

1
go get github.com/mattn/go-oci8

如果没有报错就说明安装成功了。

go-oci8 测试

测试例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
"database/sql"
"fmt"
_ "github.com/mattn/go-oci8"
"log"
)

type Users struct {
ID int
}

func main() {
log.Println("Oracle Driver Connecting....")
//用户名/密码@实例名 如system/123456@orcl、sys/123456@orcl
db, err := sql.Open("oci8", "user/pwd@226w")
if err != nil {
log.Fatal(err)
panic("数据库连接失败")
} else {
fmt.Println("Connect success!")
defer db.Close()
var users []Users = make([]Users, 0)
rows, err := db.Query("select 1 ID from dual")
if err != nil {
log.Fatal(err)
} else {
for rows.Next() {
var u Users
rows.Scan(&u.ID)
users = append(users, u)
}
fmt.Println(users)
defer rows.Close()
}
}
}

测试例2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main

import (
"database/sql"
"flag"
"fmt"
_ "github.com/mattn/go-oci8"
"log"
"strings"
)

var conn string

func main() {
flag.Parse()
if flag.NArg() >= 1 {
conn = flag.Arg(0)
} else {
conn = "user/pwd@226W"
}

db, err := sql.Open("oci8", conn)
if err != nil {
fmt.Println("can't connect ", conn, err)
return
}
if err = test_conn(db); err != nil {
fmt.Println("can't connect ", conn, err)
return
}
var in string
var sqlquery string
fmt.Print("> ")
for {
fmt.Scan(&in)
if in == "q;" {
break
}
if in[len(in)-1] != ';' {
sqlquery += in + " "
} else {
sqlquery += in[:len(in)-1]
rows, err := db.Query(sqlquery)
if err != nil {
fmt.Println("can't run ", sqlquery, "\n", err)
fmt.Print("> ")
sqlquery = ""
continue
}
cols, err := rows.Columns()
if err != nil {
log.Fatal(err)
}
fmt.Println(strings.Join(cols, "\t"))
var result = make([]string, len(cols))
var s = make([]interface{}, len(result))
for i, _ := range result {
s[i] = &result[i]
}
for rows.Next() {
rows.Scan(s...)
fmt.Println(strings.Join(result, "\t"))
}
rows.Close()
fmt.Print("> ")
sqlquery = ""
}
}

db.Close()
}

func test_conn(db *sql.DB) (err error) {
query := "select * from dual"
_, err = db.Query(query)
return err
}

注1:
上面open 的第二个参数改成简易连接也是可以的。

各种错误

  1. undefined reference to `lxgcvpc’
    在 go get github.com/mattn/go-oci8 时出现这个错:
    1
    2
    3
    4
    5
    6
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libclntsh.so: undefined reference to `lxgcvpc'
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libclntsh.so: undefined reference to `lnxnftu'
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libclntsh.so: undefined reference to `lbivor'
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libclntsh.so: undefined reference to `lemgpd'
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libclntsh.so: undefined reference to `lfimkpth'
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/libclntsh.so: undefined reference to `ssOswPclos

这说明,你的lib库有错, 你需要

1
source .bash_profile

  1. ORA-21561
    在连接oracle 时, 如果你遇到这个错,你只需要:
    1
    2
    3
    echo hostname> /etc/hostname
    vim /etc/hosts
    127.0.0.1 hostname

参考

http://usr.cc/thread-57972-1-1.html
http://www.cnblogs.com/ghj1976/p/3512555.html

用rsync取代各种第三方同步工具计划

为什么?

  1. 同步冲突无法解决 vs 灵活覆盖规则
    由于第三方同步软件基于最近修改时间,为最新副本的自动同步规则,不够灵活,很容易造成冲突。就像下面这样。 使用rsync我们可以指定哪个副本为最新,哪个可以丢弃。
  2. 实时自动同步 vs 手工同步
    第三方同步软件,在发现文件一发生修改后,就会马上进行同步,这不仅耗有限的带宽,还消耗内存,CPU及续航。rsync则可以避免这些问题。
  3. 放在别人服务器上 vs 放在自己的服务器上
    使用第三方同步软件,需要将您的文件上传到他们的服务器上。看起来不太安全。用rsync, 服务器是自己的, 传输走ssh。

    麻烦事

  4. 要学习一些geekly things,
    rsync 是个强大的linux 命令行工具,使用可能没有windows程序习惯。
  5. 手工同步
    现在同步要手工作做了。是有点麻烦。
  6. 大文件如果一次同步没那么快
    如果文件多了,一次同步,可能没有实时同步快。

基础使用

  1. 本地两文件夹互相同步
    1
    2
    rsync -zvrt -delete --progress Apps/ /e/Apps
    rsync -zvrt -delete --progress /e/Apps/ Apps/

t: 同步时,保证两边时间戳一样
z: 同步时使用压缩
v: 罗嗦模式
r: 递归同步
-delete: 同步本地已经删除文件
—progress: 显示进度

  1. 本地同步远程 && 远程同步到本地
    在根目录前加host就行 syn@comip.chinatcc.com:2074
    1
    rsync -zvrt -delete --progress Apps/ syn@comip.chinatcc.com:2074/e/Apps

典型用法

  1. backup

    1
    rsync -zvrt --delete -e 'ssh -p 2074' --progress Apps/ syn@comip.chinatcc.com:~/Apps
  2. recover

    1
    rsync -zvrt --delete -e 'ssh -p 2074' --progress syn@comip.chinatcc.com:~/Apps/ Apps/
  3. ignore updated and recover

    1
    rsync -zvrt --delete -e -u 'ssh -p 2074' --progress syn@comip.chinatcc.com:~/Apps/ Apps/

注意1: 这里~/Apps 跟~/App/ 的意义是不一样的。 ~/Apps 指从Apps这个文件夹开始同步包含这个Apps这个目录, ~/Apps/ 指的是从Apps这个文件的子文件夹开始同步, 不包含这个目录。

同步模型

  1. when arrive at office:
    1
    work pc <---recover--- FileServer

or
doing some emerge things, and some files of work pc may change,
to discard changes:

1
work pc <---recover from--- FileServer

to save changes:

1
work pc <---ignore updated and recover from--- FileServer

注1: 这种情况下如果工作电脑上的文件,昨天晚上在家里的PC也修改过那家中的修改就只能被丢弃。
注2: 这种逻辑在回家后的流程类似不多写。

  1. befor leaving office

    1
    work pc -> FileServer
  2. when arrive home

    1
    home pc <---recover from--- FileServer
  3. befor go to sleep

    1
    home pc ---backup to---> FileServer

有模型,我们基本实现了同步软件的功能的大部分功能。 如果你需要多版本功能支持,可以考虑,每天在server上将要同步的文件夹打包,然后做一个crontab每天备份,如果你还需要云端存储来保护你的数据,可以考虑将这个压缩包上传到云盘。担心隐私的话,打包的时候再加个密。

实用脚本

利用上面的脚本我写了几个,脚本方便使用。脚本在这里 。注意替换相应路径,我用来同步平时用的几个小程序。这里面我使用了windows下的rsync,详细在项目有。 bat 脚本在wndows下使用,sh 脚本在linux 下使用。

使用截图:
下载后路径:

开始同步:

参考:
http://os.51cto.com/art/201009/225962.htm
http://zh.wikipedia.org/zh/Rsync
http://baiqiuyi.com/linux/rsync-ssh-difference-port.html

用解压离线安装包的方式更新chrome

  1. 昨天用离线安装包自动更新chrome 没有成功(40.0->42 ), 于是尝试手动更新。

  2. 下载 离线安装包
    在搜索引擎上搜 “chrome 42 离线安装包“ 即可

  3. 用7zip 解压

  4. 继续解压

  1. 最后得到

  2. 找到chrome.exe 的位置
    我的是在C:\Program Files (x86)\Google\Chrome\Application。将上一步得到的两个文件夹拷贝过去, chrome.exe 覆盖

  3. 重启机器,大功告成!

不用证书较为安全的免密码ssh代理

新建一个低权限账号:

1
useradd -M -s /sbin/nologin -n vpn01

这个账号只能用来ssh连接无登录权限.

清除这个账号的密码

1
2
cat /etc/passwd
vpn01:!!:16341:0:99999:7:::

删掉那两个

修改sshd 配置

1
2
3
4
5
6
/etc/ssh/sshd_config:
PermitEmptyPassw0rds yes
/etc/ini.d/sshd restart

cat /etc/ssh/sshd_config | grep Gate
GatewayPorts yes

客户端执行:

1
ssh.exe -N -p  22 -g -C -c 3des -o StrictHostKeyChecking=no -o TCPKeepAlive=yes -o ServerAliveInterval=30 vpn01@avalon.pw -R 3388:localhost:3389
  1. 如果你需要在本地打开一个代理端口可以使用
1
ssh -v -v -N -p 22 -g -C -c 3des vpn01@avalon.pw -D 0.0.0.0:1080
  1. 如果你需要将3388端口绑定到非localhost 你需要:
    这样你就把本地的3389 端口映射到服务器的3388端口了。

交叉编译

Linux

先执行一次生成目标系统相应的库:

1
2
cd $GOROOT/src
CGO_ENABLED=0 GOOS=[ linux | windows ... ] GOARCH=[ amd64 | 386 ] ./make.bash

比如:
编译32位windows程序

1
2
cd $GOROOT/src
CGO_ENABLED=0 GOOS=windows GOARCH=386 ./make.bash

以后编译的时候用:

1
CGO_ENABLED=0 GOOS=[ linux | windows ... ] GOARCH=[ amd64 | 386 ] go build *

比如:

1
CGO_ENABLED=0 GOOS=windows GOARCH=386  go build *

注1:这种方式不支持CGO

Windows

下面以在windows64下生成windows32程序为例
生成目标系统相关的库

1
2
3
4
5
cd %GOROOT%/src
set CGO_ENABLED=0
set GOOS=windows
set GOARCH=386
call make.bat

要编译的时候保证环境变量里有下面的环境变量就行:

1
2
3
CGO_ENABLED=0
GOOS=windows
GOARCH=386

注1: 需要mingw环境
golang 下各种OS各种架构应该是可以互相交叉编译,只试了linux, windows其它没测。

Summary of the second half year of 2011

The time of second half year of 2011 in ZhuHai is important to me. I learn a lot and grown a lot in that time.

The Job

I quit the job. Why? I told my boss that I want to take a look the outside world, a different company, a different environment maybe what I need. I told my friends that I feel tired and just want to have a rest. But, later on, I know the really thing I want is time, free time. Free time means the free.

My Ambition

I have time now, So, I deeply think a lot of things. Soon, few days past. At some moment, I found the physic world is too reasonable, just like a huge machine drives by gears; the age of the world is so long, even to the whold history time of the human race no more than a spark. However long our life and no mater how much things we do the world will not change anything for us. In some case, seems we are stand still on this world. So, my ambition to change the world failed, sad. If I have no goal where can I go? Compare my ambition with others, noting interested me. Everything is meanness for me.! Ha-ha, my heart is empty! That is really great feeling. With the thought want nothing, I found a interesting thing to do, the mental world of human!

What is Knowledge?

An empty heart make me happy all the time, but I doubt this is a good living way. The world must have something worth for me to pursuit.

There are some questions that you may asked before, but no one can answer them, I call them the ultimate questions. The
questions may look like:

Where do we come from?
Who really am I?
What is the purpose of the world?

We are looking for answers of these questions for thousand of years, may even more long. we build kinds of philosophies base, and create science. Toady, these question still unknown. We need more and more knowledge, more knowledge means more time. How long, God knows!

What is the knowledge? Knowledge is: the facts that they really exist; the rule that control the world; the theory that works at everywhere and in any time. History generates the fact; science generates is the rule; mathematics generates the theory. Mathematics knowledge is the pure true knowledge.

I am looking for the truth of the world!