存档:2009年七月

shell里面如何进行mysql的操作

七月 8, 2009 | linux, mysql | RSS 2.0

参考并致谢:
http://bbs2.chinaunix.net/viewthread.php?tid=971798

下面这个是我学到的,并能操作分表后的多个数据表,根据你分表的数量,这样你可以进行批量修改表的结构等
#!/bin/bash
USER=username
PASS=password
MYSQL=your mysqlbin path
SQLOPT=” -u$USER -p$PASS -S/tmp/mysql-new.sock $@”
DBNAME=your databasename;
TABLENUMBER=80
mysql_opt() {
  db_name=$1
  sql_query=$2
  $MYSQL $SQLOPT << EOF
    USE $db_name;
    set names utf8;
    $sql_query;
    QUIT
EOF
}

for  i in `seq 0 78`
do
mysql_opt $DBNAME “select * from blog_$i limit 2 G;”

sleep 1;

done
如果是简单的执行一条语句,则可以:
mysql -uroot -padmin -e “$sql”

没有评论 »

安装php的调试工具xhprof

七月 5, 2009 | php | RSS 2.0

facebook出的一个php的调试工具:
相关文档在:http://mirror.facebook.net/facebook/xhprof/doc.html
http://pecl.php.net/package/xhprof
首先就是需要到http://pecl.php.net/package/xhprof下载,或者直接
wget http://pecl.php.net/package/xhprof
然后:
tar xzvf xhprof-0.9.2.tgz
cd xhprof-0.9.2
注意在安装前需要安装phpize如果没有安装
在ubuntu下可以apt-get install php5-dev
如果安装不上,则可以

ubuntu 安装:
apt-get install autoconf
apt-get install php5-dev

centos 安装:
yum install php-devel
yum install autoconf

安装完成后
root@liufabin-laptop:/opt/xhprof-0.9.2# cd extension/
root@liufabin-laptop:/opt/xhprof-0.9.2/extension# phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519
下一步非常重要:
  ./configure –with-php-config=/usr/bin/php-config
checking for grep that handles long lines and -e… /bin/grep
checking for egrep… /bin/grep -E
checking for a sed that does not truncate output… /bin/sed
checking for gcc… gcc
checking for C compiler default output file name… a.out
checking whether the C compiler works… yes
checking whether we are cross compiling… no
checking for suffix of executables…
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether gcc accepts -g… yes
checking for gcc option to accept ISO C89… none needed
checking whether gcc and cc understand -c and -o together… yes
checking for system library directory… lib
checking if compiler supports -R… no
checking if compiler supports -Wl,-rpath,… yes
checking build system type… i686-pc-linux-gnu
checking host system type… i686-pc-linux-gnu
checking target system type… i686-pc-linux-gnu
checking for PHP prefix… /usr
checking for PHP includes… -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
checking for PHP extension directory… /usr/lib/php5/20060613+lfs
checking for PHP installed headers prefix… /usr/include/php5
checking for re2c… no
configure: WARNING: You will need re2c 0.12.0 or later if you want to regenerate PHP parsers.
checking for gawk… no
checking for nawk… nawk
checking if nawk is broken… no
checking whether to enable xhprof support… yes, shared
checking for a sed that does not truncate output… (cached) /bin/sed
checking for fgrep… /bin/grep -F
checking for ld used by gcc… /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld… yes
checking for BSD- or MS-compatible name lister (nm)… /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface… BSD nm
checking whether ln -s works… yes
checking the maximum length of command line arguments… 1572864
checking whether the shell understands some XSI constructs… yes
checking whether the shell understands “+=”… yes
checking for /usr/bin/ld option to reload object files… -r
checking how to recognize dependent libraries… pass_all
checking for ar… ar
checking for strip… strip
checking for ranlib… ranlib
checking command to parse /usr/bin/nm -B output from gcc object… ok
checking how to run the C preprocessor… gcc -E
checking for ANSI C header files… yes
checking for sys/types.h… yes
checking for sys/stat.h… yes
checking for stdlib.h… yes
checking for string.h… yes
checking for memory.h… yes
checking for strings.h… yes
checking for inttypes.h… yes
checking for stdint.h… yes
checking for unistd.h… yes
checking for dlfcn.h… yes
checking for objdir… .libs
checking if gcc supports -fno-rtti -fno-exceptions… no
checking for gcc option to produce PIC… -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works… yes
checking if gcc static flag -static works… yes
checking if gcc supports -c -o file.o… yes
checking if gcc supports -c -o file.o… (cached) yes
checking whether the gcc linker (/usr/bin/ld) supports shared libraries… yes
checking whether -lc should be explicitly linked in… no
checking dynamic linker characteristics… GNU/Linux ld.so
checking how to hardcode library paths into programs… immediate
checking whether stripping libraries is possible… yes
checking if libtool supports shared libraries… yes
checking whether to build shared libraries… yes
checking whether to build static libraries… no
configure: creating ./config.status
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing libtool commands
nfig
这个是默认的目录,你可以按照自己安装的目录更改
然后make&&make install 然后make test
即可,然后找到php.ini
添加:

1241 [xhprof]
1242 extension=xhprof.so
1243 ;#
1244 ;# directory used by default implementation of the iXHProfRuns
1245 ;# interface (namely, the XHProfRuns_Default class) for storing
1246 ;# XHProf runs.
1247 ;#
1248 xhprof.output_dir=/opt/xhprof/
 kilall -9 php
然后重启即可
你如果看到这个就算成功了
安装php的调试工具xhprof -  小和尚 - 小和尚真情无限
然后添加测试脚本:

<?php function bar($x) {  if ($x > 0) {    bar($x - 1);  }} function foo() {  for ($idx = 0; $idx < 2; $idx++) {    bar($idx);    $x = strlen("abc");  }} // start profilingxhprof_enable(); // run programfoo(); // stop profiler$xhprof_data = xhprof_disable(); // display raw xhprof data for the profiler runprint_r($xhprof_data);

结果为:Array(    [foo==>bar] => Array        (            [ct] => 2            [wt] => 15        )

    [foo==>strlen] => Array        (            [ct] => 2            [wt] => 0        )

    [bar==>bar@1] => Array        (            [ct] => 1            [wt] => 2        )

    [main()==>foo] => Array        (            [ct] => 1            [wt] => 38        )

    [main()==>xhprof_disable] => Array        (            [ct] => 1            [wt] => 0        )

    [main()] => Array        (            [ct] => 1            [wt] => 45        )

)

具体可以看文档

1条评论 »

同一台机器mysql–replication

七月 5, 2009 | linux, mysql | RSS 2.0

参考文章:
1:http://www.howtoforge.com/one_machine_mysql_replication
2:http://robinwu.javaeye.com/blog/310839
如果机器上已经用apt-get install安装了mysql,则可以用以下方式去除
 1 apt-get –purge remove mysql-server 
2
apt-get –purge remove mysql-common 
创建文件夹/misu:

tar xzvf /mysql-5.0.67.tar.gz.tar.gz

cd /misc//mysql-5.0.67.tar.gz

./configure –prefix=/usr/local/mysql-master

make

make install

cd /usr/local/mysql-master/bin

./mysql_install_db

(it will create a var folder )

cd ../var

cp /misc//mysql-5.0.67.tar.gz/support-files/my-medium.cnf my.cnf

cd ..

groupadd mysql

useradd -g mysql mysql

chown -R root .

chown -R mysql var

chgrp -R mysql

查看my.cnf

[mysqld]

port = 3306

socket = /usr/local/mysql-master/mysql.sock

#skip-networking // we have skip this in our case as we are doing both master and slave on same machine.

# Replication Master Server (default)

# binary logging is required for replication

log-bin

# required unique id between 1 and 2^32 - 1

# defaults to 1 if master-host is not set

# but will not function as a master if omitted

server-id = 1

cd /usr/local/mysql-master/bin

