mysql数据同步到tc的table表,直接写文件,php读文件查询。
看手册,不解释。
有问题加qq 176300676
编译安装脚本,自理
使用:
不过我们在使用的过程中,发现这个函数有版本兼容的问题,后看代码,发现它的json加密规则比较麻烦,没拆出来分析,就自己使用一个json类 http://sourceforge.net/projects/cjson/ 重写了一遍json_object函数。
目前在mysql-5.0.22版本上使用基本没问题,但在mysql5.5以上版本会有问题,udf接口格式好像有了变化。
mysql-5.0.22版本给出的例子是c++的代码。使用了extern语法,为了兼容gcc的语法。而我实际使用纯C环境,就抛弃了extern的语法。
编译完成,复制so文件至公共库,然后执行以下语句:
使用 :
PHP调用:
结果显示:
]]>array(3) {
["id"]=>
int(2)
["intval"]=>
int(43324)
["value"]=>
string(10) “fdsfdsfdsa”
}
array(3) {
["id"]=>
int(5)
["intval"]=>
int(432432432)
["value"]=>
string(10) “fdsafdsasa”
}
一个很简单的模拟用户访问论坛帖子的loadrunner测试脚本
最近跟测试组学习loadrunner的使用,测试组的姑娘们习惯用界面进行操作,而习惯linux平台使用的我很多功能都使用代码来实现了。
最近用loadrunner写了一个模拟社区用户压测论坛数据库的一个脚本,进行数据库的压力测试和优化工作。
用户行为分析:
在社区中,看帖的人是发帖人的10倍以上,而看帖人大概80%以上都在看新帖,20以下的用户有挖坟行为。
因此,设定如此的比率:每11个用户,1个发帖,8个看最近30%的帖,两个看老的70%的帖。
我的论坛帖子回复表大概是:973505个帖子的回复,两千多万的回帖。
根据这些数据,配合mysql的c api,写如下脚本:
注:脚本的my_mysql_insert()函数是有问题的,多线程下有一个资源符没处理好,因为还不太了解loadrunner的线程机制,所以留下了一个bug。
在做完这个脚本后,我发现我们测试机性能都不错,很难在一个5G大小的单表上主键查询造成很大的压力,所以,计划把dz论坛架设,用php+mysql真实环境下进行压测,这样可以顺便练习http函数下的loadrunner编程。
mysql优化,主要是索引,大量消耗资源的查询一定要做相关索引优化,具体优化思想我是阅读的这本书-http://www.douban.com/subject/3039216/ 里边mysql优化部分讲得非常简洁明了。而对于cpu的优化,就是尽力把整体时间拉开,cpu会比较平稳。具体做法是用sleep把sql语句分隔,这样用时间换取了稳定。当然,有些报表数据一旦作出就不会再变,这时最好把这些数据文本缓存,以后查询只读取文本即可。
]]>sunboyu-amp-fastcgi-suexec-v21-alpha
此脚本在Centos4.7ServerCD 最小安装,root账户下执行完全正常。
]]>准备:
apache2.2.* http://httpd.apache.org/download.cgi 如果有stable版本,最好用stable版,其实最新版作为调试也不错
PHP5.2.* http://www.php.net/downloads.php 目前是5.2.6
mysql5.1 http://dev.mysql.com/downloads/mysql/5.1.html 下载windows安装版本
1、安装Apache,安装路径随意,默认即可,手工管理更好,建议放到 d:\Apache 放D盘是为了以后重装的话,所有的配置文件都还在(备份不仅要备份数据,还要备份不起眼的配置文件)
2、安装Mysql,同上,安装到 D:\Mysql 理由同上
3、解压PHP5.2.×至目录 D:\PHP5 理由还是同上
4、配置环境变量:在桌面上找到“我的电脑”,右键点击“我的电脑”,选择“属性”里的“高级”标签,点击“环境变量”,在“系统变量”里找到“Path”,点击“编辑”按钮,在“变量值”后面加上“;D:\PHP5\;D:\PHP5\ext”,然后点“确定”退出。环境变量配置完成。(windows下Path环境变量是做什么用,google一下先,这样设置后,系统会自动搜索PHP目录里的可执行文件和加载类库)
5、进入PHP5目录下,找到php.ini-dist文件,将其复制到C盘的WINDOWS目录下,并将其文件名修改为“php.ini”。(php会默认从这个路径找php.ini这个文件,当然也可以手工指定这个目录,修改apache的配置文件即可)
6、再次进入PHP5目录下,将以下文件复制到C盘的WINDOWS目录下的System32里:(原因,我也不太清楚,但貌似可以设置一个环境变量指向这个路径就ok,那个方法一直没找到,也没理解)
fdftk.dll
gds32.dll
libeay32.dll
libmcrypt.dll
libmhash.dll
libmysql.dll
msql.dll
news.txt
ntwdblib.dll
7、打开Apache的主目录,进入conf文件夹,找到“httpd.conf”文件,为操作方便起见,建议使用Dreamweaver打开它。打开后在第126行左右找到 LoadModule vhost_alias_module modules/mod_vhost_alias.so,在后面添加以下语句:
LoadModule php5_module “D:/PHP5/php5apache2_2_filter.dll
“AddType application/x-httpd-php .php
添加完成后保存文件。
8、在apache主目录下htdocs下创建index.php文件,内容为<?php phpinfo(); ?>保存。
9、在浏览器的地址栏里输入http://127.0.0.1/index.php。即可打开显示PHP信息的页面。配置成功。
]]>
DB.Mysql.Class.php
DB.Mysql.Fact.Class.php
CREATE VIEW v_user AS SELECT * FROM user LEFT JOIN user_info ON (user.id = user_info.user_id)
然后查看
mysql>use oa
mysql>show tables
结果显示,v_user是库oa中的一个表。因此我们使用的时候,可以直接使用语句 SELECT * FROM v_user
而这句也就等于 SELECT * FROM user LEFT JOIN user_info ON (user.id = user_info.user_id)
而实习在系统中,v_user 只有这么一个文件v_user.frm 也就是只有结构,没有数据和索引.打开后,可以看到详细的存储的内容
TYPE=VIEW
query=select `oa`.`user`.`id` AS `id`,`oa`.`user`.`username` AS `username`,`oa`.`user`.`nickname` AS `nickname`,`oa`.`user`.`password` AS `password`,`oa`.`user`.`lastlogintime` AS `lastlogintime`,`oa`.`user`.`lastloginip` AS `lastloginip`,`oa`.`user`.`loginrand` AS `loginrand`,`oa`.`user_info`.`user_id` AS `user_id`,`oa`.`user_info`.`user_realname` AS `user_realname`,`oa`.`user_info`.`user_sex` AS `user_sex`,`oa`.`user_info`.`user_age` AS `user_age`,`oa`.`user_info`.`user_birthplace` AS `user_birthplace`,`oa`.`user_info`.`user_homeaddress` AS `user_homeaddress`,`oa`.`user_info`.`user_idcard` AS `user_idcard`,`oa`.`user_info`.`user_graduateschool` AS `user_graduateschool`,`oa`.`user_info`.`user_educational` AS `user_educational`,`oa`.`user_info`.`user_graduatetime` AS `user_graduatetime`,`oa`.`user_info`.`user_homephone` AS `user_homephone`,`oa`.`user_info`.`user_cellphone` AS `user_cellphone`,`oa`.`user_info`.`user_workphone` AS `user_workphone`,`oa`.`user_info`.`user_fax` AS `user_fax`,`oa`.`user_info`.`user_email` AS `user_email`,`oa`.`user_info`.`user_oicq` AS `user_oicq`,`oa`.`user_info`.`user_msn` AS `user_msn` from (`oa`.`user` left join `oa`.`user_info` on((`oa`.`user`.`id` = `oa`.`user_info`.`user_id`)))
md5=19f7a28463ad7447c3b4cc5428888fa6
updatable=0
algorithm=0
definer_user=root
definer_host=localhost
suid=1
with_check_option=0
revision=1
timestamp=2008-08-28 11:58:17
create-version=1
source=SELECT * FROM user LEFT JOIN user_info ON ( user.id = user_info.user_id )
都是啥意思,没有深究,只是能根据字面猜出来,如果只是用,到此位置即可。
]]>首先创建表,注,这里使用了adodb中数据字典的描述方法:
然后session类:
session机制,之前已经介绍,实现代码如下
把session与用户id对应,使用以下语句即可
mysql有三部分的内容,session session_data session_expires分别为session的id session的内容,session的过期时间。
创建这样一个数据库
CREATE TABLE IF NOT EXISTS `sessions` (
`session` varchar(255) character set utf8 collate utf8_bin NOT NULL,
`session_expires` int(10) unsigned NOT NULL default ‘0′,
`session_data` mediumtext collate utf8_unicode_ci,
KEY `session` (`session`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
相对于文件方式的session存储,这两个函数是不用动的
function open($save_path, $session_name)
{
global $sess_save_path;
$sess_save_path = $save_path;
return(true);
}
function close()
{
return(true);
}
而需要修改的是读,写,删,过期操作的函数
读的操作,很简单,只是从数据库中,根据sessionid,读出date字段中的内容
function read($sessID) {
global $php_errormsg;
// fetch session-data
$query = ”
SELECT session_data FROM sessions
WHERE session = ‘$sessID’
AND session_expires >
“.time();
$result = $this->mdb2->query($query);
// return data or an empty string at failure
if (MDB2::isError($result)) {
$php_errormsg .= $result->getMessage();
$php_errormsg .= $result->getDebugInfo ();
return false;
}
list($value)=@$result->fetchrow();
return $value;
}
同理,写的操作就是update数据,destory的操作为删除session的记录,另外有个gc的操作是删除过期的session。
具体的代码没有去实现,但在PHP手册上都有。同时手册还介绍了memcache储存的方式,效率应该会不错的。
这是本月最后一篇日志,也算为下月找个题。
]]>Transaction Summary
还有就是mysql,client的问题
checking whether to include mime_magic support… yes
checking for MING support… no
checking for mSQL support… yes
checking mSQL version… 1.0
checking for MSSQL support via FreeTDS… no
checking for MySQL support… yes
checking for specified location of the MySQL UNIX socket… /tmp/mysql.sock
checking for MySQL UNIX socket location… /tmp/mysql.sock
checking for mysql_close in -lmysqlclient… no
checking for mysql_error in -lmysqlclient… no
configure: error: mysql configure failed. Please check config.log for more information.
make: *** No targets specified and no makefile found. Stop.
rpm -Uvh glibc-kernheaders-2.4-9.1.98.EL.x86_64.rpm
rpm -Uvh glibc-headers-2.3.4-2.19.x86_64.rpm
rpm -Uvh glibc-devel-2.3.4-2.19.x86_64.rpm
rpm -Uvh cpp-3.4.5-2.x86_64.rpm
rpm -Uvh gcc-3.4.5-2.x86_64.rpm
rpm -Uvh libstdc++-devel-3.4.5-2.x86_64.rpm
rpm -Uvh gcc-c++-3.4.5-2.x86_64.rpm
自从mysql官方不支持集群后,我就对mysql有点恐怖了,这么依赖,基础应用也快要不行了。
]]>mysql 编译参数
./configure –enable-thread-safe-client –enable-local-infile –enable-largefile –with-charset=utf8 –with-uca –with-gnu-ld –with-pic –with-mysqld-libs –with-comment –with-query-cache –with-bench –with-big-tables –with-innodb –with-mysqld-use=mysql
/usr/local/bin/mysql_install_db
/usr/local/bin/mysqld_safe –user mysql
如果安装时候有错误提示,也许是缺少这个组件 http://www.sunboyu.cn/sourse/termcap-1.3.1.tar.gz
]]>memcached http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
memcache http://pecl.php.net/get/memcache-3.0.1.tgz
libevent http://www.monkey.org/~provos/libevent-1.2a.tar.gz
目前想到的功能大概有这些,想到再加。
目前正在研究每一个组件的性能和参数。
]]>这两条是程序有硬伤的时候,直接报出来。如果逻辑上的错误,咋调呢?我的方法比较奔,但的确有效。
这是程序差错的方法。mysql就更简单了,把你认为有错误的sql语句echo出来,放到phpmyadmin里去执行,看那个报错就行了。
最后一点:下载个星际译王,当然是指英文不太利索的。
]]>http://www.bloodshed.net/ 这个开发工具不错,一个轻量级的。
昨天第一课,用C连接mysql,失败了,继续。
]]>连接分 pconnect connect,connect是每次查询都新建一个连接。pconnect是在同一页面中,所有查询都使用一个连接。
]]>基本能满足一个网管备份mysql的日常操作。
由于是第一个版本,没做太多的功能,实现再说。
这个东东也是展示下PHP功能的强大。PHP代替Perl也不是不可能的。
]]>
进入默认页面,直接回车即可
测试光盘文件的完整性,我一般都跳过
直接下一步
我一般都选择英文,因为命令行里好像不很支持中文
格盘,yes即可。注意:这里是在虚拟机或者一个空机器上进行安装,如果你在其他操作系统上安装双系统,此操作危险!
NEXT,使用默认的即可
YES,继续
分配IP地址,或者使用dhcp,此知识点不懂的话,不归本文管。
设置系统时区,一般用UTC,虽然有时候会用东八区。
设置root帐号的密码
选择定制安装或者是定制安装,当然我们要定制安装。
选择Development中的Development Libraries,Development Tools
选择Base System中的Base和Legacy Software Support
选择这四个,其他的就都不要选了,需要的话再慢慢装。
NEXT开始安装
装完,重启
root登录
这样安装AMP(APACHE MYSQL PHP)即可
]]>安装环境:centos5.0 基本系统+开发工具
安装xml组件
yum install libxml2 libxml2-devel
编译安装 php
./configure –enable-fastcgi –enable-force-cgi-redirect
make && make install
————————————————————————–
[root@localhost php-5.2.5]# make install
Installing PHP SAPI module: cgi
Installing PHP CGI binary: /usr/local/bin/
Installing PHP CLI binary: /usr/local/bin/
Installing PHP CLI man page: /usr/local/man/man1/
Installing build environment: /usr/local/lib/php/build/
Installing header files: /usr/local/include/php/
Installing helper programs: /usr/local/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/lib/php/
[PEAR] Console_Getopt - installed: 1.2.3
[PEAR] Archive_Tar - installed: 1.3.2
[PEAR] Structures_Graph- installed: 1.0.2
pear/PEAR can optionally use package “pear/XML_RPC” (version >= 1.4.0)
[PEAR] PEAR - installed: 1.6.1
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
Installing PDO headers: /usr/local/include/php/ext/pdo/
————————————————————————–
make test
yum安装mysql
yum install mysql mysql-devel mysql-server
复制spawn-fcgi 至 /usr/local/bin/
chmod +x spawn-fcgi
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
使用spawn-fcgi监听127.0.0.1的10080端口 进程10 用户www
————————————————————————–
[root@localhost bin]# spawn-fcgi -a 127.0.0.1 -p 10080 -C 10 -u www -f php-cgi
X-Powered-By: PHP/5.2.5
Content-type: text/html
spawn-fcgi.c.211: child exited with: 0, Success
————————————————————————–
[root@localhost bin]# spawn-fcgi -a 127.0.0.1 -p 10080 -C 10 -u www -f php-cgi
spawn-fcgi.c.190: child spawned successfully: PID: 27936
————————————————————————–
安装pcre
yum install pcre pcre-devel
编译安装 nginx
./configure –user=www –group=www
————————————————————————–
Configuration summary
+ threads are not used
+ using system PCRE library
+ OpenSSL library is not used
+ md5 library is not used
+ sha1 library is not used
+ using system zlib library
nginx path prefix: “/usr/local/nginx”
nginx binary file: “/usr/local/nginx/sbin/nginx”
nginx configuration file: “/usr/local/nginx/conf/nginx.conf”
nginx pid file: “/usr/local/nginx/logs/nginx.pid”
nginx error log file: “/usr/local/nginx/logs/error.log”
nginx http access log file: “/usr/local/nginx/logs/access.log”
nginx http client request body temporary files: “/usr/local/nginx/client_body_temp”
nginx http proxy temporary files: “/usr/local/nginx/proxy_temp”
nginx http fastcgi temporary files: “/usr/local/nginx/fastcgi_temp”
————————————————————————–
make && make install
touche /usr/local/nginx/conf/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
ulimit -SHn 51200
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
配置开机自动启动Nginx + PHP
vi /etc/rc.local
在末尾增加以下内容:
ulimit -SHn 51200
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u www -f /usr/local/bin/php-cgi
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
优化Linux内核参数
vi /etc/sysctl.conf
在末尾增加以下内容:
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000
使配置立即生效:
/sbin/sysctl -p
在不停止Nginx服务的情况下平滑变更Nginx配置
修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/webserver/nginx/sbin/nginx -t
这时,输入以下命令查看Nginx主进程号:
ps -ef | grep “nginx: master process” | grep -v “grep” | awk -F ‘ ‘ ‘{print $2}’
至于日常维护,还不太清楚,咱们下次分解
—————————————————–
Nginx的编译参数如下:
[root@localhost]#./configure –prefix=/usr/local/server/nginx –with-openssl=/usr/include \
–with-pcre=/usr/include/pcre/ –with-http_stub_status_module –without-http_memcached_module \
–without-http_fastcgi_module –without-http_rewrite_module –without-http_map_module \
–without-http_geo_module –without-http_autoindex_module
]]>