Tag Archive for 'MySQL'

Range number in MySQL

To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement:

SELECT FLOOR(7 + (RAND() * 5));

文件自动增长和自动收缩SQL Server

文件自动增长和自动收缩

SQL Server允许用户设置数据库初始值、最大值,可以自动增长或者自动收缩。通过这些设置,可以防止数据库空间问题而导致的应用程序修改失败或者SQL Server把硬盘空间耗尽之类的事情发生。一般来讲,如果数据库不是很繁忙,默认的设置(开启自动增长)能够满足大部分的需求。但是数据文件和日志文件增长本身是一件耗费系统资源和影响性能的工作。所以如果完全依赖SQL Server自动完成,可能会导致系统性能不够稳定。一个管理得比较精细的系统,应该预先考虑到可能的空间使用需求,提前规划并引导数据的流向。尽量避免空间用尽而使得SQL Server不得不自动增长的现象发生。同时也要确保每一次自动增长都能够在可接受的时间内完成,及时满足客户端应用的需求。

那么怎么才能达到这样的目的呢?在谈论最佳配置之前,首先要讨论一下SQL Server数据文件和日志文件空间申请的一些特点。还是以下面这个数据库(如图1-31所示)为例。它有3个数据文件(假设它们属于同一个文件组)和两个日志文件(见表1-7): Continue reading ‘文件自动增长和自动收缩SQL Server’

MySQL主从热备配置(含innodb)

Replication Mysql

mysql主从热备有2种配置方式,备份某些库或者忽略备份某些库,建议选择后者。
Continue reading ‘MySQL主从热备配置(含innodb)’

Oracle

MySQL恢复单一字段数据

1
UPDATE people_user AS p LEFT JOIN people_user_tmp AS t ON t.user_id = p.user_id SET p.user_pic = t.user_pic WHERE p.user_id<1871