./mysqld_safe –defaults-file=/usr/local/mysql-master/var/my.cnf


MySQL Slave:

Now extract mysql-4.1.12.tar.gz at different dir,

cd /opt/mysql-4.1.12

./configure –prefix=/usr/local/mysql-slave

make

make install

cd /usr/local/mysql-slave

cd bin

./mysql_install_db

(it will create a var folder )

cd ../var

cp /opt/mysql-4.1.12/support-files/my-medium.cnf my.cnf

cd ..

groupadd mysql

useradd -g mysql mysql

chown -R root .

chown -R mysql var

chgrp -R mysql .

Edit my.cnf in the var folder
[mysqld]
port = 3307
socket = /usr/local/mysql-slave/var/mysql.sock

#skip-networking

server-id = 2

# The replication master for this slave – required
master-host = localhost
master-user = slavedb
master-password = q1w2e3r4t5
master-port = 3306
Now starts the mysql server by:

cd /usr/local/mysql-slave/bin

./mysqld_safe –defaults-file=/usr/local/mysql-slave/var/my.cnf &

Configure Replication:

connect to mysql master by:

mysql –sock=/usr/local/mysql-master/mysql.sock

Create account at master for slave:

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO slavedb@”192.168.1.27/255.255.255.0″ identified by ‘q1w2e3r4t5′;Query OK, 0 rows affected (0.28 sec)

connect to mysql slave by:

mysql –sock=/usr/local/mysql-slave/mysql.sock

mysql> slave start;

Query OK, 0 rows affected, 1 warning (0.04 sec)


没有评论 »

安装crawl,libevent,berkeley-db

七月 2, 2009 | linux | RSS 2.0

 项目:http://www.monkey.org/~provos/crawl/
需要安装:libevent和Berkeley
先安装Berkeley
下载到:http://www.oracle.com/technology/software/products/berkeley-db/index.html
然后:tar xzvf  db-4.7.25.tar.gz
比较简单的就是:
cd build_unix/
如果以gcc和g++编译的话,须执行#env CC=gcc CCC=g++ ../dist/configure,

编译.
../dist/configure –prefix=/opt/Berkeley
make
make install

最后:

#vi /etc/ld.so.conf并将berkeleyDB的lib路径加到该文件的最后一行,这样才能找到并加载它的库文件.ld.so.conf是什么东西?它就是系统动态链接库的配置文件。此文件内,存放着可被LINUX共享的动态链接库所在目录的名字(系统目录/lib,/usr/lib除外),各个目录名间以空白字符(空格,换行等)或冒号或逗号分隔。

成功后,测试一段代码是否争:代码为:

编译:

gcc test.c -ggdb -I /opt/Berkeley/include/ -L/opt/Berkeley/lib/ -ldb -lpthread -o test.out
从网上找到的测试代码:红色部分是修改的

#include <db.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h> //添加

/* DB的函数执行完成后,返回0代表成功,否则失败 */
void print_error(int ret)
{
  if(ret != 0)
    printf(”ERROR: %s”,db_strerror(ret));
}

/* 数据结构DBT在使用前,应首先初始化,否则编译可通过但运行时报参数错误 */
void init_DBT(DBT * key, DBT * data)
{
  memset(key, 0, sizeof(DBT));
  memset(data, 0, sizeof(DBT));
}

