Skip to content


修改numa和io调度优化mysql性能

一.NUMA设置 单机单实例,建议关闭NUMA,关闭的方法有三种: 1.硬件层,在BIOS中设置关闭; 2.OS内核,启动时设置numa=off; 3.可以用numactl命令将内存分配策略修改为interleave(交叉)

方法3 修改mysql.server 330行加上numactl vi /opt/mysql/bin/mysql.server

/usr/bin/numactl –interleave all $bindir/mysqld_safe –datadir=$datadir –pid-file=$server_pid_file $other_args >/dev/null 2>&1 & wait_for_pid created $!; return_value=$?

numastat 查看内存分配

node0 node1 numa_hit 56506002860 201877592362 numa_miss 9099468163 1450668930 numa_foreign 1450668930 9099468163 interleave_hit 6205106 4793392 local_node 56485823400 201848609519 other_node 9119647623 1479651773

二.IO调度算法 Linux有四种IO调度算法:CFQ,Deadline,Anticipatory和NOOP,CFQ是默认的IO调度算法。完全随机的访问环境下,CFQ与Deadline,NOOP性能差异很小,但是一旦有大的连续IO,CFQ可能会造成小IO的响应延时增加,所以数据库环境建议修改为deadline算法,表现更稳定。 IO调度算法都是基于磁盘设计,所以减少磁头移动是最重要的考虑因素之一,但是使用Flash存储设备之后,不再需要考虑磁头移动的问题,可以使用NOOP算法。NOOP的含义就是NonOperation,意味着不会做任何的IO优化,完全按照请求来FIFO的方式来处理IO。

IO调度,默认cfq echo ‘deadline’ > /sys/block/sdb/queue/scheduler cat /sys/block/sdb/queue/scheduler

noop anticipatory [deadline] cfq

减少预读,默认128 echo ’16’ > /sys/block/sda/queue/read_ahead_kb

增大队列,默认128 echo ‘512’ > /sys/block/sda/queue/nr_requests

尽量不使用交换区,默认60 echo ‘0’ > /proc/sys/vm/swappiness

开机运行 vi /etc/rc.local

echo ‘deadline’ > /sys/block/sdb/queue/scheduler echo ’16’ > /sys/block/sda/queue/read_ahead_kb echo ‘512’ > /sys/block/sda/queue/nr_requests

vi /etc/sysctl.conf

vm.swappiness=0

参考: http://blog.wl0.org/2011/03/how-to-start-mysqld-using-numactl/ http://jcole.us/blog/archives/2010/09/28/mysql-swap-insanity-and-the-numa-architecture/ http://linuxcommand.org/man_pages/numactl8.html http://software.intel.com/zh-cn/blogs/2008/11/24/numaxeon1/ http://www.mysqlops.com/2011/07/01/mysql_multi_using_numactl.html https://wickie.hlrs.de/platforms/index.php/Thread_And_Memory_Pinning http://www.cyberciti.biz/tips/linux-hugetlbfs-and-mysql-performance.html http://www.hellodb.net/2011/07/mysql-linux-hardware-tuning.html http://blog.csdn.net/liuben/article/details/5482167 http://hatemysql.com/2011/07/05/mysql%E6%9C%BA%E5%99%A8%E9%85%8D%E7%BD%AE%E6%A0%87%E5%87%86/

Posted in Mysql.

Tagged with , , , .


discuzX2 读写分离及配置mysql复制

之前配置了fastcgi双机负载均衡discuzx2,现在做读写分离

主服务器ip:192.168.0.17 从服务器ip:192.168.0.23

1.主服务器设置 确保在服务器和从服务器上安装的MySQL版本与6.5节,“不同MySQL版本之间的复制兼容性”所示的表兼容。理想情况,应在主服务器和从服务器上使用最近版本的MySQL。 在主服务器上为服务器设置一个连接账户。该账户必须授予REPLICATION SLAVE权限。如果账户仅用于复制(推荐这样做),则不需要再授予任何其它权限。 假定你的从服务器ip为192.168.0.23,想要创建用户名为repl的一个账户,从服务器可以使用该账户从你的域内的任何主机使用密码slavepass来访问主服务器。要创建该账户,可使用GRANT语句:

GRANT REPLICATION SLAVE ON *.* TO ‘repl’@’192.168.0.23’ IDENTIFIED BY ‘slavepass’;

更新权限

flush privileges;

确保主服务器主机上my.cnf文件的[mysqld]部分包括一个log-bin选项。该部分还应有一个server-id=Master_id选项,其中master_id必须为1到232–1之间的一个正整数值。例如:

log-bin=/opt/mysql/var_log/mysql-bin expire_logs_days = 10 server-id=1 binlog-do-db=discuzx log-slave-updates

2.从服务器设置 停止用于从服务器的服务器并在其my.cnf文件中添加下面的行: slave_id值同Master_id值一样,必须为1到232–1之间的一个正整数值。并且,从服务器的ID必须与主服务器的ID不相同。例如:

[mysqld] server-id=2 replicate-do-db=discuzx replicate-ignore-table=discuzx.pre_common_session slave-net-timeout=60 master-connect-retry=10 slave-skip-errors #concurrent_insert=2 low-priority-updates=1 max_write_lock_count=1 master-host=192.168.0.17 master-port=3306 master-user=repl master-password=slavepass