提高mysql随机查询的效率

  1. 基本使用:
    1
    SELECT * FROM `table` ORDER BY RAND() LIMIT 0,10;

    但是在ORDER BY后面用到RAND(),查询速度非常慢.

  2. MAX()*RAND()提高效率
    1
    2
    3
    SELECT * FROM `table`
    WHERE id &gt;= (SELECT floor( RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`)) + (SELECT MIN(id) FROM `table`)))
    ORDER BY id LIMIT 1;
  3. JOIN
    1
    2
    3
    4
    SELECT *
    FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+(SELECT MIN(id) FROM `table`)) AS id) AS t2
    WHERE t1.id &gt;= t2.id
    ORDER BY t1.id LIMIT 1;

Continue reading ‘提高mysql随机查询的效率’

MySQL 效能監控工具

管理 MySQL 最讓人困擾的就是如何有效的掌握 MySQL 的健康狀況,因為 MySQL 雖然有提供許多系統變數值供您參考,但這些零散的數據若要手動搜集與過濾將會是一件十分沒有效率的事情(除非您寫 Scripts 去分析)。而接下來要介紹的這套 “工具” 其實是由 hackmysql.com 的站長所撰寫的 Perl Scritps,旨在協助 MySQL DBA 搜集與分析 MySQL 的運作狀況。

使用Perl和MySQL找出书名中的中文词语

使用Perl找出书名中的中文词语,用于搜索引擎:

  1. 导入中文词库到MySQL数据库
    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
    #!/usr/bin/perl
    # $Id$
    # Add booknames to database
    use DBI;

    my $database = 'zhu_tags_filter';
    my $hostname = '192.168.1.249';
    my $port = '3306';
    my $user = 'root';
    my $password = '123456';
    my $input_file = '/mnt/books/dict.txt';

    $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
    $dbh = DBI->connect($dsn, $user, $password);
    $sth = $dbh->codepare("set NAMES 'utf8'");
    $sth->execute;
    $sth->finish;

    open(INPUT, "< $input_file")
        or die "Couldn't open $input_file";

    while (<INPUT>) {
        $dbh->do("INSERT INTO dict(name) VALUES(?)",undef, $_);
    }
    close(INPUT);

    $dbh->disconnect();
  2. 匹配中文词库
    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
    31
    32
    33
    #!/usr/bin/perl
    # $Id$
    # Generat keywords form Chinese dict and booknams
    use strict;
    use warnings;
    use DBI;

    my $db = 'zhu_tags_filter';
    my $host = '192.168.1.249';
    my $user = 'root';
    my $password = '123456';

    my $dbh = DBI->connect("DBI:mysql:database=$db;host=$host",
                           $user, $password);
    $dbh->do("SET NAMES 'utf8'");


    my $sth = $dbh->codepare("SELECT name FROM dict");
    $sth->execute();

    my $n = 0;
    do {
        while (my @row = $sth->fetchrow_array()) {
            $n++;
            my $keyword = $row[0];
            my $hn = $dbh->codepare("SELECT id FROM all_booknames WHERE name LIKE '%$keyword%' LIMIT 1");
            $hn->execute;
            if ($hn->rows > 0){
                $dbh->do("INSERT INTO keywords (name) VALUES('$row[0]')");
            }
            $hn->finish;
        }
    } until(!$sth->more_results)

MySQL error connections

连接MySQL得到这样的错误提示

1
Warning: mysql_pconnect(): Host '67.19.xx.xxx' is blocked because of many connection errors. Unblock with 'mysqladmin flush-hosts' in C:\Websites\ratedesi\include\connectdb.php on line 9 Host '67.19.xx.xxx' is blocked because of many connection errors. Unblock with 'mysqladmin flush-hosts'

在MySQL手册中找到这样的说明

A.2.5 `Host ‘HOST_NAME’ is blocked’
————————————
If you get the following error, it means that `mysqld’ has received
many connect requests from the host `’HOST_NAME that have
been interrupted in the middle:

Host ‘HOST_NAME’ is blocked because of many connection errors.
Unblock with ‘mysqladmin flush-hosts’

The number of interrupted connect requests allowed is determined by
the value of the `max_connect_errors’ system variable. After
`max_connect_errors’ failed requests, `mysqld’ assumes that
something is wrong (for example, that someone is trying to break
in), and blocks the host from further connections until you execute
a `mysqladmin flush-hosts’ command or issue a `FLUSH HOSTS’
statement. See *Note server-system-variables::.

By default, `mysqld’ blocks a host after 10 connection errors. You
can adjust the value by starting the server like this:

shell> mysqld_safe –max_connect_errors=10000 &
If you get this error message for a given host, you should first
verify that there isn’t anything wrong with TCP/IP connections from
that host. If you are having network problems, it does you no good
to increase the value of the `max_connect_errors’ variable.

这可能是比如PHP的未注销连接引起,查看MySQL的进程可以查看一下

1
 mysqladmin -u <user> -p<password> processlist

Apache 2.2.3 PHP 5.2.0 Mysql 4.1 Install

以 apache handler 和 fastcgi 两种方式安装 PHP 5.2.0, 并使用 –with-mysql 和 –with-mysqli 两种方式连接 Mysql 数据库

  1. 配置 PHP 安装
    1
    './configure' '--codefix=/opt/php5' '--with-apxs2=/opt/bin/apxs' '--enable-fastcgi' '--with-mysql=/usr/local/mysql' '--enable-calendar' '--enable-force-cgi-redirect' '--with-configure-file-path=/opt/php5/cgi' '--enable-trans-sid' '--with-gd' '--with-xml' '--with-ttf=/usr' '--with-freetype-dir=/usr' '--enable-exif' '--with-dom-xslt=/usr' '--with-jpeg-dir=/usr/local/jpeg6b' '--with-png-dir=/usr' '--with-zlib-dir=/usr' '--enable-ftp' '--with-curl=/usr' '--enable-mbstring' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-openssl=/usr' '--with-gettext' '--enable-sockets' '--enable-memory-limit' '--with-xmlrpc' '--enable-debug=no' '--enable-track-vars' '--with-iconv' '--enable-sqlite-utf8' '--with-pgsql=/opt/postgresql'
  2. 这里我还安装了 Postgresql 所以使用了 ‘–with-pgsql=/opt/postgresql’ ,不过奇怪的是 如果我使用
    1
    --with-pdo-mysql --with-pdo-pgsql

    就会出现配置安装的错误。

  3. 同时使用 mysql 和 mysqli 在 make 的时候可能会出现很多警告,在 MAKEFILE 文件中找到以 EXTRA_LIBS 开始的行,删除第二个 -lmysqlclient_r