温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具
所有文章 友链 关于我

温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具

Mysql 索引的增删查

2017-08-09

原文:http://www.cnblogs.com/tianhuilove/archive/2011/09/05/2167795.html

阅读全文 >>
  • mysql

展开全文 >>

mysql 小技巧

2017-08-09

使用tee指定输入日志

tee命令可以将mysql操作日志输出到指定文件中,如果文件已存在则追加。命令行参数可以使用–tee=file_path,命令中可以使用tee或\T,想要退出使用日志可以使用\t

1
2
3
4
5
6
$ mysql -u root -p --tee=mysql.log
Logging to file 'mysql.log'
mysql> tee mysql.log 或 \T mysql.log
Logging to file 'mysql.log'
mysql> \t mysql.log
Outfile disabled

使用os命令

1
2
3
4
mysql> system uname;
Darwin
mysql> \! uname;
Darwin

执行sql文件

1
mysql> source mysql.sql
  • mysql

展开全文 >>

mysql prompt 提示符

2017-08-09

原文:http://www.thegeekstuff.com/2010/02/mysql_ps1-6-examples-to-make-your-mysql-prompt-like-angelina-jolie/

在命令行中使用

1
2
3
4
5
6
7
8
mysql> prompt \u@\d>
PROMPT set to '\u@\d> '
root@(none)> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
root@test>

在命令行参数中使用

需要使用双引号括起来

1
2
3
wxnacy@wxnacydeMacBook-Pro: ~ $ mysql -u root -p --prompt="\u@\d> "    
root@(none)> use test;
root@test>

在bash层修改 MYSQL_PS1 变量

1
2
3
wxnacy@wxnacydeMacBook-Pro: ~ $ export MYSQL_PS1="\u@\d> "                              
wxnacy@wxnacydeMacBook-Pro: ~ $ mysql -u root -p
root@(none)>

在/etc/my.cnf中配置

也可以在配置中一劳永逸

1
2
3
4
5
6
[mysql]  
prompt=\\u@\\d>\\ # 要多加一个反斜线\
  
又或者:  
[mysql]  
prompt="\u@\d> "

更多参数

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
Option  Description  
  
\c  A counter that increments for each statement you issue  
\D  The full current date  
\d  The default database  
\h  The server host  
\l  The current delimiter (new in 5.0.25)  
\m  Minutes of the current time  
\n  A newline character  
\O  The current month in three-letter format (Jan, Feb, …)  
\o  The current month in numeric format  
\P  am/pm  
\p  The current TCP/IP port or socket file  
\R  The current time, in 24-hour military time (0–23)  
\r  The current time, standard 12-hour time (1–12)  
\S  Semicolon  
\s  Seconds of the current time  
\t  A tab character  
\U  Your full user_name@host_name account name  
   
\u  Your user name  
\v  The server version  
\w  The current day of the week in three-letter format (Mon, Tue, …)  
\Y  The current year, four digits  
\y  The current year, two digits  
\_  A space  
\   A space (a space follows the backslash)  
\'  Single quote  
\"  Double quote  
\\  A literal “\” backslash character  
\x  x, for any “x” not listed above
  • mysql

展开全文 >>

mysql select 格式化输出

2017-08-09

使用\G输出结果按行垂直显示结果

当使用命令 select 结果,如果表属性特别多的时候,查看起来非常难受,在 select 语句
后使用\G(必须大写),可以让结果按行垂直显示

1
mysql> select * from user\G;

1
2
3
4
5
6
7
8

*************************** 1. row ***************************
id: 1
name: wxnacy
is_del: 0
create_ts: 2017-08-08 07:46:42
update_ts: 2017-08-08 07:46:42
1 row in set (0.00 sec)

以html格式输出

使用mysql客户端的参数–html或者-T,则所有SQL的查询结果会自动生成为html的table代码

1
2
$ mysql -u root --html -p
mysql> select * from user;

1
<TABLE BORDER=1><TR><TH>id</TH><TH>name</TH><TH>is_del</TH><TH>create_ts</TH> <TH>update_ts</TH></TR><TR><TD>1</TD><TD>wxnacy</TD><TD>0</TD><TD>2017-08-08 07:46:42</TD><TD>2017-08-08 07:46:42</TD></TR></TABLE>

