• /bin/rm: argument list too long 的处理方法

    #!/bin/bash

    # 设定需要删除的文件夹
    RM_DIR=’/data/files’

    cd $RM_DIR
    for I in `ls`
    do
    rm -f $I
    done

  • IE里Cookie跨域不能读取

    1.页面里的COOKIE不能是浏览器进程的COOKIE(包括验证票和不设置超时时间的COOKIE),否则跨域会取不到.这点做跨域COOKIE的人比较少提到.不过实际上留意下几家大学做的方案,有细微的提到他们的验证模块里的COOKIE是有设置超时时间的.

    2.当利用IFRAME时,记得要在相应的动态页的页头添加一下P3P的信息,否则IE会自觉的把IFRAME框里的COOKIE给阻止掉,产生问题.本身不保存自然就取不到了.这个其实是FRAMESET和COOKIE的问题,用FRAME或者IFRAME都会遇到.

    3.测试时输出TRACE,会减少很多测试的工作量.

    只需要设置 P3P HTTP Header,在隐含 iframe 里面跨域设置 cookie 就可以成功。他们所用的内容是:

    P3P: CP=’CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR’

    ASP直接在头部加了头部申明,测试有效。
    <%Response.AddHeader "P3P", "CP=CAO PSA OUR"%>

    php的话,应该是如下写法:
    -PHP
    header('P3P: CP=CAO PSA OUR');

    ASP.NET的话
    通过在代码上加
    -C#
    Response.AddHeader("P3P", "CP=CAO PSA OUR");

    或者在Window服务中将ASP.NET State Service 启动。

    JSP:
    response.setHeader("P3P","CP=CAO PSA OUR")

    英语原文解析
    You can add a P3P compact policy header to your child content, and you can declare that no malicious actions are performed with the data of the user. If Internet Explorer detects a satisfactory policy, then Internet Explorer permits the cookie to be set.

    Visit the following MSDN Web site for a complete list of satisfactory and unsatisfactory policy codes:

    Privacy in Internet Explorer 6
    http://msdn.microsoft.com/workshop/security/privacy/overview/privacyie6.asp
    A simple compact policy that fulfills this criteria follows:

    P3P: CP=”CAO PSA OUR”

    This code sample shows that your site provides you access to your own contact information (CAO), that any analyzed data is only “pseudo-analyzed”, which means that the data is connected to your online persona and not to your physical identity (PSA), and that your data is not supplied to any outside agencies for those agencies to use (OUR).

    You can set this header if you use the Response.AddHeader method in an ASP page. In ASP.NET, you can use the Response.AppendHeader method. You can use the IIS Management Snap-In (inetmgr) to add to a static file.

    Follow these steps to add this header to a static file:

    1. Click Start, click Run, and then type inetmgr.
    2. In the left navigation page, click the appropriate file or directory in your Web site to which you want to add the header, right-click the file, and then click Properties.
    3. Click the HTTP Headers tab.
    4. In the Custom HTTP Headers group box, click Add.
    5. Type P3P for the header name, and then for the compact policy string, type CP=…, where “…” is the appropriate code for your compact policy.

    Alternatively, Internet Explorer users can modify their privacy settings so that they are prompted to accept third party content. The following steps show how to modify the privacy settings:

    1. Run Internet Explorer.
    2. Click Tools, and then click Internet Options.
    3. Click the Privacy tab, and then click Advanced.
    4. Click to select the Override automatic cookie handling check box.
    5. To allow ASP and ASP.NET session cookies to be set, click to select the Always allow session cookies check box.
    6. To receive a prompt for any type of third party cookie, click Prompt in the Third-party Cookies list.

  • linux mrtg服务器网络监控

    其它软件包的检查:
    [root@mail doc]# rpm -qa|grep gd
    gd-1.8.4-4
    gd-devel-1.8.4-4

    [root@mail doc]# rpm -qa|grep perl
    perl-5.6.0-17
    mod_perl-1.24_01-3

    [root@mail doc]# rpm -qa|grep libp
    libpng-1.0.12-2
    libpng-devel-1.0.12-2

    [root@mail doc]# rpm -qa|grep zlib
    zlib-1.1.3-24
    zlib-devel-1.1.3-24
    [root@mail doc]# rpm -qa|grep gcc
    gcc-2.96-98
    gcc-g77-2.96-98
    gcc-c++-2.96-98

    目前mrtg的最新版本为mrtg-2.16.1:
    wget ftp://ftp.idilis.ro/mirrors/mrtg/mrtg-2.16.1.tar.gz
    [root@mail src]# tar xvfz mrtg-2.16.1.tar.gz
    [root@mail src]# cd mrtg-2.16.1
    [root@mail mrtg-2.16.1]# ./configure –prefix=/data/mrtg
    [root@mail mrtg-2.16.1]# make
    [root@mail mrtg-2.16.1]# make install

    到现在我们就已经正确地安装了MRTG系统。

    配置SNMP服务

    对于不同的设备,配置SNMP支持的方法是不一致的,具体请参考设备的随机文档,一般里 面都有详细的介绍。这里我们讨论在Linux环境下配置SNMP服务器,以实现对本机流出流入数据的分析和报表(我的应用环境是使用Linux带动一个小型局域网上网,监控本机进出 流量)。

    在linux环境下安装snmp软件包是很容易的,只需要安装相应的软件包即可:

    [root@mail doc]# rpm -qa|grep snmp
    net-snmp-libs-5.3.1-14.el5
    net-snmp-libs-5.3.1-14.el5
    net-snmp-5.3.1-14.el5

    [root@mail doc]# /etc/rc.d/init.d/snmpd start
    Starting snmpd: [ OK ]

    如果命令输出如上所示,就表示snmp服务器启动正常。

    为了配合mrtg使用,还要修改snmpd的配置,以使其允许mrtg读取其interface(网络接口) 流量数据。

    vi /etc/snmp/snmpd.conf
    装下面这个的#注
    view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
    然后将

    access notConfigGroup “” any noauth exact systemview none none

    修改为:

    access notConfigGroup “” any noauth exact mib2 none none

    在55行左右加入:
    view    systemview    included   .1.3.6.1.2.1.2

    然后再重新启动snmpd:

    /etc/rc.d/init.d/snmpd restart

    生成MRTG配置文件

    # mkdir /data/mrtg/cfg/

    配置文件:/data/mrtg/cfg/mrtg.cfg

    # cd /data/mrtg/bin

    # ./cfgmaker –global ‘WorkDir: /data/apache2/htdocs/mrtg’ –global ‘Options[_]: bits,growright’  –output /data/mrtg/cfg/mrtg.cfg  public@192.168.3.9

    更新信息.
    env LANG=C /data/mrtg/bin/mrtg /data/mrtg/cfg/mrtg.cfg

    # mkdir /data/apache2/htdocs/mrtg

    生成mrtg的index文件

    # ./indexmaker –output  /data/apache2/htdocs/mrtg/index.html /data/mrtg/cfg/mrtg.cfg

    <VirtualHost mrgt.yourdomanin.com>
    servername mrgt.yourdomanin.com
    documentroot /data/apache2/htdocs/mrtg
    customlog logs/mrgt.yourdomanin.com common
    </VirtualHost>

    <directory “/data/apache2/htdocs/mrtg/”>
    options followsymlinks  includes
    allowoverride none
    order allow,deny
    allow from all
    authname “MRTG流量察看”
    authtype basic
    authuserfile /data/apache2/passdir/mrtgpass
    require user 4ujk
    </directory>

    生成密码:
    mkdir /data/apache2/passdir/
    /data/apache2/bin/htpasswd -bc /data/apache2/passdir/mrtgpass username userpass

    让系统每隔5分钟执行一次mrtg,生成新的MRTG流量图
    # crontab –e
    */5 * * * * env LANG=C /data/mrtg/bin/mrtg /data/mrtg/cfg/mrtg.cfg

    =============================================

  • mysql 日期格式与时间戳的相互转换

    2008-08-08  ->1178845140

    update 表名 set l字段名=UNIX_TIMESTAMP(字段名)

    1178845140 ->2008-08-08

    update 表名 set l字段名=FROM_UNIXTIME(字段名)

  • linux 常用命令

    vi编辑器黛色

    echo "syntax enable" >> ~/.vimrc

    网络连接状态

    netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn

    squid缓存状态

    squidclient -h 211.154.255.103 -p 80 mgr:info

    从日志文件中找出若干匹配的行

    tail -n 22000 122.225.115.199.log |grep -E 'CST 2011|Total space in arena|StoreEntries with MemObjects' | more

    Debian 管理服务 需要先安装个rcconf,然后直接用:

    rcconf

    cpu信息

    cat /proc/cpuinfo

    硬件信息

    ls+tab,会出一堆命令,基本是这些

    MySQL的mysqldump工具的基本用法

    几个常用用例:

    1.导出整个数据库

    mysqldump -u 用户名 -p 数据库名 > 导出的文件名    
     mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql

    2.导出一个表

    mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
     mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql

    3.导出一个数据库结构

     mysqldump -u wcnc -p -d --add-drop-table smgp_apps_wcnc >d:\wcnc_db.sql
     -d 没有数据 --add-drop-table 在每个create语句之前增加一个drop table

    4.导入数据库 常用source 命令

    进入mysql数据库控制台,
    如mysql -u root -p 
    mysql>use 数据库
    然后使用source命令,后面参数为脚本文件(如这里用到的.sql)
    mysql>source d:\wcnc_db.sql
  • sysctl调整Linux系统性能

    sysctl是一个允许您改变正在运行中的Linux系统的接口。它包含一些 TCP/IP 堆栈和虚拟内存系统的高级选项, 这可以让有经验的管理员提高引人注目的系统性能。用sysctl可以读取设置超过五百个系统变量。基于这点,sysctl(8)提供两个功能:读取和修改系统设置。
    查看所有可读变量:
    %
    sysctl -a
    读一个指定的变量,例如kern.maxproc
    %sysctl kern.maxprockern.maxproc: 1044
    要设置一个指定的变量,直接用variable=value这样的语法:
    #sysctl kern.maxfiles=5000
    kern.maxfiles: 2088 -> 5000
    您可以使用sysctl修改系统变量,也可以通过编辑sysctl.conf文件来修改系统变量。sysctl.conf 看起来很像rc.conf。它用variable=value的形式来设定值。指定的值在系统进入多用户模式之后被设定。并不是所有的变量都可以在这个模式下设定。
    sysctl 变量的设置通常是字符串、数字或者布尔型。 (布尔型用1来表示’yes’,用0来表示’no’)。

    sysctl -w kernel.sysrq=0
    sysctl -w kernel.core_uses_pid=1
    sysctl -w net.ipv4.conf.default.accept_redirects=0
    sysctl -w net.ipv4.conf.default.accept_source_route=0
    sysctl -w net.ipv4.conf.default.rp_filter=1
    sysctl -w net.ipv4.tcp_syncookies=1
    sysctl -w net.ipv4.tcp_max_syn_backlog=2048
    sysctl -w net.ipv4.tcp_fin_timeout=30
    sysctl -w net.ipv4.tcp_synack_retries=2
    sysctl -w net.ipv4.tcp_keepalive_time=3600
    sysctl -w net.ipv4.tcp_window_scaling=1
    sysctl -w net.ipv4.tcp_sack=1

    配置sysctl

    编辑此文件:

    vi /etc/sysctl.conf

    如果该文件为空,则输入以下内容,否则请根据情况自己做调整:

    # Controls source route verification
    # Default should work for all interfaces
    net.ipv4.conf.default.rp_filter = 1
    # net.ipv4.conf.all.rp_filter = 1
    # net.ipv4.conf.lo.rp_filter = 1
    # net.ipv4.conf.eth0.rp_filter = 1

    # Disables IP source routing
    # Default should work for all interfaces
    net.ipv4.conf.default.accept_source_route = 0
    # net.ipv4.conf.all.accept_source_route = 0
    # net.ipv4.conf.lo.accept_source_route = 0
    # net.ipv4.conf.eth0.accept_source_route = 0

    # Controls the System Request debugging functionality of the kernel
    kernel.sysrq = 0

    # Controls whether core dumps will append the PID to the core filename.
    # Useful for debugging multi-threaded applications.
    kernel.core_uses_pid = 1

    # Increase maximum amount of memory allocated to shm
    # Only uncomment if needed!
    # kernel.shmmax = 67108864

    # Disable ICMP Redirect Acceptance
    # Default should work for all interfaces
    net.ipv4.conf.default.accept_redirects = 0
    # net.ipv4.conf.all.accept_redirects = 0
    # net.ipv4.conf.lo.accept_redirects = 0
    # net.ipv4.conf.eth0.accept_redirects = 0

    # Enable Log Spoofed Packets, Source Routed Packets, Redirect Packets
    # Default should work for all interfaces
    net.ipv4.conf.default.log_martians = 1
    # net.ipv4.conf.all.log_martians = 1
    # net.ipv4.conf.lo.log_martians = 1
    # net.ipv4.conf.eth0.log_martians = 1

    # Decrease the time default value for tcp_fin_timeout connection
    net.ipv4.tcp_fin_timeout = 25

    # Decrease the time default value for tcp_keepalive_time connection
    net.ipv4.tcp_keepalive_time = 1200

    # Turn on the tcp_window_scaling
    net.ipv4.tcp_window_scaling = 1

    # Turn on the tcp_sack
    net.ipv4.tcp_sack = 1

    # tcp_fack should be on because of sack
    net.ipv4.tcp_fack = 1

    # Turn on the tcp_timestamps
    net.ipv4.tcp_timestamps = 1

    # Enable TCP SYN Cookie Protection
    net.ipv4.tcp_syncookies = 1

    # Enable ignoring broadcasts request
    net.ipv4.icmp_echo_ignore_broadcasts = 1

    # Enable bad error message Protection
    net.ipv4.icmp_ignore_bogus_error_responses = 1

    # Make more local ports available
    # net.ipv4.ip_local_port_range = 1024 65000

    # Set TCP Re-Ordering value in kernel to ‘5′
    net.ipv4.tcp_reordering = 5

    # Lower syn retry rates
    net.ipv4.tcp_synack_retries = 2
    net.ipv4.tcp_syn_retries = 3

    # Set Max SYN Backlog to ‘2048′
    net.ipv4.tcp_max_syn_backlog = 2048

    # Various Settings
    net.core.netdev_max_backlog = 1024

    # Increase the maximum number of skb-heads to be cached
    net.core.hot_list_length = 256

    # Increase the tcp-time-wait buckets pool size
    net.ipv4.tcp_max_tw_buckets = 360000

    # This will increase the amount of memory available for socket input/output queues
    net.core.rmem_default = 65535
    net.core.rmem_max = 8388608
    net.ipv4.tcp_rmem = 4096 87380 8388608
    net.core.wmem_default = 65535
    net.core.wmem_max = 8388608
    net.ipv4.tcp_wmem = 4096 65535 8388608
    net.ipv4.tcp_mem = 8388608 8388608 8388608
    net.core.optmem_max = 40960

    如果希望屏蔽别人 ping 你的主机,则加入以下代码:

    # Disable ping requests
    net.ipv4.icmp_echo_ignore_all = 1

    编辑完成后,请执行以下命令使变动立即生效:

    /sbin/sysctl -p
    /sbin/sysctl -w net.ipv4.route.flush=1

    我们常常在 Linux 的 /proc/sys 目录下,手动设定一些 kernel 的参数或是直接 echo 特定的值给一个 proc下的虚拟档案,俾利某些档案之开启,常见的例如设定开机时自动启动 IP Forwarding:
    echo “1” > /proc/sys/net/ipv4/ip_forward

    其实,在 Linux 我们还可以用 sysctl command 便可以简易的去检视、设定或自动配置 特定的 kernel 设定。我们可以在系统提示符号下输入「sysctl -a」,摘要如后:abi.defhandler_coff = 117440515

    dev.raid.speed_limit_max = 100000

    net.ipv4.conf.default.send_redirects = 1

    net.ipv4.conf.default.secure_redirects = 1

    net.ipv4.conf.default.accept_redirects = 1

    net.ipv4.conf.default.mc_forwarding = 0

    net.ipv4.neigh.lo.delay_first_probe_time = 5

    net.ipv4.neigh.lo.base_reachable_time = 30

    net.ipv4.icmp_ratelimit = 100

    net.ipv4.inet_peer_gc_mintime = 10

    net.ipv4.igmp_max_memberships = 20

    net.ipv4.ip_no_pmtu_disc = 0

    net.core.no_cong_thresh = 20

    net.core.netdev_max_backlog = 300

    net.core.rmem_default = 65535

    net.core.wmem_max = 65535

    vm.kswapd = 512 32 8

    vm.overcommit_memory = 0

    vm.bdflush = 30 64 64 256 500 3000 60 0 0

    vm.freepages = 351 702 1053

    kernel.sem = 250 32000 32 128

    kernel.panic = 0

    kernel.domainname = (none)

    kernel.hostname = pc02.shinewave.com.tw

    kernel.version = #1 Tue Oct 30 20:11:04 EST 2001

    kernel.osrelease = 2.4.9-13

    kernel.ostype = Linux

    fs.dentry-state = 1611 969 45 0 0 0

    fs.file-nr = 1121 73 8192

    fs.inode-state = 1333 523 0 0 0 0 0

    从上述的语法我们大概可看出 sysctl 的表示法乃把目录结构的「/」以「.」表示,一层一层的连结下去。当然以echo 特定的值给一个 proc下的虚拟档案也是可以用 sysctl加以表示,例如:

    #sysctl –w net.ipv4.ip_forward =”1”

    或是直接在 /etc/sysctl.conf 增删修改特定档案的 0,1值亦可:

    # Enables packet forwarding

    net.ipv4.ip_forward = 1

    # Enables source route verification

    net.ipv4.conf.default.rp_filter = 1

    # Disables the magic-sysrq key

    kernel.sysrq = 0

    当然如果考虑 reboot 后仍有效, 直接在 /etc/sysctl.conf 增删修改特定档案的 0,1值才可使之保留设定(以RedHat 为例,每次开机系统启动后, init 会执行 /etc/rc.d/rc.sysinit,便会使用 /etc/sysctl.conf 的预设值去执行 sysctl)。

    相关参考档案:

    /sbin/sysctl

    /etc/sysctl.conf

    sysctl 及sysctl.conf manpage

    /usr/src/linux-x.y.z/Documentation/sysctl/*

    /usr/share/doc/kernel-doc-x.y.z/sysctl/* (RedHat)

  • 常用php函数

    切换多个 br  成为一个br  preg_replace(“/(<br\s*\/?>\s*)+/”, “<br/>”, $c)

  • MYSQL常用命令

    1.查看MYSQL 数据库编码
    SHOW VARIABLES LIKE ‘character_set_%’;
    mysql> show variables like ‘character_set_%’;
    其中,set_connection是连接编码,latin1是瑞典编码。
    2.导入.TXT文件与导出*.TXT
    load data infile ‘TXT文件的完整路径’ into table 表名;
    linux:TXT文件的完整路径=‘/tmp/aaa.txt’
    windowns:TXT文件的完整路径=’e:\\aaa.txt’
    .txt的文件用Tab隔离
    表中有auto_increment属性的字段,在.txt文件中使用null
    select   *   from  table_name  into  outfile  ‘ path/filename.txt ‘ ;
    3.新增用户
    一:登录进入MySQL数据库后:
    (1).use mysql;
    (2).INSERT INTO `user` VALUES (‘localhost’, ‘newuser’, PASSWORD( ‘newpassword’ ) , ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ”, ”, ”, ”, ‘0’, ‘0’, ‘0’, ‘0’);
    (3).FLUSH PRIVILEGES ;
    提示:PASSWORD函数的运用需要注意版本,参考函数的调用:OLD_PASSWORD
    注意:第三点是插入数据成功后,让他生效的。
    二:用 grant 分配
    grant   all   on   * . *   to  admin @localhost  identified  by   ‘ admin ‘ ;
    说明:  admin 用户(密码为admin)分配所有权限(除grant),对所有数据库和所有表.
    grant   all   on   db_name . *   to  chenlb@ %  identified  by   ‘ chenlbpassword ‘ ;
    说明: chenlb用户有所有权限,只对 db_name的所有表.
    grant   select , insert   on   db_name .tb_name  to  clb @chenlb .com identified  by   ” ;
    说明: clb 用户(密码为空,只有 chenlb.com 主机才有效,
    如果改为%.chenlb.com就对所有后缀为chenlb.com的主机都有效)
    只对 db_name 中的 tb_name 表中有 select,insert 权限.
    权限可以加上(delete,update,drop,create ……)。
    4.重命名表
    ALTER   TABLE  `table_name` RENAME `new_table_neme` ;
    5.改用户密码
    use  mysql;
    update   user   set  password = password(“new_pass”)  where   user = “root”;
    flush  privileges ;
    mysqladmin  – u username  – p pw password newpassword
    6.设远程用户
    use  mysql;
    update   user   set  Host = ‘ % ‘   where   user = ‘ user_name ‘ ;
    flush  privileges ;
    或为特定的IP
    注意:否则,登录时出错–Access denied for user ‘clb’@’localhost’ (using password: YES)
    7.mysql导出\入.sql文件
    导出:
    mysqldump  – u  user_name   – p  [ your_password ]  db_nmae  >  path\ file_name .sql
    如果[your_password]缺省,然后提示输入密码:有密码则输入,否则直接回车
    导入:
    mysql  – u  user   – p  [ your_password ]   db_name   <  path\filename.sql
    同上。
    在linux上导出
    如果用mysqldump导出出现了乱码也没有关系,可以运行iconv来转换一下
    iconv -c -f UTF-8 -t GB2312 库文件名 > 新的gb2312的库文件名
    8.设置密码的几种方法
    //不行1.mysqladmin -u user_name password new_password
    9.符合条件的从那里开始,在多少以内.
    select   *   from  talbe_name limit  0 , 30 ;
    说明:从符合条件的第1条开始,读出30条,如果不够30条,取最大.
    10.添加字段
    ALTER   TABLE  `table_name`  ADD  `colum_name`  VARCHAR (  11  )  NOT   NULL  AFTER `colum_name` ;
    11.函数
    select coalesce(null,3,null)
    返回列表的第一个非NULL值
    select greatest(3,5)
    返回列表的最大值
    select least(2,0)
    返回列表的最小值
    12.加入单引号
    INSERT INTO `userinfo` (`id`, `username`, `lastlogin`)
    VALUES (NULL, ”’陈”’, ‘2006-07-28’);
    说明用两个单引号.
    13.输出警告
    warnings
    14.安装/删除服务
    mysqld  — install MySQL –defaults-file=C:\my-opts.cnf
    mysqld –remove
    15.改变自增值
    ALTER TABLE `table_name` AUTO_INCREMENT =1
    16.设主键
    ALTER TABLE `table_name` ADD PRIMARY KEY (`id`)
    17.搜索
    SELECT * FROM `email_list` WHERE relation LIKE ‘%老%’
    18.表结构
    DESCRIBE db_name;
    19.添加索引(即:唯一)
    ALTER TABLE `user` ADD UNIQUE (`username`)
    20.删除
    delete from text order by id desc limit 2;
    21.建表
    CREATE TABLE `tt` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `name` VARCHAR( 50 ) NOT NULL
    ) ENGINE = MYISAM ;
    22.帐户
    drop   user  svn @localhost ;

    GRANT ALL PRIVILEGES ON *.* TO admin@”8.8.8.8″ IDENTIFIED BY ‘111111’ WITH GRANT OPTION;

  • MySQL的mysqldump工具的基本用法

    几个常用用例:
    1.导出整个数据库
    mysqldump -u 用户名 -p 数据库名 > 导出的文件名
    mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql
    2.导出一个表
    mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
    mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql
    3.导出一个数据库结构
    mysqldump -u wcnc -p -d –add-drop-table smgp_apps_wcnc >d:\wcnc_db.sql
    -d 没有数据 –add-drop-table 在每个create语句之前增加一个drop table
    4.导入数据库 常用source 命令
    进入mysql数据库控制台,
    如mysql -u root -p
    mysql>use 数据库
    然后使用source命令,后面参数为脚本文件(如这里用到的.sql)
    mysql>source d:\wcnc_db.sql
  • MySQL主从同步配置

    MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。(这与同步复制可以进行对比,同步复 制是MySQL簇的一个特征)。主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个 从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通 知新的更新。
    单向复制有利于健壮性、速度和系统管理:
    ·          主服务器/从服务器设置增加了健壮性。主服务器出现问题时,你可以切换到从服务器。
    ·          通过在主服务器和从服务器之间切分处理客户查询的负载,可以得到 更好的客户响应时间。SELECT查询可以发送到从服务器以降低主服务器的查询处理负荷。但修改数据的语句仍然应发送到主服务器,以便主服务器和从服务器 保持同步。如果非更新查询为主,该负载均衡策略很有效,但一般是更新查询。
    ·          使用复制的另一个好处是可以使用一个从服务器执行备份,而不会干扰主服务器。在备份过程中主服务器可以继续处理更新。
    MYSQL复制需要理解注意的地方:
    MySQL复制基于主服务器在二进制日志中记录所有对数据库的更改(更新、删除等等)。因此,要进行复制,必须在主服务器上启用二进制日志。
    二进制日志只是一个从启用二进制日志的固定时间点开始的数据库操作记录。所以设置任何从服务器时,首先要把从服务器的数据库与主服务器开启二进制时的数据库同步。如果启动从服务器时,其数据库与主服务器数据库启动二进制日志时的状态不相同,从服务器很可能失败。
    MYSQL复制设置步骤:
    1.同步主从数据库
    登录数据库管理命令行,执行FLUSH TABLES WITH READ LOCK语句清空所有表和块的写入语句。
    mysql> FLUSH TABLES WITH READ LOCK;
    进入MYSQL的数据库存放路径,使用tar对要设备主从复制的数据库进行压缩归档:
    shell> tar -cvf /tmp/mysql-snapshot.tar ./this_db
    确认MYSQL当前是否开启了二进制日志,如果当前已经开启了二进制日志请执行:SHOW MASTER     STATUS;命令,记录当前二进志日志文件名和日志的偏移量,这在设置从服务器时用来确定要读取    二进制日志的位置。
    mysql > SHOW MASTER STATUS;
    +—————+———-+————–+——————+
    | File         | Position | Binlog_Do_DB        | Binlog_Ignore_DB |
    +—————+———-+————–+——————+
    | mysql-bin.003 | 73            | test                    | manual,mysql |
    +—————+———-+————–+——————+
    如果当前没有开启二进制日志请设置MYSQL配置文件,开启MYSQL的二进制功能,然后重新启动 MYSQL,登录到MYSQL命令行,重新启用MYSQL写操作。
    mysql> UNLOCK TABLES;
    接下来把刚才压缩备份的数据库复制到从服务器的数据库存放目录,解压,更改这些文件的权限, 确保对这些文件和目录的权限正确,MySQL的运行用户必须能够读写这些文件。
    2.设置MYSQL配置文件
    确保主服务器的mysql配置文件my.cnf文件的[mysqld]部分包括一个log-bin选项,如果需要只   对某一个数据库进行复制,在log-bin下加入binlog-do-db=XXX和一个server-id=Master_id选项,其中server-id的值必须为1到232–1之间的一个正整数值。
    [mysqld]
    log-bin=mysql-bin
    server-id=1
    登录到MYSQL命令行,为从服务器创建一个同步帐户:
    mysql> GRANT REPLICATION SLAVE ON *.* TO ‘repl’@’slave_ip’ IDENTIFIED BY ‘pass’;
    编辑从服务器的MYSQL配置文件,确保从服务器的server-id与主服务器的server-id不冲突。
    [mysqld]
    server-id=2
    3.开始主从同步
    在从服务器上执行下面的语句,用你的系统的实际值替换选项值:
    mysql> CHANGE MASTER TO
    ->MASTER_HOST=’master_host_name’,
    ->MASTER_USER=’replication_user_name’,
    ->MASTER_PASSWORD=’replication_password’,
    ->MASTER_LOG_FILE=’recorded_log_file_name’,
    -> MASTER_LOG_POS=recorded_log_position;
    master_host_name:主服务器的计算机名或IP地址
    replication_user_name:主服务器为从服务器设置的复制帐号
    replication_password:复制帐号密码
    recofded_log_file_name:主从服务器数据库同步时所记录的二进制文件名
    recorded_log_position:主从服务器数据库同步时所记录的二进制偏移量,也就是要读取数据库          日志的位置。
    启动从服务器线程:mysql> START SLAVE;
    执行这些程序后,从服务器应连接主服务器,并执行自从数据库同步以来发生的任何更新。
    从服务器复制时,会在其数据目录中生成dmaster.info和relay-log.info两个 文件。从服务器使用这两个文件记录已经处理了多少主服务器的二进制日志。不要移除或编辑这些文件,除非你清楚你正在做什么并完全理解其含义。即使这样,最 好是使用CHANGE MASTER To语句来修改。

    CHANGE MASTER TOmaster_def[,master_def] …

     
    master_def:
           MASTER_HOST = 'host_name'
         | MASTER_USER = 'user_name'
         | MASTER_PASSWORD = 'password'
         | MASTER_PORT =port_num
         | MASTER_CONNECT_RETRY =count
         | MASTER_LOG_FILE = 'master_log_name'
         | MASTER_LOG_POS =master_log_pos
         | RELAY_LOG_FILE = 'relay_log_name'
         | RELAY_LOG_POS =relay_log_pos
         | MASTER_SSL = {0|1}
         | MASTER_SSL_CA = 'ca_file_name'
         | MASTER_SSL_CAPATH = 'ca_directory_name'
         | MASTER_SSL_CERT = 'cert_file_name'
         | MASTER_SSL_KEY = 'key_file_name'
         | MASTER_SSL_CIPHER = 'cipher_list'

    可以更改从属服务器用于与主服务器进行连接和通讯的参数。

    MASTER_USER,MASTER_PASSWORD,MASTER_SSL,MASTER_SSL_CA,MASTER_SSL_CAPATH,MASTER_SSL_CERT,MASTER_SSL_KEYMASTER_SSL_CIPHER用于向从属服务器提供有关如何与主服务器连接的信息。

    即使对于在编译时没有SSL支持的从属服务器,SSL选项(MASTER_SSL,MASTER_SSL_CA,MASTER_SSL_CAPATH,MASTER_SSL_CERT,MASTER_SSL_KEYMASTER_SSL_CIPHER)也可以被更改。它们被保存到master.info文件中,但是会被忽略,直到您使用一个SSL支持已启用的服务器。

    如果您不指定一个给定的参数,则它会保持其原有的值。例外情况在后面的讨论中进行了说明。举例说明,如果用于连接到您的MySQL主服务器的 密码被更改了,您只需发布这些语句,就可以告知从属服务器新的密码:

    mysql>STOP SLAVE; -- if replication was running
    mysql>CHANGE MASTER TO MASTER_PASSWORD='new3cret';
    mysql>START SLAVE; -- if you want to restart replication

    没有必要指定没有改变的参数(主机、接口、用户等)。

    MASTER_HOSTMASTER_PORT是主服务器主机和其TCP/IP接口的主机名(或IP地址)。注意,如果MASTER_HOSTlocalhost相等,那么,和MySQL的其它部分一样,接口可以被忽略(例如,如果可以使用Unix插槽文件)。

    如果您指定了MASTER_HOSTMASTER_PORT,则从属服务器会假定主服务器与以前不一样(即使您指定的主机或接口值与当前值是一样的。)在此情况下,主服务器二进制日志的名称和位置的原有值不再适用,因此,如果您不指定语句中的MASTER_LOG_FILEMASTER_LOG_POSMASTER_LOG_FILE=”MASTER_LOG_POS=4会被静默地添加。

    MASTER_LOG_FILEMASTER_LOG_POS坐标点,从属服务器I/O线程在启动之后从主服务器读取。如果您只指定了其中一个,则从属服务器不能指定RELAY_LOG_FILERELAY_LOG_POS。如果MSATER_LOG_FILEMASTER_LOG_POS都没有被指定,则从属服务器会使用在CHANGE MASTER被发布前的最后一个slave SQL thread坐标。当您只想改变要使用的 密码时,这可以确保复制的连续性。即使从属服务器SQL线程落后于从属服务器I/O线程,也可以确保复制的连续性。

    CHANGE MASTER会删除所有的中继日志文件并启动一个新的日志,除非您指定了RELAY_LOG_FILERELAY_LOG_POS。在此情况下,中继日志被保持;relay_log_purge全局变量被静默地设置为0

    CHANGE MASTER TO可以更新master.inforelay-log.info文件的内容。

    当您拥有主服务器快照并拥有日志和对应的偏移量时,CHANGE MASTER对于设置从属服务器是有用的。在把快照载入从属服务器之后,您可以在从属服务器上运行CHANGE MASTER TO MASTER_LOG_FILE=’log_name_on_master‘, MASTER_LOG_POS=log_offset_on_master

    举例说明:

    mysql>CHANGE MASTER TO
         ->    MASTER_HOST='master2.mycompany.com',
         ->    MASTER_USER='replication',
         ->    MASTER_PASSWORD='bigs3cret',
         ->    MASTER_PORT=3306,
         ->    MASTER_LOG_FILE='master2-bin.001',
         ->    MASTER_LOG_POS=4,
         ->    MASTER_CONNECT_RETRY=10;
     
    mysql>CHANGE MASTER TO
         ->    RELAY_LOG_FILE='slave-relay-bin.006',
         ->    RELAY_LOG_POS=4025;

    第一个例子可以更改主服务器及其二进制日志坐标。当想要设置从属服务器来复制主服务器时使用。

    第二个例子显示了较少被使用的一个操作。当从属服务器含有中继日志,并且您出于某种原因想要执行此日志时使用。要这么做时,不需要连接主服务器。您只需要使用CHANGE MASTER TO并启动SQL线程(START SLAVE SQL_THREAD)。

    您甚至可以在一个用于独立非从属服务器的非复制型设置中使用第二种操作,在崩溃之后进行复原。假设您的服务器已崩溃,同时您已恢复了备份。您想要重新播放服务器自己的二进制日志(不是中继日志,而是正规的二进制文件),例如名为myhost-bin.*。首先,应在安全的地方制作这些二进制日志的备份,以防您没有完全遵守以下步骤,意外地让服务器清理了二进制文件。使用SET GLOBAL relay_log_purge=0,进一步增加安全性。然后启动不含–log-bin选项的服务器。使用–replicate-same-server-id,–relay-log=myhost-bin(让服务器相信,这些正规的二进制日志是中继日志)和–skip-slave-startoptions选项。当服务器启动后,发布以下语句:

    mysql>CHANGE MASTER TO
         ->    RELAY_LOG_FILE='myhost-bin.153',
         ->    RELAY_LOG_POS=410,
         ->    MASTER_HOST='some_dummy_string';
    mysql>START SLAVE SQL_THREAD;

    服务器会读取并执行自己的二进制日志,完成崩溃复原。当复原完成后,运行STOP SLAVE,关闭服务器,删除master.inforelay-log.info,并使用原来的选项重新启动服务器。

    要让服务器认为它是一个从属服务器,需要指定MASTER_HOST(甚至使用假值)。