3.在主服务器上先锁表,禁止写入操作

FLUSH TABLES WITH READ LOCK;

备份数据库

cd /opt/mysql/ tar -cvf ./mysql-snapshot.tar ./var/discuzx

4.从服务器解压

scp -P 6022 ./mysql-snapshot.tar [email protected]:. tar -xvf ./mysql-snapshot.tar mv ./var/discuzx /opt/mysql/var/

5.设置同步 进入master上的mysql,查看master状态

SHOW MASTER STATUS; File Position Binlog_Do_DB Binlog_Ignore_DB mysql-bin.000001 6416812 discuzx File列显示日志名,而Position显示偏移量。在该例子中,二进制日志值为mysql-bin.000001,偏移量为6416812。记录该值。以后设置从服务器时需要使用这些值。它们表示复制坐标,从服务器应从该点开始从主服务器上进行新的更新。 取得快照并记录日志名和偏移量后,可以在主服务器上重新启用写活动: 在从服务器上执行下面的语句,用你的系统的实际值替换选项值: CHANGE MASTER TO MASTER_HOST=’192.168.0.17′, MASTER_USER=’repl’, MASTER_PASSWORD=’slavepass’, MASTER_LOG_FILE=’mysql-bin.000001′, MASTER_LOG_POS=6416812; 启动从服务器线程并查看状态: START SLAVE; show slave status; //Slave_IO_Running 与 Slave_SQL_Running 状态都要为Yes //Seconds_Behind_Master为主服务器同频延迟 查看进程 show processlist; //应该有两行state值为: Has read all relay log; waiting for the slave I/O thread to update it Waiting for master to send event 复制从I/O线程状态 下面列出了从服务器的I/O线程的State列的最常见的状态。该状态也出现在Slave_IO_State列,由SHOW SLAVE STATUS显示。这说明你可以只通过该语句仔细浏览所发生的事情。 · Connecting to master 线程正试图连接主服务器。 · Checking master version 建立同主服务器之间的连接后立即临时出现的状态。 · Registering slave on master 建立同主服务器之间的连接后立即临时出现的状态。 · Requesting binlog dump 建立同主服务器之间的连接后立即临时出现的状态。线程向主服务器发送一条请求,索取从请求的二进制日志文件名和位置开始的二进制日志的内容。 · Waiting to reconnect after a failed binlog dump request 如果二进制日志转储请求失败(由于没有连接),线程进入睡眠状态,然后定期尝试重新连接。可以使用–master-connect-retry选项指定重试之间的间隔。 · Reconnecting after a failed binlog dump request 线程正尝试重新连接主服务器。 · Waiting for master to send event 线程已经连接上主服务器,正等待二进制日志事件到达。如果主服务器正空闲,会持续较长的时间。如果等待持续slave_read_timeout秒,则发生超时。此时,线程认为连接被中断并企图重新连接。 · Queueing master event to the relay log 线程已经读取一个事件,正将它复制到中继日志供SQL线程来处理。 · Waiting to reconnect after a failed master event read 读取时(由于没有连接)出现错误。线程企图重新连接前将睡眠master-connect-retry秒。 · Reconnecting after a failed master event read 线程正尝试重新连接主服务器。当连接重新建立后,状态变为Waiting for master to send event。 · Waiting for the slave SQL thread to free enough relay log space 正使用一个非零relay_log_space_limit值,中继日志已经增长到其组合大小超过该值。I/O线程正等待直到SQL线程处理中继日志内容并删除部分中继日志文件来释放足够的空间。 · Waiting for slave mutex on exit 线程停止时发生的一个很简单的状态。 复制从SQL线程状态 下面列出了从服务器的SQL线程的State列的最常见的状态。 · Reading event from the relay log 线程已经从中继日志读取一个事件,可以对事件进行处理了。 · Has read all relay log; waiting for the slave I/O thread to update it 线程已经处理了中继日志文件中的所有事件,现在正等待I/O线程将新事件写入中继日志。 · Waiting for slave mutex on exit 线程停止时发生的一个很简单的状态。 I/O线程的State列也可以显示语句的文本。这说明线程已经从中继日志读取了一个事件,从中提取了语句,并且正在执行语句。 最后将master上的表解锁 UNLOCK TABLES; 在主服务器上,SHOW PROCESSLIST的输出看上去应为: SHOW PROCESSLIST\G; //state状态应该为Has sent all binlog to slave; waiting for binlog to be updated 复制主线程状态 下面列出了主服务器的Binlog Dump线程的State列的最常见的状态。如果你没有在主服务器上看见任何Binlog Dump线程,这说明复制没有在运行—即,目前没有连接任何从服务器。 · Sending binlog event to slave 二进制日志由各种事件组成,一个事件通常为一个更新加一些其它信息。线程已经从二进制日志读取了一个事件并且正将它发送到从服务器。 · Finished reading one binlog; switching to next binlog 线程已经读完二进制日志文件并且正打开下一个要发送到从服务器的日志文件。 · Has sent all binlog to slave; waiting for binlog to be updated 线程已经从二进制日志读取所有主要的更新并已经发送到了从服务器。线程现在正空闲,等待由主服务器上新的更新导致的出现在二进制日志中的新事件。 · Waiting to finalize termination 线程停止时发生的一个很简单的状态。 6.从服务器添加discuzx用户 在从服务器增加c1g_formaster用户访问从服务器权限 7.设置discuzx读写分离 vi bbs/config/config_global.php $_config[‘db’][‘common’][‘slave_except_table’] = ‘common_session’; #不读从服务器的common_session表 $_config[‘db’][‘slave’] = array(); $_config[‘db’][‘slave’][‘1’][‘dbhost’] = ‘192.168.0.23’; //mysql 从库的host $_config[‘db’][‘slave’][‘1’][‘dbuser’] = ‘c1g_formaster’; //mysql 从库的数据库用户名 $_config[‘db’][‘slave’][‘1’][‘dbpw’] = ‘password’; //mysql 从库的数据库密码 $_config[‘db’][‘slave’][‘1’][‘dbcharset’] = ‘gbk’; $_config[‘db’][‘slave’][‘1’][‘pconnect’] = ‘0’; $_config[‘db’][‘slave’][‘1’][‘dbname’] = ‘discuzx’; //mysql从库的数据库名 $_config[‘db’][‘slave’][‘1’][‘tablepre’] = ‘pre_’; 8.运行效果 内网同步大概会占三四十兆流量; 可能由于我的从服务器还跑着php,负载比较大; 从库运行半天后在更新时会死锁,反而影响了论坛正常运行,还是切回单机; ==============2011-12-06 更新 清除slave设置 在sql里运行 stop slave; reset slave; #这将清除 master.info,relay-log.info 关闭mysql,移险日志 cd /opt/mysql/var/ mv hostname-relay-bin.000609 hostname-relay-bin.000609.bak mv hostname-relay-bin.index hostname-relay-bin.index.bak 重启mysql ==================== 2011-12-19 更新 修改主服务器expire_logs_days位置放到log-bin下面 expire_logs_days = 10 参考: http://www.discuz.net/thread-2348271-1-1.html http://liuyu.blog.51cto.com/183345/d-4 http://dev.mysql.com/doc/refman/5.1/zh/replication.html http://blog.lxneng.com/?cat=7

