jimmy's profileJDlogPhotosBlogLists Tools Help

jimmy

March 20

MySQL: 诡异的MySQL server has gone away及其解决

MySQL: 诡异的MySQL server has gone away及其解决

jimmy | 15 三月, 2007 20:32

在Mysql执行show status,通常更关注缓存效果、进程数等,往往忽略了两个值:

Variable_name Value
Aborted_clients 3792
Aborted_connects 376

通常只占query的0.0x%,所以并不为人所重视。而且在传统Web应用上,query错误对用户而言影响并不大,只是重新刷新一下页面就OK了。最近的基础改造中,把很多应用作为service运行,无法提示用户重新刷新,这种情况下,可能就会影响到服务的品质。

通过程序脚本的日志跟踪,主要报错信息为“MySQL server has gone away”。官方的解释是:

The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection.

Some other common reasons for the MySQL server has gone away error are:

  • You (or the db administrator) has killed the running thread with a KILL statement or a mysqladmin kill command.

  • You tried to run a query after closing the connection to the server. This indicates a logic error in the application that should be corrected.

  • A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host.

  • You got a timeout from the TCP/IP connection on the client side. This may happen if you have been using the commands: mysql_options(..., MYSQL_OPT_READ_TIMEOUT,...) or mysql_options(..., MYSQL_OPT_WRITE_TIMEOUT,...). In this case increasing the timeout may help solve the problem.

  • You have encountered a timeout on the server side and the automatic reconnection in the client is disabled (the reconnect flag in the MYSQL structure is equal to 0).

  • You are using a Windows client and the server had dropped the connection (probably because wait_timeout expired) before the command was issued.

    The problem on Windows is that in some cases MySQL doesn't get an error from the OS when writing to the TCP/IP connection to the server, but instead gets the error when trying to read the answer from the connection.

    In this case, even if the reconnect flag in the MYSQL structure is equal to 1, MySQL does not automatically reconnect and re-issue the query as it doesn't know if the server did get the original query or not.

    The solution to this is to either do a mysql_ping on the connection if there has been a long time since the last query (this is what MyODBC does) or set wait_timeout on the mysqld server so high that it in practice never times out.

  • You can also get these errors if you send a query to the server that is incorrect or too large. If mysqld receives a packet that is too large or out of order, it assumes that something has gone wrong with the client and closes the connection. If you need big queries (for example, if you are working with big BLOB columns), you can increase the query limit by setting the server's max_allowed_packet variable, which has a default value of 1MB. You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section A.1.2.9, “Packet too large.

    An INSERT or REPLACE statement that inserts a great many rows can also cause these sorts of errors. Either one of these statements sends a single request to the server irrespective of the number of rows to be inserted; thus, you can often avoid the error by reducing the number of rows sent per INSERT or REPLACE.

  • You also get a lost connection if you are sending a packet 16MB or larger if your client is older than 4.0.8 and your server is 4.0.8 and above, or the other way around.

  • It is also possible to see this error if hostname lookups fail (for example, if the DNS server on which your server or network relies goes down). This is because MySQL is dependent on the host system for name resolution, but has no way of knowing whether it is working — from MySQL's point of view the problem is indistinguishable from any other network timeout.

    You may also see the MySQL server has gone away error if MySQL is started with the --skip-networking option.

    Another networking issue that can cause this error occurs if the MySQL port (default 3306) is blocked by your firewall, thus preventing any connections at all to the MySQL server.

  • You can also encounter this error with applications that fork child processes, all of which try to use the same connection to the MySQL server. This can be avoided by using a separate connection for each child process.

  • You have encountered a bug where the server died while executing the query.


据此分析,可能原因有3:

1,Mysql服务端与客户端版本不匹配。

2,Mysql服务端配置有缺陷或者优化不足

3,需要改进程序脚本

通过更换多个服务端与客户端版本,发现只能部分减少报错,并不能完全解决。排除1。

对服务端进行了彻底的优化,也未能达到理想效果。在timeout的取值设置上,从经验值的10,到PHP默认的60,进行了多次尝试。而Mysql官方默认值(8小时)明显是不可能的。从而对2也进行了排除。(更多优化的经验分享,将在以后整理提供)

