2006年06月15日
你知道MySQL10条鲜为人知的技巧吗
作者: Techrepublic.com.com
2006-02-06 04:58 PM

本文为你介绍MySQL中十条鲜为人知的技巧。

Click here to find out more!

MySQL易学易用,且附带丰富的技术文档,这二个因素使之被广泛应用。然而,随着MySQL发展之迅速,即使一个MySQL老手有时也会为该软件出其不意的功能感叹。本文将为你介绍这些不为人知的特性。

以XML格式查看查询结果

通过使用传统—xml 选项调用MySQL命令行客户程序,你可以以XML格式(而不是传统的列表形式)来查看MySQL查询结果。如果你打算将查询输出与其它程序集成在一起,这一技巧非常有用,这里是一个例子:

表A

shell> mysql –xml

mysql> SELECT * FROM test.stories;
<?xml version="1.0"?>

<resultset statement="SELECT * FROM test.stories">
<row>
<id>1</id>
<headline>This is a test</headline>
<tstamp>2005-07-28 00:14:57</tstamp>
</row>

<row>
<id>2</id>
<headline>This is the second test</headline>
<tstamp>2005-07-28 00:15:11</tstamp>
</row>
</resultset>
2 rows in set (0.11 sec)

快速重建索引

通常情况下,如果你想改变服务器的全文搜索变量,你需要在表格中重新建立全文索引,以确保你的更新得到映射。这一操作将会花费大量的时间,特别是如果你需要处理很多数据的时候。一种快速的解决方法是使用REPAIR TABLE命令,以下为演示过程:

表B

mysql> REPAIR TABLE content QUICK;
+———–+——–+———-+———-+
| Table| Op| Msg_type | Msg_text |
+———–+——–+———-+———-+
| content| repair | status| OK|
+———–+——–+———-+———-+
1 row in set (0.05 sec)

压缩一定的表格类型

如果你处理的是只读MyISAM表格,MySQL允许你将其压缩以节省磁盘空间。对此可以使用包括myisampack,如下所示:

表C

shell> myisampackmovies.MYI
Compressing movies.MYD: (146 records)
- Calculating statistics
- Compressing file
41.05%

使用传统SQL

MySQL支持SQL查询中的传统用法,支持IF与CASE结构。以下是一个简单的例子:

表D

mysql> SELECT IF (priv=1, ‘admin’, ‘guest’) As usertype FROM privs WHERE username = ‘joe’;
+———-+
| usertype |
+———-+
| admin|
+———-+
1 row in set (0.00 sec)

以CSV格式输出表格数据

MySQL输出文件包含一个全部SQL命令列表。如果你想将输出文件导入到MySQL,这一功能非常实用,但如果目标程序(比如Excel)不能与SQL相互通讯,这一方法将行不通。在这种情况下,可以通过告诉MySQL以CSV格式建立输出文件,这种CSV格式很方便地导入到绝大部分的程序。这里演示了mysqldump的操作过程:

shell> mysqldump -T . –fields-terminated-by=", " mydbmytable

这将在当前目录中生成一个文本文件,包含来自mydb.mytable列表中以逗号为间隔符的记录。

以激活strict模式减少“bad”数据的出现

MySQL服务器能够以多种不同的模式运行,而每一种都针对于特定的目的而优化。在默认情况下,没有设置模式。然而,通过在服务器命令行中添加以下选项可以很容易地改变模式的设置并将MySQL以“strict”模式运行:

shell> mysqld –sql_mode="STRICT_ALL_TABLES" &

在“strict”模式下,通过MySQL的中止查询执行并返回一个错误,服务器的很多自动修正功能都被无效化。同样,该模式下也将会执行更为严格的时间检查。

监视服务器

你可以通过运行SHOW STATUS命令获得一份服务器运行与统计的报告,包括打开连接的次数,激活查询次数,服务器正常运行时间等等。例如:

表 E

mysql> SHOW STATUS;
+——————+——-+
| Variable_name| Value |
+——————+——-+
| Aborted_clients| 0|
| Aborted_connects | 0|

| Uptime| 851|
+——————+——-+
156 rows in set (0.16 sec)

自动返回CREATE TABLE代码

MySQL允许你自动获得SQL命令重新建立一个特定的表格。只简单地运行SHOW CREATE TABLE命令,并查看表格建立代码,如下所示:

表 F