以xml格式输出

使用mysql客户端的参数–xml或者-X选项,可以将结果输出为xml格式

1
2
$ mysql -u root --xml -p
mysql> select * from user;

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0"?>

<resultset statement="select * from user;" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="id">1</field>
<field name="name">wxnacy</field>
<field name="is_del">0</field>
<field name="create_ts">2017-08-08 07:46:42</field>
<field name="update_ts">2017-08-08 07:46:42</field>
</row>
</resultset>

使用pager进入more或less查看结果

当select查询结果很多时,一屏幕往往无法装下。使用pager命令后在进行select命令,可以进入类似linux中more或less查看文件的效果

1
2
3
4
5
6
mysql> pager more
PAGER set to 'more'
mysql> select * from user;
...
mysql> nopager #退出pager状态
PAGER set to stdout

  • mysql

展开全文 >>

ansible入门笔记1 Get Started

2017-08-08

专辑:ansible学习笔记

Ansilbe 是一个部署一群远程主机的工具。远程的主机可以是远程虚拟机或物理机,也
可以是本地主机。Ansilbe 是一个部署一群远程主机的工具。远程的主机可以是远程虚拟机
或物理机,也可以是本地主机。

  • 安装ansible
    • 命令
    • 配置hosts
  • 第一条命令
  • 指定hosts文件

安装ansible

命令

1
pip install ansible

配置hosts

修改/etc/ansible/hosts 全局hosts文件,没有的话自己创建

1
2
3
4
5
[wxnacy] # 如果服务器使用密码登录就用这个方式保存密码,避免每次输入
wxnacy.server.org ansible_ssh_pass=your_pass ansible_ssh_user=your_name

[prod] # 如果服务器使用sshkey登陆(推荐使用)
prod.server.org ansible_ssh_user=your_name ansible_ssh_private_key_file=key_path

第一条命令

  • 首先执行ping,查看是否可以连接服务器
1
2
3
4
5
6
7
8
9
10
11
$ ansible all -m ping


wxnacy.server.org | SUCCESS => {
"changed": false,
"ping": "pong"
}
prod.server.org | SUCCESS => {
"changed": false,
"ping": "pong"
}

得到如上样式结果即为正确,另外执行 ansible wxnacy -m ping 可以针对某一个服务器
组进行操作

  • 现在我们用另一个命令看下是不是真的可以拿到远程服务器的信息
1
2
3
4
$ ansible prod -m shell -a "uname -a"

prod.server.org | SUCCESS | rc=0 >>
Linux ip-172-31-5-249 4.9.27-14.31.amzn1.x86_64 #1 SMP Wed May 10 01:58:40 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

指定hosts文件

在一些项目中,全局hosts配置不能满足需求,需要指定hosts命令,可以执行如下命令

1
ansible my -i hosts_path -m ping

  • ansible

展开全文 >>

ansible入门笔记2 Playbooks

2017-08-08

专辑:ansible学习笔记

Playbooks 是 Ansible的配置,部署,编排语言.他们可以被描述为一个需要希望远程主机执行命令的方案,或者一组IT程序运行的命令集合.

一个playbook就是一个YAML文件,所以playbook文件一般都以.yml结尾,一个playbook文件由一个或多个play组成,每个play定义了在一个或多个远程主机上执行的一系列的task,其中每个task一般就是调用一个ansible的模块,如调用copy模块复制文件到远程主机或调用shell模块执行命令。

简单的配置

配置deploy.yml完成进入远程服务器的某个目录并执行git pull操作

1
2
3
4
5
6
- hosts: wxnacy # 它会默认使用/etc/ansible/hosts 中配置的服务器组名 也可以单独设置hosts地址
tasks:
- name: cd path and git pull # 命令名称
shell: git pull # 执行命令
args:
chdir: ~/workdir # 进入目录

运行

1
2
3
4
5
6
7
8
9
10
$ ansible-playbook deploy.yml

PLAY [wxnacy] **********************************************************************************************************************************************************************************************