针对3对程序代码进行分析,发现程序中大量应用了类似如下的代码(为便于理解,用原始api描述):

$conn=mysql_connect( ... ... );

... ... ... ...

if(!$conn){ //reconnect

$conn=mysql_connect( ... ... );

}

mysql_query($sql, $conn);

这段代码的含义,与Mysql官方建议的方法思路相符[ If you have a script, you just have to issue the query again for the client to do an automatic reconnection. ]。在实际分析中发现,if(!$conn)并不是可靠的,程序通过了if(!$conn)的检验后,仍然会返回上述错误。

对程序进行了改写:

if(!conn){ // connect ...}

elseif(!mysql_ping($conn)){ // reconnect ... }

mysql_query($sql, $conn);

经实际观测,MySQL server has gone away的报错基本解决。

BTW: 附带一个关于 reconnect 的疑问,

在php4x+client3x+mysql4x的旧环境下,reconnet的代码:

$conn=mysql_connect(...) 可以正常工作。

但是,在php5x+client4x+mysql4x的新环境下,$conn=mysql_connect(...)返回的$conn有部分情况下不可用。需要书写为:

mysql_close($conn);

$conn=mysql_connect(...);

返回的$conn才可以正常使用。原因未明。未做深入研究,也未见相关讨论。或许mysql官方的BUG汇报中会有吧。

~~呵呵~~

欢迎转贴,请注明来处。【本帖地址】: http://www.jimmydong.com/blog/post/1/123

MySQL: 4.X 版本数据库向 5.15 以上迁移

MySQL: 4.X 版本数据库向 5.15 以上迁移

jimmy | 15 三月, 2007 13:53

会有两个问题:

1,表结构变化

2,权限表及辅助表变化

解决问题1,官方的建议是全部导出,然后在新的库里面导入。

实际操作中,旧表直接搬迁后是可以继续使用的,只是效率和一些新功能可能会受到影响。保持线上服务的同时,运行mysql_repaire,或者是在phpmyadmnin里面修复一下,就可以了。

注意:要先进行备份。升级后的表,如果需要退回到mysql4x版本,是不能够使用的。

解决问题2,如果不升级也是可以继续使用的,但是在日志中会看到:

070315 13:36:35 [ERROR] mysql.user has no `Event_priv` column at position 29
070315 13:36:35 [ERROR] SCHEDULER: The system tables are damaged. The scheduler subsystem will be unusable during this run.

同时会导致 SHOW VARIABLES; 命令不可用,无法查看系统变量情况。

官方的解决方案是: mysql/bin/mysql_fix_privilege_tables,但实际操作中发现并不能解决(Beta版的bug?)。手工处理会稍微复杂一些,因为表结构变化,直接导出、导入是不行的。

A,导出user表内容到user.sql

B,用scripts/mysql_install_db生成新结构的表,覆盖旧表。

C,修改user.sql,删除create段,修改INSERT段,将SQL语句补全为:

INSERT INTO `user` (`Host`,`User`,`Password`,`Select_priv`,`Inser ... ...) VALUES (.. ... ...)

D,删除user表内容,将user.sql导入。

E,重启数据库。

Done。

~~呵呵~~


欢迎转贴,请注明来处。【本帖地址】: http://www.jimmydong.com/blog/post/1/122

MySQL: Linux机上运行多个版本的MySQL

MySQL: Linux机上运行多个版本的MySQL

jimmy | 14 三月, 2007 10:15

在同一台服务器上部署多个MySQL服务,可以有效提高机器利用率。而且,将不同内容分属不同服务上,也相对更安全些。

MySQL提供了mysqld_multi,但实际应用中没有人用它。弊端:

1,暂停、重启,会导致所有服务都停下来。

2,配置文件冗长,拆分、迁移时麻烦。

业内比较常用的方法是,将数据库数据文件和配置文件,以及开启关闭脚本,共同放在一个目录下。启动服务时,作为一个单独的服务;关闭时对其他服务没有影响。搬迁时,只要将这个目录迁移到目标机器上就OK了。

一个典型的例子:


10.10.82.83 [server2]$ cat start.sh
#!/bin/sh
rundir="/opt/server2";
echo $rundir;
/usr/local/mysql/bin/mysqld_safe --defaults-file="$rundir"/my.cnf --pid-file="$rundir"/mysql.pid --datadir="$rundir"/var --socket="$rundir"/mysql.sock --log-error="$rundir"/err.log --log-slow-queries="$rundir"/slow_query.log&

上例中使用的MySQL是按照官方建议安装在/usr/local/mysql下的。

有时候需要在同一台服务器上使用多个不同版本,比如,测试新版本等。按照以上逻辑,将新MySQLD安装在 /usr/local/mysql-new下,修改 start.sh 为 /usr/local/mysql-new/bin/mysqld_safe。数据库可以正常启动,但是,观察发现并不是新版本。原因何在?

官方的解释是:需要下载源代码版本,编译时指定运行目录。其实并非必须如此。

研究mysqld_safe文件,发现其逻辑为:

A,从命令行取配置参数

B,从配置文件取配置参数

C,对于mysqld的目录(basedir)和数据文件目录(datadir),如果以上都没有在配置中指定,则先检查当前目录是否可用,否则设置/usr/local/mysql为默认。

所以,虽然使用的是mysql-new下的mysqld_safe,但运行的还是/usr/local/mysql/bin/mysqld,也就是旧版本。需要相应修改启动脚本,或者是配置文件:

#!/bin/sh
rundir="/opt/server0";
echo $rundir;
/usr/local/mysql-new/bin/mysqld_safe --defaults-file="$rundir"/my.cnf --ledir=/usr/local/mysql-new/bin --basedir=/usr/local/mysql-new --pid-file="$rundir"/mysql.pid --datadir="$rundir"/var --socket="$rundir"/mysql.sock --log-error="$rundir"/err.log --log-slow-queries="$rundir"/slow_query.log&

这样就可以在同一台机器上同时运行多个版本的Mysql了。


~~呵呵~~

欢迎转贴,请注明来处。【本帖地址】: http://www.jimmydong.com/blog/post/1/121

MySQL: time_format格式

MySQL: time_format格式

jimmy | 13 三月, 2007 14:51

TIME_FORMAT(time,format)

eg: time_format(the_time, '%Y-%m-%d %T')

说明符

说明

%a

工作日的缩写名称 (Sun..Sat)

%b

月份的缩写名称 (Jan..Dec)

%c

月份,数字形式(0..12)

%D

带有英语后缀的该月日期 (0th, 1st, 2nd, 3rd, ...)

%d

该月日期, 数字形式 (00..31)

%e

该月日期, 数字形式(0..31)

%f

微秒 (000000..999999)

%H

小时(00..23)

%h

小时(01..12)

%I

小时 (01..12)

%i

分钟,数字形式 (00..59)

%j

一年中的天数 (001..366)

%k

小时 (0..23)

%l

小时 (1..12)

%M

月份名称 (January..December)

%m

月份, 数字形式 (00..12)

%p

上午(AM)或下午( PM

%r

时间 , 12小时制 (小时hh:分钟mm:秒数ss 后加 AMPM)

%S

(00..59)

%s

(00..59)

%T

时间 , 24小时制 (小时hh:分钟mm:秒数ss)

%U

(00..53), 其中周日为每周的第一天

%u

(00..53), 其中周一为每周的第一天

%V

(01..53), 其中周日为每周的第一天 ; %X同时使用

%v

(01..53), 其中周一为每周的第一天 ; %x同时使用

%W

工作日名称 (周日..周六)

%w

一周中的每日 (0=周日..6=周六)

%X

该周的年份,其中周日为每周的第一天, 数字形式,4位数;%V同时使用

%x

该周的年份,其中周一为每周的第一天, 数字形式,4位数;%v同时使用

%Y

年份, 数字形式,4位数

%y

年份, 数字形式 (2位数)

%%

%’文字字符

~~呵呵~~


欢迎转贴,请注明来处。【本帖地址】: http://www.jimmydong.com/blog/post/1/120

转: apache2 MPM的选择与配置

转: apache2 MPM的选择与配置

jimmy | 06 三月, 2007 20:27

 谈到Apache,大多数系统管理员对其稳定版1.3印象颇深。虽然Apache 2.0的系列开发版早已由Alpha、Beta发展到现在的GA(General Availability)版,但是一些人潜意识里还认为开发版并非可用于生产环境的稳定版本。尤其是1.3版的API与2.0版不兼容,使得大量模块必须要重写才能在2.0版上使用。Apache 1.3和2.0之间的内部变化的确较大,用Apache创始人Brian Behlendorf自己的话来说:“这个版本包括了数百个新的特性,所以这个产品应该具有3.1或8i这样的产品编号,而不是2.0。”Apache 2.0中加入了很多的核心改进和新功能,如Unix线程、多协议支持、新的构建系统、对非Unix平台的更佳支持、IPv6支持、新的Apache API、过滤器、多语言错误响应、原生的Windows NT Unicode支持、更简单化的配置,以及升级的正则表达式库等。它当然还包括对许多模块的重要改进,同时也加入了一些新的模块。

  为了使Apache更加平滑地从1.3版升级到2.0版,Apache开发团队做了很多工作。目前很多重要的模块已经可以很好地支持2.0版,如 PHP、FastCGI、Mod_perl、Mod_python等。在httpd.conf的指令配置语法上,目前的2.0版(2.0.45)与1.3 版的兼容性已做得相当好。比如,以前的2.0版如果要使用PHP,一般用过滤器实现;现在的PHP官方文档中已经使用1.3版中的LoadModule语句做为加载PHP的推荐方式。只要略微了解一下Apache 2.0的新特性,从1.3版升级到2.0版将是一件非常容易的事情。使用Apache 2.0是大势所趋,因为Apache的开发团队已经把开发重心转移到2.0版上。1.3版自2002年10月发布了1.3.27后一直没有新版本推出,而 2.0版在与1.3.27同时发布2.0.43后,在今年1月发布了2.0.44,并于今年3月末发布了2.0.45,并包含了很多改进和修正。

  MPM的引入带来性能改善

  Apache 2.0在性能上的改善最吸引人。在支持POSIX线程的Unix系统上,Apache可以通过不同的MPM运行在一种多进程与多线程相混合的模式下,增强部分配置的可扩充性能。相比于Apache 1.3,2.0版本做了大量的优化来提升处理能力和可伸缩性,并且大多数改进在默认状态下即可生效。但是在编译和运行时刻,2.0也有许多可以显著提高性能的选择。本文不想叙述那些以功能换取速度的指令,如HostnameLookups等,而只是说明在2.0中影响性能的最核心特性:MPM(Multi -Processing Modules,多道处理模块)的基本工作原理和配置指令。

  毫不夸张地说,MPM的引入是Apache 2.0最重要的变化。大家知道,Apache是基于模块化的设计,而Apache 2.0更扩展了模块化设计到Web服务器的最基本功能。服务器装载了一种多道处理模块,负责绑定本机网络端口、接受请求,并调度子进程来处理请求。扩展模块化设计有两个重要好处:

  ◆ Apache可以更简洁、有效地支持多种操作系统;

  ◆ 服务器可以按站点的特殊需要进行自定制。

  在用户级,MPM看起来和其它Apache模块非常类似。主要区别是在任意时刻只能有一种MPM被装载到服务器中。

  指定MPM的方法

  下面以Red Hat Linux 9为平台,说明在Apache 2.0中如何指定MPM (Apache采用2.0.45)。先解压缩源代码包httpd-2.0.45.tar.gz,生成httpd-2.0.45目录(Apache 1.3源代码包的命名规则是apache_1.3.NN.tar.gz,而2.0版则是httpd-2.0.NN.tar.gz,其中NN是次版本号)。

  进入httpd-2.0.45目录,运行以下代码:

$ ./configure --help|grep mpm


  显示如下:

--with-mpm=MPM
Choose the process model for Apache to use.
MPM={beos|worker|prefork|mpmt_os2| perchild|leader|threadpool}


  上述操作用来选择要使用的进程模型,即哪种MPM模块。Beos、mpmt_os2分别是BeOS和OS/2上缺省的MPM,perchild主要设计目的是以不同的用户和组的身份来运行不同的子进程。这在运行多个需要CGI的虚拟主机时特别有用,会比1.3版中的SuExec机制做得更好。 leader和threadpool都是基于worker的变体,还处于实验性阶段,某些情况下并不会按照预期设想的那样工作,所以Apache官方也并不推荐使用。因此,我们主要阐述prefork和worker这两种和性能关系最大的产品级MPM ( 有关其它的MPM详细说明,请参见Apache官方文档:http://httpd.apache.org/docs-2.0/mod/)。

  prefork的工作原理及配置

  如果不用“--with-mpm”显式指定某种MPM,prefork就是Unix平台上缺省的MPM。它所采用的预派生子进程方式也是Apache 1.3中采用的模式。prefork本身并没有使用到线程,2.0版使用它是为了与1.3版保持兼容性;另一方面,prefork用单独的子进程来处理不同的请求,进程之间是彼此独立的,这也使其成为最稳定的MPM之一。

  若使用prefork,在make编译和make install安装后,使用“httpd -l”来确定当前使用的MPM,应该会看到prefork.c(如果看到worker.c说明使用的是worker MPM,依此类推)。再查看缺省生成的httpd.conf配置文件,里面包含如下配置段:


StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0


  prefork的工作原理是,控制进程在最初建立“StartServers”个子进程后,为了满足MinSpareServers设置的需要创建一个进程,等待一秒钟,继续创建两个,再等待一秒钟,继续创建四个……如此按指数级增加创建的进程数,最多达到每秒32个,直到满足 MinSpareServers设置的值为止。这就是预派生(prefork)的由来。这种模式可以不必在请求到来时再产生新的进程,从而减小了系统开销以增加性能。

  MaxSpareServers设置了最大的空闲进程数,如果空闲进程数大于这个值,Apache会自动kill掉一些多余进程。这个值不要设得过大,但如果设的值比MinSpareServers小,Apache会自动把其调整为MinSpareServers+ 1。如果站点负载较大,可考虑同时加大MinSpareServers和MaxSpareServers。

   MaxRequestsPerChild设置的是每个子进程可处理的请求数。每个子进程在处理了“MaxRequestsPerChild”个请求后将自动销毁。0意味着无限,即子进程永不销毁。虽然缺省设为0可以使每个子进程处理更多的请求,但如果设成非零值也有两点重要的好处:

  ◆ 可防止意外的内存泄漏;

  ◆ 在服务器负载下降的时侯会自动减少子进程数。

  因此,可根据服务器的负载来调整这个值。笔者认为10000左右比较合适。

  MaxClients是这些指令中最为重要的一个,设定的是Apache可以同时处理的请求,是对Apache性能影响最大的参数。其缺省值150是远远不够的,如果请求总数已达到这个值(可通过ps -ef|grep http|wc -l来确认),那么后面的请求就要排队,直到某个已处理请求完毕。这就是系统资源还剩下很多而HTTP访问却很慢的主要原因。系统管理员可以根据硬件配置和负载情况来动态调整这个值。虽然理论上这个值越大,可以处理的请求就越多,但Apache默认的限制不能大于256。如果把这个值设为大于256,那么 Apache将无法起动。事实上,256对于负载稍重的站点也是不够的。在Apache 1.3中,这是个硬限制。如果要加大这个值,必须在“configure”前手工修改的源代码树下的src/include/httpd.h中查找 256,就会发现“#define HARD_SERVER_LIMIT 256”这行。把256改为要增大的值(如4000),然后重新编译Apache即可。在Apache 2.0中新加入了ServerLimit指令,使得无须重编译Apache就可以加大MaxClients。下面是笔者的prefork配置段:


StartServers 10
MinSpareServers 10
MaxSpareServers 15
ServerLimit 2000
MaxClients 1000
MaxRequestsPerChild 10000


  上述配置中,ServerLimit的最大值是20000,对于大多数站点已经足够。如果一定要再加大这个数值,对位于源代码树下server/mpm/prefork/prefork.c中以下两行做相应修改即可:

#define DEFAULT_SERVER_LIMIT 256
#define MAX_SERVER_LIMIT 20000


  worker的工作原理及配置

  相对于prefork,worker是2.0 版中全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理,所以可以处理相对海量的请求,而系统资源的开销要小于基于进程的服务器。但是, worker也使用了多进程,每个进程又生成多个线程,以获得基于进程服务器的稳定性。这种MPM的工作方式将是Apache 2.0的发展趋势。

  在configure -with-mpm=worker后,进行make编译、make install安装。在缺省生成的httpd.conf中有以下配置段:


StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0


  worker的工作原理是,由主控制进程生成“StartServers”个子进程,每个子进程中包含固定的ThreadsPerChild线程数,各个线程独立地处理请求。同样,为了不在请求到来时再生成线程,MinSpareThreads和MaxSpareThreads设置了最少和最多的空闲线程数;而MaxClients设置了所有子进程中的线程总数。如果现有子进程中的线程总数不能满足负载,控制进程将派生新的子进程。

  MinSpareThreads和MaxSpareThreads的最大缺省值分别是75和250。这两个参数对Apache的性能影响并不大,可以按照实际情况相应调节。

  ThreadsPerChild是worker MPM中与性能相关最密切的指令。ThreadsPerChild的最大缺省值是64,如果负载较大,64也是不够的。这时要显式使用 ThreadLimit指令,它的最大缺省值是20000。上述两个值位于源码树server/mpm/worker/worker.c中的以下两行:

#define DEFAULT_THREAD_LIMIT 64
#define MAX_THREAD_LIMIT 20000


  这两行对应着ThreadsPerChild和ThreadLimit的限制数。最好在configure之前就把64改成所希望的值。注意,不要把这两个值设得太高,超过系统的处理能力,从而因Apache不起动使系统很不稳定。

  Worker模式下所能同时处理的请求总数是由子进程总数乘以ThreadsPerChild值决定的,应该大于等于MaxClients。如果负载很大,现有的子进程数不能满足时,控制进程会派生新的子进程。默认最大的子进程总数是16,加大时也需要显式声明ServerLimit(最大值是 20000)。这两个值位于源码树server/mpm/worker/worker.c中的以下两行:

#define DEFAULT_SERVER_LIMIT 16
#define MAX_SERVER_LIMIT 20000


  需要注意的是,如果显式声明了ServerLimit,那么它乘以ThreadsPerChild的值必须大于等于MaxClients,而且 MaxClients必须是ThreadsPerChild的整数倍,否则Apache将会自动调节到一个相应值(可能是个非期望值)。下面是笔者的 worker配置段:


StartServers 3
MaxClients 2000
ServerLimit 25
MinSpareThreads 50
MaxSpareThreads 200
ThreadLimit 200
ThreadsPerChild 100
MaxRequestsPerChild 0


  通过上面的叙述,可以了解到Apache 2.0中prefork和worker这两个重要MPM的工作原理,并可根据实际情况来配置Apache相关的核心参数,以获得最大的性能和稳定性。


欢迎转贴,请注明来处。【本帖地址】: http://www.jimmydong.com/blog/post/1/118

MySQL5的分区研究

MySQL5的分区研究

jimmy | 06 二月, 2007 11:14

MySQL5.1提供的分区技术,令人对其充满希望。

根据官方文档,分区技术能够极大的帮助DBA人员。支持的分区模式包括:

Range

List

Hash

Key

----

安装配置记录:

wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.16-beta-linux-i686-icc-glibc23.tar.gz/from/http://mysql.cdpa.nsysu.edu.tw/

tar -xzvf mysql-5.1.16-beta-linux-i686-icc-glibc23.tar.gz

mv mysql-5.1.16-beta-linux-i686-icc-glibc23 /usr/local/mysql-5.1

修改启动脚本,启动


检查当前版本是否支持分区:

SHOW VARIABLES LIKE '%partition%';

测试带分区的表:(From 手册)

CREATE TABLE tr (id INT, name VARCHAR(50), purchased DATE)
PARTITION BY RANGE(YEAR(purchased))
(
PARTITION p0 VALUES LESS THAN (1990),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (2000),
PARTITION p3 VALUES LESS THAN (2005)
);


INSERT INTO tr VALUES
(1, 'desk organiser', '2003-10-15'),
(2, 'CD player', '1993-11-05'),
(3, 'TV set', '1996-03-10'),
(4, 'bookcase', '1982-01-10'),
(5, 'exercise bike', '2004-05-09'),
(6, 'sofa', '1987-06-05'),
(7, 'popcorn maker', '2001-11-22'),
(8, 'aquarium', '1992-08-04'),
(9, 'study desk', '1984-09-16'),
(10, 'lava lamp', '1998-12-25');

从服务器上可以看到文件结构为:

10.10.82.83 [testuser]$ l
total 404M
-rw-rw---- 1 mysql mysql 8.5K 2007-03-14 15:26:29 tr.frm
-rw-rw---- 1 mysql mysql 32 2007-03-14 15:26:29 tr.par
-rw-rw---- 1 mysql mysql 68 2007-03-14 15:26:48 tr#P#p0.MYD
-rw-rw---- 1 mysql mysql 1.0K 2007-03-14 15:26:48 tr#P#p0.MYI
-rw-rw---- 1 mysql mysql 48 2007-03-14 15:26:48 tr#P#p1.MYD
-rw-rw---- 1 mysql mysql 1.0K 2007-03-14 15:26:48 tr#P#p1.MYI
-rw-rw---- 1 mysql mysql 44 2007-03-14 15:26:48 tr#P#p2.MYD
-rw-rw---- 1 mysql mysql 1.0K 2007-03-14 15:26:48 tr#P#p2.MYI
-rw-rw---- 1 mysql mysql 84 2007-03-14 15:26:48 tr#P#p3.MYD
-rw-rw---- 1 mysql mysql 1.0K 2007-03-14 15:26:48 tr#P#p3.MYI

说明创建分区成功。

测试分区管理,删除:

ALTER TABLE tr DROP PARTITION p2;

添加:

ALTER TABLE tr ADD PARTITION (PARTITION p2 VALUES LESS THAN (2000));

注意:

1,删除分区后,分区内的数据也不可恢复的删除了。

2,只有range和list分区可以进行无数据影响的添加操作。其他种类的分区,不要进行分区管理。或者是,先把数据全导出,改变分区结构后再导入。

3,作为分区依据的列值是可以修改的,修改后会转移到对应分区中。(这点很有意义哟~)

下面是实战:

CREATE TABLE `user` (
`id` int(11) not null auto_increment,
`cn` varchar(22) not null default '',
`nickname` varchar(32) not null default '',
`genderid` tinyint(1) unsigned default null,
`artnum` int(8) unsigned default '0',
`elitenum` int(8) unsigned default '0',
`commnum` int(11) not null default '0',
`delnum` int(8) unsigned default '0',
`loginnum` int(8) unsigned default '1',
`linetime` int(8) unsigned default '0',
`lastdate` datetime default null,
`power` int(10) not null default '0',
`score` int(8) default '0',
`inputdate` datetime default null,
`finallydate` datetime default null,
`ad3` tinyint(4) not null default '0',
primary key (`id`),
key `nickname` (`nickname`),
key `finallydate` (`finallydate`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=4589157
PARTITION BY RANGE(id)
(
PARTITION p5 VALUES LESS THAN (5000000),
PARTITION p6 VALUES LESS THAN (6000000),
PARTITION p7 VALUES LESS THAN (7000000),
PARTITION p8 VALUES LESS THAN (8000000),
PARTITION p9 VALUES LESS THAN (9000000),
PARTITION p10 VALUES LESS THAN (10000000),
PARTITION p11 VALUES LESS THAN (11000000),
PARTITION p12 VALUES LESS THAN (12000000),
PARTITION p13 VALUES LESS THAN (13000000),
PARTITION p14 VALUES LESS THAN (14000000),
PARTITION p15 VALUES LESS THAN (15000000),
PARTITION p16 VALUES LESS THAN (16000000),
PARTITION p0 VALUES LESS THAN MAXVALUE
);

关入400万数据,性能对比:

(待补充)

方案二,使用Key进行划分

create table `user2` (
`cn` varchar(22) not null default '',
`nickname` varchar(32) not null default '',
`genderid` tinyint(1) unsigned default null,
`artnum` int(8) unsigned default '0',
`elitenum` int(8) unsigned default '0',
`commnum` int(11) not null default '0',
`delnum` int(8) unsigned default '0',
`loginnum` int(8) unsigned default '1',
`linetime` int(8) unsigned default '0',
`lastdate` datetime default null,
`power` int(10) not null default '0',
`score` int(8) default '0',
`inputdate` datetime default null,
`finallydate` datetime default null,
`ad3` tinyint(4) not null default '0',
primary key (`cn`),
key `nickname` (`nickname`),
key `finallydate` (`finallydate`)
) engine=myisam default charset=gb2312
partition by key(cn)
partitions 3;

增加分区数:ALTER TABLE user2 ADD PARTITION PARTITIONS 10;

减少分区数:ALTER TABLE user2 COALESCE PARTITION 6;

增加和减少分区时,数据并不丢失。

一个不很方便的地方在于,当划分分区时,有unique的列时可能会拒绝操作。

另外,关于性能方面。range,list划分会对提高性能有帮助,但是作用多大,还是跟具体应用有关的。可以肯定的是,不会比我们传统上用程序逻辑将数据分散到多个表的方法效率更高,只是更方便而已。对于hash,key的划分,需要消耗大量计算时间,如果设计不当,可能还会造成性能的下降。所以,这也是我没有立即写性能对比数据的原因。

以后在实际应用中,采用分区技术确实能够有效提高性能得到验证后,再回来补充性能部分的内容。

~~呵呵~~



欢迎转贴,请注明来处。【本帖地址】: http://www.jimmydong.com/blog/post/1/119

OpenVPN快速应用

OpenVPN快速应用

jimmy | 31 一月, 2007 21:01

本文要点:

如何快速搭建一个单Client-Server的VPN应用,适用Windows的客户端,Linux的服务器端。目的是能够快速连入机房环境,更有效的维护和管理服务系统。

对于如何用OpenVPN构架一个企业的通用VPN应用,本文并不适宜。

参考:

OpenVPNTM Static Key Mini-HOWTO


首先,下载安装OpenVPN。

(假定: 当前服务器外网IP为 www.myserver.com ,内网IP为 192.168.0.3)

生成公共Key:

openvpn --genkey --secret openvpn_static.key

将Key放到C:下,然后复制一份到服务器/etc下。


服务器端编辑配置文件:

#vi /etc/openvpn.cfg

dev tun
ifconfig 10.8.0.1 10.8.0.2
secret /etc/openvpn_static.key
keepalive 10 60
ping-timer-rem
persist-tun
persist-key
user root
group root
daemon

启动服务:

/usr/local/sbin/openvpn /etc/openvpn.cfg

开启IP转发:

echo 1 > /proc/sys/net/ipv4/ip_forward

编辑客户端配置文件: c:openvpn.ovpn


remote www.myserver.com
/>dev tun
ifconfig 10.8.0.2 10.8.0.1
secret C:openvpn_static.key
keepalive 10 60
ping-timer-rem
persist-tun
persist-key
route 192.168.0.0 255.255.0.0


配置文件上点右键,选择“start openvpn on this config file”,即可连入服务器。

同时,可以访问到与服务器同一局网的机器。对于需要连入服务器环境管理多台机器是非常方便的。

~~呵呵~~



欢迎转贴,请注明来处。【本帖地址】: http://www.jimmydong.com/blog/post/1/117

 
There are no photo albums.
No list items have been added yet.