mysql> SHOW CREATE TABLE products;
—————————————————–
| Table| Create Table
+———-+—————————————–
| products | CREATE TABLE `products` (
`id` int(8) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default ”,
`price` int(10) default NULL,
PRIMARY KEY(`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+———-+—————————————–
1 row in set (0.27 sec)

建立一个更为有用的命令提示:

在缺省情况下,MySQL命令行客户程序显示一个简单的mysql>提示符。然而,你可以使用特定的修改内容来改变这一提示符使之变得更为有效,这些内容包括:当前用户名称,主机名称,以及当前选择的数据库。如下所示:

表 G

mysql> prompt \U:/\d>
PROMPT set to ‘\U:/\d>’
root@localhost:/db1>

表A

功能

说明

实例

checkdate

($month,

?date, $year)

如果应用的值构成一个有效日期,则该函数返回为真。例如,对于错误日期2005年2月31日,此函数返回为假。

在日期用于计算或保存在数据库中之前,可用此函数检查日期并使日期生效。

<?php

// returns false

echo checkdate(2,30,2005) ?

"valid" :

"invalid";

 

// returns true

echo checkdate(4,6,2010) ? "valid" :

"invalid";

?>

getdate($ts)

在没有自变量的情况下,该函数以结合数组的方式返回当前日期与时间。数组中的每个元素代表日期/时间值中的一个特定组成部分。可向函数提交可选的时间标签自变量,以获得与时间标签对应的日期/时间值。

应用此函数来获得一系列离散的,容易分离的日期/时间值。

<?php

// returns timestamp for

13:15:23

7-Jun-2006

echo mktime(13,15,23,6,7,2006);

?>

mktime($hour,

$minute,

$second,

$month, $day,

$year)

此函数的作用与getdate()的作用相反:它由一系列的日期与时间值生成一个UNIX时间标签(GMT时间1970年1月1日到现在消逝的秒数)。不用自变量时,它生成当前时间的UNIX时间标签。

用此函数获得即时时间的UNIX时间标签。这种时间标签通常用于许多数据库与程序语言中。

<?php

// returns timestamp for

13:15:23

7-Jun-2006

echo mktime(13,15,23,6,7,2006);

?>

date($format,

$ts)

此函数将UNIX时间标签格式化成一个可人为阅读的日期字符串。它是PHP日期/时间API中功能最为强大的函数,可用在一系列的修正值中,将整数时间标签转变为所需的字符串格式。

 

为显示格式化时间或日期时,应用此函数。

<?php

// format current date

// returns "13-Sep-2005 01:16 PM"

echo date("d-M-Y h:i A", mktime

());

?>

strtotime

($str)

此函数将可人为阅读的英文日期/时间字符串转换成UNIX时间标签。

应用此函数将非标准化的日期/时间字符串转换成标准、兼容的UNIX时间标签。

<?php

// returns 13-Sep-05

echo date("d-M-y",

strtotime("today"));

 

// returns 14-Sep-05

echo date("d-M-y",

strtotime("tomorrow"));

 

// returns 16-Sep-05

echo date("d-M-y", strtotime("today +3

days"));

?>

strftime

($format,

?ts)

如前面的setlocale()函数定义的那样,此函数将UNIX时间标签格式化成适用于当前环境的日期字符串。

应用此函数建立与当前环境兼容的日期字符串。

<?php

// set locale to France (on

Windows)

setlocale(LC_TIME, "fra_fra");

 

// format month/day names

// as per locale setting

// returns "septembre" and

"mardi"

echo strftime("Month: %B ");

echo strftime("Day: %A ");

?>

microtime()

此函数返回GMT时间1970年1月1日到现在所消逝的秒数与微秒数。

在基准特定的代码块,准确测量它的执行时间时应用此函数。

<?php

// get starting value

$start = microtime();

 

// run some code

for ($x=0; $x<1000; $x++) {

牋?$null = $x * $x;

}

 

// get ending value

$end = microtime();

 

// calculate time taken for

code

execution

echo "Elapsed time: " . ($end –

 

$start) ." sec";

?>

gmmktime

($hour,

$minute,

$second,

$month, $day,

$year)

此函数由一系列用GMT时间表示的日期与时间值生成一个UNIX时间标签。不用自变量时,它生成一个当前GMT即时时间的UNIX时间标签。

用此函数来获得GMT即时时间的UNIX时间标签。

<?php

// returns timestamp for

12:25:23

9-Jul-2006

echo gmmktime

(12,25,23,7,9,2006);

?>

gmdate

($format,

$ts)

此函数将UNIX时间标签格式化成可人为阅读的日期字符串。此日期字符串以GMT(非当地时间)表示。

用GMT表示时间标签时应用此函数。

<?php

// format current date into GMT

// returns "13-Sep-2005 08:32

AM"

echo gmdate("d-M-Y h:i A",

mktime());

?>

date_default_

timezone_set

($tz)and

date_default_

timezone_get()

此函数此后所有的日期/时间函数调用设定并恢复默认的时区。

:此函数仅在PHP 5.1+中有效。 

此函数是一个方便的捷径,可为以后的时间操作设定时区。

<?php

// set timezone to UTC

date_default_timezone_set

(‘UTC’);

?>

责任编辑:张琎

2006年06月08日

/*
** Title………: PHP4 HTTP Compression Speeds up the Web
** Version…….: 1.20
** Author……..: catoc <catoc@163.net>
** Filename……: gzdoc.php
** Last changed..: 18/10/2000
** Requirments…: PHP4 >= 4.0.1
** PHP was configured with –with-zlib[=DIR]
** Notes………: Dynamic Content Acceleration compresses
** the data transmission data on the fly
** code by sun jin hu (catoc) <catoc@163.net>
** Most newer browsers since 1998/1999 have
** been equipped to support the HTTP 1.1
** standard known as "content-encoding."
** Essentially the browser indicates to the
** server that it can accept "content encoding"
** and if the server is capable it will then
** compress the data and transmit it. The
** browser decompresses it and then renders
** the page.
**
** Modified by John Lim (jlim@natsoft.com.my)
** based on ideas by Sandy McArthur, Jr
** Usage……..:
** No space before the beginning of the first ‘<?’ tag.
** ————Start of file———-
** |<?
** | include(‘gzdoc.php’);
** |? >
** |<HTML>
** |… the page …
** |</HTML>
** |<?
** | gzdocout();
** |? >
** ————-End of file———–
*/
ob_start();
ob_implicit_flush(0);
function CheckCanGzip(){
global $HTTP_ACCEPT_ENCODING;
if (headers_sent() || connection_timeout() || connection_aborted()){
return 0;
}
if (strpos($HTTP_ACCEPT_ENCODING, ‘x-gzip’) !== false) return "x-gzip";
if (strpos($HTTP_ACCEPT_ENCODING,’gzip’) !== false) return "gzip";
return 0;
}
/* $level = compression level 0-9, 0=none, 9=max */
function GzDocOut($level=1,$debug=0){
$ENCODING = CheckCanGzip();
if ($ENCODING){
print "n<!– Use compress $ENCODING –>n";
$Contents = ob_get_contents();
ob_end_clean();
if ($debug){
$s = "<p>Not compress length: ".strlen($Contents);
$s .= "
Compressed length: ".strlen(gzcompress($Contents,$level));
$Contents .= $s;
}
header("Content-Encoding: $ENCODING");
print "x1fx8bx08×00x00×00x00×00";
$Size = strlen($Contents);
$Crc = crc32($Contents);
$Contents = gzcompress($Contents,$level);
$Contents = substr($Contents, 0, strlen($Contents) – 4);
print $Contents;
print pack(‘V’,$Crc);
print pack(‘V’,$Size);
exit;
}else{
ob_end_flush();
exit;
}
}
?>

2006年06月01日

查看完全版本 : VSFTPD+MySQL虚拟用户


walker
04-11-09, 10:04
作者:施文建

  VSFTPD是一种在UNIX/Linux中非常安全且快速的FTP服务器,目前已经被许多大型站点所采用。VSFTPD支持将用户名和口令保存在数据库文件或数据库服务器中。VSFTPD称这种形式的用户为虚拟用户。相对于FTP的本地(系统)用户来说,虚拟用户只是FTP服务器的专有用户,虚拟用户只能访问FTP服务器所提供的资源,这大大增强系统本身的安全性。相对于匿名用户而言,虚拟用户需要用户名和密码才能获取FTP服务器中的文件,增加了对用户和下载的可管理性。对于需要提供下载服务,但又不希望所有人都可以匿名下载;既需要对下载用户进行管理,又考虑到主机安全和管理方便的 FTP站点来说,虚拟用户是一种极好的解决方案。本文介绍在RedHat Linux 9上如何将VSFTPD的虚拟用户名和密码保存在MySQL数据库服务器中。

  一、VSFTPD的安装

  目前,VSFTPD的最新版本是1.2.0版。官方下载地址为ftp://vsftpd.beasts.org/users/cevans/vsftpd-1.2.0.tar.gz。在安装前,需要先做以下准备工作:

  VSFTPD默认配置中需要“nobody”用户。在系统中添加此用户,如果用户已经存在,useradd命令有相应提示。
  [root@hpe45 root]# useradd nobody
  useradd: user nobody exists

  VSFTPD默认配置中需要“/usr/share/empty”目录。在系统中此目录,如果目录已经存在,mkdir命令有相应提示。
  [root@hpe45 root]# mkdir /usr/share/empty/
  mkdir: cannot create directory ‘/usr/share/empty’: File exists

  VSFTPD提供匿名FTP服务时,需要“ftp”用户和一个有效的匿名目录。
  [root@hpe45 root]# mkdir /var/ftp/
  [root@hpe45 root]# useradd -d /var/ftp ftp
  接下来的操作对于ftp用户是否已经存在都是有用的。
  [root@hpe45 root]# chown root.root /var/ftp
  [root@hpe45 root]# chmod og-w /var/ftp

  以上准备工作完成后,我们就可以开始编译源代码了。假定我们下载的vsftpd-1.2.0.tar.gz在/root目录,执行以下命令:
  [root@hpe45 root]# tar zxvf vsftpd-1.2.0.tar.gz
  [root@hpe45 root]# cd vsftpd-1.2.0
  [root@hpe45 vsftpd-1.2.0]# make
  [root@hpe45 vsftpd-1.2.0]# make install

  上面的“make install”命令将编译好的二进制文件、手册等复制到相应目录。在RHL9上,可能需要手动执行以下复制:
  [root@hpe45 vsftpd-1.2.0]# cp vsftpd /usr/local/sbin/vsftpd
  [root@hpe45 vsftpd-1.2.0]# cp vsftpd.conf.5 /usr/local/share/man/man5
  [root@hpe45 vsftpd-1.2.0]# cp vsftpd.8 /usr/local/share/man/man8

  接下来,我们复制一个简单的配置文件作为基础供后面修改。
  [root@hpe45 vsftpd-1.2.0]# cp vsftpd.conf /etc
  [root@hpe45 vsftpd-1.2.0]# cp RedHat/vsftpd.pam /etc/pam.d/ftp
  复制PAM验证文件,以允许本地用户登录VSFTPD。
  [root@hpe45 vsftpd-1.2.0]# cp RedHat/vsftpd.pam /etc/pam.d/ftp

  二、创建guest用户

  VSFTPD采用PAM方式验证虚拟用户。由于虚拟用户的用户名/口令被单独保存,因此在验证时,VSFTPD需要用一个系统用户的身份来读取数据库文件或数据库服务器以完成验证,这就是VSFTPD的guest用户。这正如同匿名用户也需要有一个系统用户ftp一样。当然,我们也可以把 guest用户看成是虚拟用户在系统中的代表。下面在系统中添加vsftpdguest用户,作为VSFTPD的guest。
  [root@hpe45 vsftpd-1.2.0]# useradd vsftpdguest
  当虚拟用户登录后,所在的位置为vsftpdguest的自家目录/home/vsftpdguest。如果要让虚拟用户登录到/var/ftp等其他目录,修改vsftpdguest的自家目录即可。

  三、设置VSFTPD配置文件

  在/etc/vsftpd.conf文件中,加入以下选项:
  guest_enable=YES
  guest_username=vsftpdguest

  然后执行以下命令,让VSFTPD在后台运行:
  [root@hpe45 vsftpd-1.2.0]# /usr/local/sbin/vsftpd &

  四、将虚拟用户保存在MySQL数据库服务器中

  我们建立数据库vsftpdvu,表users,字段name和passwd用于保存虚拟用户的用户名和口令,同时增加两个虚拟用户xiaotong和xiaowang。

  [root@hpe45 vsftpd-1.2.0]# mysql -p
  mysql>create database vsftpdvu;
  mysql>use vsftpdvu;
  mysql>create table users(name char(16) binary,passwd char(16) binary);
  mysql>insert into users (name,passwd) values (‘xiaotong’,password(‘qqmywife’));
  mysql>insert into users (name,passwd) values (‘xiaowang’,password(‘ttmywife’));
  mysql>quit

  然后,授权vsftpdguest可以读vsftpdvu数据库的users表。执行以下命令:
  [root@hpe45 vsftpd-1.2.0]# mysql -u root mysql -p
  mysql>grant select on vsftpdvu.users to vsftpdguest@localhost identified by ‘i52serial0′;
  mysql>quit

  如果要验证刚才的操作是否成功可以执行下面命令:
  [root@hpe45 vsftpd]#mysql -u vsftpdguest -pi52serial0 vsftpdvu
  mysql>select * from users;
  如果成功,将会列出xiaotong、xiaowang和加密后的密码

  五、设置MySQL的PAM验证

  这里我们要用到一个利用mysql进行pam验证的开源项目(http://sourceforge.net/projects/pam- mysql/)。首先从网站下载它的程序包pam_myql-0.5.tar.gz,复制到/root目录中。在编译安装之前,要确保mysql- devel的RPM包已经安装在你的机器上,如果没有请从RHL安装光盘中安装该包。然后,执行以下命令:
  [root@hpe45 root]#tar xvzf pam_mysql-0.5.tar.gz
  [root@hpe45 root]#cd pam_mysql
  [root@hpe45 pam_mysql]#make
  [root@hpe45 pam_mysql]#make install
  make install这一步可能会出现错误,那只好手动将该目录下生成的pam_mysql.o复制到/lib/security目录下。
  接下来,我们要设置vsftpd的PAM验证文件。打开/etc/pam.d/ftp文件,加入以下内容:
  auth required pam_mysql.o user=vsftpdguest passwd=i52serial0 host=localhost db=vsftpdvu table=users usercolumn=name passwdcolumn=passwd crypt=2
  account required pam_mysql.o user=vsftpdguest passwd=i52serial0 host=localhost db=vsftpdvu table=users usercolumn=name passwdcolumn=passwd crypt=2
  上面涉及到的参数,只要对应前面数据库的设置就可以明白它们的含义。这里需要说明的是crypt参数。crypt表示口令字段中口令的加密方式:crypt=0,口令以明文方式(不加密)保存在数据库中;crypt=1,口令使用UNIX系统的DES加密方式加密后保存在数据库中; crypt=2,口令经过MySQL的password()函数加密后保存。

  六、进一步的虚拟用户设置

  经过以上的步骤,虚拟用户就可以正常使用了。这里介绍进一步的虚拟用户设置。首先,介绍虚拟用户的权限设置。

  VSFTPD-1.2.0新添了virtual_use_local_privs参数,当该参数激活(YES)时,虚拟用户使用与本地用户相同的权限。当此参数关闭(NO)时,虚拟用户使用与匿名用户相同的权限,这也就是VSFTPD-1.2.0之前版本对虚拟用户权限的处理方法。这两者种做法相比,后者更加严格一些,特别是在有写访问的情形下。默认情况下此参数是关闭的(NO)。
  当virtual_use_local_privs=YES时,只需设置write_enable=YES,虚拟用户就可以就拥有写权限。而virtual_use_local_privs=NO时,对虚拟用户权限的设置就更多一些更严格一些。
  控制虚拟用户浏览目录:如果让用户不能浏览目录,但仍可以对文件操作,那么需要执行以下二个步骤:一,配置文件中,anon_world_readable_only=YES。二,虚拟用户目录的权限改为只能由vsftpdguest操作:
  [root@hpe45 root]# chown vsftpdguest.vsftpdguest /home/vsftpdguest
  [root@hpe45 root]# chmod 700 /home/vsftpdguest
  允许虚拟用户上传文件:
  write_enable=YES
  anon_upload_enable=YES
  允许虚拟用户修改文件名和删除文件:
  anon_other_write_enable=YES
  由于以上选项的设置同样会对匿名用户生效。如果不想匿名用户趁机拥有同样的权限,最好是禁止匿名用户登录。

  其次,由于虚拟用户在系统中是vsftpdguest身份,所以可以访问到系统的其他目录。为了更加安全,我们可以将虚拟用户限制在自家目录下。有两种做法:一,在配置文件中增加以下选项
  chroot_local_user=NO
  chroot_list_enable=YES
  chroot_list_file=/etc/vsftpd.chroot_list
  然后,在/etc/vsftpd.chroot_list文件中加入虚拟用户名xiaotong和xiaowang。
  第二种做法,在配置文件中修改chroot_local_user=YES。
  经过修改后,虚拟用户登录后其根目录就限制在/home/vsftpdguest下,无法访问其他目录。

  七、虚拟用户的个人目录

  大家可以发现,无论是哪个虚拟用户,登录后所在的目录都是/home/vsftpdguest,即都是guest_username用户的自家目录。下面,介绍如何为每个虚拟用户建立自家目录。首先,在主配置文件中加入以下选项:
  user_config_dir=/etc/vsftpd/vsftpd_user_conf
  然后,生成/etc/vsftpd/vsftpd_user_conf目录,并在该目录下建立与特定虚拟用户同名的文件:
  [root@hpe45 root]# mkdir /etc/vsftpd/vsftpd_user_conf
  [root@hpe45 root]# cd /etc/vsftpd/vsftpd_user_conf
  [root@hpe45 vsftpd_user_conf]# touch xiaowang
  以上的操作为虚拟用户xiaowang建立了个人配置文件/etc/vsftpd/vsftpd_user_conf/xiaowang。接下来,在xiaowang的个人配置文件中将xiaowang的自家目录修改为/home/xiaowang,配置选项为:
  local_root=/home/xiaowang
  然后,新建xiaowang目录,并将权限设为vsftpdguest:
  [root@hpe45 vsftpd_user_conf]# mkdir /home/xiaowang
  [root@hpe45 vsftpd_user_conf]# chown vsftpdguest.vsftpdguest ./xiaowang
  [root@hpe45 vsftpd_user_conf]# chmod 600 /home/xiaowang
  经过以上设置,xiaowang登录VSFTPD后,用“pwd”指令就可以发现被自己被定位到自己的“/home/xiaowang”目录。
  从文件系统层次来看,由于“/home/xiaowang”目录的权限是属于vsftpdguest的,所以其他的虚拟用户同样也可以访问 xiaowang的自家目录。解决这个问题也很简单,我们只需要让VSFTPD负责将虚拟用户限制在其自家目录,就可以避免虚拟用户的互相访问。具体做法参照前面第六步中所述,这里不再赘述。经过以上设置后,虚拟用户就可以拥有属于自己的目录了。
2006年05月24日

Building a Web Server, a Step-by-Step Instruction Guide

Binary Install — MySQL 4.0.23 for Linux

Requirements
  • Perl
Download
Our Configuration
  • Install to : /usr/local/mysql
Setup
  • ...]# tar -xzf mysql-standard-4.0.23-pc-linux-i686.tar.gz
  • ...]# mv mysql-standard-4.0.23-pc-linux-i686 /usr/local/
  • ...]# cd /usr/local
  • /usr/local]# ln -s mysql-standard-4.0.23-pc-linux-i686 mysql
  • [establish symbolic link /usr/local/mysql to /usr/local/mysql-standard-4.0.23-pc-linux-i686]
  • /usr/local]# cd mysql
  • /usr/local/mysql]# groupadd mysql
  • [create new group 'mysql' under /etc/group]
  • /usr/local/mysql]# useradd -g mysql mysql
  • [create new user 'mysql' under the group 'mysql']
  • /usr/local/mysql]# cp support-files/my-medium.cnf /etc/my.cnf
  • [the location of my.cnf is searched in the order of: global options - /etc/my.cnf, server-specific options - /usr/local/mysql/data/my.cnf, user-specific options - ~/my.cnf]
  • /usr/local/mysql]# ./scripts/mysql_install_db --user=mysql
  • [create the mysql database, privilege tables, and the func table]
  • /usr/local/mysql]# chown -R root:mysql .
  • [change user:group ownership to 'root:mysql' under all files and directories of /usr/local/mysql]
  • /usr/local/mysql]# chown -R mysql:mysql data
  • [change user:group ownership to 'mysql:mysql' under all files and directories of /usr/local/mysql/data]
Startup

There are several ways to start the MySQL daemon, choose one that suits your needs.

  • [Default] Listen on all interfaces (0.0.0.0)
    • /usr/local/mysql]# ./bin/mysqld_safe --user=mysql &
  • Listen on loopback only (127.0.0.1)
    • /usr/local/mysql]# ./bin/mysqld_safe --user=mysql --bind-address=127.0.0.1 &
Cleanup
  • /usr/local/mysql]# ./bin/mysql_secure_installation
  • [remove anonymous users, remove remote instance of user 'root', and delete test database with privileges set]
Running MySQL
  • Enter the command-line MySQL interface.
    • /usr/local/mysql/bin]# ./mysql -u <user> -p<password>
    • [note that there is no space between '-p' and 'password', and that '<>' is not typed]
  • Shutdown the MySQL daemon.
    • /usr/local/mysql/bin]# ./mysqladmin -u <user> -p<password> shutdown
    • [note that there is no space between '-p' and 'password', and that '<>' is not typed]
Help
  • Display help options.
    • /usr/local/mysql/bin]# ./mysqld --help
  • See what values a running MySQL server is using.
    • /usr/local/mysql/bin]# ./mysqladmin -u <user> -p<password> variables
  • Display MySQL server extended status.
    • /usr/local/mysql/bin]# ./mysqladmin -u <user> -p<password> extended-status
  • Display MySQL version information.
    • /usr/local/mysql/bin]# ./mysqladmin -V
  • Log non-critical warnings to the log file.
    • Add --log-warnings to the mysqld_safe startup line, before the &
2006年05月20日

This tutorial is designed to guide you through the initial steps of setting up Apache, MySQL, and PHP on Linux. The Linux distribution being utilized for this tutorial is Fedora Core 1, however the steps should be very similar across most distributions. This tutorial makes the assumption that you have the required development tools loaded for compiling programs from source, these tools are beyond the scope of this document and will not be covered here. Also, it assumes you can use the “vi” text editor for basic editing tasks.


Apache, MySQL, and PHP have become one of the most utilized combinations for developing content driven websites. They are robust, flexible, provide a decent level of security, and they are available for many different platforms. That being said, lets get to building a web server.

 

Get the sourceballs


The first thing you need to do is obtain the sourceballs for each package, we will be compiling each package from scratch here, and, while there are also binary packages available for some distributions, I find your end results are usually better when building each package for your machine. Make sure you get the source files.

Here are the links and the package versions available at the time this tutorial was written

Apache
current version: 2.0.48
URL: http://httpd.apache.org/download.cgi
MySQL
current version: 4.0.16
URL: http://www.mysql.com/downloads/mysql-4.0.html
PHP
current version: 4.3.4
URL: http://www.php.net/downloads.php

Ok, so you’ve got the files now what ?, well now the fun begins..

 

Installation


The first thing we need to do is extract the sourceballs so we can work with the files included in them. Beginning now we will be working as root, so open a terminal window, change to the directory in which you saved your downloaded files and become root by issuing the “su” command, enter the root password and you should be good to go.

To extract the sourceballs type the following commands;

#tar -zxf httpd-2.0.48.tar.gz
#tar -zxf mysql-4.0.16.tar.gz
#tar -zxf php-4.3.4.tar.gz

The commands above will extract the sourceballs into their own separate directories. Now lets move on to compiling the source into usable programs. We’ll start with Apache.

 

Compiling Apache


Change into the directory created when you untarred the sourceball as follows;

#cd httpd-2.0.48

Follow this command by typing;

#./configure –prefix=/usr/local/apache2 –enable-mods-shared=most (enter)

This tells Apache to install in the /usr/local/apache2 directory, and to build most of the available loadable modules. There are a ton of options with Apache, but these should work for the most part. Once the configure is done and the system returns the prompt to you, issue the following command;

#make

This will take a few minutes, once the prompt comes back again issue the following command;

#make install

Wait for a few minutes and viola !, Apache is installed with the exception of a few minor changes we still need to make. They are as follows..

Issue the following command;

#vi /usr/local/apache2/conf/httpd.conf

Check to make sure the following line is present in the file at the bottom of the LoadModule list, if it is not there add it;

LoadModule php4_module modules/libphp4.so

Find the DirectoryIndex line and edit it so it looks like the following;

DirectoryIndex index.html index.html.var index.php

Find the AddType application section and add the following line;

AddType application/x-httpd-php .php

Thats it, save the file and we are done with Apache. Now, on to MySQL !

 

Compiling MySQL


Change into the MySQL source directory as follows;

#cd mysql-4.0.16

Follow this command by typing;

#./configure –prefix=/usr/local/mysql –localstatedir=/usr/local/mysql/data –disable-maintainer-mode –with-mysqld-user=mysql –enable-large-files-without-debug

Sit back and wait for a while while configure does its thing, once the system returns the prompt to you issue the following command;

#make

Unless you have a very fast machine this will take some time, so spend time with your family, grab a beer, go for a walk, or whatever you’re into. When you get back, assuming the system has returned the prompt to you issue the following command;

#make install

Cool !, MySQL is installed, there are only a couple things left to do to get it working, first we need to create a group for MySQL as follows;

#/usr/sbin/groupadd mysql

Then we create a user called mysql which belongs to the mysql group;

#/usr/sbin/useradd -g mysql mysql

Now we install the database files as follows;

#./scripts/mysql_install_db

Then we make a couple minor ownership changes;

# chown -R root:mysql /usr/local/mysql
# chown -R mysql:mysql /usr/local/mysql/data

Last but not least, we use vi to add a line the ld.so.conf file as follows;

#vi /etc/ld.so.conf

And we add the following line;

/usr/local/mysql/lib/mysql

Thats it, MySQL is installed, you can run it by issuing the following command;

#/usr/local/mysql/bin/mysqld_safe –user=mysql &

And as long as we’re here we might as well set a root password for MySQL as follows;

#/usr/local/mysql/bin/mysqladmin -u root password new_password

Where “new_password” is the password you want to use.

Ok, so far so good, on to PHP !

 

Compiling PHP


Change into the PHP source directory as follows;

#cd php-4.3.4

Follow this command by typing;

#./configure –prefix=/usr/local/php –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql=/usr/local/mysql

Once the prompt comes back to you issue the following command;

#make

Hang out for awhile, and then yep, you guessed it, once you have the prompt back;

#make install

Once the install finishes and you have the prompt back issue the following command;

#cp php.ini-recommended /usr/local/php/lib/php.ini

Then edit that file;

#vi /usr/local/php/lib/php.ini

And change the following;

Find the “doc_root” section and enter the correct path for the directory which serves your web content, such as;

doc_root= “/usr/local/apache2/htdocs/”
(this is default for apache2)

Then find the file_uploads section and change it to reflect the following;

file_uploads=Off
(for security reasons)

Thats if for PHP, now lets see if it all works..

 

Testing


Assuming your MySQL process is still running from earlier, lets start Apache by issuing the following command;

#/usr/local/apache2/bin/apachectl start

This starts the Apache web server, now change into the following directory;

#cd /usr/local/apache2/htdocs

And using vi create a file called test.php;

#vi test.php

Add the following line to the file;

<?php phpinfo(); ?>

Save the file, then fire up your browser and point it to localhost/test.php. You should see a listing of all kinds of cool info about Apache, PHP, etc. If you do then your set !, if you don’t, then take a look at your logs for Apache and MySql, and remember Google is your friend. But hopefully you do, and now you have a fully functioning setup.

Ok, one last step and we’ll be done, you have everything running now, but you had to start Apache and MySql manually, that’s something you don’t want to have to remember to do everytime you reboot your machine, so lets fix it.

 

Starting Apache and MySQL Automatically


Lets start with MySQL, as root make your working directory that of the MySQL source directory you worked with earlier, something similar to;

#cd /home/xxxx/mysql-4.0.16

Then, copy the file mysql.server to your /etc/init.d directory as follows;

#cp support-files/mysql.server /etc/init.d/mysql

Ok, lets create some links in the startup folders for run levels 3 and 5.

#cd /etc/rc3.d
#ln -s ../init.d/mysql S85mysql
#ln -s ../init.d/mysql K85mysql
#cd /etc/rc5.d
#ln -s ../init.d/mysql S85mysql
#ln -s ../init.d/mysql K85mysql
#cd ../init.d
#chmod 755 mysql

Thats it for MySQL, it should start automatically now when you reboot your machine. Now lets do the same for Apache, still logged in as root, make your working directory that of the Apache binaries as follows;

#cd /usr/local/apache2/bin

Then, copy the file called apachectl as follows;

#cp apachectl /etc/init.d/httpd

Now, for some more links;

#cd /etc/rc3.d
#ln -s ../init.d/httpd S85httpd
#ln -s ../init.d/httpd K85httpd
#cd /etc/rc5.d
#ln -s ../init.d/httpd S85httpd
#ln -s ../init.d/httpd K85httpd

And thats it for Apache !, it should start automatically along with MySQL the next time you boot your machine.

That brings us to the end of this tutorial, hopefully you found it helpful, and Good Luck!


By Rick Nicholas
Information Systems Manager
Humane Society Silicon Valley
www.hssv.org, www.geekspeek.org
 

2006年05月08日

function CompareDate(date1,date2)
    {
        // date format must be "YYYY-MM-DD HH24:MI"
        var _date1 = new Array();
        var _date2 = new Array();

        var EQUAL   = 0; // ÏàµÈ
        var LESS    = 1; // СÓÚ
        var GREATER = 2; // ´óÓÚ

        _date1 = date1.substr(0,10).split("-").concat(date1.substr(11).split(":"));
        _date2 = date2.substr(0,10).split("-").concat(date2.substr(11).split(":"));

        var d1 = new Date(_date1[0],_date1[1],_date1[2],_date1[3],_date1[4]);
        var d2 = new Date(_date2[0],_date2[1],_date2[2],_date2[3],_date2[4]);
        if (d1.getTime()<d2.getTime()) {
          return LESS;
        }else if(d1.getTime()> d2.getTime()){
          return GREATER;
        }else{
          return EQUAL;
        }
    }

http://woooh.com/post/learn_ajax.html

2006年04月27日

 这个东东害我搞了n久,终于搞好, 不敢独享.

   1. 目录要 everyone 可以读/执行(read/execute)

   2. 在 httpd.conf  下加入以下代码, 本例子是要映射到名字为 Dennis 机器的 rd 目录下

Alias /rd "//Dennis/rd"
<Directory "//Dennis/rd">
    Options Indexes FollowSymLinks MultiViews IncludesNoExec 
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

2006年04月25日

在 Windows 下的 Apache Server 用 Windows Domain Password 对某一个资料夹作认证的方法

  1. Download authentication mod_auth_sspi module from http://www.deadbeef.com/index.php/mod_auth_sspi/sspi.html
  2. 解压后把 /bin/ 下的 mod_auth_sspi.so copy 至 D:/apache2/modules/mod_auth_sspi.so
  3. 在 httpd.conf 中加入
     LoadModule sspi_auth_module modules/mod_auth_sspi.so
  4. 对要做制的目录做以下设定:
 Alias /issue "D:/Inetpub/wwwroot/mantis"
<Directory "D:/Inetpub/wwwroot/mantis">
    Options Indexes FollowSymLinks MultiViews IncludesNoExec
    AddOutputFilter Includes html
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
   
    AuthName "My Domain"
    AuthType SSPI
    SSPIAuth On
    SSPIAuthoritative On

    require valid-user
</Directory>