手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2008年08月14日的文章

Mysql动态修改参数

    mysql数据库也像ORACLE数据库一样,可以动态的修改参数,可以修改会话级变量只对当前会话产生影响;也可以修改全局变量,对所有新连接的会话都产生影响。

修改会话级变量

用show variables 命令查看当前参数的值,like 'pattern'用于模式匹配,查找指定的参数

mysql> show variables like '%sort_buffer_size%';
+---------------------------+------------+
| Variable_name             | Value      |
+---------------------------+------------+
| sort_buffer_size          | 6291448    |
+---------------------------+------------+
1 rows in set (0.00 sec)

用set SESSION命令设置会话级变量的新值

mysql> set SESSION sort_buffer_size=7000000;
Query OK, 0 rows affected (0.00 sec)

--修改会话级变量对当前会话来说立刻生效
mysql> show variables like '%sort_buffer_size%';       
+---------------------------+------------+
| Variable_name             | Value      |
+---------------------------+------------+
| sort_buffer_size          | 7000000    |
+---------------------------+------------+
1 rows in set (0.00 sec)

mysql> exit
Bye
退出重新连接后,此参数恢复原值
[root@devdbc_stb root]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40 to server version: 5.0.37-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show variables like '%sort_buffer_size%';       
+---------------------------+------------+
| Variable_name             | Value      |
+---------------------------+------------+
| sort_buffer_size          | 6291448    |
+---------------------------+------------+
1 rows in set (0.00 sec)

修改全局变量
[root@devdbc_stb root]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40 to server version: 5.0.37-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show variables like '%sort_buffer_size%';       
+---------------------------+------------+
| Variable_name             | Value      |
+---------------------------+------------+
| sort_buffer_size          | 6291448    |
+---------------------------+------------+
1 rows in set (0.00 sec)

用set GLOBAL 命令设置全局变量

mysql> set GLOBAL sort_buffer_size = 7000000;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%sort_buffer_size%';        
+---------------------------+------------+
| Variable_name             | Value      |
+---------------------------+------------+
| sort_buffer_size          | 6291448    |
+---------------------------+------------+
1 rows in set (0.00 sec)
当前此参数的值并不发生变化,先退出,然后重新连进去
mysql> exit
Bye
[root@devdbc_stb root]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 41 to server version: 5.0.37-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show variables like '%sort_buffer_size%';
+---------------------------+------------+
| Variable_name             | Value      |
+---------------------------+------------+
| sort_buffer_size          | 7000000    |
+---------------------------+------------+
1 rows in set (0.00 sec)
新的参数值生效

Tags: mysql, 优化, database, 修改

mysql的大小写敏感性

  mysql的大小写敏感性主要分为两部份:一个是表名区别大小写;二是字段值不区分大小写.这个跟我们希望的恰好相反,而这却是mysql 的默认设置,不得不令人费解,而这些却是oracle数据库比较基本的东西。

   首先说说MYSQL建表的特点?如果你创建一张test表,然后你到data目录下会发现以下三个文件: 

 test.frm
 test.MYD
 test.MYI

即创建一个表,它会自动生成三个文件。这点跟oracle也有很大的不同,如果这样,那如何使用裸设备呢?看来要用mysql数据库,是不得不要用文件系统。而通常操作系统都对用户同时打开的文件数有限制,一般为1024个,使用mysql要注意.

  那如何设置让mysql的表名不区分大小写呢?修改/etc/my.cnf文件,在[mysqld] 下增加参数lower_case_table_names = 1

这个参数的含义是使所有的表名都转化成小写来处理,如果你在原系统中已有大写的表名,要先把它们重命名为小写,以免加了此参数后,以前大写的表无法识别.

   另外一个问题,就是默认的字段值不区分大小写?这点是比较令人头痛的事。如果你在一有唯一约束的列上插入两行值'A'和'a',Mysql会认为它是相同的,而在oracle中就不会。请看下面的测试:

mysql> create table test4(nick varchar(20) primary key);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test4 values('A');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test4 values('a');
ERROR 1062: Duplicate entry 'a' for key 1

而如何设置让其列值区分大小写呢?

mysql> create table test4(nick varchar(20) binary primary key);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test4 values('A');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test4 values('a');
Query OK, 1 row affected (0.00 sec)