Posted in Discuz/Uchome/Ucenter, Mysql.

Tagged with , , .


inotify+rsync+nginx+fastcgi双机负载均衡discuzx2

一.概述

服务器 web1 R410 E55202 16G SAS300G2 centos5.5 ip:192.168.1.21 web2(new) R410 E56062 16G SAS300G2 centos5.5 ip:192.168.1.23 db R410 E55042 16G SAS146G2 centos5.3

discuz论坛现有500多万贴子,近2万人在线,400万pv/日, 新增web2服务器分担论坛php处理. 尝试过在web2用nfs mount web1的程序来跑,但是访问速度太慢,无法接受 改用inotify+rsync效果不错,原来web1负载8左右,现两台各自3.5

web2安装

web2安装好php后配置php-fpm

192.168.1.23:9002 #监听地址及端口 rsyncuser#后面rsync同步的用户 website#同步用户的用户组 128#目前开128个 大概占用物理内存4G左右 192.168.1.21,192.168.1.23 #充许请求的服务器(web1,web2自已)

web2打开iptables端口

iptables -A INPUT -p tcp -m tcp -s 192.168.1.21 –dport 9002 -j ACCEPT /etc/init.d/iptables save

db服务器加权限 给web2加上和web1一样的用户访问权限(discuz,ucenter…)

web1安装设置

web1安装inotify https://github.com/rvoicilas/inotify-tools/wiki/ inotify-tools 3.14 is the latest version, released on the 7th of March 2010.

wget –no-check-certificate http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz tar zxvf inotify-tools-3.14.tar.gz cd inotify-tools-3.14 make make install 完成后,注意查看manpage,man inotify、man inotifywait

查看是否支持inotify 从kernel 2.6.13开始正式并入内核,RHEL5已经支持。 看看是否有 /proc/sys/fs/inotify/目录,以确定内核是否支持inotify ll /proc/sys/fs/inotify

total 0 -rw-r–r– 1 root root 0 Sep 14 14:01 max_queued_events -rw-r–r– 1 root root 0 Sep 14 14:01 max_user_instances -rw-r–r– 1 root root 0 Sep 14 14:01 max_user_watches

测试inotify /usr/local/bin/inotifywait -mrq –timefmt ‘%d/%m/%y %H:%M’ –format ‘%T %w%f’ -e modify,delete,create,attrib /opt/lampp/htdocs/bbs

16/09/11 15:59 /opt/lampp/htdocs/bbs/data/cache/forum_threadviews_1.log 16/09/11 15:59 /opt/lampp/htdocs/bbs/data/cache/forum_threadviews_1.log 16/09/11 15:59 /opt/lampp/htdocs/bbs/data/cache/forum_threadviews_1.log ….

rsync同步 方法一:使用ssh用户免登录 方法二:使用rsync认证免登录

我这里使用方法一

web2增加同步用户 useradd -g website rsyncuser passwd rsyncuser

web1创建公钥并传到web2 ssh-keygen -t rsa

Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is:

复制到web2

scp -P 22 ~/.ssh/id_rsa.pub [email protected]:.

web2设置免登录 将id_rsa.pub导入到.ssh/authorized_keys

