禁止通过IP访问——nginx

有时我们只希望能够通过域名访问,而不希望通过ip访问,这就需要设置nginx.conf配置文件了,现总结如下:

  • 禁止通过http方式访问

主要实现代码如下:

server
 {
    listen 80 default_server;
    server_name _;
    return 500;
  }
  • 禁止通过https防止访问

一定要SSL证书,不然会导致域名也无法访问!证书可以随便填,直接填920.ai域名证书的路径

server 
    {
        listen 443 default_server;
        server_name _;
        ssl on;
        ssl_certificate /etc/letsencrypt/live/920.ai/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/920.ai/privkey.pem;
        return 500;
    }

nginx端口映射tcp

添加到

/etc/nginx/nginx.conf

添加内容:

stream {
    upstream ssr {
        hash $remote_addr consistent;
        server 127.0.0.1:31905 weight=5 max_fails=3 fail_timeout=30s;
    }
    server {
       listen 4008;
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass ssr;
    }
}

监听4008,映射到127.0.0.1:31905,名字ssr。

CentOS下安装gperftools优化nginx

一、下载软件包

http://mirror.yongbok.net/nongnu/libunwind/libunwind-1.1.tar.gz  #下载libunwind

https://gperftools.googlecode.com/files/gperftools-2.0.tar.gz   #下载gperftools
上传软件包到服务器的/usr/local/src目录下面

二、安装gperftools

1、安装libunwind(安装gperftools前需要先安装libunwind)

cd /usr/local/src  #进入安装目录
tar zxvf libunwind-1.1.tar.gz  #解压
cd libunwind-1.1
./configure #配置
make #编译
make install #安装

2、安装gperftools

cd /usr/local/src
tar zxvf gperftools-2.0.tar.gz
cd gperftools-2.0 #进入目录
./configure --enable-frame-pointers #配置
make
make install

3、配置gperftools

vi /etc/ld.so.conf.d/usr_local_lib.conf  #编辑,添加以下内容
/usr/local/lib
:wq! #保存退出

/sbin/ldconfig  #执行此命令

cd /usr/local/src/nginx-1.2.4 #进入nginx安装包目录

./configure --prefix=/usr/local/nginx --with-google_perftools_module --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.31
#重新配置nginx,添加--with-google_perftools_module参数

make #编译
make install  #安装

mkdir /tmp/tcmalloc  #新建目录
chmod  777 /tmp/tcmalloc -R  #设置目录权限

4、配置nginx

未分类

vi /usr/local/nginx/conf/nginx.conf #编辑,在#pid logs/nginx.pid;这行的下面添加
google_perftools_profiles /tmp/tcmalloc;
:wq! #保存退出
service nginx restart  #重启nginx

三、测试

lsof -n | grep tcmalloc #测试tcmalloc
lsof -n | grep nginx  #测试nginx

未分类

未分类

至此,CentOS下安装gperftools优化nginx完成。

分析nginx upstream权重为0和删除节点

前言:

nginx upstream是大家经常使用的东西,通过upstream方法可以轻易的实现服务的负载均衡。 但往往很多人不理解nginx upstream删除主机跟权重配置为0有什么细节上的区别,从官方的介绍里也只能简单得知,weight = 0 不转发新请求,删除主机用来下线。 但你要明白这类操作对于长连接的影响。因为如果你这个理解不到位,很容易造成服务的异常,哪怕只是一小片时间。 好了,闲话不多说,我们直接通过测试来验证他的结果。

权重设为0: upstream把某个节点的权重重置为0,nginx不再往该节点转发新请求,老连接就这么挂着, 已经建立的连接不会中断, 直至超时或主动断开.新连接不会转到节点上来。

移除主机: 在upstream列表中删除该节点,nginx会首先把不在传输中的连接关闭,继而等待数据传输完毕后,把连接关闭掉。

Nginx upstream相关配置

# xiaorui.cc

upstream bakend {
    server 127.0.0.1:7080;
    server 127.0.0.1:7081;
}

