Skip navigation.

极湖

无不用其“极”

Posts tagged with "DATABASE"

获取 PostgreSQL 数据库之 sequence 名称列表的 SQL

, , ,

较低版本(1.0 ?) CakePHP 的文件
cake/libs/model/dbo/dbo_postgres.php
中,有一句 SQL:

SELECT sequence_name FROM information_schema.sequences

在 PostgreSQL 8 中,以上 SQL 运行出错。

根据原意修改如下:

SELECT c.relname as sequence_name FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n WHERE c.relnamespace = n.oid AND c.relkind='S' and n.nspname = 'public'

其中的 'public' 是 schema 名,需要根据实际情况修改。

顺便把 dbo_postgres.php 中包含以上 SQL 的函数修改如下:
    function sequenceExists($seq) {
        $cache = parent::__cacheDescription('sequences');
        if($cache != null) {
            return in_array($seq, $cache);
        }
        $sequences = array();
        // ★修改这句
        //$res = $this->rawQuery("SELECT sequence_name FROM information_schema.sequences");
        $schema = $this->config['schema'];
        $res = $this->rawQuery("SELECT c.relname as sequence_name FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n WHERE c.relnamespace = n.oid AND c.relkind='S' AND n.nspname = '{$schema}'");
        while($row = $this->fetchRow($res)) {
            $sequences[] = $row[0]['sequence_name'];
        }
        
        parent::__cacheDescription('sequences', $sequences);
        return in_array($seq, $sequences);
    }

需要说明的是,在新版本的 CakePHP 中,已见不到以上函数。

CakePHP 动态设置 tablePrefix 的一个例子

, ,

遇到一个问题

用 CakePHP 开发的网站,有几个子网站,每个子网站有一个 webroot,共用数据库,其中有部分数据表格不能共用,因此需要根据实际情况设置表格的前缀。

这个在 CakePHP 下其实很简单,只是没有先例,所以摸索了半天,最后得出的方法如下:

首先,在各个 webroot 下的 index.php 追加一个常量(在载入 bootstrap.php 之前)

例:
app/webroot-a/index.php
define('SITE_ID', 1);

app/webroot-b/index.php
define('SITE_ID', 2);

app/webroot-c/index.php
define('SITE_ID', 3);


然后,在需要不同前缀的表格的 Model 中加入 settableprefix() 函数

例:
app/models/users.php
<?php
class Users extends AppModel {
    var $name = 'Users';
    var $useTable = 'users';
    var $primaryKey = 'user_id';

    function settableprefix() {
        if(SITE_ID == 1) {
            $this->tablePrefix = 'a_';
        } elseif(SITE_ID == 2)  {
            $this->tablePrefix = 'b_';
        } elseif(SITE_ID == 3)  {
            $this->tablePrefix = 'c_';
        }
    }
}
?>

这样就可以了。

其实,index.php 中的常量也可以不用追加,直接利用现成的常量就行,比如 WEBROOT_DIR。追加常量是为了程序照顾运行的效率。

简明 PostgreSQL 建库步骤

, ,

1.切换至 PostgreSQL 超级用户
$ su - postgres

2.初始化数据库 (仅在数据库未初始化时需要此步骤)
$ initdb --no-locale --encoding=EUC_JP

3.创建用户和数据库
$ createuser -d -P user1
$ createdb -U user1 -O user1 userdb1


4.数据库连接测试
$ psql -U user1 -W userdb1

关于优化MySQL应对高访问网站的讨论

, , ,

原文: http://www-css.fnal.gov/dsg/external/freeware/mysqlTuning.html
翻译: 小白(又名 风子)

MySQL优化
关于优化MySQL应对高访问网站的讨论

Tim,
首席技术长官,DCS

MySQL设置,高并发连接
本人有一网站每天需要负责大约3000用户,每天长达5到7小时的在线工作。所以在繁忙时段,我们要为2000多并发用户提供服务。在PHP和MySQL的设置比较合理的前提下,即使是入门级的Intel平台的服务器,MySQL一样可以应对的很好。

首先是硬件需求
1. 最好可以将Apache/PHP和MySQL分开跑在两台独立的服务器上,Linux版本及分发不限
2. 尽量减少服务器的其他负载,将最多的资源留给Apache/PHP和MySQL服务

