• 最大内存
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [work@xxxxx ~]$ ./redis/bin/redis-cli
    #最大可使用的内存是多少
    127.0.0.1:6379> config get maxmemory
    1) "maxmemory"
    2) "0"
    127.0.0.1:6379> config set maxmemory 100mb  #设置100M
    OK
    127.0.0.1:6379> config set maxmemory 0   #设置不限制
    OK
    127.0.0.1:6379> config get maxmemory
    1) "maxmemory"
    2) "0"    #在64位操作系统下不限制内存大小,在32位操作系统下最多使用3GB内存
  • 内存淘汰策略
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    127.0.0.1:6379> config get maxmemory-policy
    1) "maxmemory-policy"
    2) "noeviction"  #默认的redis内存淘汰策略,满了以后就放不进去了
    127.0.0.1:6379> config set maxmemory-policy allkeys-lru
    OK
    127.0.0.1:6379> config get maxmemory-policy
    1) "maxmemory-policy"
    2) "allkeys-lru"


    #noeviction(默认策略):对于写请求不再提供服务,直接返回错误(DEL请求和部分特殊请求除外)
    #allkeys-lru:从所有key中使用LRU算法进行淘汰
    #volatile-lru:从设置了过期时间的key中使用LRU算法进行淘汰
    #allkeys-random:从所有key中随机淘汰数据
    #volatile-random:从设置了过期时间的key中随机淘汰
    #volatile-ttl:在设置了过期时间的key中,根据key的过期时间进行淘汰,越早过期的越优先被淘汰
    #当使用volatile-lru、volatile-random、volatile-ttl这三种策略时,如果没有key可以被淘汰,则和noeviction一样返回错误
  • 当前状态
  • 1
    2
    #基础监控返回内容参考 https://redis.io/commands/info
    127.0.0.1:6379> info
  • 数据持久化
  • 1
    2
    3
    4
    127.0.0.1:6379> save  #同步dump rdb文件,生产环境是禁用,导致阻塞
    OK
    127.0.0.1:6379> bgsave #异步
    Background saving started
  • 慢日志
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    127.0.0.1:6379> SLOWLOG LEN   #慢日志的长度
    (integer) 9
    127.0.0.1:6379> SLOWLOG GET 1  #获取一条慢日志
    1) 1) (integer) 8
       2) (integer) 1569908358
       3) (integer) 33015
       4) 1) "save"
    127.0.0.1:6379> SLOWLOG RESET  #慢日志重置,内存中的reset后永久丢失
    OK
  • 运维工具
  • Redis实用监控工具一览

    聊聊redis的监控工具

  • ifconfig
  • 本机ip

  • hostname -i
  • 本机ip

  • curl ifconfig.io
  • 本机外网ip

  • ss
  • (https://www.cnblogs.com/yychuyu/p/11459575.html)
    Socket Statistics 的缩写,用来统计 socket 连接的相关信息,它跟 netstat 差不多,但有着比 netstat 更强大的统计功能,能够显示更多更详细的连接信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ss -s  #分类统计socket链接情况
    ss -a | wc -l 统计所有socket链接数目
    ss -ta #所有 TCP socket
    ss -ua #所有 UDP socket
    ss -wa #所有 RAW socket
    ss -xa #所有 UNIX socket
    ss -4a #所有 IPV4 socket
    ss -6a #所有 IPV6 socket
    ss -t  #要查看刚建立的 TCP 连接
    ss -lt #要仅显示监听 socket ,尝试
  • netstat
  • (https://www.cnblogs.com/ftl1012/p/netstat.html)
    netstat命令用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告

    1
    netstat -nlp #数字(不解析主机端口)显示正在监听的进程pid
  • tcpdump
  • 功能强大

    1
    tcpdump  -XvvennSs 0 -i eth0 tcp[20:2]=0x4745 or tcp[20:2]=0x4854   #抓取http
  • lsof
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    lsof -i:80 #查看打开80端口的所有进程
    lsof -i [46] [protocol][@hostname|hostaddr][:service|port]
    46 --> IPv4 or IPv6
    protocol --> TCP or UDP
    hostname --> Internet host name
    hostaddr --> IPv4地址
    service --> /etc/service中的 service name (可以不只一个)
    port --> 端口号 (可以不只一个)
    例如:
    lsof -i tcp@127.0.0.1  #tcp监控本地
    lsof -i 4 #显示ipv4
  • iftop
  • https://www.vpser.net/manage/iftop.html

    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
    1.进制转换
        echo "obase=16;89"|bc

    2.时间戳转换

        2.1
            date -d '2013-2-22 22:14' +%s
            1361542440
        2.2
            date -d @1361542596
            Fri Feb 22 22:16:36 CST 2013
        2.3
            date -d @1361542596 +"%Y-%m-%d %H:%M:%S"
            2013-02-22 22:16:36

    3.json 格式化

        echo '{"aa":"good"}' | jq
        {
          "aa": "good"
        }

    4.其他:
        计算器:bc
        md5工具:md5sum

    vim curl-time.txt 添加以下内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    \n
             remote_ip: %{remote_ip}\n
           remote_port: %{remote_port}\n
              local_ip: %{local_ip}\n
            local_port: %{local_port}\n
                  http: %{http_code}\n
                   dns: %{time_namelookup}s\n
              redirect: %{time_redirect}s\n
          time_connect: %{time_connect}s\n
       time_appconnect: %{time_appconnect}s\n
      time_pretransfer: %{time_pretransfer}s\n
    time_starttransfer: %{time_starttransfer}s\n
         size_download: %{size_download}bytes\n
        speed_download: %{speed_download}B/s\n
                      ----------\n
            time_total: %{time_total}s\n
    \n

    试一试:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    curl  -w "@/Users/mac/Desktop/curl-time.txt" www.baidu.com
    <!DOCTYPE html>
    <!--STATUS OK--><html>*******</html>

             remote_ip: 180.101.49.12 #服务器ip
           remote_port: 80            #服务器端口
              local_ip: 30.208.75.45  #本地ip
            local_port: 49200         #本地端口
                  http: 200           #http状态码
                   dns: 0.030925s     #从开始到域名解析完成的时间
              redirect: 0.000000s     #所有重定向步骤(包括名称查找、连接、预传输和传输)所用的时间(秒)。显示多个重定向的完整执行时间。
          time_connect: 0.069830s     #从开始到tcp协议建立完成的时间
       time_appconnect: 0.000000s     #从开始到SSL/SSH/etc connect/handshake协议完成的时间
      time_pretransfer: 0.070846s     #从开始到文件传输即将开始
    time_starttransfer: 0.114130s     #从开始到第一个字节即将传输(time_starttransfer-time_pretransfer可以代表服务器处理的时间)
         size_download: 2381bytes     #总下载量
        speed_download: 20788.000B/s  #下载平均速度
                      ----------
            time_total: 0.114534s

    也可以直接输出,不用格式化文件方式。例如:
    curl  -w "time_total:%{time_total}" www.baidu.com

    如果这些环节还不够怎么办
    man curl
    找到:
    -w, –write-out 看看哪些变量是需要的可以再添加以下。

    云服务中只有一台可以访问外网,另外几台都没有外网ip。如何让其他机器能够访问外网

    1. 代理机器:可以访问外网的机器

    利用goproxy 安装配置非常简单https://github.com/snail007/goproxy/blob/master/README_ZH.md#%E8%87%AA%E5%8A%A8%E5%AE%89%E8%A3%85

    1
    2
    3
    4
    #安装
    curl -L https://raw.githubusercontent.com/snail007/goproxy/master/install_auto.sh | bash
    #启动
    proxy http -t tcp -p "0.0.0.0:38080" --daemon

    2. 需要访问外网的机器

    vim /etc/profile 添加

    1
    [work@xyz ~]$vim /etc/profile

    #内网机器不能上网需要配置个代理
    http_proxy=172.18.142.251:38080
    https_proxy=$http_proxy
    ftp_proxy=$http_proxy
    no_proxy=172.18.142.*,*.local,localhost,127.0.0.1
    export http_proxy https_proxy ftp_proxy no_proxy

    [work@xyz ~]$ source /etc/profile #即可

    git配置代理:

    1
    2
    3
    4
    5
    6
    [work@xyz ~]$ cat /home/work/.ssh/config
    Host gitee.com
    HostName gitee.com
    User git
    Port 22
    ProxyCommand nc --proxy 172.18.142.251:38080 %h %p

    [work@xyz ~]$chmod 600 /home/work/.ssh/config

    此处参考https://www.jianshu.com/p/05e3a2959efe

    但是goproxy 有个问题
    非注册版本自动版本检查退出

    https://github.com/snail007/goproxy/wiki/%E6%8F%90%E7%A4%BA%E8%AE%BF%E9%97%AE%E6%9B%B4%E6%96%B0%E5%A4%B1%E8%B4%A5%EF%BC%9F

    建议换成tinyproxy
    https://blog.csdn.net/shorile/article/details/79020927
    tinyproxy nc 支持有问题,所以git的代理走https的 tinyproxy没问题

    1. 压测工具
      1. https://github.com/rakyll/hey
    2. 域名支持
      1. 非www跳转www
    3. 文件系统优化
      1. 关闭 accesstime
        1. done
      2. 修改ulimit
        1. done ulimt -n 65535
    4. nginx
      1. rsync 主动同步同时解决 data目录单点问题
        1. https://rsync.samba.org/
        2. https://rsync.samba.org/how-rsync-works.html
        3. https://xdays.me/rsync%E6%96%87%E4%BB%B6%E5%90%8C%E6%AD%A5%E6%9C%8D%E5%8A%A1.html
      2. 图片反向代理缓存
        1. http://phl.iteye.com/blog/2256356
        2. http://labs.frickle.com/nginx_ngx_cache_purge/
      3. 进程数
    5.  php
      1. 升级php7
        1. 这个还是必须的:性能提升非常大
        2. https://blog.csdn.net/xiao_zhui/article/details/72556781
      2. php7性能优化
        1. http://www.laruence.com/2015/12/04/3086.html
        2. 开启opcache
          1. 软连发布需要特别注意:
            1. 建议参考:https://github.com/meolu/walle-web/issues/109
              1. opcache.validate_timestamps = 0,即关闭 opcache 文件更新检查;发布代码,高级任务里,发布完成后执行 curl 调用 web(php-fpm) 下的php, opcache_reset(); 不要使用 文件session、文件缓存之类的。
              2. php cli的opcache和 fpm的是分离的。
        3. 开启hugepage
        4. [root@good work]# sysctl vm.nr_hugepages=512[root@good work]# cat /proc/meminfo | grep Huge[root@good work]#
      3. laravel 性能优化
        1. https://segmentfault.com/a/1190000011569012
      4. 进程数目
    6.  缓存
      1. 优化核心入口缓存 效率提升2/3
      2. redis缓存链接数优化 多次变一次
      3. apc缓存
      4. 走本地redis
    7. 服务器升级cpu
    8. 日志保留时间 和 磁盘监控
    9. 机器端口策略
    10.  其他
      1. 邮件发送服务申请公共账号
    11. 数据库安全

     

    #查看当前挂载了的磁盘
    [root@good ~]# fdisk -l
    磁盘 /dev/vda:42.9 GB, 42949672960 字节,83886080 个扇区
    Units = 扇区 of 1 * 512 = 512 bytes
    扇区大小(逻辑/物理):512 字节 / 512 字节
    I/O 大小(最小/最佳):512 字节 / 512 字节
    磁盘标签类型:dos
    磁盘标识符:0x0008d73a

    设备 Boot Start End Blocks Id System
    /dev/vda1 * 2048 83884031 41940992 83 Linux

    继续阅读

    当我们在 shell 的 bash 里操作多行内容的字符串,我们往往会想到 普通的字符串处理办法 例如:

    1
    2
    string="Hello linux"
    echo $string

    其实 bash 提供了一个非常好的解决办法,就是 “Multi-line”
    变量的基本使用
    e.g. 包含变量

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    cat > myfile.txt <<EOF
    this file has $variable $names $inside
    EOF


    # 注入文档到 myfile.txt
    cat myfile.txt
    #输入:
    #this file has

    variable="ONE"
    names="TWO"
    inside="expanded variables"

    cat > myfile.txt <<EOF
    this file has $variable $names $inside
    EOF


    #print out the content of myfile.txt
    cat myfile.txt
    #输入:
    #this file has ONE TWO expanded variables

    无变量

    1
    2
    3
    4
    5
    6
    7
    8
    9
    cat > myfile.txt <<"EOF"
    this file has $variable $dollar $name $inside
    EOF

    cat myfile.txt
    #得到
    #this file has $variable $dollar $name $inside

    #PS:引用符号 "EOF" 决定是否需要输入变量

    无变量 – 例子 2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    cat > myfile.txt <<EOF
    this file has $variable \$dollar \$name \$inside
    EOF


    cat myfile.txt
    # 得到
    # this file has $variable $dollar $name $inside

    #转义 dollar "$" 符号,bash将取消变量的解析

    将一个多行文本赋值到变量里面
    例1:

    1
    2
    3
    4
    5
    6
    7
    8
    read -d '' stringvar <<-"_EOF_"

    all the leading dollars in the $variable $name are $retained

    _EOF_
    # 输入变量
    echo $stringvar;
    # all the leading dollars in the $variable $name are $retained

    例2:

    1
    2
    3
    4
    5
    6
    read -d '' help <<- "_EOF_"
      usage: up [--level <n>| -n <levels>][--help][--version]

      Report bugs to:
      up home page:
    _EOF_

    例3:

    1
    2
    3
    4
    5
    6
    VARIABLE1="<?xml version="1.0" encoding='UTF-8'?>
    <report>
      <img src="a-vs-b.jpg"/>
      <caption>Thus is a future post on Multi Line Strings in bash
      <date>1511</date>-<date>1512</date>.</caption>
    </report>"

    例4:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    VARIABLE2=$(cat <<EOF
    <?xml version="1.0" encoding='UTF-8'?>
    <report>
      <img src="a-vs-b.jpg"/>
      <caption>Thus is a future post on Multi Line Strings in bash
      <date>1511</date>-<date>1512</date>.</caption>
    </report>
    EOF
    )

    例5:

    1
    2
    3
    4
    5
    6
    7
    8
    VARABLE3=`cat <<EOF
    <?xml version="1.0" encoding='UTF-8'?>
    <report>
      <img src="a-vs-b.jpg"/>
      <caption>Thus is a future post on Multi Line Strings in bash
      <date>1511</date>-<date>1512</date>.</caption>
    </report>
    EOF`

    例6 (直接写入文件):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    cat > heredocfile.txt <<_EOF_
    I am line 1
    I am line 2
    I'm the last line
    _EOF_

    # 测试
    cat heredocfile.txt
    # I am line 1
    # I am line 2
    # I'm the last line

    # and then, change your echo statement to include the '-e' option
    # which will turn on escape sequence processing:
    echo -e $USAGE >&2

    例7:

    1
    2
    3
    4
    5
    6
    7
    sudo cat > /aaaa.txt <<_EOF_
    I am line 1
    I am line 2
    I'm the last line
    _EOF_

    # sudo and >>: permission denied

    例8:

    1
    2
    3
    4
    # create
    sudo tee /aaa.txt << EOF
      echo "Hello World 20314"
    EOF

    例9(可向文本文件追加):

    1
    2
    3
    4
    # Append to Sudo
    sudo tee -a  /aaa.txt << EOF
     echo "This Line is appended"
    EOF

    例如10:

    1
    2
    3
    4
    5
    6
    sudo sh -c "cat > /aaa.txt" <<"EOT"
    this text gets saved as sudo - $10 - ten dollars ...
    EOT

    cat /aaa.txt
    #this text gets saved as sudo - $10 - ten dollars ...

    例11:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    cat << "EOF" | sudo tee /aaa.txt
    let's count
    $one
    two
    $three
    four

    EOF

    cat /aaa.txt
    #let's count
    #$one
    #two
    #$three
    #four

    关于 tee
    > tee –help
    Usage: tee [OPTION]… [FILE]…
    Copy standard input to each FILE, and also to standard output.

    -a, –append append to the given FILEs, do not overwrite
    -i, –ignore-interrupts ignore interrupt signals
    –help display this help and exit
    –version output version information and exit

    If a FILE is -, copy again to standard output.

    Report tee bugs to bug-coreutils@gnu.org
    GNU coreutils home page:
    General help using GNU software:
    For complete documentation, run: info coreutils ‘tee invocation’

    参考:
    1. Heredoc Quoting – Credit to Ignacio Vazquez-Abrams: http://serverfault.com/questions/399428/how-do-you-escape-characters-in-heredoc
    2. eredoc Quoting – Credit to Dennis Williamson: http://stackoverflow.com/questions/3731513/how-do-you-type-a-tab-in-a-bash-here-document
    3. http://serverfault.com/questions/72476/clean-way-to-write-complex-multi-line-string-to-a-variable
    4. http://arstechnica.com/civis/viewtopic.php?p=21091503
    5. http://superuser.com/questions/201829/sudo-permission-denied
    6. http://stackoverflow.com/questions/4937792/using-variables-inside-a-bash-heredoc
    7. http://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work
    8. http://www.unix.com/shell-programming-scripting/187477-variables-heredoc.html

    来源:http://www.woola.net/detail/2016-09-05-bash-multi-line-text.html

    Encrypting Your File

    tar and gzip the file, then encrypt it using des3 and a secret key.
    tar cvzf – mysql_dump.sql | openssl des3 -salt -k #YOUR PASSWORD# | dd of=encrypted_mysql_dump
    That simple!

    Decrypting Your File

    dd if=encrypted_mysql_dump |openssl des3 -d -k #YOUR PASSWORD# |tar xvzf –

    1
    2
    ls -ial   #获取文件节点
    find . -maxdepth 1 -type f -inum 748010  -delete   #通过节点删除

    1. 每个文件有唯一的索引号
    2. ls -i 可获得索引号
    3. find命令重命名:
    find . -inum 索引号 -exec mv {} newname \;
    -exec后为shell命令,{}代表当前文件名,\;表示shell命令结束
    4. 批量重命名:
    ls -i | awk ‘{printf(“find . -inum %s -exec mv {} %03d.txt \;\n”,$1,++i)}’ | sh
    awk的printf命令与C语言类似,$1表示已空格分隔的第一个参数,++i变量未初始化,默认为0