cd /home/rsyncuser mkdir .ssh cat id_rsa.pub >> .ssh/authorized_keys chown -R rsyncuser:website .ssh rm id_rsa.pub

可以在web1测试下登录 ssh -p 22 [email protected]

同步脚本 inofity 的exclude支持posix正则,但只能写一个. 注意两边放bbs的路径要一样 可以先用rsync同步一下

vi inotify.sh

#!/bin/sh yesterday=`date +%Y%m%d` src=/opt/lampp/htdocs/bbs [email protected]:/opt/lampp/htdocs/ /usr/local/bin/inotifywait -mrq –exclude “data/(threadcache|log|template|sendmail\.lock|cache)” –timefmt ‘%d/%m/%y %H:%M’ –format ‘%T %w%f’ \ -e modify,delete,create,attrib ${src} | while read file do echo -e $(date +%Y-%m-%d_%H:%M:%S)”\r” >>rsynclog/inotify.${yesterday}.log rsync -av –delete –exclude “data/threadcache/” –exclude “data/log/” –exclude “data/template/” –exclude “data/sendmail.lock” ${src} -e “/usr/bin/ssh -p 22” ${des} >>rsynclog/inotify.${yesterday}.log done

在web2上创建不同步的目录和文件 cd /opt/lampp/htdocs/bbs mkdir data/{threadcache,log,template} chown -R rsyncuser:website data/{threadcache,log,template} chmod -R 0775 data/{threadcache,log,template} touch data/sendmail.lock chown -R rsyncuser:website data/sendmail.lock chmod -R 0775 data/sendmail.lock

设置权限并运行 chmod 700 ./inofity.sh ./inofity.sh &

加到开机运行

echo ‘cd /opt/shell && ./inofity.sh & ‘>>/etc/rc.local

cache目录的更新 如果使用memcache可以跳过此步 使用文件缓存时 /opt/lampp/htdocs/bbs/data/cache 目录有新注册会员及在线人数等cache,更新很频繁不适于放入inotify中 vi rsync.sh

#!/bin/sh #* * * * * cd /opt/shell && /bin/sh ./rsync.sh > /dev/null 2>&1 yesterday=`date +%Y%m%d` src=/opt/lampp/htdocs/bbs/data/cache [email protected]:/opt/lampp/htdocs/bbs/data/ echo -e $(date +%Y-%m-%d_%H:%M:%S)”\r” >>rsynclog/rsync.${yesterday}.log rsync -av –delete ${src} -e “/usr/bin/ssh -p 22” ${des2} >>rsynclog/rsync.${yesterday}.log

设置权限 chmod 775 ./rsync.sh

crontab -e 加入crontab中 每分钟运行

* * * * * cd /opt/shell && /bin/sh ./rsync.sh > /dev/null 2>&1

第一次同步完后,开启php /opt/php/sbin/php-fpm start

web1开启负载均衡

web1上调整nginx 先创建个test.php来测试,可以在phpinfo里看到不同的机器名或通firebug查看文件header来确定是否在web2上

echo ” > test.php

vi /opt/nginx/conf/nginx.conf

