Skip to content

Commit

Permalink
add-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
wenxiaoning committed Oct 25, 2017
1 parent ae237fc commit cdb896c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
# vim-mysql
# vim-mysql

<!-- more -->
<!-- toc -->
## 安装
### Pathogen
```bash
$ cd ~/.vim/bundle
$ git clone https://github.com/wxnacy/vim-mysql.git
$ cd ~/.vim/bundle/vim-mysql
$ ./install.sh
```

### Vundle
修改 `~/.vimrc`
```bash
Plugin 'wxnacy/vim-mysql'
```
在 Vim 中运行
```bash
:PluginInstall
```
```bash
$ cd ~/.vim/bundle/vim-mysql
$ ./install.sh
```

## 配置
配置密码
```bash
echo 'export MYSQL_LOCAL_USER=your_local_user' >> ~/.bash_profile
echo 'export MYSQL_LOCAL_PASSWORD=your_local_password' >> ~/.bash_profile
echo 'export MYSQL_LOCAL_HOST=your_local_host' >> ~/.bash_profile
source ~/.bash_profile
```
这样可以配置 ***local***环境的 Mysql 信息,如果需要 ***prod*** 环境
可以继续配置 `MYSQL_PROD_USER, MYSQL_PROD_PASSWORD, MYSQL_PROD_HOST`

## 使用
```bash
<Leader>rs # 在 normal 模式运行光标所在行的命令
<Leader>rsp # 在 prod 环境中运行 mysql 命令
```
6 changes: 6 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

VIM_MYSQL=`pwd`
echo $VIM_MYSQL
echo `pwd`

ln -sf ${VIM_MYSQL}/plugin/mysql_run /usr/local/bin/mysql_run
29 changes: 29 additions & 0 deletions plugin/mysql_run
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env expect

set timeout 10

if { $argc == 0 } {
set ENV local
}
if { $argc > 0 } {
set ENV [lindex $argv 0]
set sql [lindex $argv 1]
}
if { $ENV == "local" } {
set name $env(MYSQL_LOCAL_USER)
set password $env(MYSQL_LOCAL_PASSWORD)
set host $env(MYSQL_LOCAL_HOST)
}
if { $ENV == "prod" } {
set name $env(MYSQL_PROD_USER)
set password $env(MYSQL_PROD_PASSWORD)
set host $env(MYSQL_PROD_HOST)
}

spawn sh -c "mysql -u$name -p -h ${host} --tee=/data/log/mysql/`date +%Y-%m-%d`.log"
expect "*assword:*"
send "$password\r"
expect "*>"
send "$sql;\r"
send "exit;\r"
interact
1 change: 1 addition & 0 deletions test/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from screen limit 1\G;

0 comments on commit cdb896c

Please sign in to comment.