Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Aug 172010
 

Set the maximum allowed number of connections in MySQL

Answer:

MySQL has a setting which prevent the database from overloaded due to too many concurrent connections. This value should depends on the processing power of your database server, e.g. CPU, memory, disks I/O.

To change the value, edit the MySQL configurations, e.g. /etc/my.cnf

..
max_connections=100
..

So if a client connecting to the database exceeded this value, they will be blocked.

Don't forget to restart MySQL to take effect.

# /sbin/service mysqld restart

Aug 102010
 

Change the maximum allowed binary log file size in MySQL

Answer:

By default, the MySQL's maximum binary log file is 1073741824 bytes (1GB).

If you want to change it, edit the MySQL configurations, e.g. /etc/my.cnf

[mysqld]
...
...
max_binlog_size 4294967296

Restart MySQL to take effect.

# /sbin/service mysqld restart

Now the new maximum binary log file is set to 4GB.

Aug 022010
 

Sleep in MySQL

Answer:

In MySQL, you can issue an query to sleep(), e.g.

mysql> SELECT SLEEP(2);
+----------+
| SLEEP(2) |
+----------+
|        0 |
+----------+
1 row in set (2.00 sec)
Jul 202010
 

What is the storage overhead in using VARCHAR in MySQL

Answer:

MySQL allow VARCHAR to have length from a value from 0 to 65,535.

Since MySQL need to store the length of each VARCHAR field, in additional to the actual data itself, it needs 16 bits = 2 bytes for each field. But if the length of VARCHAR during table creation is less than or equal to 255, it only take 1 byte per field.

Reference: http://dev.mysql.com/doc/refman/5.1/en/char.html

Jul 192010
 

Compare binary value in MySQL

Answer:

Assume you have the following table.

mysql> CREATE TABLE t ( 
    c BINARY(3)
) ENGINE = INNODB;

And you insert some sample value...

mysql> INSERT INTO t VALUES ('fo');
mysql> INSERT INTO t VALUES ('foo');

Finally you select it

mysql> SELECT * FROM t WHERE c = 'foo';
+------+
| c    |
+------+
| foo  |
+------+
1 row in set (0.00 sec)

mysql> SELECT * FROM t WHERE c = 'fo';
Empty set (0.00 sec)

Why you cannot find the result in the second query? Because the column is BINARY, the trailing space(s) is not ignored like this.

You need to query like...

mysql> SELECT * FROM WHERE c = 'fo\0';
+------+
| c    |
+------+
| fo   |
+------+
1 row in set (0.00 sec)

As you can see, you need to append the the 0x00 (the zero byte). at the end of the character.