It event poll

小小网站,记录你的点滴知识!

直接迁移:

mv /lib/var/mysql /data/mysql

或复制

cp -a /lib/var/mysql /data/mysql

注:-a 把源目录权限也复制到新目录去,否则即使解决了chroot问题,一样启动不了mysql的

修改 /etc/mysql/my.cnf mysql数据路径datadir:

[mysqld]

datadir = /data/mysql

因为ubuntu的mysql是有chroot的,所以还要做以下操作:

vim /etc/apparmor.d/usr.sbin.mysqld

/var/lib/mysql/ r, 替换为 /data/mysql/ r,

/var/lib/mysql/** rwk, 替换为 /data/mysql/** rwk,

vim /etc/apparmor.d/abstractions/mysql

/var/lib/mysql/mysql.sock rw, 替换为 /data/mysql/mysql.sock rw,

sudo /etc/init.d/apparmor restart 重启apparmor

/etc/init.d/mysql start 启动mysql

成功

对别的软件也有用


服务器搭建很多年了,经过不断的调优,对于目前业务也基本处于很稳定状态,却一直没有出过像样得文档,趁着上架两台新机器,记录一下。。。

LNMP = Linux + Nginx + Mysql + PHP

一、Linux

安装就不多叙述了,如果连Ubuntu的安装都不会,这个文章也不要往下看了,没意义。。。

1、更新系统:

apt-get update
apt-get upgrade

2、增加文件打开数

vim /etc/sysctl.conf

#max open file
fs.file-max = 65535
echo "*    -    nproc    30000" >> /etc/security/limits.conf
echo "*    -    nofile    65535" >> /etc/security/limits.conf
echo "session required        pam_limits.so" >> /etc/pam.d/common-session
echo "ulimit -SHn 65535" >> /etc/profile
ulimit -SHn 65535<br>

3、配置SSH,修改端口,禁止root登陆,禁止X11图形登陆

sed -i 's/Port 22/Port 822/g' /etc/ssh/sshd_config
sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
sed -i 's/ServerKeyBits 768/ServerKeyBits 1024/g' /etc/ssh/sshd_config
sed -i 's/X11Forwarding yes/X11Forwarding no/g' /etc/ssh/sshd_config<br>

4、安装编译环境

apt-get install sysstat sysv-rc-conf build-essential ntp mpt-status<br>

5、安装Web环境

apt-get install libxml2 libxml2-dev libevent-1.4-2 libevent-dev libcurl4-openssl-dev libgd2-xpm libgd2-xpm-dev libpng12-0 libpng12-dev  libgif4 libgiftiio-dev libfreetype6 libfreetype6-dev  zlib1g zlib1g-dev libbz2-1.0 libbz2-dev libtool libmcrypt4 libmcrypt-dev libmysqlclient-dev libpcre3 libpcre3-dev autoconf<br>

二、Nginx

下载地址: http://nginx.org/en/download.html

安装:

mkdir /var/tmp/nginx
./configure --prefix=/usr --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/main_error.log --http-log-path=/var/log/nginx/ --http-client-body-temp-path=/var/tmp/nginx/client_body/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --with-http_stub_status_module
make
make install
which nginx #检查nginx是否安装成功

三、PHP

下载地址:http://cn2.php.net/downloads.php

安装:

./configure --prefix=/usr --with-config-file-path=/etc/php --with-mysql --with-mysqli --with-pdo-mysql --with-gd --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr --with-libxml-dir=/usr --with-zlib --enable-exif --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-bz2 --with-curl --with-curlwrappers --with-mcrypt --with-openssl --with-gettext --enable-filter --enable-simplexml --enable-mbstring --enable-soap --enable-dba --enable-ftp --enable-fpm --enable-sockets
make
make install

安装扩展:

pecl install memcache
pecl install redis<br>

四、Mysql

安装:

apt-get install mysql-server<br>

配置:

vim /etc/mysql/my.cnf

bind-address = 0.0.0.0
log_error = /var/log/mysql/error.log
slow-query-log
slow_query_log_file=/var/log/mysql/slow.log
long_query_time=2<br>

重启使配置生效:

/etc/init.d/mysql restart

第 1 部分: Comet 简介

http://www.ibm.com/developerworks/cn/web/wa-reverseajax1/

第 2 部分: WebSockets

http://www.ibm.com/developerworks/cn/web/wa-reverseajax2/

第 3 部分: Web 服务器和 Socket.IO

http://www.ibm.com/developerworks/cn/web/wa-reverseajax3/


UUID含义是 通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OSF) 的组织应用在分布式计算环境 (Distributed Computing Environment, DCE) 领域的一部分。 

UUID 的目的,是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过中央控制端来做辨识资讯的指定。如此一来,每个人都可以建立不与其它人冲突的 UUID。在这样的情况下,就不需考虑数据库建立时的名称重复问题。目前最广泛应用的 UUID,即是微软的 Microsoft's Globally Unique Identifiers (GUIDs),而其他重要的应用,则有 Linux ext2/ext3 档案系统、LUKS 加密分割区、GNOME、KDE、Mac OS X 等等。 

UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。通常平台会提供生成的API。按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字 

UUID由以下几部分的组合: 

(1)当前日期和时间,UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相同。 

(2)时钟序列。 

(3)全局唯一的IEEE机器识别号,如果有网卡,从网卡MAC地址获得,没有网卡以其他方式获得。 

UUID的唯一缺陷在于生成的结果串会比较长。关于UUID这个标准使用最普遍的是微软的GUID(Globals Unique Identifiers)。在ColdFusion中可以用CreateUUID()函数很简单地生成UUID,其格式为:xxxxxxxx-xxxx- xxxx-xxxxxxxxxxxxxxxx(8-4-4-16),其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。 

而 标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx (8-4-4-4-12),可以从cflib 下载CreateGUID() UDF进行转换。

第一种代码:

function create_guid(){
    $microTime = microtime();
    list($a_dec, $a_sec) = explode(" ", $microTime);
    $dec_hex = dechex($a_dec* 1000000);
    $sec_hex = dechex($a_sec);
    ensure_length($dec_hex, 5);
    ensure_length($sec_hex, 6);
    $guid = "";
    $guid .= $dec_hex;
    $guid .= create_guid_section(3);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= create_guid_section(4);
    $guid .= '-';
    $guid .= $sec_hex;
    $guid .= create_guid_section(6);
    return $guid;
}
function ensure_length(&$string, $length){   
    $strlen = strlen($string);   
    if($strlen < $length)   
    {   
        $string = str_pad($string,$length,"0");   
    }   
    else if($strlen > $length)   
    {   
        $string = substr($string, 0, $length);   
    }  
 }
function create_guid_section($characters){
    $return = "";
    for($i=0; $i<$characters; $i++)
    {
        $return .= dechex(mt_rand(0,15));
    }
    return $return;
}
echo create_guid();

第二种代码:

function create_uuid($prefix = ""){    //可以指定前缀
    $str = md5(uniqid(mt_rand(), true));   
    $uuid  = substr($str,0,8) . '-';   
    $uuid .= substr($str,8,4) . '-';   
    $uuid .= substr($str,12,4) . '-';   
    $uuid .= substr($str,16,4) . '-';   
    $uuid .= substr($str,20,12);   
    return $prefix . $uuid;
}
echo create_uuid();

用 file_get_contents() 函数抓取网页会发生乱码现象。有两个原因会导致乱码,一个是编码问题,一个是目标页面开了Gzip,下面说的就是开了Gzip功能如何才能不乱码的方法。把抓取到的内容转下编码即可($content=iconv("GBK", "UTF-8//IGNORE", $content);),我们这里讨论的是如何抓取开了Gzip的页面。怎么判断呢?获取的头部当中有Content-Encoding: gzip说明内容是GZIP压缩的。用FireBug看一下就知道页面开了gzip没有。下面是用firebug查看我的博客的头信息,Gzip是开了的。

请求头信息原始头信息
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Connection keep-alive
Cookie __utma=225240837.787252530.1317310581.1335406161.1335411401.1537; __utmz=225240837.1326850415.887.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=%E4%BB%BB%E4%BD%95%E9%A1%B9%E7%9B%AE%E9%83%BD%E4%B8%8D%E4%BC%9A%E9%82%A3%E4%B9%88%E7%AE%80%E5%8D%95%20site%3Awww.nowamagic.net; PHPSESSID=888mj4425p8s0m7s0frre3ovc7; __utmc=225240837; __utmb=225240837.1.10.1335411401
Host www.nowamagic.net
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0

1. 使用自带的zlib库

如果服务器已经装了zlib库,用下面的代码可以轻易解决乱码问题。

$data = file_get_contents("compress.zlib://".$url);

2. 使用CURL代替file_get_contents

function curl_get($url, $gzip=false) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    if($gzip) curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // 关键在这里
    $content = curl_exec($curl);
    curl_close($curl);
    return $content;
}

3. 使用gzip解压函数

function gzdecode($data) { 
  $len = strlen($data); 
  if ($len < 18 || strcmp(substr($data,0,2),"x1fx8b")) { 
    return null;  // Not GZIP format (See RFC 1952) 
  } 
  $method = ord(substr($data,2,1));  // Compression method 
  $flags  = ord(substr($data,3,1));  // Flags 
  if ($flags & 31 != $flags) { 
    // Reserved bits are set -- NOT ALLOWED by RFC 1952 
    return null; 
  } 
  // NOTE: $mtime may be negative (PHP integer limitations) 
  $mtime = unpack("V", substr($data,4,4)); 
  $mtime = $mtime[1]; 
  $xfl   = substr($data,8,1); 
  $os    = substr($data,8,1); 
  $headerlen = 10; 
  $extralen  = 0; 
  $extra     = ""; 
  if ($flags & 4) { 
    // 2-byte length prefixed EXTRA data in header 
    if ($len - $headerlen - 2 < 8) { 
      return false;    // Invalid format 
    } 
    $extralen = unpack("v",substr($data,8,2)); 
    $extralen = $extralen[1]; 
    if ($len - $headerlen - 2 - $extralen < 8) { 
      return false;    // Invalid format 
    } 
    $extra = substr($data,10,$extralen); 
    $headerlen += 2 + $extralen; 
  }
  $filenamelen = 0; 
  $filename = ""; 
  if ($flags & 8) { 
    // C-style string file NAME data in header 
    if ($len - $headerlen - 1 < 8) { 
      return false;    // Invalid format 
    } 
    $filenamelen = strpos(substr($data,8+$extralen),chr(0)); 
    if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) { 
      return false;    // Invalid format 
    } 
    $filename = substr($data,$headerlen,$filenamelen); 
    $headerlen += $filenamelen + 1; 
  }
  $commentlen = 0; 
  $comment = ""; 
  if ($flags & 16) { 
    // C-style string COMMENT data in header 
    if ($len - $headerlen - 1 < 8) { 
      return false;    // Invalid format 
    } 
    $commentlen = strpos(substr($data,8+$extralen+$filenamelen),chr(0)); 
    if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) { 
      return false;    // Invalid header format 
    } 
    $comment = substr($data,$headerlen,$commentlen); 
    $headerlen += $commentlen + 1; 
  }
  $headercrc = ""; 
  if ($flags & 1) { 
    // 2-bytes (lowest order) of CRC32 on header present 
    if ($len - $headerlen - 2 < 8) { 
      return false;    // Invalid format 
    } 
    $calccrc = crc32(substr($data,0,$headerlen)) & 0xffff; 
    $headercrc = unpack("v", substr($data,$headerlen,2)); 
    $headercrc = $headercrc[1]; 
    if ($headercrc != $calccrc) { 
      return false;    // Bad header CRC 
    } 
    $headerlen += 2; 
  }
  // GZIP FOOTER - These be negative due to PHP's limitations 
  $datacrc = unpack("V",substr($data,-8,4)); 
  $datacrc = $datacrc[1]; 
  $isize = unpack("V",substr($data,-4)); 
  $isize = $isize[1];
  // Perform the decompression: 
  $bodylen = $len-$headerlen-8; 
  if ($bodylen < 1) { 
    // This should never happen - IMPLEMENTATION BUG! 
    return null; 
  } 
  $body = substr($data,$headerlen,$bodylen); 
  $data = ""; 
  if ($bodylen > 0) { 
    switch ($method) { 
      case 8: 
        // Currently the only supported compression method: 
        $data = gzinflate($body); 
        break; 
      default: 
        // Unknown compression method 
        return false; 
    } 
  } else { 
    // I'm not sure if zero-byte body content is allowed. 
    // Allow it for now...  Do nothing... 
  }
  // Verifiy decompressed size and CRC32: 
  // NOTE: This may fail with large data sizes depending on how 
  //       PHP's integer limitations affect strlen() since $isize 
  //       may be negative for large sizes. 
  if ($isize != strlen($data) || crc32($data) != $datacrc) { 
    // Bad format!  Length or CRC doesn't match! 
    return false; 
  } 
  return $data; 
}

使用:

$html = file_get_contents('http://www.jb51.net/');
$html = gzdecode($html);