TASK [cd path and git pull] *************************************************************************************************************************************************************************************
ok: [wxnacy.server.org]


PLAY RECAP *************************************************************************************************************************************************************************************************
wxnacy.server.org : ok=2 changed=1 unreachable=0 failed=0

执行完运行命令ansible会在webservers组中依次执行tasks,返回以上样式结果极为成功,结果通过红黄绿三种颜色标明了不同的执行结果,红色表示有task执行失败,黄色表示改变了远程主机状态。

  • ansible

展开全文 >>

ansible入门笔记3 Playbooks 简单扩展

2017-08-08

专辑:ansible学习笔记

Playbooks 在实际应用过程中,需要用到一些额外扩展功能,比如外部传参或指定hosts文件,本章节将着重介绍

阅读全文 >>
  • ansible

展开全文 >>

2017-08-08 新建article表,完善`insert_comment_trigger`

2017-08-08

今天目标创建acticle表,并完善insert_comment_trigger,以支持更新article中的comment_count,comment表新增comment_id字段

专辑:mysql每天练习

  • 新建article

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    drop table if exists `article`;
    create table `article`(
    id int(11) not null auto_increment,
    name varchar(32) not null comment '标题',
    url varchar(512) not null comment '地址',
    comment_count int(11) not null default 0 comment '评论数量',
    is_del tinyint(1) not null default 0 comment '是否删除',
    create_ts timestamp not null default current_timestamp comment '创建时间',
    update_ts timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key(`id`)
    )engine=InnoDB default charset=utf8mb4 comment '文章表';
  • 修改insert_comment

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    drop trigger if exists `insert_comment`;
    delimiter $
    create trigger `insert_comment` after insert on comment for each row
    begin
    declare count int;
    if (new.resource_type = 'video') then
    update video set comment_count = comment_count + 1 where id = new.resource_id;
    elseif (new.resource_type = 'article') then
    update article set comment_count = comment_count + 1 where id = new.resource_id;
    elseif (new.resource_type = 'user') then
    select count(0) into count from user_data where user_id = new.resource_id;
    if (count >0) then
    update user_data set comment_count = comment_count + 1 where user_id = new.resource_id;
    else
    insert into user_data (user_id,comment_count) values (new.resource_id,1);
    end if;
    end if;
    end $
    delimiter ;
  • 添加字段

    1
    alter table comment add column comment_id int(11) not null default 0 comment '回复的评论注解id';
  • 测试

    1
    2
    3
    insert into article (name,url) values ('好文章','http://wen.com/wen.html');
    insert into comment(resource_id,resource_type,content) values(1,'article','好文章');
    select * from article;

执行完后,article中comment_count应该等于1

  • mysql

展开全文 >>

2017-08-07 新建user和user_data表,完善`insert_commet_trigger`

2017-08-07

今天目标新建user、user_data表,并完善insert_commet_trigger,达到根据判断resource_type来更新video还是user_data的comment_count

专辑:mysql每天练习

  • 新建user表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    drop table if exists `user`;
    create table `user`(
    id int(11) not null auto_increment,
    name varchar(32) not null comment '姓名',
    is_del int(1) not null default 0 comment '是否删除',
    create_ts timestamp not null default current_timestamp comment '创建时间',
    update_ts timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key(`id`)
    )engine=InnoDB default charset=utf8mb4 comment '用户表';
  • 新建user_data表

    1
    2
    3
    4
    5
    6
    7
    drop table if exists `user_data`;
    create table `user_data`(
    user_id int(11) not null,
    comment_count int(11) not null default 0 comment '评论数量',
    update_ts timestamp not null default current_timestamp on update current_timestamp comment '最后修改时间',
    primary key(`user_id`)
    )engine=InnoDB default charset=utf8mb4 comment '用户数据表,记录用户额外数据';
  • 重写 insert_comment_trigger

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    drop trigger if exists `insert_comment`;
    delimiter $
    create trigger `insert_comment` after insert on comment for each row
    begin
    declare count int;
    if (new.resource_type = 'user') then
    select count(0) into count from user_data where user_id = new.resource_id;
    if (count > 0 ) then
    update user_data set comment_count = comment_count +1 where user_id = new.resource_id;
    else
    insert into user_data (user_id,comment_count) values (new.resource_id,1);
    end if;
    else
    update video set comment_count = comment_count +1 where id= new.resource_id;
    end if;
    end $
    delimiter ;
  • 执行相关语句

    1
    2
    3
    4
    insert into user(name) values ('wxnacy');
    insert into comment (resource_id,resource_type,content) values (1,'user','好人');
    insert into comment (resource_id,resource_type,content) values (1,'user','大好人');
    select * from user_data;