location ~ /test\.php?$ { #fastcgi_pass 127.0.0.1:9000; fastcgi_pass 192.168.1.23:9002; add_header App-Server php2; fastcgi_index index.php; include fcgi.conf; break; }

正式上线只需针对forum.php进行负载均衡,90%的负载都在这文件上(只开启论坛情况下)

upstream backend { ip_hash; #确保分流内网能访问公网,或无对外访问需求 server 192.168.1.23:9002 max_fails=3 fail_timeout=60s; server 127.0.0.1:9000; } location ~ /forum\.php?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; #fastcgi_pass 127.0.0.1:9000; #fastcgi_pass 192.168.1.23:9002; #add_header App-Server php2; fastcgi_pass backend; fastcgi_index index.php; include fcgi.conf; break; }

fcgi.conf

fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with –enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;

重启nginx reload nginx /bin/kill -HUP cat /dev/shm/nginx.pid

discuz上传附件,后台固定在web1,读贴等操作会依据用户ip分布在两台机器上.

参考: http://www.infoq.com/cn/articles/inotify-linux-file-system-event-monitoring http://hi.baidu.com/tonyty163/blog/item/3c14ca2698672a0a918f9daf.html

Posted in Discuz/Uchome/Ucenter, 网站架构.

Tagged with , , , , , .


ACPI引起linux系统无故重启

新装机器无故重启多次。 centos6 64bit uname -a Linux Eos 2.6.32-71.el6.x86_64 #1 SMP Fri May 20 03:51:51 BST 2011 x86_64 x86_64 x86_64 GNU/Linux

硬件为Dell R410 E56062 4G1 sas300G *2

系统负载基本为0,查看日志有acpi错误。 tail -n1000 /var/log/messages |grep -i error

Sep 11 10:25:25 C1gstudio kernel: ACPI Error: No handler for Region [IPMI] (ffff88012b6b7420) [IPMI] (20090903/evregion-319) Sep 11 10:25:25 C1gstudio kernel: ACPI Error: Region IPMI(7) has no handler (20090903/exfldio-295) Sep 11 10:25:25 C1gstudio kernel: ACPI Error (psparse-0537): Method parse/execution failed [\_SB_.PMI0._GHL] (Node ffff88012b6b6470), AE_NOT_EXIST Sep 11 10:25:25 C1gstudio kernel: ACPI Error (psparse-0537): Method parse/execution failed [\_SB_.PMI0._PMC] (Node ffff88012b6b64f0), AE_NOT_EXIST Sep 11 10:25:28 C1gstudio abrtd: dbus error: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory Sep 11 10:25:28 C1gstudio abrtd: Error requesting DBus name com.redhat.abrt, possible reasons: abrt run by non-root; dbus config is incorrect; or dbus daemon needs to be restarted to reload dbus config Sep 11 10:35:51 C1gstudio kernel: ACPI Error: No handler for Region [IPMI] (ffff88012b6b7420) [IPMI] (20090903/evregion-319) Sep 11 10:35:51 C1gstudio kernel: ACPI Error: Region IPMI(7) has no handler (20090903/exfldio-295) Sep 11 10:35:51 C1gstudio kernel: ACPI Error (psparse-0537): Method parse/execution failed [\_SB_.PMI0._GHL] (Node ffff88012b6b6470), AE_NOT_EXIST Sep 11 10:35:51 C1gstudio kernel: ACPI Error (psparse-0537): Method parse/execution failed [\_SB_.PMI0._PMC] (Node ffff88012b6b64f0), AE_NOT_EXIST Sep 11 10:35:53 C1gstudio abrtd: dbus error: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory Sep 11 10:35:53 C1gstudio abrtd: Error requesting DBus name com.redhat.abrt, possible reasons: abrt run by non-root; dbus config is incorrect; or dbus daemon needs to be restarted to reload dbus config

vi /boot/grub/grub.conf 在kernel一行最后加上acpi=off noacip

kernel … acpi=off noacip

然后重启

Posted in LINUX, 技术.

Tagged with , , .


php5.2.14中strtotime格式时间戳错误引起日期混乱

状况是用smarty version 2.6.5-dev格式的时间在线上服务器显示时间不对.

{$smarty.now|date_format:”%Y/%m/%d %H:%M”}

会显示成 5068/08/16 10:30

检查smarty/plugins/shared.make_timestamp.php strtotime函数在不同版本下会有不同返回结果

$time = strtotime($string);

编写代码确认 code:

echo time(); echo ‘—‘; var_dump(strtotime(time())); ?>

win php5.3.1 result: 1313465002—bool(false)

linux php5.2.14 result: 1313465026—int(96457670026)

“96457670026”?这个就是引起错误的原因.

修改下smarty/plugins/shared.make_timestamp.php 解决问题

if (is_numeric($string) && $string != -1) return $string; $time = strtotime($string); if (is_numeric($time) && $time != -1) return $time;

Posted in PHP.

Tagged with , , , .


更新个php验证email正则

网上随处可见的验证规则会漏掉用户名中常见字符”.”,”-“,”_”; 以及后缀中可能是二级或三级域名的情况.

‘ ‘+’ 和 ‘?’的规则 ‘‘ 匹配0或多次 ‘+’ 匹配1或多次 ‘?’ 匹配0或1次

下面函数的验证规则为: 必需有一个不以”.”开头的字符后接0到多个字符 接上@须有一个字符开头接上0到多个包含”.”的子域名 最后接上”.”开头2到4个字符的域名后缀

function validEmail($address) { if(!preg_match(“/^[0-9a-zA-Z_-]+(\.[0-9a-zA-Z_-]+)*@[a-zA-Z0-9_-]+(\.{1}[a-zA-Z0-9_-]+)*\.{1}[a-zA-Z]{2,4}$/i”,$address)) { return False; } return True; }

Posted in PHP.

Tagged with , , .


Dell R410 网卡在Centos5.5 上丢包

现象是每几十次访问网页就会一次打不开,数据库,临控等才时常取不到数据. 好像r410的网卡和centos5.5不怎么兼容

uname -a

Linux c1gserver 2.6.18-164.el5 #1 SMP Thu Sep 3 03:28:30 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

dmesg |grep bnx

Broadcom NetXtreme II Gigabit Ethernet Driver bnx2 v1.9.3 (March 17, 2009) bnx2: eth0: using MSIX bnx2: eth0 NIC Copper Link is Up, 100 Mbps full duplex

ifconfig 有大量的丢包;dropped:6620400

eth0 Link encap:Ethernet HWaddr 00:26:B9:3B:XX:C3 inet addr:61.255.xx.xx Bcast:61.255.xx.xx Mask:255.255.255.192 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:7330597243 errors:1 dropped:6620400 overruns:0 frame:1 TX packets:8116489614 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3435468718053 (3.1 TiB) TX bytes:3656517601592 (3.3 TiB) Interrupt:82 Memory:da000000-da012800

一.先尝试增加网卡缓存来解决问题 1.ethtool -g eth0

Ring parameters for eth0: Pre-set maximums: RX: 1020 RX Mini: 0 RX Jumbo: 4080 TX: 255 Current hardware settings: RX: 255 RX Mini: 0 RX Jumbo: 0 TX: 255

2.ethtool -G eth0 rx 1000 ethtool -g eth0

Ring parameters for eth0: Pre-set maximums: RX: 1020 RX Mini: 0 RX Jumbo: 4080 TX: 255 Current hardware settings: RX: 1000 RX Mini: 0 RX Jumbo: 0 TX: 255

ifconfig

eth0 Link encap:Ethernet HWaddr 00:26:B9:3B:XX:C3 inet addr:61.255.xx.xx Bcast:61.255.xx.xx Mask:255.255.255.192 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1348879 errors:0 dropped:2714 overruns:0 frame:0 TX packets:1449564 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:646430926 (616.4 MiB) TX bytes:576842481 (550.1 MiB) Interrupt:82 Memory:da000000-da012800

ethtool -S eth0

NIC statistics: rx_bytes: 645417070 rx_error_bytes: 0 tx_bytes: 575096746 tx_error_bytes: 0 rx_ucast_packets: 1345249 rx_mcast_packets: 17 rx_bcast_packets: 826 tx_ucast_packets: 1446406 tx_mcast_packets: 0 tx_bcast_packets: 0 tx_mac_errors: 0 tx_carrier_errors: 0 rx_crc_errors: 0 rx_align_errors: 0 tx_single_collisions: 0 tx_multi_collisions: 0 tx_deferred: 0 tx_excess_collisions: 0 tx_late_collisions: 0 tx_total_collisions: 0 rx_fragments: 0 rx_jabbers: 0 rx_undersize_packets: 0 rx_oversize_packets: 0 rx_64_byte_packets: 620303 rx_65_to_127_byte_packets: 87492 rx_128_to_255_byte_packets: 36556 rx_256_to_511_byte_packets: 212099 rx_512_to_1023_byte_packets: 45961 rx_1024_to_1522_byte_packets: 343681 rx_1523_to_9022_byte_packets: 0 tx_64_byte_packets: 368814 tx_65_to_127_byte_packets: 714315 tx_128_to_255_byte_packets: 13048 tx_256_to_511_byte_packets: 18258 tx_512_to_1023_byte_packets: 39752 tx_1024_to_1522_byte_packets: 292219 tx_1523_to_9022_byte_packets: 0 rx_xon_frames: 0 rx_xoff_frames: 0 tx_xon_frames: 0 tx_xoff_frames: 0 rx_mac_ctrl_frames: 0 rx_filtered_packets: 0 rx_discards: 0 rx_fw_discards: 2704

rx_fw_discards: 2704 问题没有解决,依然丢包

二.更新网卡驱动 lsmod |grep bnx2

bnx2 209997 0

ethtool -i eth0

driver: bnx2 version: 1.9.3 firmware-version: 5.0.6 NCSI 2.0.3 bus-info: 0000:01:00.0

modinfo bnx2

filename: /lib/modules/2.6.18-164.el5/kernel/drivers/net/bnx2.ko version: 1.9.3 license: GPL description: Broadcom NetXtreme II BCM5706/5708/5709/5716 Driver author: Michael Chan srcversion: D151EAED8C1037CA480DE9A alias: pci:v000014E4d0000163Csv*sd*bc*sc*i* alias: pci:v000014E4d0000163Bsv*sd*bc*sc*i* alias: pci:v000014E4d0000163Asv*sd*bc*sc*i* alias: pci:v000014E4d00001639sv*sd*bc*sc*i* alias: pci:v000014E4d000016ACsv*sd*bc*sc*i* alias: pci:v000014E4d000016AAsv*sd*bc*sc*i* alias: pci:v000014E4d000016AAsv0000103Csd00003102bc*sc*i* alias: pci:v000014E4d0000164Csv*sd*bc*sc*i* alias: pci:v000014E4d0000164Asv*sd*bc*sc*i* alias: pci:v000014E4d0000164Asv0000103Csd00003106bc*sc*i* alias: pci:v000014E4d0000164Asv0000103Csd00003101bc*sc*i* depends: vermagic: 2.6.18-164.el5 SMP mod_unload gcc-4.1 parm: disable_msi:Disable Message Signaled Interrupt (MSI) (int) parm: enable_entropy:Allow bnx2 to populate the /dev/random entropy pool (int) module_sig: 883f3504a9f766557f09578a977b7e112ebf209f6aab61295466fb787675fec1378b254df8d186609f705f1f37e06d7e2958667d258f6afe6ac1621228

1.从官网下载驱动 http://zh-cn.broadcom.com/support/ethernet_nic/netxtremeii.php

Linux iSCSI HBA only supported on RHEL 5.4, SUSE SLES 11 SP1 or newer versions of these distributions 6.2.23 03/18/11
24MB

2.安装包 unzip linux-6.2.23.zip cd Server/Linux/Driver/ rpm -ivh netxtreme2-6.2.23-1.src.rpm

1:netxtreme2 ########################################### [100%]

3.编译 cd /usr/src/redhat/ rpmbuild -bb SPECS/netxtreme2.spec

Wrote: /usr/src/redhat/RPMS/x86_64/netxtreme2-6.2.23-1.x86_64.rpm Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.43778 + umask 022 + cd /usr/src/redhat/BUILD + cd netxtreme2-6.2.23 + rm -rf /var/tmp/netxtreme2-buildroot /usr/src/redhat/BUILD/file.list.netxtreme2 + exit 0

exit 0表示成功(1-255是失败)

4.安装生成的rpm包 cd RPMS/x86_64/ rpm -ivh netxtreme2-6.2.23-1.x86_64.rpm

Preparing… ########################################### [100%] 1:netxtreme2 ########################################### [100%]

新的驱动会生成在/lib/modules/2.6.18-164.el5/updates/

5.加载驱动 depmod -a modprobe bnx2 service network restart lsmod |grep bnx2

bnx2 209997 0

ethtool -i eth0

driver: bnx2 version: 1.9.3 firmware-version: 5.0.6 NCSI 2.0.3 bus-info: 0000:01:00.0

看着好像没区别,modinfo可以看到详细信息 modinfo bnx2

filename: /lib/modules/2.6.18-164.el5/updates/bnx2.ko version: 2.0.23b license: GPL description: Broadcom NetXtreme II BCM5706/5708/5709/5716 Driver author: Michael Chan srcversion: 6E0DD070AB24C11F50B2712 alias: pci:v000014E4d0000163Csv*sd*bc*sc*i* alias: pci:v000014E4d0000163Bsv*sd*bc*sc*i* alias: pci:v000014E4d0000163Asv*sd*bc*sc*i* alias: pci:v000014E4d00001639sv*sd*bc*sc*i* alias: pci:v000014E4d000016ACsv*sd*bc*sc*i* alias: pci:v000014E4d000016AAsv*sd*bc*sc*i* alias: pci:v000014E4d000016AAsv0000103Csd00003102bc*sc*i* alias: pci:v000014E4d0000164Csv*sd*bc*sc*i* alias: pci:v000014E4d0000164Asv*sd*bc*sc*i* alias: pci:v000014E4d0000164Asv0000103Csd00003106bc*sc*i* alias: pci:v000014E4d0000164Asv0000103Csd00003101bc*sc*i* depends: vermagic: 2.6.18-164.el5 SMP mod_unload gcc-4.1 parm: disable_msi:Disable Message Signaled Interrupt (MSI) (int) parm: stop_on_tx_timeout:For debugging purposes, prevent a chip reset when a tx timeout occurs (int)

观察一段时间没有问题

20110607更新 附加进一步解决方法

在/etc/modprobe.conf中加入一行

options bnx2 disable_msi=1

创建一个脚本来重启服务 vi net.sh

/etc/init.d/network stop rmmod bnx2 modprobe bnx2 /etc/init.d/network start

chmod a+x net.sh ./net.sh

参考: http://www.pcwind.net/dell-r410.html http://bbs.linuxtone.org/thread-3813-1-1.html

Posted in linux 维护优化, 技术.

Tagged with , , , .


Lempelf一键包for rhel/centos 64bit发布

Lempelf一键安装包是什么?

Lempelf一键安装包是用Shell编写的在Linux平台快速安装nginx+php+mysql的Shell程序。

我们为什么需要它?

编译安装需要输入大量的命令,如果是配置生产环境需要耗费大量的时间。 不会Linux的站长或Linux新手想使用Linux作为生产环境……

它有什么优势?

无需一个一个的输入命令,无需值守,编译安装优化编译参数,提高性能,解决不必要的软件间依赖。

介绍及下载:https://blog.c1gstudio.com/lempelfpage

Posted in Lempelf一键包, shell, 技术.

Tagged with .


把xen虚拟机映像文件转化成基于lvm分区的

把xen虚拟机映像文件转化成基于lvm分区的

1.查看当前有两块硬盘 fdisk -l

Disk /dev/sda: 146.8 GB, 146815733760 bytes 255 heads, 63 sectors/track, 17849 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200781 83 Linux /dev/sda2 26 17849 143171280 8e Linux LVM Disk /dev/sdb: 146.8 GB, 146815733760 bytes 255 heads, 63 sectors/track, 17849 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 * 1 17849 143372061 8e Linux LVM

2.查看lmv组 vgscan

Reading all physical volumes. This may take a while… Found volume group “VolGroup00” using metadata type lvm2

只有一个组

3.查看lv分区 lvscan

ACTIVE ‘/dev/VolGroup00/LogVol02’ [30.00 GB] inherit ACTIVE ‘/dev/VolGroup00/LogVol03’ [92.53 GB] inherit ACTIVE ‘/dev/VolGroup00/LogVol01’ [10.00 GB] inherit ACTIVE ‘/dev/VolGroup00/LogVol00’ [4.00 GB] inherit

4.查看pv情况 pvscan

PV /dev/sda2 VG VolGroup00 lvm2 [136.53 GB / 0 free] Total: 1 [136.53 GB] / in use: 1 [136.53 GB] / in no VG: 0 [0 ]

硬盘/dev/sda2 分配给VolGroup00组,第二块硬盘还未分配

5.给第二块硬盘创建pv pvcreate /dev/sdb1

Physical volume “/dev/sdb1” successfully created

6.再次查看pv,已分配 pvscan

PV /dev/sda2 VG VolGroup00 lvm2 [136.53 GB / 0 free] PV /dev/sdb1 lvm2 [136.73 GB] Total: 2 [273.26 GB] / in use: 1 [136.53 GB] / in no VG: 1 [136.73 GB]

7.创建新卷组 vgcreate VolGroup01 /dev/sdb1

Volume group “VolGroup01” successfully created

8.激活新卷组 vgchange -a y VolGroup01

0 logical volume(s) in volume group “VolGroup01” now active

9.查看vm1虚拟机映像文件大小 ls -lh /opt/vm1/vm1.img

-rw-r–r– 1 root root 15G Apr 12 15:32 /opt/vm1/vm1.img

10.创建一个和vm1虚拟机映像文件一样大小的lvm分区 lvcreate -L15G -n Vol01 VolGroup01

Logical volume “Vol01” created

11.关闭虚拟机 xm shutdown vm1

12.复制虚拟机映像文件和配置文件 dd if=/opt/vm1/vm1.img of=/dev/VolGroup01/Vol01

30722048+0 records in 30722048+0 records out 15729688576 bytes (16 GB) copied, 646.077 seconds, 24.3 MB/s

\cp /etc/xen/vm1 /etc/xen/vm3

13.修改虚拟机配置文件 vi /etc/xen/vm3

name = “vm1” uuid = “85386e79-9f79-e243-9b62-3c9da736ae9f” maxmem = 2048 memory = 2048 vcpus = 2 bootloader = “/usr/bin/pygrub” on_poweroff = “destroy” on_reboot = “restart” on_crash = “restart” disk = [ “tap:aio:/opt/vm1/vm1.img,xvda,w” ] vif = [ “mac=00:16:36:47:a2:89,bridge=virbr0,script=vif-bridge” ]

修改name,uuid,mac保持唯一,通常将最后一位加1,修改disk路径. 修改后的配置

name = “vm3” uuid = “85386e79-9f79-e243-9b62-3c9da736ae1f” maxmem = 2048 memory = 2048 vcpus = 2 bootloader = “/usr/bin/pygrub” on_poweroff = “destroy” on_reboot = “restart” on_crash = “restart” disk = [ “phy:/dev/VolGroup01/Vol01,xvda,w” ] vif = [ “mac=00:16:36:47:a2:81,bridge=virbr0,script=vif-bridge” ]

14.启动并进入虚拟机 vm create vm3 -c

15.修改主机名 15.1 hostname vm3

15.2 vi /etc/sysconfig/network

15.3 vi /etc/hosts

16.修改网卡ip和mac,和xen配置对应 vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0 BOOTPROTO=static BROADCAST=192.168.122.255 HWADDR=00:16:36:47:A2:81 IPADDR=192.168.122.13 NETMASK=255.255.255.0 NETWORK=192.168.122.0 ONBOOT=yes

17.重启网络,完成修改 /etc/init.d/network restart

18.测试 18.1 ctrl+]退出虚拟机 18.2启动”vm1″虚拟机 18.3测试vm1网络 ping 192.168.122.11 18.4测试vm3网络 ping 192.168.122.13 18.5测试虚拟机之间的网络 vm console vm3 ping 192.168.122.11

ps:随宿主启动及服务相关iptables不要忘记

参考:https://blog.c1gstudio.com/archives/1215

Posted in 技术, 虚拟化/xen.

Tagged with , .


XEN虚拟机复制

以映像文件方式保存的虚拟机复制非常方便. 只要拷贝xen vm的img镜像文件和配置文件,并修改相应配置文就可以了

1.关闭虚拟机 xm shutdown vm1

2.复制虚拟机映像文件和配置文件 \cp /opt/vm1/vm1.img /opt/vm1/vm2.img \cp /etc/xen/vm1 /etc/xen/vm2

3.修改虚拟机配置文件 vi /etc/xen/vm2

name = “vm1” uuid = “85386e79-9f79-e243-9b62-3c9da736ae9f” maxmem = 2048 memory = 2048 vcpus = 2 bootloader = “/usr/bin/pygrub” on_poweroff = “destroy” on_reboot = “restart” on_crash = “restart” disk = [ “tap:aio:/opt/vm1/vm1.img,xvda,w” ] vif = [ “mac=00:16:36:47:a2:89,bridge=virbr0,script=vif-bridge” ]

修改name,uuid,mac保持唯一,通常将最后一位加1,修改disk路径. 修改后的配置

name = “vm2” uuid = “85386e79-9f79-e243-9b62-3c9da736ae0f” maxmem = 2048 memory = 2048 vcpus = 2 bootloader = “/usr/bin/pygrub” on_poweroff = “destroy” on_reboot = “restart” on_crash = “restart” disk = [ “tap:aio:/opt/vm1/vm2.img,xvda,w” ] vif = [ “mac=00:16:36:47:a2:80,bridge=virbr0,script=vif-bridge” ]

4.启动并进入虚拟机 vm create vm2 -c

5.修改主机名 5.1 hostname vm2

5.2 vi /etc/sysconfig/network

5.3 vi /etc/hosts

6.修改网卡ip和mac,和xen配置对应 vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0 BOOTPROTO=static BROADCAST=192.168.122.255 HWADDR=00:16:36:47:A2:80 IPADDR=192.168.122.12 NETMASK=255.255.255.0 NETWORK=192.168.122.0 ONBOOT=yes

7.重启网络,完成修改 /etc/init.d/network restart

8.测试 8.1 ctrl+]退出虚拟机 8.2启动”vm1″虚拟机 8.3测试vm1网络 ping 192.168.122.11 8.4测试vm2网络 ping 192.168.122.12 8.5测试虚拟机之间的网络 vm console vm2 ping 192.168.122.11

ps:随宿主启动及服务相关iptables不要忘记

Posted in 技术, 虚拟化/xen.

Tagged with , .