我正在使用的服务器配置如下:
1. Apache/PHP: 奔腾III, 600MHz, 512M内存
2. MySQL: 双奔腾III, 750MHz, 2G内存

如此有偏向的配置主要是因为数据库的负荷非常重。我们的网站为会员制的在线教学系统,只有登录以后才能使用其中的功能,而且内容高度自订化。学生可以在系统中访问属于本人的一整套服务,譬如课程,成绩卡,时间表等等。教师可在网站上在线创建课程,包括课文,语音材料(包括文本生成的音频材料)等等。

1) PHP 程序编写: 一定要使用持续连接!
从Apache/PHP到MySQL的通讯中,建立和关闭连接是一个非常大的开销。通过使用持续连接,大型的数据访问网站可以共享连接来交换数据,避免了载入每页时都要重复的连接请求。有鉴于用户每次点击都可能会产生一次连接,持续连接的左右是相当重要的。
确定使用mysql_pconnect来取代mysql_connect,同时在php.ini或者程序文件中的ini_set行应当做出相应的调整。

2) Apache设置(httpd.conf)。我们的网站进行了很多针对性的优化,经过多次试验对比,我们最终确定了一组参数。最终的结果使我们在top可以得到最大的CPU空闲百分比。
MinSpareServers 10
MaxSpareServers 20
StartServers 70
MaxClients 255

3) Mysql设置(my.cnf)。在MySQL的配置文件重,我们在[mysqld]模块中加入了如下内容
set-variable = max_connections = 300
(这个数字必须大于Apache的MaxClients,否则Apache将无法充分利用连接)
set-variable = max_user_connections = 300
set-variable = table_cacle = 1200
(在数据库查询中可能发生的最多join表数量乘以max_user_connections)
在如上的参数重,max_connections和max_user_connections为我们提供稳定的服务提供了巨大的帮助。当Apache/PHP连接MySQL服务器时会被作为单用户来对待,MySQL默认并发连接数为1(译者注,鉴于原文撰写时间较早,当时的数据库默认配置较为保守。译者特地检查MySQL 5.0.32的默认位置文件,发现默认并发连接数已经提高到100,对于中小型网站来说已经游刃有余。不过网管对于这个参数的作用还是应当了然于胸的)。通过修改max_connections,使得Apache/PHP可以产生大量的持续连接。当Apache启动时,大量的并发连接已经就绪,之后的访问都可以利用这条通路来交换数据,从而大大的节约了建立连接时的系统开销。

一些MySQL的其他技巧
set-variable = max_allowed_packet = 1M (数据健壮性检查,防止失控查询)
set-variable = max_connect_errors = 999999 (防止mysqld因为过多的连接错误而重启)

译者注: 本文原创于2003年,当时的网络条件和硬件条件和如今已经有了比较大的区别,软件配置也有了相当的变化,譬如set-variable在MySQL 4.0.2以后已经可以省略。所以本文仅供参考,为有志于深入了解Apache MySQL的管理员新手们提供一点辅助的参考资料,切莫生搬硬套。

查看PostgreSQL数据表格和索引的大小

,

-- 随机顺序
SELECT relname, reltuples, relpages FROM pg_class ;

-- 按 relpages(磁盘使用量)排序(降序)
SELECT relname, reltuples, relpages FROM pg_class ORDER BY relpages DESC ;

-- 按 reltuples(记录数)排序(降序)
SELECT relname, reltuples, relpages FROM pg_class ORDER BY reltuples DESC ;

参考:PostgreSQL Tips and Tricks

PostgreSQL之rule设定查询方法

, ,

在PostgreSQL的提示符下,用"\d"或是"\d<头字母>"命令可以很方便的查询各种对象的定义。今天发现有一个叫rule的东西不能用这个命令,在网上搜索了一下才在PostgreSQL的官方网站上找到方法。原来,各种rule的定义保存在pg_rules之中,通过查询这个表格即可得到rule的定义,如:

select * from pg_rules;

select * from pg_rules where rulename='RULE_NAME';

select * from pg_rules where tablename='TABLE_NAME';

几条常用的Mysql命令

,