server {
    listen       8122 default_server;
    server_name  _;
    root         /var/lib/download;

    include /etc/nginx/default.d/*.conf;

    location / {
        proxy_pass http://bakend;
    }
}

后端服务( 使用golang net/http开发案例)

至于端口变换一下就可以了,另外为了测试活跃的长连接情况,使用chunk分块的方式来延迟返回。

# xiaorui.cc

package main

import (
    "fmt"
    "io"
    "net/http"
    "time"
)

func main() {
    fmt.Println("7080")
    http.HandleFunc("/test", HandlePost);
    fmt.Println(http.ListenAndServe(":7080", nil))
}

func HandlePost(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Connection", "Keep-Alive")
    w.Header().Set("Transfer-Encoding", "chunked")
    w.Header().Set("X-Content-Type-Options", "nosniff")

    ticker := time.NewTicker(time.Second)
    go func() {
        for t := range ticker.C {
            io.WriteString(w, "Chunkn")
            fmt.Println("Tick at", t)
        }
    }()
    time.Sleep(time.Second * 15)
    ticker.Stop()
    fmt.Println("Finished: should return Content-Length: 0 here")
    w.Header().Set("Content-Length", "0")
}

模拟的客户端

# xiaorui.cc

import time
import os

import requests

s = requests.Session()
print os.getpid()
res = s.get("http://127.0.0.1:8122/test")
print res.content
time.sleep(10)

s = requests.Session()
res = s.get("http://127.0.0.1:8122/test")
print res.content

总结, 不管是权重配置为0 和 移除主机 都能保证服务的安全的退出,优雅灰度上下线。

END.

体验nginx端的HTTP2 Server Push 来

未分类

最新的nginx主线版本(1.13.9)终于支持http2 server push了:至于server push是什么,有什么好处,不赘述。

Changes with nginx 1.13.9                                        20 Feb 2018

    *) Feature: HTTP/2 server push support; the "http2_push" and
       "http2_push_preload" directives.

    *) Bugfix: "header already sent" alerts might appear in logs when using
       cache; the bug had appeared in 1.9.13.

    *) Bugfix: a segmentation fault might occur in a worker process if the
       "ssl_verify_client" directive was used and no SSL certificate was
       specified in a virtual server.

    *) Bugfix: in the ngx_http_v2_module.

    *) Bugfix: in the ngx_http_dav_module.

于是将本地服务器的nginx从最新的稳定版(1.12.2)升级到1.13.9。并且根据对应的

http2_push

http2_push_preload

指令,做相应的配置。

未分类

This directive appeared in version 1.13.9.

Pre-emptively sends (pushes) a request to the specified uri along with the response to the original request. Only relative URIs with absolute path will be processed, for example:

http2_push /static/css/main.css;

The uri value can contain variables.

Several http2_push directives can be specified on the same configuration level. The off parameter cancels the effect of the http2_push directives inherited from the previous configuration level.

未分类

This directive appeared in version 1.13.9.

Enables automatic conversion of preload links specified in the “Link” response header fields intopush requests.

默认情况下都是off,关闭的,需要手动去打开,这两个指令都支持在http,server或者location段内配置。一般在server段内配置好全站需要的资源引用即可。而在location端内,则可以针对具体类似页面做配置。

针对http2_push指令这里需要注意的几点:

  1. http2_push 后面必须跟的是资源的绝对路径

  2. 该资源路径可以包含变量

  3. 同一个配置级别(比如,server段内)可以配置多个http2_push

针对http2_push_preload这个指令,用于设置是否让在页面内标记的资源是否主动推送。

这里贴上部分配置:

# the whole www.hzshuangmei.com site
http2_push /alidata1/web/static.hzshuangmei.com/img/pc/favicon.ico ;
http2_push /alidata1/web/static.hzshuangmei.com/img/pc/kst/default.png ;
http2_push /alidata1/web/static.hzshuangmei.com/css/pc/common.css ;
http2_push /alidata1/web/static.hzshuangmei.com/js/pc/layer/theme/default/layer.css ;
http2_push /alidata1/web/static.hzshuangmei.com/js/pc/common.js ;
http2_push /alidata1/web/static.hzshuangmei.com/js/pc/layer/layer.js ;
http2_push /alidata1/web/static.hzshuangmei.com/js/pc/kst_popup.js ;

# the index page
location =/{

  http2_push /alidata1/web/static.hzshuangmei.com/css/pc/index.css ;
  http2_push /alidata1/web/static.hzshuangmei.com/js/pc/js ;

}

# the brand page
location ^~/brand/ {

  http2_push /alidata1/web/static.hzshuangmei.com/css/pc/brandzt/theme.css;
  http2_push /alidata1/web/static.hzshuangmei.com/js/pc/brandzt/theme.js ;

}

# the list of  the projects pages
location ^~/projects/*$
{
  http2_push /alidata1/web/static.hzshuangmei.com/css/pc/list_project.css ;
  http2_push /alidata1/web/static.hzshuangmei.com/js/pc/list_project.js ;
}

# the project landing page
location  ^~/projects/*/*.html$ {
  http2_push /alidata1/web/static.hzshuangmei.com/css/pc/tjb.css ;
  http2_push /alidata1/web/static.hzshuangmei.com/img/pc/tjb_bg.jpg ;
}

# the list of the doctors pages
location ^~/doctors/*$
{
  http2_push /alidata1/web/static.hzshuangmei.com/css/pc/list_doctors.css ;
  http2_push /alidata1/web/static.hzshuangmei.com/js/pc/list_doctors.js ;
}
# the doctor landing page
location  ^~/doctors/*/*.html$
{
  http2_push /alidata1/web/static.hzshuangmei.com/css/pc/tjb.css ;
  http2_push /alidata1/web/static.hzshuangmei.com/img/pc/tjb_bg.jpg ;
}

# the list of the cases pages
 location ^~/cases/*$
{
   http2_push /alidata1/web/static.hzshuangmei.com/css/pc/list_case.css ;
   http2_push /alidata1/web/static.hzshuangmei.com/js/pc/list_case.js ;
}

# the case landing page
location  ^~/cases/*/*.html$
{
  http2_push /alidata1/web/static.hzshuangmei.com/css/pc/article_case.css ;
  http2_push /alidata1/web/static.hzshuangmei.com/css/pc/article_common.css ;
   http2_push /alidata1/web/static.hzshuangmei.com/js/pc/article_common.js ;
}

# the list of the news pages
 location ^~/news/*$
{
   http2_push /alidata1/web/static.hzshuangmei.com/css/pc/list_news.css ;
   http2_push /alidata1/web/static.hzshuangmei.com/js/pc/list_news.js ;
}

# the news landing page
location  ^~/news/*/*.html$
{
  http2_push /alidata1/web/static.hzshuangmei.com/css/pc/article_common.css ;
  http2_push /alidata1/web/static.hzshuangmei.com/js/pc/article_common.js;
}

将上面的文件引入到主机配置文件中。另外在http段内设置:

http2_push_preload on;

体验效果:

未分类

检测验证http/2 server push

可以使用以下两种方法之一轻松验证服务器推送是否有效:

  1. Web浏览器中的开发人员工具

  2. 一个命令行的HTTP / 2客户端如 nghttp

使用开发人员工具进行验证(Google Chrome)

以下以Google Chrome为例,说明如何使用Web浏览器中的开发人员工具来验证服务器推送是否有效。在该图中,Chrome开发人员工具的“ 网络”标签上的“ 启动器”列指出,作为/demo.html请求的一部分,有多个资源被推送到客户端。

未分类

使用命令行客户端进行验证(nghttp)

除了Web浏览器工具,您还可以使用nghttp2.org项目中的nghttp命令行客户端来验证服务器推送是否有效。您可以从GitHub下载命令行客户端,或在可用的位置安装相应的操作系统软件包。对于Ubuntu,请使用该软件包。nghttpnghttp2-client

在输出中,星号(*)表示服务器推送的资源。

$ nghttp -ans https://example.com/demo.htmlid  responseEnd requestStart  process code size request path
 13    +84.25ms       +136us  84.11ms  200  492 /demo.html
  2    +84.33ms *   +84.09ms    246us  200  266 /style.css
  4   +261.94ms *   +84.12ms 177.83ms  200  40K /image2.jpg
  6   +685.95ms *   +84.12ms 601.82ms  200 173K /image1.jpg

参考资料:

https://www.nginx.com/blog/nginx-1-13-9-http2-server-push/

基于consul实现nginx的动态upstream

1. 相关组件

未分类

consul下载地址 https://releases.hashicorp.com/consul/1.0.6/consul_1.0.6_linux_amd64.zip

nginx-upsync-module下载地址 https://github.com/weibocom/nginx-upsync-module/archive/nginx-upsync-1.8.x.zip

nginx下载地址 http://nginx.org/download/nginx-1.8.0.tar.gz

2. 简单Demo

2.1 安装并配置Consul

# 解压包
unzip consul_1.0.6_linux_amd64.zip
# 移动consul到格式位置
mv consul /root/services/account_consul/
# 启动consul
/root/services/account_consul/consul agent -server -bootstrap-expect=1 -data-dir=/root/data/consul/ -node=accounting01 -bind=172.31.132.207 -config-dir=/root/data/consul_config/ -client 0.0.0.0 -ui
# 添加upstreams配置
curl -X PUT http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8000
curl -X PUT http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8001

访问http://ip:8500/ui/

未分类

上图我们可以看出,我们已经通过http请求存储了upstreams配置信息, 接下来我们需要让Nginx从consul自动更新upstreams配置;

2.2 重新编译安装Nginx,添加upsync模块

2.2.1 install

# 解压
tar -zxvf nginx-1.8.0.tar.gz
unzip nginx-upsync-1.8.x.zip
# 编译安装
cd nginx-1.8.0
./configure   --prefix=/usr/local/nginx     --with-pcre  --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module  --add-module=/root/packages/nginx-upsync-module-nginx-upsync-1.8.x
make && make install
# 检查Nginx是否按照成功
/usr/local/nginx/sbin/nginx

2.2.2 config

vim /usr/local/nginx/conf/nginx.conf
# 修改用户
(PS:由于我是root环境启动的服务, 若Nginx的用户不是root的话, 某些文件无法访问, 例如: 静态文件)
user  root; 
# 添加额外配置文件路径
(PS: 非必须, 但是我习惯这么做)
http {
    ...
    include /usr/local/nginx/conf/sites-enabled/*;
    server {
    ....
    }
}
# 添加Nginx的upstream配置
(accouting_server) ➜  conf cd sites-enabled
(accouting_server) ➜  sites-enabled pwd
/usr/local/nginx/conf/sites-enabled
(accouting_server) ➜  sites-enabled ll
total 8.0K
-rw-r--r-- 1 root root 915 Feb 24 18:13 accounting
-rw-r--r-- 1 root root 213 Feb 24 18:13 consul
(accouting_server) ➜  sites-enabled cat accounting
upstream accounting {
    server 127.0.0.1:11111;
    upsync 172.31.132.207:8500/v1/kv/upstreams/accounting/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
    upsync_dump_path /usr/local/nginx/conf/servers/accounting.conf;
}
server {
    listen 80;
    #server_name accounting.willdx.me;
    server_name 47.104.93.126
    charset    utf-8;
    client_max_body_size    20M;
    access_log    /var/log/nginx/accounting/access.log;
    error_log    /var/log/nginx/accounting/error.log;
    location /media  {
        alias /root/mycode/accouting_server/accouting_project/media;
    }
    location /static {
        alias /root/mycode/accouting_server/accouting_project/static;
    }
    location / {
        #proxy_set_header Host $host;
        #proxy_pass http://localhost:8888/;
        #proxy_redirect http://0.0.0.0:8888/ /;
        proxy_pass http://accounting;
    }
}

2.2.3 测试

# 启动测试服务
python -m http.server 8000
python -m http.server 8001

测试1: 负载均衡

# 测试
for i in `seq 10000`; do curl http://47.104.93.126/; sleep 1; done;
# 发现请求是负载均衡的

测试2: 测试动态的更改数据之后Nginx请求

# 删除一个配置
curl -X DELETE  http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8000
true#
# 再次测试2.2.3步骤
发现请求不走127.0.0.1:8000了
# 开启8000
curl -X PUT  http://172.31.132.207:8500/v1/kv/upstreams/accounting/127.0.0.1:8000
# 再次测试2.2.3步骤
发现请求又走127.0.0.1:8000了

2.3 Demo小结

我们可以consul来进行集中的配置管理, 服务变更的时候, 我们只需要更改配置即可;

linux中NFS服务安全加固实例

概述

NFS 的不安全性,主要体现于以下 4 个方面:

  • 缺少访问控制机制
  • 没有真正的用户验证机制,只针对 RPC/Mount 请求进行过程验证
  • 较早版本的 NFS 可以使未授权用户获得有效的文件句柄
  • 在 RPC 远程调用中, SUID 程序具有超级用户权限

加固方案

为有效应对以上安全隐患,推荐您使用下述加固方案。

配置共享目录(/etc/exports)
使用 anonuid,anongid 配置共享目录,这样可以使挂载到 NFS 服务器的客户机仅具有最小权限。不要使用 no_root_squash。

使用网络访问控制
使用 安全组策略 或 iptable 防火墙限制能够连接到 NFS 服务器的机器范围。

iptables -A INPUT -i eth0 -p TCP -s 192.168.0.0/24 --dport 111 -j ACCEPT
iptables -A INPUT -i eth0 -p UDP -s 192.168.0.0/24 --dport 111 -j ACCEPT
iptables -A INPUT -i eth0 -p TCP -s 140.0.0.0/8 --dport 111 -j ACCEPT
iptables -A INPUT -i eth0 -p UDP -s 140.0.0.0/8 --dport 111 -j ACCEPT

账号验证

使用 Kerberos V5 作为登录验证系统,要求所有访问人员使用账号登录,提高安全性。

设置 NFSD 的 COPY 数目
在 Linux 中,NFSD 的 COPY 数目定义在启动文件 /etc/rc.d/init.d/nfs 中,默认值为 8。

最佳的 COPY 数目一般取决于可能的客户机数目。您可以通过测试来找到 COPY 数目的近似最佳值,并手动设置该参数。

选择传输协议
对于不同的网络情况,有针对地选择 UDP 或 TCP 传输协议。传输协议可以自动选择,也可以手动设置。

mount -t nfs -o sync,tcp,noatime,rsize=1024,wsize=1024 EXPORT_MACHINE:/EXPORTED_DIR /DIR

UDP 协议传输速度快,非连接传输时便捷,但其传输稳定性不如 TCP,当网络不稳定或者黑客入侵时很容易使 NFS 性能大幅降低,甚至导致网络瘫痪。一般情况下,使用 TCP 的 NFS 比较稳定,使用 UDP 的 NFS 速度较快。

在机器较少,网络状况较好的情况下,使用 UDP 协议能带来较好的性能。

当机器较多,网络情况复杂时,推荐使用 TCP 协议(V2 只支持 UDP 协议)。

在局域网中使用 UDP 协议较好,因为局域网有比较稳定的网络保证,使用 UDP 可以带来更好的性能。

在广域网中推荐使用 TCP 协议,TCP 协议能让 NFS 在复杂的网络环境中保持最好的传输稳定性。

限制客户机数量
修改 /etc/hosts.allow 和 /etc /hosts.deny 来限制客户机数量。

/etc/hosts.allow

portmap: 192.168.0.0/255.255.255.0 : allow
portmap: 140.116.44.125 : allow

/etc/hosts.deny

portmap: ALL : deny

改变默认的 NFS 端口
NFS 默认使用的是 111 端口,使用 port 参数可以改变这个端口值。改变默认端口值能够在一定程度上增强安全性。

配置 nosuid 和 noexec
SUID (Set User ID) 或 SGID (Set Group ID) 程序可以让普通用户以超过自己权限来执行。很多 SUID/SGID 可执行程序是必须的,但也可能被一些恶意的本地用户利用,获取本不应有的权限。

尽量减少所有者是 root,或是在 root 组中却拥有 SUID/SGID 属性的文件。您可以删除这样的文件或更改其属性,如:
使用 nosuid 选项禁止 set-UID 程序在 NFS 服务器上运行,可以在 /etc/exports 加入一行:

/www www.abc.com(rw, root_squash, nosuid)

使用 noexec 禁止直接执行其中的二进制文件。

CentOS 7简单的利用yum源安装Nginx

未分类

项目需求要使用80端口部署静态html5的官网,为了稳定高效和后续的扩充,故使用轻巧又强大的Nginx来作80端口的服务器。
  
下面就简单介绍一下简单的利用yum源安装Nginx,并申请阿里云免费提供的的SymantecSSL证书,来开启SSL加密的443端口https。

安装环境:CentOS7 64位 Minimal版(VMware)

配置网卡

使用桥接,开启网卡并设置:静态ip、网关、子网掩码、DNS

# 编辑网卡配置
vi /etc/sysconfig/network-scripts/ifcfg-eno16777736

未分类

TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.0.200
GATEWAY=192.168.0.1
NETMASK=255.255.255.0
DNS1=192.168.0.1
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=11b992a7-1630-4a26-bd62-8ce65e3e5c78
DEVICE=eno16777736
ONBOOT=yes
# 重启网络服务
systemctl restart network
# 查看ip
ip addr

配置防火墙

http协议默认端口为80端口,SSL加密的https协议的默认端口为443端口。
远程访问,需要打开防火墙。CentOS 7 中默认防火墙是firewalld,默认为关闭状态。

# 启动Firewall
systemctl start firewalld
# 设置开机自启动
systemctl enable firewalld
# 开放http80端口
firewall-cmd --permanent --add-port=80/tcp
# 开放https443端口
firewall-cmd --permanent --add-port=443/tcp
# 重载防火墙配置
firewall-cmd --reload

# 查看所有已开放端口
firewall-cmd --list-ports

# 若无firewall-cmd命令则先安装firewalld
yum install firewalld -y

先安装nginx的yum源
http://nginx.org/en/linux_packages.html#stable 找到链接,安装:

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

未分类

查看:

yum info nginx

未分类

安装:

yum install nginx

未分类

安装完成后nginx就已经启动了,可以查看进程:

ps -ef | grep nginx

未分类

如果一切进展顺利的话,现在你可以通过你的域名或IP来访问你的Web页面来预览一下Nginx的默认页面

未分类

如果看到这个页面,那么说明你的CentOS 7 中 web服务器已经正确安装。

开机自启:

systemctl enable nginx

Nginx配置信息

# 网站文件存放默认目录
/usr/share/nginx/html
# 网站默认站点配置
/etc/nginx/conf.d/default.conf
# 自定义Nginx站点配置文件存放目录
/etc/nginx/conf.d/
# Nginx全局配置
/etc/nginx/nginx.conf

启动nginx:

systemctl start nginx

Nginx手动启动

nginx -c nginx.conf

在这里你可以改变设置用户运行Nginx守护程序进程一样,和工作进程的数量得到了Nginx正在运行,等等。

据运维同学反馈,通过yum源安装之后,在安装插件时会有所限制。但针对目前学习,已经足够了。

NFS服务常见故障排查和解决方法

NFS,全名叫Network File System,中文叫网络文件系统,是Linux、UNIX系统的分布式文件系统的一个组成部分,可实现在不同网络上共享远程文件系统。NFS由Sun公司开发,目前已经成为文件服务的一种标准之一(RFC1904,RFC1813)。其最大的功能就是可以通过网络,让不同操作系统的计算机可以共享数据,所以可以把NFS看做是一个文件服务器。NFS缺点是其读写性能比本地硬盘要差一些。

一、NFS服务常见故障排查

NFS服务出现了故障,主要从以下几个方面检查原因:

(1)检查NFS客户机和服务器的负荷是否太高,Server和Client之间的网络是否正常;

(2)检查/etc/exports文件的正确性;

(3)必要时重启NFS和portmap服务;

(4)运行下列命令重新启动portmap和NFS:

# /etc/init.d/portmap restart
# /etc/init.d/nfs restart
# /etc/init.d/rpcbind restart (在RHEL/CentOS 6.x里面)
# chkconfig portmap on
# chkconfig nfs on
# chkconfig rpcbind on (在RHEL/CentOS 6.x里面)

注意:在RHEL/CentOS 6.x里面,portmap服务改名为rpcbind服务了;顺便说一下,rpcbind服务也是图形界面的关键基础服务,不启动此服务,不能启动图形桌面。

(5) 检查Client上的mount命令或/etc/fstab的语法是否正确;

(6) 查看内核是否支持NFS和RPC服务。一般正常安装的Linux系统都会默认支持NFS和RPC服务,除非你自己重新编译的内核,而且没选择nfs支持选项编译。

二、NFS常见故障解决方法

1、The rpcbind failure error
故障现象:
nfs mount: server1:: RPC: Rpcbind failure
RPC: Timed Out
nfs mount: retrying: /mntpoint
原因:
第一,可能因为客户机的hosts文件中存在错误的ip地址、主机名或节点名组合;
第二,服务器因为过载而暂时停止服务。
2、The server not responding error
现象:
NFS server server2 not responding, still trying
原因:
第一,网络不通,用ping命令检测一下。
第二,服务器关机。
3、The NFS client fails a reboot error
现象:
启动客户机后停住了,不断显示如下提示信息:
Setting default interface for multicast: add net 224.0.0.0: gateway:
client_node_name.
原因:
在etc/vfstab的mount选项中使用了fg而又无法成功mount服务器上的资源,改成bg或将该行注释掉,直到服务器可用为止。
4、The service not responding error
现象:
nfs mount: dbserver: NFS: Service not responding
nfs mount: retrying: /mntpoint
原因:
第一,当前级别不是级别3,用who -r查看,用init 3切换。
第二,NFS Server守护进程不存在,用ps -ef | grep nfs检查,用/etc/init.d/nfs start启动。
5、The program not registered error
现象:
nfs mount: dbserver: RPC: Program not registered
nfs mount: retrying: /mntpoint
原因:
第一,当前级别不是级别3。
第二,mountd守护进程没有启动,用/etc/init.d/nfs脚本启动NFS守护进程。
第三,看/etc/dfs/dfstab中的条目是否正常。
6、The stale file handle error
现象:
stale NFS file handle
原因:
服务器上的共享资源移动位置了,在客户端使用umount和mount重新挂接就可以了。
7、The unknown host error
现象:
nfs mount: sserver1:: RPC: Unknown host
原因:
hosts文件中的内容不正确。
8、The mount point error
现象:
mount: mount-point /DS9 does not exist.
原因:
该挂接点在客户机上不存在,注意检查命令行或/etc/vfstab文件中相关条目的拼写。
9、The no such file error
现象:
No such file or directory.
原因:
该挂接点在服务器上不存在,注意检查命令行或/etc/vfstab文件中相关条目的拼写。
10、No route to host
错误现象:
# mount 10.10.11.211:/opt/data/xmldb /c2c-web1/data/xmldb -t nfs -o rw
mount: mount to NFS server ‘10.10.11.211’ failed: System Error: No route to host.

原因:
防火墙被打开,关闭防火墙。
这个原因很多人都忽视了,如果开启了防火墙(包括iptables和硬件防火墙),NFS默认使用111端口,我们先要检测是否打开了这个端口,还要检查TCP_Wrappers的设定。
11、Not owner
现象:
# mount -F nfs -o rw 10.10.2.3:/mnt/c2c/data/resinfo2 /data/data/resinfo2
nfs mount: mount: /data/data/resinfo2: Not owner

原因:
这是Solaris 10版本挂载较低版本nfs时报的错误。

解决:
需要用-o vers=3参数

示例:
# mount -F nfs -o vers=3 10.10.2.3:/mnt/c2c/data/resinfo2 /data/data/resinfo2
12、RPC: Program not registered & retrying
现象:
nfs mount: 10.10.2.3: : RPC: Program not registered
nfs mount: retrying: /data/data/resinfo2

原因:
没有启动NFS共享端服务。

解决:需要重新启动share端的NFS服务,
Linux:
mount: RPC: Program not registered
# /etc/init.d/nfs restart

Solaris:
mount: RPC: Program not registered
# /etc/rc.d/init.d/nfs restart
13、can’t contact portmapper: RPC: Remote system error – Connection refused
现象:
# exportfs -a
can’t contact portmapper: RPC: Remote system error – Connection refused

原因:
出现这个错误信息是由于server端的portmap没有启动。

解决:
# /etc/init.d/portmap start

批量 kill mysql 中运行时间长的sql

以下内容来自mysql手册:

13.5.5.3. KILL语法
KILL [CONNECTION | QUERY] thread_id
每个与mysqld的连接都在一个独立的线程里运行,您可以使用SHOW PROCESSLIST语句查看哪些线程正在运行,并使用KILL thread_id语句终止一个线程。

KILL允许自选的CONNECTION或QUERY修改符:

  • KILL CONNECTION与不含修改符的KILL一样:它会终止与给定的thread_id有关的连接。

  • KILL QUERY会终止连接当前正在执行的语句,但是会保持连接的原状。
    如果您拥有PROCESS权限,则您可以查看所有线程。如果您拥有SUPER权限,您可以终止所有线程和语句。否则,您只能查看和终止您自己的线程和语句。
    您也可以使用mysqladmin processlist和mysqladmin kill命令来检查和终止线程。
    注释:您不能同时使用KILL和Embedded MySQL Server库,因为内植的服务器只运行主机应用程序的线程。它不能创建任何自身的连接线程。
    当您进行一个KILL时,对线程设置一个特有的终止标记。在多数情况下,线程终止可能要花一些时间,这是因为终止标记只会在在特定的间隔被检查:

  • 在SELECT, ORDER BY和GROUP BY循环中,在读取一组行后检查标记。如果设置了终止标记,则该语句被放弃。

  • 在ALTER TABLE过程中,在每组行从原来的表中被读取前,检查终止标记。如果设置了终止标记,则语句被放弃,临时表被删除。

  • 在UPDATE或DELETE运行期间,在每个组读取之后以及每个已更行或已删除的行之后,检查终止标记。如果终止标记被设置,则该语句被放弃。注意,如果您正在使用事务,则变更不会被 回滚。

  • GET_LOCK()会放弃和返回NULL。

  • INSERT DELAYED线程会快速地刷新(插入)它在存储器中的所有的行,然后终止。

  • 如果线程在表锁定管理程序中(状态:锁定),则表锁定被快速地放弃。

  • 如果在写入调用中,线程正在等待空闲的磁盘空间,则写入被放弃,并伴随”disk full”错误消息。

  • 警告:对MyISAM表终止一个REPAIR TABLE或OPTIMIZE TABLE操作会导致出现一个被损坏的没有用的表。对这样的表的任何读取或写入都会失败,直到您再次优化或修复它(不中断)。

1、通过information_schema.processlist表中的连接信息生成需要处理掉的MySQL连接的语句临时文件,然后执行临时文件中生成的指令

mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root';
+------------------------+| concat('KILL ',id,';')
+------------------------+| KILL 3101;
| KILL 2946;
+------------------------+2 rows in set (0.00 sec)
mysql>select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt';
Query OK, 2 rows affected (0.00 sec)
mysql>source /tmp/a.txt;
Query OK, 0 rows affected (0.00 sec)

2、
  
杀掉当前所有的MySQL连接

mysqladmin -uroot -p processlist|awk -F "|" '{print $2}'|xargs -n 1 mysqladmin -uroot -p kill

杀掉指定用户运行的连接,这里为Mike

mysqladmin -uroot -p processlist|awk -F "|" '{if($3 == "Mike")print $2}'|xargs -n 1 mysqladmin -uroot -p kill

 
3、通过SHEL脚本实现

#杀掉锁定的MySQL连接

for id in mysqladmin processlist|grep -i locked|awk '{print $1}'
do
mysqladmin kill ${id}
done

4、通过Maatkit工具集中提供的mk-kill命令进行

#杀掉超过60秒的sql
mk-kill -busy-time 60 -kill
#如果你想先不杀,先看看有哪些sql运行超过60秒
mk-kill -busy-time 60 -print
#如果你想杀掉,同时输出杀掉了哪些进程
mk-kill -busy-time 60 -print –kill