2013年3月31日星期日

twemproxy deployment


更新autoconf 
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.68.tar.xz
xz -d autoconf-2.68.tar.xz
tar -xf autoconf-2.68.tar
cd autoconf-2.68
./configure && make && make install
 
wget https://github.com/twitter/twemproxy/zipball/master --no-check-certificate
unzip master
cd twitter-twemproxy
/usr/local/bin/autoreconf -fiv  #autoreconf 版本要是前面装的版本,不是系统默认版本太旧不支持。
./configure --prefix=/home/twemproxy --enable-debug=log
make && make install
cp -R conf /home/twemproxy/
 
启动脚本(start script):
 
#! /bin/sh
 
nutcracker_BIN=/home/twemproxy/bin/nutcracker
nutcracker_CONF=/home/twemproxy/conf/nutcracker.yml
nutcracker_PID=/home/twemproxy/nutcracker.pid
 
nutcracker_opts="-d -o /home/twemproxy/nut.log -s 22120 -c $nutcracker_CONF -p $nutcracker_PID -v 5 -m 512"
 
 
wait_for_pid () {
        try=0
 
        while test $try -lt 35 ; do
 
                case "$1" in
                        'created')
                        if [ -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
 
                        'removed')
                        if [ ! -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                esac
 
                echo -n .
                try=`expr $try + 1`
                sleep 1
 
        done
 
}
 
case "$1" in
        start)
                echo -n "Starting nutcracker "
 
                $nutcracker_BIN $nutcracker_opts
 
                if [ "$?" != 0 ] ; then
                        echo " failed"
                        exit 1
                fi
 
                wait_for_pid created $nutcracker_PID
 
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
 
        stop)
                echo -n "Gracefully shutting down nutcracker "
 
                if [ ! -r $nutcracker_PID ] ; then
                        echo "warning, no pid file found - nutcracker is not running ?"
                        exit 1
                fi
 
                kill -QUIT `cat $nutcracker_PID` && rm -rf $nutcracker_PID
 
                wait_for_pid removed $nutcracker_PID
 
                if [ -n "$try" ] ; then
                        echo " Stop failed. "
                        exit 1
                else
                        echo " done"
                fi
        ;;
 
        restart)
                $0 stop
                $0 start
        ;;
 
        reload)
 
                echo -n "Reload service nutcracker "
 
                if [ ! -r $nutcracker_PID ] ; then
                        echo "warning, no pid file found - nutcracker is not running ?"
                        exit 1
                fi
 
                kill -USR2 `cat $nutcracker_PID` && rm -rf $nutcracker_PID
 
                echo " done"
        ;;
 
        *)
                echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
        ;;
 
esac
 

分析 access 时出现 No space left on device


 
[root@SHANGH21-108-DX-APP logs]# cat access.log |sort -k 6 |cut -d ' ' -f 2,7 |sort -k 3 |uniq -c |sort -k 1 |tail -n 10
sort: write failed: /tmp/sortPGEq4w: No space left on device
 
 
sort 指定tmp 目录,找一个大一点的分区:
 
 cat access.log |sort -T /dev/shm   -k 6 |cut -d ' ' -f 2,7

ttserver 无法存数据一例


[root@ua2 ~]# tailf /diska/ttserver/ttserver.log
2012-11-15T17:50:07+08:00       ERROR   do_mc_set: operation failed
2012-11-15T17:50:07+08:00       ERROR   do_mc_set: operation failed
2012-11-15T17:50:07+08:00       ERROR   do_mc_set: operation failed
2012-11-15T17:50:07+08:00       ERROR   do_mc_set: operation failed
2012-11-15T17:50:07+08:00       ERROR   do_mc_set: operation failed
2012-11-15T17:50:08+08:00       ERROR   do_mc_set: operation failed
2012-11-15T17:50:08+08:00       ERROR   do_mc_set: operation failed
2012-11-15T17:50:08+08:00       ERROR   do_mc_set: operation failed
2012-11-15T17:50:08+08:00       ERROR   do_mc_set: operation failed
停掉服务, 移走原来的ktserver 数据目录,重建目录,重启服务

mysql 导出所有用户权限列表shell

#!/bin/bash
#Function export user privileges

