WordPress Network域名更替

Why I met this problem

我安装的WordPress是3.2.1版本,内含了建立多站点的功能(参考Create A Network),出于好奇我就一口气建立了三个站点,建立过程倒也没什么,挺顺利的。我是在局域网里面建立的,域名直接就用的局域网IP地址。麻烦从此开始:

  • 局域网IP是动态分配的,IP变了,所有站点就不好再用了
  • 我在WordPress安装的电脑上无法用localhost本地访问所有站点

How to solve

其实解决办法也挺简单的,我的这个WordPress Network纯属我个人和女朋友使用的,记录我们日常的生活工作之用,并不需要其他人使用。也就是说其实我只需要三台电脑能够访问这些站点,我自己两台(其中一台作为服务器安装了WordPress),我女朋友一台。解决方法如下:

  • 在mysql相关数据库中使用ttlovedog.com代替原来使用的IP地址
  • 修改WordPress根目录下的wp-config.php文件,将DOMAIN_CURRENT_SITE的值修改为ttlovedog.com
  • 修改系统的hosts文件
    • Windows: C:\Windows\System32\drivers\etc\hosts(Win7下禁止直接修改,可拷贝至别处修改后再覆盖)
    • Linux: /etc/hosts
    • 非服务器:添加 "服务器ip ttlovedog.com"
    • 服务器:添加 "127.0.0.1 ttlovedog.com"

MySQL数据库字符串替换

我不懂MySQL查询语句的用法也不懂PHP语言,借助phpMyAdmin我可以很方便地在数据库中搜索出包含原来IP地址的域,然后手动修改。如果只是少量数据修改,这样做很方便。可是现在需要在整个数据库里替换,工作量就相当大了。

我在网上搜索了,基本上都只是用到了这么一句"update $tab set $col=replace($col, '$src_str', '$dst_str') where $col LIKE '\%$src_str\%';",这个语句可以批量将数据库中名表单$tab里列$col里面的$src_str替换为$dst_str,其中$col需包含$src_str。

但这个语句应用到整个数据库范围内还是不方便。还好最近用Perl用得挺上手的,我就写了一个Perl脚本,顺利解决了这个问题。

View Code
 1 #!/usr/bin/perl -w
2 # find and replace a string gloably in a mysql database
3 use strict;
4
5 my $HOST="localhost"; #your mysql host
6 my $USER="****"; #your mysql username
7 my $PASS="****"; #your mysql password
8 my $DB="******"; #your database name
9 my $src_str = "****"; # old string
10 my $dst_str = "****"; # new string
11
12 #################################################
13 my @tables = ();
14 my @columns = ();
15 my $result = "";
16
17 sub mysql{
18 my $cmd = "mysql -h$HOST -u$USER -p$PASS $DB -e\"$_[0]\"";
19 #print $cmd."\n";
20 return `$cmd`;
21 }
22
23 #get table list in the database
24 $result = mysql("show tables;");
25 @tables = split(/[\r\n]+/, $result);
26 shift @tables;
27 printf "%d tables found in the database $DB\n",scalar @tables;
28 #printf "%s\n",$tables[0];
29
30 foreach my $tab(@tables){
31 #$tab = "wp_blogs";
32 printf "\nProcessing table %s...\n", $tab;
33 #get the columns list
34 $result = mysql("select COLUMN_NAME from information_schema.COLUMNS where TABLE_SCHEMA='$DB' AND TABLE_NAME='$tab'");
35 @columns = split(/[\r\n]+/, $result);
36 shift @columns;
37 foreach my $col(@columns){
38 my $num1 = 0;
39 my $num2 = 0;
40 $result = mysql("select $col from $DB.$tab where $col LIKE '\%$src_str\%';");
41 my @tmp = split(/[\r\n]+/, $result);
42 if(scalar @tmp){
43 $num1 = scalar @tmp - 1;
44 }
45
46 #find and replace this columns
47 mysql("update $tab set $col=replace($col, '$src_str', '$dst_str') where $col LIKE '\%$src_str\%';");
48
49 $result = mysql("select $col from $DB.$tab where $col LIKE '\%$src_str\%';");
50 @tmp = split(/[\r\n]+/, $result);
51 if(scalar @tmp){
52 $num2 = scalar @tmp - 1;
53 }
54
55 if($num1 > 0){
56 printf " %d items found, %d items replaced in column %s\n", $num1, $num1-$num2, $col;
57 }
58 }
59 print "\n\n";
60 }

结语

发现脚本语言确实挺好用的,目前自己只会Perl,对最基本的Bash还不熟,有空要来补补课。

测试了一下,现在三台电脑用新域名都可以正常访问了。Happy~

顺便测试了一下Table of Contents这个插件,插件本身没有多大问题,问题在于我想要这个插件能在屏幕上位置固定,即我滚动页面时,该目录位置不动,就像有的网站浮动广告的效果,TinyMCE Advanced插入层Position属性默认是absolute,修改为fixed后,顺利达到我想要的效果~~:<div style="position: fixed; left: 68%; top: 30%;">[toc label="本文目录"]</div>

WordPress, I love you~

posted @ 2011-10-26 13:08  ncturtle  阅读(218)  评论(0编辑  收藏  举报