int  main(){
  DB *dbp;     
  DBT key, data;
  u_int32_t flags;
  int ret;

  char *fruit = “apple”;
  int number = 15;

  typedef struct customer
  {
    int c_id;
    char name[10];
    char address[20];
    int age;
  } CUSTOMER;
  CUSTOMER cust;
  int key_cust_c_id = 1;

  cust.c_id = 1;
  strncpy(cust.name, “javer”, 9);
  strncpy(cust.address, “chengdu”, 19);
  cust.age = 32;

  /* 首先创建数据库句柄 */
  ret = db_create(&dbp, NULL, 0);
  print_error(ret);

  /* 创建数据库标志 */
  flags = DB_CREATE; 

  /* 创建一个名为single.db的数据库,使用B+树访问算法,本段代码演示对简单数据类型的处理
   * */
  ret = dbp->open(dbp, NULL, “single.db”, NULL, DB_BTREE, flags, 0);
  print_error(ret);

  init_DBT(&key, &data);

  /* 分别对关键字和数据赋值和规定长度
   * */
  key.data = fruit;
  key.size = strlen(fruit) + 1;
  data.data = &number;
  data.size = sizeof(int);

  /* 把记录写入数据库中,不允许覆盖关键字相同的记录
   * */
  ret = dbp->put(dbp, NULL, &key, &data,DB_NOOVERWRITE);
  print_error(ret);

  /* 手动把缓存中的数据刷新到硬盘文件中,实际上在关闭数据库时,数据会被自动刷新
   * */
  //dbp->sync();//注释掉没找到错误,注释掉后可以运行,原来的拷贝是中文错误

  init_DBT(&key, &data);

  key.data = fruit;
  key.size = strlen(fruit) + 1;

  /* 从数据库中查询关键字为apple的记录
   * */
  ret = dbp->get(dbp, NULL, &key, &data, 0);
  print_error(ret);

  /* 特别要注意数据结构DBT的字段data为void
   * *型,所以在对data赋值和取值时,要做必要的类型转换。
   * */
  printf(”The number = %d”, *(int*)(data.data));

  if(dbp != NULL)
    dbp->close(dbp, 0);

  ret = db_create(&dbp, NULL, 0);
  print_error(ret);

  flags = DB_CREATE; 

  /* 创建一个名为complex.db的数据库,使用HASH访问算法,本段代码演示对复杂数据结构的处理
   * */
  ret = dbp->open(dbp, NULL, “complex.db”, NULL, DB_HASH, flags, 0);
  print_error(ret);

  init_DBT(&key, &data);

  key.size = sizeof(int);
  key.data = &(cust.c_id);

  data.size = sizeof(CUSTOMER);
  data.data = &cust;

  ret = dbp->put(dbp, NULL, &key, &data,DB_NOOVERWRITE);
  print_error(ret);

  memset(&cust, 0, sizeof(CUSTOMER));

  key.size = sizeof(int);
  key.data = &key_cust_c_id;

  data.data = &cust;
  data.ulen = sizeof(CUSTOMER);
  data.flags = DB_DBT_USERMEM;

  dbp->get(dbp, NULL, &key, &data, 0);
  print_error(ret);

  printf(”c_id = %d name = %s address = %s age = %d”,
      cust.c_id, cust.name, cust.address, cust.age);

  if(dbp != NULL)
    dbp->close(dbp, 0);
  return 0;
}

运行.test.out:

ERROR: DB_KEYEXIST: Key/data pair already exists
The number = 15
ERROR: DB_KEYEXIST: Key/data pair already exists
ERROR: DB_KEYEXIST: Key/data pair already exists
c_id = 1 name = javer address = chengdu age = 32
说明安装成功:以后研究:

然后安装libevent:

cd libevent:

[root@localhost]#cd libevent-1.2[root@localhost]#./configure --prefix=/opt/libevent && make && make install

安装成功后,安装crawl

参数为:

 ./configure --prefix=/opt/crall  --with-libevent=/usr/checking build system type... i686-pc-linux-gnuchecking host system type... i686-pc-linux-gnuchecking target system type... i686-pc-linux-gnuchecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking whether make sets ${MAKE}... yeschecking for working aclocal-1.4... missingchecking for working autoconf... missingchecking for working automake-1.4... missingchecking for working autoheader... missingchecking for working makeinfo... missingchecking for gcc... gccchecking for C compiler default output... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables... checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ANSI C... none neededchecking for a BSD-compatible install... /usr/bin/install -cchecking if we may use "-I/usr/include"... nochecking for libevent... /usr/configure: error: event.h or libevent.a not found in /usr/

而且已经把libevent-1.4.so.2 copy到/usr/lib
为什么不可以呢?

没有评论 »