在声明字符类型后,在后面加一个binary,mysql就可以区分大小写了。

Tags: mysql, database, 大小写

Mysql的一些实用字符串函数

1、concat()函数
   1.1 MySQL的concat函数可以连接一个或者多个字符串,如
       mysql> select concat('10');
       +--------------+
       | concat('10') |
       +--------------+
       | 10           |
       +--------------+
       1 row in set (0.00 sec)

       mysql> select concat('11','22','33');
       +------------------------+
       | concat('11','22','33') |
       +------------------------+
       | 112233                 |
       +------------------------+
       1 row in set (0.00 sec)

       而Oracle的concat函数只能连接两个字符串
       SQL> select concat('11','22') from dual;

   1.2 MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
       mysql> select concat('11','22',null);
       +------------------------+
       | concat('11','22',null) |
       +------------------------+
       | NULL                   |
       +------------------------+
       1 row in set (0.00 sec)

       而Oracle的concat函数连接的时候,只要有一个字符串不是NULL,就不会返回NULL
       SQL> select concat('11',NULL) from dual;

       CONCAT
       --
       11

2、concat_ws()函数, 表示concat with separator,即有分隔符的字符串连接
   如连接后以逗号分隔
       mysql> select concat_ws(',','11','22','33');
       +-------------------------------+
       | concat_ws(',','11','22','33') |
       +-------------------------------+
       | 11,22,33                      |
       +-------------------------------+
       1 row in set (0.00 sec)
  
   和concat不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL
       mysql> select concat_ws(',','11','22',NULL);
       +-------------------------------+
       | concat_ws(',','11','22',NULL) |
       +-------------------------------+
       | 11,22                         |
       +-------------------------------+
       1 row in set (0.00 sec)

3、group_concat()可用来行转列, Oracle没有这样的函数
   完整的语法如下
   group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
   如下例子
   mysql> select * from aa;
   +------+------+
   | id   | name |
   +------+------+
   |    1 | 10   |
   |    1 | 20   |
   |    1 | 20   |
   |    2 | 20   |
   |    3 | 200  |
   |    3 | 500  |
   +------+------+
   6 rows in set (0.00 sec)
  
   3.1 以id分组,把name字段的值打印在一行,逗号分隔(默认)
       mysql> select id,group_concat(name) from aa group by id;
       +------+--------------------+
       | id   | group_concat(name) |
       +------+--------------------+
       |    1 | 10,20,20           |
       |    2 | 20                 |
       |    3 | 200,500            |
       +------+--------------------+
       3 rows in set (0.00 sec)
  
   3.2 以id分组,把name字段的值打印在一行,分号分隔
       mysql> select id,group_concat(name separator ';') from aa group by id;
       +------+----------------------------------+
       | id   | group_concat(name separator ';') |
       +------+----------------------------------+
       |    1 | 10;20;20                         |
       |    2 | 20                               |
       |    3 | 200;500                          |
       +------+----------------------------------+
       3 rows in set (0.00 sec)

   3.3 以id分组,把去冗余的name字段的值打印在一行,逗号分隔
       mysql> select id,group_concat(distinct name) from aa group by id;
       +------+-----------------------------+
       | id   | group_concat(distinct name) |
       +------+-----------------------------+
       |    1 | 10,20                       |
       |    2 | 20                          |
       |    3 | 200,500                     |
       +------+-----------------------------+
       3 rows in set (0.00 sec)

   3.4 以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序
       mysql> select id,group_concat(name order by name desc) from aa group by id;
       +------+---------------------------------------+
       | id   | group_concat(name order by name desc) |
       +------+---------------------------------------+
       |    1 | 20,20,10                              |
       |    2 | 20                                    |
       |    3 | 500,200                               |
       +------+---------------------------------------+
       3 rows in set (0.00 sec)

4、repeat()函数,用来复制字符串,如下'ab'表示要复制的字符串,2表示复制的份数
   mysql> select repeat('ab',2);
   +----------------+
   | repeat('ab',2) |
   +----------------+
   | abab           |
   +----------------+
   1 row in set (0.00 sec)

   又如
   mysql> select repeat('a',2);
   +---------------+
   | repeat('a',2) |
   +---------------+
   | aa            |
   +---------------+
   1 row in set (0.00 sec)

Tags: mysql, database, 字符串