我的论坛更换服务器.论坛进行转移.顺便进行了升级.因为服务器用的mysql是4.1(原来安家空间是4.0),而4.1在编译 mysql 时,指定了一个默认的字符集,这个字符集是 utf8;就是这个utf8,因为不是很熟悉mysql,所以在升级的时候给我带来了相当大的麻烦.前后删除重建数据表不下20次. 在这里把我的一些经验之谈写出来,如果你也遇到我同样的情况,就可以少走一些弯路.
mysql4.0–>mysql4.1(默认字符集 utf8) discuz4.0–>discuz4.1
1.首先是论坛转移.
(1)先看看你的论坛的默认字符集是什么.进入你的phpmyadmin,里面有 mysql 连接校对 边上有一个下拉,点最上面的整理,页面会刷新,看看出来的是字符.如果是 utf8_swedish_ci ,说明默认的字符校对就是latin1了,你的情况就和我一样了.如果出来的是gbk_chinese_ci;那么你可以跳过此文.一知道了字符集的类型,就可以开始转移论坛了.
(2)首先新建一个discuz4.0,这时我们可以看到正常的文件.但是将备份的数据库导入后,会发现出来的全部是乱码,满篇都是??? 问题出在那里? 因为新建的discuz用的是 gbk_chinese_ci来和数据库进行连接校对的.新建的数据表的编码也是gbk_chinese_ci. 我们将备份数据导 ,因为备份数据是mysql4.0的,并没有连接校对字符集,所以mysql4.1会自动为倒入的备份数据添加一个默认的字符集,这个字符集是 utf8.如此一来,矛盾就出现了. 前台的编码访问模式是gbk_chinese_ci,而后台的编码模式却是utf8_swedish_ci.就像访问网页时,用utf-8去访问gb2312一样,自然而然的出现了错误.
解决方案:很明显,就是让前台用正确的编码访问模式去访问后台的编码.后台默认的编码是utf8_swedish_ci 我们让前台用utf8_swedish_ci 去访问就是了.打开文件include/db_mysql_class.php,大约在31~34行,有这样的一段代码.
if($this->version() > ‘4.1′ && in_array(strtolower($globals['charset']), array(’gbk’, ‘big5′, ‘utf-8′))) {
//attention: remove the following line if you want to use the default database character set.
mysql_query(”set names ‘”.str_replace(’-', ”, $globals['charset']).”‘”);
}
在”mysql_query(”set names ‘”.str_replace(’-', ”, $globals['charset']).”‘”);”它的意思是让程序用dz定义的字符集去访问mysql(gbk_chinese_ci),在前面加上”"//”将这一行注释掉.程序就会用mysql的默认字符集(utf8_swedish_ci)去访问mysql里面的数据.保存好文件,在后台更新缓存和统计. wowo~ 页面显示正常了. 高兴死了.
以上部份由旺角网络的旺才指点,参考文章 http://www.discuz.net/thread-201676-1-1.html
2.论坛升级.
(1)disucz4.1也是同样设置了用 gbk_chinese_ci 去访问数据的.所以在运行upgrade6.php前同样要先修改程序访问后台的字符集,不然出了错就糗大了.打开文件include/db_mysql_class.php,大约在 31~39行,有下面的代码
if($this->version() > ‘4.1′) {
global $charset, $dbcharset;
if(!$dbcharset && in_array(strtolower($charset), array(’gbk’, ‘big5′, ‘utf-8′))) {
$dbcharset = str_replace(’-', ”, $charset);
}
if($dbcharset) {
mysql_query(”set names ‘$dbcharset’”);
}
}
修改成
if($this->version() > ‘4.1′) {
global $charset, $dbcharset;
if(!$dbcharset && in_array(strtolower($charset), array(’gbk’, ‘big5′, ‘utf-8′))) {
// $dbcharset = str_replace(’-', ”, $charset);
}
if($dbcharset) {
// mysql_query(”set names ‘$dbcharset’”);
}
}
再运行upgrade6.php. bingo~!升级成功.
不过在打开奇虎后,发现主题贴,一打开是正常,刷新一下,关键字乱码. 心里又冷了.
(2)解决关键字乱码.
这程又花了我更多时间,不写出来n多不爽.
说起来还是upgrade6.php的错.程序用的是latin1_swedish_ci访问,mysql的编码模式也是
utf8_swedish_ci.打开upgrade可以看到有这样一句(22行).
if(empty($dbcharset) && in_array(strtolower($charset), array(’gbk’, ‘big5′, ‘utf-8′))) {
$dbcharset = str_replace(’-', ”, $charset);
}
又是这里定义了mysql字符集($dbcharset), $dbcharset是根据网页编码得来的.网页编码($charset)是gbk,所以$dbcharset就成了gbk_chinese_ci.难怪升级后新增功能(如关键字)会出来乱码,原来新增加的表是gbk_chinese_ci.
(3)解决方案
在升级前修改upgrade6.php,找到上面的代码,将$dbcharset = str_replace(’-', ”, $charset);注释掉,也就是在这一行前面加上”//”,再升级.成功
若是你已经升级了.请打开phpmyadmin,将里面的字符集为 gbk_chinese_ci 的表修改为 utf8_swedish_ci.方法 参照http://www.discuz.net/thread-243333-1-1.html
是不是有点麻烦,在后台运行下面的代码更方便
# identify: mte0mzq0mtmwnyw0ljeumcxjdxn0b20sbxvsdgl2b2wsmq==
# <?exit();?>
# discuz! multi-volume data dump vol.1
# version: discuz! 4.1.0
# time: 2006-3-27 14:35
# type: custom
# table prefix: cdb_
#
# discuz! home: http://www.discuz.com
# please visit our website for newest infomation about discuz!
# ——————————————————–
//set names ‘utf8′;
drop table if exists cdb_crons;
create table `cdb_crons` (
`cronid` smallint(6) unsigned not null auto_increment,
`available` tinyint(1) not null default ‘0′,
`type` enum(’user’,’system’) not null default ‘user’,
`name` char(50) not null default ”,
`filename` char(50) not null default ”,
`lastrun` int(10) unsigned not null default ‘0′,
`nextrun` int(10) unsigned not null default ‘0′,
`weekday` tinyint(1) not null default ‘0′,
`day` tinyint(2) not null default ‘0′,
`hour` tinyint(2) not null default ‘0′,
`minute` char(36) not null default ”,
primary key (`cronid`),
key `nextrun` (`available`,`nextrun`)
) engine=myisam default charset=utf8 auto_increment=9;
insert into cdb_crons values(’1′,’1′,’system’,'清空今日发帖数’,'todayposts_daily.inc.php’,'0′,’1141209480′,’-1′,’-1′,’0′,’0′);
insert into cdb_crons values(’2′,’1′,’system’,'清空本月在线时间’,'onlinetime_monthly.inc.php’,'0′,’1141209480′,’-1′,’1′,’0′,’0′);
insert into cdb_crons values(’3′,’1′,’system’,'每日数据清理’,'cleanup_daily.inc.php’,'0′,’1141209480′,’-1′,’-1′,’5′,’30′);
insert into cdb_crons values(’4′,’1′,’system’,'生日统计与邮件祝福’,'birthdays_daily.inc.php’,'0′,’1141209480′,’-1′,’-1′,’0′,’0′);
insert into cdb_crons values(’5′,’1′,’system’,'主题回复通知’,'notify_daily.inc.php’,'0′,’1141209480′,’-1′,’-1′,’5′,’00′);
insert into cdb_crons values(’6′,’1′,’system’,'每日公告清理’,'announcements_daily.inc.php’,'0′,’1141209480′,’-1′,’-1′,’0′,’0′);
insert into cdb_crons values(’7′,’1′,’system’,'限时操作清理’,'threadexpiries_daily.inc.php’,'0′,’1141209480′,’-1′,’-1′,’5′,’0′);
insert into cdb_crons values(’8′,’1′,’system’,'论坛推广清理’,'promotions_hourly.inc.php’,'0′,’1141209480′,’-1′,’-1′,’0′,’00′);
drop table if exists cdb_modworks;
create table `cdb_modworks` (
`uid` mediumint(8) unsigned not null default ‘0′,
`modaction` char(3) not null default ”,
`dateline` date not null default ‘2006-01-01′,
`count` smallint(6) unsigned not null default ‘0′,
`posts` smallint(6) unsigned not null default ‘0′,
key `uid` (`uid`,`dateline`)
) engine=myisam default charset=utf8;
drop table if exists cdb_pluginhooks;
create table `cdb_pluginhooks` (
`pluginhookid` mediumint(8) unsigned not null auto_increment,
`pluginid` smallint(6) unsigned not null default ‘0′,
`available` tinyint(1) not null default ‘0′,
`title` varchar(255) not null default ”,
`description` mediumtext not null,
`code` mediumtext not null,
primary key (`pluginhookid`),
key `pluginid` (`pluginid`),
key `available` (`available`)
) engine=myisam default charset=utf8 auto_increment=1;
drop table if exists cdb_pmsearchindex;
create table `cdb_pmsearchindex` (
`searchid` int(10) unsigned not null auto_increment,
`keywords` varchar(255) not null default ”,
`searchstring` varchar(255) not null default ”,
`uid` mediumint(8) unsigned not null default ‘0′,
`dateline` int(10) unsigned not null default ‘0′,
`expiration` int(10) unsigned not null default ‘0′,
`pms` smallint(6) unsigned not null default ‘0′,
`pmids` text not null,
primary key (`searchid`)
) engine=myisam default charset=utf8 auto_increment=1;
drop table if exists cdb_promotions;
create table `cdb_promotions` (
`ip` char(15) not null default ”,
`uid` mediumint(8) not null default ‘0′,
`username` char(15) not null default ”,
primary key (`ip`)
) engine=myisam default charset=utf8;
drop table if exists cdb_relatedthreads;
create table `cdb_relatedthreads` (
`tid` mediumint(8) not null default ‘0′,
`expiration` int(10) not null default ‘0′,
`keywords` varchar(255) not null default ”,
`relatedthreads` text not null,
primary key (`tid`)
) engine=myisam default charset=utf8;
ok 就么多了. 以上文字针对mysql4.1 且默认字符集为latin1.如果是其他字符集请酌情修改(gbk不在本文讨论范围) 本人水平有限,有出错的地方还请高手指教.写出来不容易,若转载请保留言署名.
by freeher@im286 http://bearlet.com qq 35645368