expgrants()
{
  mysql -B -u'root' -p'123456' -N $@ -e "SELECT CONCAT(
    'SHOW GRANTS FOR ''', user, '''@''', host, ''';'
    ) AS query FROM mysql.user"
| \
  mysql -u'root' -p'123456' $@ | \
  sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/-- \1 /;/--/{x;p;x;}'
}
expgrants > ./grants.sql

mysqldump (正确导入数据)的另一种做法


首先是从原表里面导出数据,由于旧的表数据格式是latin1 ,需要更新为utf8;
mysqldump -t --default-character-set=latin1 56_flash_flv admin_56_master_log2012 > admin_56_master_log2012
 
由于数据的量不是很大,导出来后通过notpead++ 进行转换。
格式-->转为UTF8 无BOM 格式-->保存文件。
 
然后把文件放到另外一台机器,执行mysqldump 的导入命令:
mysqldump --default-character-set=utf8 backend_56 admin_56_master_log2012 < admin_56_master_log2012
 
这样执行后,命令一直没有报错,但是执行完后表里面没有数据。几行数据的话,复制粘贴想怎么折腾就怎么折腾。数据
多了就的考虑方式了和方法了。
看了一下dump出来文件的内容,可以发现一个INSERT 开头的一行。担心问题不知道出在哪里,所以是单个表导出的。
 
cat admin_56_master_log|grep ^INSERT |mysql --default-character-set=utf8 56_flash_flv
 
这招貌似也可以啊,嘻嘻!
 
那么导数据究竟是什么原因呢?按照导出的规则,这个看似没有问题啊,
mysqldump --default-character-set=utf8 backend_56 admin_56_master_log2012 < admin_56_master_log2012
 
后面试了一下,发现原来是后面不应该有表名,只要有database 就可以了,insert 的时候会自己去找对应的表。

mysql 从库重建手记


挂的是一台监控系统的从库,起不来,怎么都起不来,没办法重建。
 
mysqldump --single-transaction --master-data monitor > monitor.sql
 
mysqldump: Got errno 28 on write
没想到空间满了:
du -sh mysqllogsa/
222G    mysqllogsa/
 
mysql> show variables like '%expire_logs_days%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 15    |
+------------------+-------+
1 row in set (0.00 sec)
 
mysql>
 
要换成7 天啦:
mysql> set global expire_logs_days=7;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show variables like '%expire_logs_days%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 7     |
+------------------+-------+
1 row in set (0.00 sec)
 
mysql>
命令改完了记得也要改配置文件参数:my.cnf:
expire_logs_days = 7
 
执行删除的命令:
 
mysql> PURGE MASTER LOGS BEFORE '2013-01-24 15:00:00';
Query OK, 0 rows affected (1.09 sec)
 
瞬间变成了一半大小了:
 
[root@NANY181-209-SJHL-APP diska]# du -sh
107G    .
 
再次执行dump 命令(南无阿弥陀佛):
mysqldump --single-transaction --master-data monitor > monitor.sql
 
搞完之后,将文件复制到备库。
 
备库上的操作:
mkdir mysqldatasa
chown -R mysql:mysql mysqldatasa/
 
配置好my.cnf 文件
启动mysql:检查错误文件发现:
Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
 
没有mysql 表,那当然不行了,创建:
cd /home/percona-mysql-5.5.23/scripts
./mysql_install_db --datadir=/diskb/mysqldatasa/ --basedir=/home/percona-mysql-5.5.23/ --defaults-file=/diskb/mysqldatasa/my.cnf
 
后面发现起不来,
/bin/sh /home/percona-mysql-5.5.23/bin/mysqld_safe --defaults-file=/diskb/mysqldatasa/my.cnf &
 
把旧的文件删除掉,重新来过,后面补充一个 --user 参数,再试,就可以了:
 
./mysql_install_db --datadir=/diskb/mysqldatasa/ --basedir=/home/percona-mysql-5.5.23/ --defaults-file=/diskb/mysqldatasa/my.cnf --user=mysql
 
数据库起来后,进去创建一个database monitor;
 
然后就开始导入数据了:
 
[root@NANY181-206-SJHL-APP backup]# mysql -uroot --socket=/dev/shm/mysql.sock monitor < monitor.sql
 
 
这个时间未知啊,先不管吧,晚上吃饭,要管肚子先
 
 
数据都dump 到数据库里面去了,现在开始设置从库了:
mysql> CHANGE MASTER TO  MASTER_HOST='10.26.80.209', MASTER_USER='dbbak', MASTER_PASSWORD='justdoit', MASTER_LOG_FILE='mysql-bin.003360', MASTER_LOG_POS=470705080,MASTER_PORT=49710;
Query OK, 0 rows affected (0.00 sec)
 
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
 
检查从库的状态:好像有报错哦,提示是表不存在:
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.26.80.209
                  Master_User: dbbak
                  Master_Port: 49710
                Connect_Retry: 60
              Master_Log_File: mysql-bin.003361
          Read_Master_Log_Pos: 94436927
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 2370253
        Relay_Master_Log_File: mysql-bin.003360
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 1146
                   Last_Error: Error 'Table 'monitor.app_alarm_data_20130201_03' doesn't exist' on query. Default database: 'monitor'. Query: 'replace into App_alarm_data_20130201_03 values(67112963, 2610160506, 157, '3081')'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 473075080
              Relay_Log_Space: 160203490
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 1146
               Last_SQL_Error: Error 'Table 'monitor.app_alarm_data_20130201_03' doesn't exist' on query. Default database: 'monitor'. Query: 'replace into App_alarm_data_20130201_03 values(67112963, 2610160506, 157, '3081')'
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 80209
1 row in set (0.00 sec)
 
ERROR:
No query specified
 
mysql>
 
根据错误提示,发现这个表不存在,在主库上show tables 看了一下,今天的日表都没有,一直报错来着,于是创建这个表咯。
另外有一个跳过此错误的设置,我这里由于错误过多,也不好skip 了
mysql> slave stop;
Query OK, 0 rows affected (0.22 sec)
 
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;
Query OK, 0 rows affected (0.00 sec)
 
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
 
slave stop; slave start; 了几次,终于ok 了
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.26.80.209
                  Master_User: dbbak
                  Master_Port: 49710
                Connect_Retry: 60
              Master_Log_File: mysql-bin.003362
          Read_Master_Log_Pos: 278331406
               Relay_Log_File: mysql-relay-bin.000037
                Relay_Log_Pos: 111368755
        Relay_Master_Log_File: mysql-bin.003362
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 278331406
              Relay_Log_Space: 111369057
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 80209
1 row in set (0.00 sec)
 
ERROR:
No query specified
 
mysql>

nginx rewrite 配置两例

无法查看这则摘要。请 点击此处查看博文。

perl 取时间段php fpm log


#!/usr/bin/perl
#write by litao
#date 2013-2-21
#for filter php php-fpm log between two times
use Date::Manip;
if ($#ARGV != 1 ) {
    print "usage: php-fpm.pl start-time end-time\n";
    print "date format like:\n";
    print "21-Feb-2013 00:00:00";
    exit;
}

#shift date to date variable
my $datestr1=$ARGV[0];
my $datestr2=$ARGV[1];
my $date1 = new Date::Manip::Date;
my $date2 = new Date::Manip::Date;

$date1->parse($datestr1);
$date2->parse($datestr2);

open(PHPLOG, "php-fpm.log");
open(PLOT,">>output") || die ("This file will not open");
while (<PHPLOG>) {
    my $line = $_;
    chomp($line);
    (my $subtimestr) = $line =~ /\[(.*\d+:\d+:\d+)\]/;

    my $subtime1 = $date1->new_date;
    $subtime1->parse($subtimestr);
    my $result1 = $date1->cmp($subtime1);

    my $subtime2 = $date2->new_date;
    $subtime2->parse($subtimestr);
    my $result2 = $date2->cmp($subtime2);

    next if ($result1 == 1);
    last if ($result2 == -1);
    print PLOT "$line\n" if ( $result1 < 1 and $result2 > -1 );

}
close(<PHPLOG);
close(<PLOT);

查看mysql 整个库大小、单个库大小,表大小


mysql> use information_schema;
Database changed
 
#查看整个所有库大小:
 
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data  from TABLES;
+-----------+
| data      |
+-----------+
| 1368.64MB |
+-----------+
1 row in set (10.34 sec)
 
 
#查看单个库大小:
 
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data  from TABLES where table_schema='c2c2012';
+----------+
| data     |
+----------+
| 760.67MB |
+----------+
1 row in set (0.32 sec)
 
 
#查看单个表大小:
 
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data  from TABLES where table_schema='c2c2012' and table_name='c2c_cpm_earn_total_log';
+----------+
| data     |
+----------+
| 447.95MB |
+----------+
1 row in set (0.19 sec)
 

nginx location 多root 一例--理解location


 由于应用需求,这个 r 目录需要单独拉出来做处理,nginx 最开始是这样写的:
 
   server {
        root /home/webadm/htdocs;
        index index.php;

       location  /r/ {
            root /diska/htdocs;
        }

        location ~ \.php {
            fastcgi_pass   unix:/home/php/php-fastcgi.sock;
            fastcgi_index  index.php;
            include        fastcgi_params;
            expires off;
        }
   }
 
  最开始的写法是这样的,由于代码从原来的/home/webadm/htdocs/r  拷贝到 /diska/htdocs/r,所以未发现问题。
  代码更新后,发现访问r 下面的 php 相关文件直接 404.
 
  看了一下官方的文档,我们先来理解 
 
  location /r/ {
      ...
  }
# matches any query beginning with /r/ and continues searching,
# so regular expressions will be checked. This will be matched only if
# regular expressions don't find a match.
 
 也就是说当请求的是 /r/example.php 时,nginx 匹配location 后继续查找,最后匹配到 location ~\.php .这时候
 nginx 解释文件的路径在 /home/webadm/htdocs/r/example.php。由于之前的文件本来在那里,所以没发现问题。
 
那么我们再来看看这个location 的解释:
 
  location ~ /r/ {
      ...
  }
# matches any query beginning with /r/ and halts searching,
# so regular expressions will not be checked.
 
 也就是说找到匹配的location r 之后,nginx 将不往下面的 locatioin 处理了。如果将最上面的 location r 换成这
 样的配置,那么php 文件将会被浏览器直接下载:
  location ~ /r/ {
      root /diska/htdocs;
  }
 
 nginx 不支持全局的fastcgi 设定(类似于访问日志,错误日志这样)的配置,所以处理的方法有二:
 
    1. 在 location ~/r/ 中增加fastcgi 的处理。或者增加一个子location 匹配php 的,具体如下:
    
        location ~ /r/ {
            root /diska/htdocs;
            fastcgi_pass   unix:/home/php/php-fastcgi.sock;  #这里也可能是tcp
            include        fastcgi_params;
        }
  
     或者:
 
        location /r/ {
            root /diska/htdocs;
        }
        location ~ /r/.+\.php  {
            fastcgi_pass   unix:/home/php/php-fastcgi.sock;  #这里也可能是tcp
            include        fastcgi_params;
        }
   
     2. 在全局的php 配置中处理该 location:
 
        location /r/ {
            root /diska/htdocs;
        }
        location ~ \.php {
             if ($uri ~ /r/) {   #这里判断是r 目录过来的匹配的php,重新指定其root 目录
                 root /diska/htdocs;
             }
            fastcgi_pass   unix:/home/php/php-fastcgi.sock;
            fastcgi_index  index.php;
            include        fastcgi_params;
            expires off;
        }
    
        另外这里需要注意的是,前面的 location /r/  不能是 location ~/r/ ,否则php 文件会直接下载。
      

perl 统计某个目录下子目录下某类型文件的行数


实例: 统计 /diska/logs/2013-03-06/email_2013-03-06.txt  的行数,日期为变量;
 
这里需要让perl 取多个文件,方法如下:
my $log_dir = '/diska/logs/';
my @files = glob "$log_dir/*/email*.txt ;
 
定义一个计数的变量;
my $c = 0;
 
for (@files) {
   my $file = $_;
   my $lines = `cat $file|wc -l`;   #计算行数,调用shell 命令,这里也可以用perl 的方法实现
   $c  += $lines;
}
print $c;
 
整个程序如下:
 
#!/usr/bin/perl
use strict;
use warnings;

#my $work_dir='/diska/logs';

#my @files= glob "$work_dir/email_20*";
#for (@files) {
#    my $file = $_;
#    my $lines = `cat $file|wc -l`;
#    print $lines;
#}

my $c=0;
my $work_dir='/diska/logs';
my @files= glob "$work_dir/reg_log/2013-*/email_*.txt";
for (@files) {
    my $file = $_;
    my $lines = `cat $file|wc -l`;
    #print $lines;
    $c += $lines;
}
print $c;

nginx rewrite 删掉? 后面的内容


Had a similar problem, after a lot of searching the answer presented itself in the rewrite docs.
If you specify a ? at the end of a rewrite then Nginx will drop the original $args (arguments)
So for your example, this would do the trick:
location ^~ /mypage.php {
    rewrite ^/mypage.php$ http://www.example.com/mypage? permanent;
}

gmagick安装错误

cc-shared  .libs/gmagick_helpers.o .libs/gmagick_methods.o .libs/gmagick.o .libs/gmagickdraw_methods.o .libs/gmagickpixel_methods.o  -L/usr/local/webserver/GraphicsMagick/lib /usr/local/webserver/GraphicsMagick/lib/libGraphicsMagickWand.a -L/usr/lib /usr/local/webserver/GraphicsMagick/lib/libGraphicsMagick.a  -Wl,-rpath -Wl,/usr/local/webserver/GraphicsMagick/lib -Wl,-rpath -Wl,/usr/local/webserver/GraphicsMagick/lib -Wl,-soname -Wl,gmagick.so -o .libs/gmagick.so 
/usr/bin/ld: /usr/local/webserver/GraphicsMagick/lib/libGraphicsMagickWand.a(drawing_wand.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC 
/usr/local/webserver/GraphicsMagick/lib/libGraphicsMagickWand.a: could not read symbols: Bad value 
collect2: ld returned 1 exit status 

make: *** Error 1
系统:
Linux test004 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

使用64位元编译就行
CFLAGS="-O3 -fPIC" ./configure

PHP 版本:PHP 5.4.5
编译这样ok,可是php 无法使用该扩展:
 
/home/php/bin/php: symbol lookup error: /home/php/lib/php/extensions/no-debug-non-zts-20100525/gmagick.so: undefined symbol: omp_get_num_procs
 
 
真是令人无语啊,装个扩展这么麻烦,最后来来回回好几次,终于搞定:
tar -zxvf GraphicsMagick-1.3.15.tar.gz
cd GraphicsMagick-1.3.15
CFLAGS="-O3 -fPIC" ./configure  --enable-shared
 
CFLAGS="-O3 -fPIC" make
make install
 
 
 
 
tar -zxvf gmagick-1.1.2RC1.tgz
 
cd gmagick-1.1.2RC1
 
 
/home/php/bin/phpize
 
CFLAGS="-O3 -fPIC" ./configure --with-php-config=/home/php/bin/php-config
 
 
CFLAGS="-O3 -fPIC" make
 
 
make install
 
 
 
 
 
我编译过失败几次,是因为之前有编译的时候没指定prefix,可能make 的时候没有覆盖成功,因此,删掉了  /usr/lib64 下面的 libGra* 和 /usr/bin/*Gra*

shell 获取时间至毫秒

date +%s%N | cut -b1-13

测试环境php 死掉导致mongo 无法连接


PHP 程序报错:
Fails find all : not master and slaveok=false
 
这个错是一个mongo 的标准错误,是没有配置slaveok 原因。实际情况是这样:
mongo 运行在两台服务器上,各开6个端口,主从结构,测试过各个端口连接都正常。
其中一台服务器有重启过,刚开始怀疑是机器重启的原因。后面拿着程序帐号登录到机器,
进行操作是没有问题的。
纠结好半天,发现原因出自php 挂了,进程还在,重启php 的服务后,搞定。由于当时直接
killall php-fpm,未记录进程的详细信息。
 
看来找问题必须得全面去捕获,不能放弃认为一些不可能的点

配置haproxy 的log


haproxy  的log 默认是有syslog 来处理的。
为了custom log 到固定的位置,我尝试着修改相关的配置。
我的haproxy 安装在 /home/haproxy 下,其它的配置文件也在这个下面,因此想把log 也放在这里,方便查看
 
截取一段haproxy 的配置:
 
listen  http
        log 127.0.0.1 local0
        bind 0.0.0.0:49717
        #balance roundrobin
        mode tcp
        server  s1 10.11.80.153:80
 
截取一段 /etc/syslog.conf 配置:
 
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console
 
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none;local6.none;local0.!*          /var/log/messages
 
local0.*                                                /home/haproxy/logs/haproxy.log
 
需要注意的是,如果上面不加 local0.!* ,那么haproxy 产生的log 会同时写在 /var/log/messages 和 /home/haproxy/logs/haproxy.log 里面
 
截取一段 /etc/sysconfig/syslog  配置:
SYSLOGD_OPTIONS="-m 0 -r"
 
重启 haproxy服务,重启syslog 服务。观察日志。

mysqldump 设置 auto_increment为 1


默认使用mysqldump 出来的数据,设置为auto_increment 的字段的值会根据目前的 id 位置,设置值:
例如其中一个dump 出来的表如下:
 
CREATE TABLE `lrc` (
  `mid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `artist` varchar(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `lrc` text NOT NULL,
  PRIMARY KEY (`mid`),
  UNIQUE KEY `artist` (`artist`(30),`title`(50))
) ENGINE=InnoDB AUTO_INCREMENT=184469 DEFAULT CHARSET=utf8;
 
为了保证数据的相对更一致性,在导入的时候,需要将这些值改为1
 
mysqldump -u username -p -h <db-host> --opt <db-name> -d --single-transaction | sed 's/ AUTO_INCREMENT=[0-9]*\b//' > <filename>.sql

mysqldump 排除mysql db 和information_schema


由于重建从库的时候,刚开始我们可能不需要导出mysql表和information_schema 表,如果是三两个库的话,那还好,
现实情况是我的有些库里面的database 有几百个,之前有想到的做法是先都dump 出来后,再吧这几个库内容删掉。
 
这是从网上google 来的一个脚本:
 
MYUSERNAME=kris
MYPASSWORD=myPassword
MYHOST=database-server.mydomain.com

# Databases to exclude
DBEXCLUDE=db_name_i_dont_want

MYSQL=$(mysql -N -u${USER} -p${PASS} -h${HOST} <<<"SHOW DATABASES" | grep -v ${DBEXCLUDE} | grep -v mysql | grep -v information_schema | grep -v test | tr "\n" " ")

mysqldump -v -u${USER} -p${PASS} -h${HOST} --databases  --skip-lock-tables ${MYSQL} > DB-DUMP.sql
 
改进:
MYSQL=$(mysql -N  --socket=/diska/mysqldata/mysql.sock  <<<"SHOW DATABASES" | grep -v mysql | grep -v information_schema | grep -v test |sed -e "s/^/&'/g" -e "s/$/&'/g" | tr "\n" " ")
 
我们平时也不用这么麻烦用脚本来吧,先试试看看mysql 的命令:
 
[root@kankan ~]# MYSQL=$(mysql <<<"SHOW DATABASES" | grep -v mysql | grep -v information_schema | grep -v test | tr "\n" " ")
[root@kankan ~]# echo $MYSQL
Database app_adv_statistics_20110825 backup c2c c2c2012 game
[root@kankan ~]#
 
这里MYSQL 变量多了一个Database,也记得要把这个拿掉哦
 
 
 

有select 没有select into file 权限


mysql> select distinct ip from T_ipinfo into outfile "/tmp/ip.txt";
ERROR 1045 (28000): Access denied for user 'cds_web'@'211.151.181.209' (using password: YES)
mysql> select distinct ip from T_ipinfo;                          
+-----------------+
| ip              |
+-----------------+
| 10.11.80.102    |
| 10.11.80.103    |
| 10.11.80.108    |
 
 
查看file 的权限选项:
 
mysql> select  user,host,File_priv  from user;
+------------+-----------------------------+-----------+
| user       | host                        | File_priv |
+------------+-----------------------------+-----------+
| root       | localhost                   | Y         |
| root       | SHANGH21-104-DX-APP.opi.com | Y         |
| root       | 127.0.0.1                   | Y         |
| root       | ::1                         | Y         |
| monitor    | 127.0.0.1                   | N         |
| dba        | localhost                   | Y         |
| cds_master | 127.0.0.1                   | N         |
| cds_web    | 211.151.181.21             | N         |
| dbbak      | %                           | N         |
+------------+-----------------------------+-----------+
9 rows in set (0.00 sec)
 
mysql>
 
没有file 的权限就给嘛
mysql> grant FILE on cds.* to cds_web@'211.151.181.21';
ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
 
既然出错了
 
google 之:
After googling i cme to know that “the FILE privileges are global and cannot be applied to a single database”
So correct SQL Statement is:
mysql> grant FILE on *.* to cds_web@'211.151.181.21';
Query OK, 0 rows affected (0.00 sec)
 
mysql>