建库
create database `mydb` default character set utf8 collate utf8_general_ci;
grant all privileges on mydb.* to mydb_user@'localhost' identified by "mydb_pass";
grant all privileges on mydb.* to mydb_user@'%' identified by "mydb_pass";

设置密码
set password for 'root'@'localhost' = old_password('root_pass');
set password for 'mydb_user'@'localhost' = old_password('mydb_pass');
set password for 'mydb_user'@'%' = old_password('mydb_pass');

备份
# mysqldump --default-character-set=utf8 -uroot --all-databses > alldb_dump.sql
# mysqldump -u mydb_user -p mydb_pass --compact --default-character-set=utf8 mydb > mydb_dump.sql

恢复
# mysql -u root -p --default-character-set=utf8 < alldb_dump.sql
# mysql -u mydb_user -p --default-character-set=utf8 mydb < mydb_dump.sql

MySQL和PostgreSQL性能调优参考

, ,

MySQL数据库的分析报告工具

,

在一个叫 Hack MySQL 的网站上,发现了一个 mysqlreport的Perl脚本,在MySQL服务器上运行该脚本,就能给你制作一份漂亮的MySQL运行情况报告,对于MySQL的性能分析以及调优很有帮助。

运行该脚本之前,可能需要修改的地方:

# Default values if nothing else
$mycnf{'host'} ||= 'localhost';
$mycnf{'port'} ||= 3306;
$mycnf{'socket'} ||= '/var/run/mysqld/mysqld.sock'; # Debian default
$mycnf{'user'} ||= $ENV{'USER'};

我只修改了其中关于socket文件的一句:

$mycnf{'socket'} ||= '/tmp/mysql.sock'; # Debian default

执行结果就出来了:

MySQL 4.1.16-log uptime 12 13:20:50 Thu Feb 8 11:10:26 2007

__ Key _________________________________________________________________
Buffer used 372.00k of 16.00M %Used: 2.27
Current 1.86M %Usage: 11.62
Write ratio 0.000
Read ratio 0.012

__ Questions ___________________________________________________________
Total 2.40M 2.2/s
Slow 0 0/s %Total: 0.00 %DMS: 0.00
DMS 540 0.0/s 0.02

__ Table Locks _________________________________________________________
Waited 0 0/s %Total: 0.00
Immediate 540 0.0/s

__ Tables ______________________________________________________________
Open 7 of 64 %Cache: 10.94
Opened 7 0.0/s

__ Connections _________________________________________________________
Max used 3 of 100 %Max: 3.00
Total 18.08k 0.0/s

__ Created Temp ________________________________________________________
Disk table 0 0/s
Table 0 0/s
File 0 0/s


改变 mySQL 数据库文件路径的方法

,

原文地址:
http://developer.spikesource.com/wiki/index.php/How_to_change_the_mysql_database_location

Identify the database files you wish to migrate into the new data directory
You can use the following command to list the current databases in mysql

mysqlshow -u root -p


Keep this list available as you will reference it in a later step
Shutdown the MySQL database, if it is running

mysqladmin -u root -p shutdown


Locate the MySQL configuration file. This is usually located in the directory $OSS_HOME/var/mysql

Note: You should make a backup copy of each file you're editing before modifying the contents

The property you need to change is named datadir. The default value is $OSS_HOME/var/mysql, which places the database files in the same directory as the log files and other runtime generated files. This property is usually located in the [mysqld_safe] section in the configuration file

You need to change:

[mysqld_safe]
datadir      = /opt/oss/var/mysql


to

[mysqld_safe]
datadir      = /opt/oss/var/mysql/data



Save these changes
Create the directory you specified in the datadir property

mkdir -p /optoss/var/mysql/data


Check to make sure the user assigned to execute mysql has read/write privileges on this directory
You may need to modify the directory settings using chown and chmod

Move the databases listed in the first step to the new data directory

mv test /opt/oss/var/mysql/data


Note: You can use the copy (cp) command instead if you prefer. Remember to remove the copied files once the migration is complete
You will need to move/copy each database into the new data directory

After all the databases have been migrated to the new data directory, you should start MySQL

openpkg rc mysql start


The database files will now be managed under the new data directory. If you encounter any problems during startup, you check the hostname.err file located in the data directory.
November 2009
S M T W T F S
October 2009December 2009
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30