执行语句后,user_data中comment_count应该等于2

  • mysql

展开全文 >>

2017-08-06 新建video和comment表,以及`insert_commet_trigger`

2017-08-06

今天的目标是新建一个video视频表和comment评论表,并编写trigger,达到当insert一条评论类型为video的数据到comment中时,video中comment_count字段可以自加一

专辑:mysql每天练习

  • 新建video表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    drop table if exists `video`;
    create table `video`(
    id int(11) not null auto_increment,
    name varchar(32) not null comment '标题',
    url varchar(512) not null comment '视频地址',
    is_del int(1) not null default 0 comment '是否删除',
    create_ts timestamp not null default current_timestamp comment '创建时间',
    update_ts timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key(`id`)
    )engine=InnoDB default charset=utf8mb4 comment '视频表';
  • 添加comment_count字段(不是忘了,就是想练一下add column语句╭(╯^╰)╮)

    1
    alter table video add column comment_count int(11) not null default 0 comment '评论数量';
  • 新建comment表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    drop table if exists `comment`;
    create table `comment`(
    id int(11) not null auto_increment,
    resource_id int(11) not null comment '被评论资源id',
    resource_type varchar(16) not null comment '被评论资源类型',
    content varchar(1024) not null comment '评论内容',
    is_del int(1) not null default 0 comment '是否删除',
    create_ts timestamp not null default current_timestamp comment '创建时间',
    update_ts timestamp not null default current_timestamp on update current_timestamp comment '修改时间',
    primary key(`id`)
    )engine=InnoDB default charset=utf8mb4 comment '评论表';
  • 新建trigger

    1
    2
    3
    4
    5
    6
    7
    8
    9
    drop trigger if exists `insert_comment`;
    delimiter $
    create trigger `insert_comment` after insert on comment for each row
    begin
    if (new.resource_type = 'video') then
    update video set comment_count = comment_count +1 where id = new.resource_id;
    end if;
    end $
    delimiter ;
  • 执行相应操作语句

1
2
3
insert into video (name,url) values ('蜘蛛侠:返校季','http://woyemeiyou/wangzhi.mp4');
insert into comment (resource_id,resource_type,content) values (1,'video','找不到资源');
select * from video;

最后结果中video表中的comment_count应该等于1

  • mysql

展开全文 >>

&laquo; Prev1…60616263Next &raquo;
© 2017 - 2022 温欣爸比 京ICP备15062634号 总访问量3409次 访客数3361人次 本文总阅读量47次
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • python
  • flask
  • javascript
  • docker
  • 工具
  • openresty
  • 微信
  • java
  • hexo
  • 杂谈
  • vim
  • git
  • mysql
  • http
  • linux
  • mac
  • tmux
  • ssh
  • 算法
  • 开发
  • node
  • 杂文
  • jinja2
  • maven
  • spring
  • 北京
  • 生活
  • springboot
  • react
  • shell
  • graphql
  • iterm
  • expect
  • nginx
  • sqlalchemy
  • html
  • electron
  • vagrant
  • elastic
  • 宝贝
  • ansible
  • css
  • jquery
  • go
  • markdown
  • awk
  • redis
  • leetcode
  • zsh
  • 漫威
  • ssr
  • android
  • ffmpeg
  • chrome
  • vmware
  • youtube
  • windows
  • jupyter
  • excel
  • jq
  • Mac
  • Homebrew
  • mongo
  • py2
  • HomeBrew
  • movie
  • nodejs

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Guru99
每天看书
每天背单词
每天一篇
写写代码
听听周杰伦
爱爱老婆