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.

Jan 092011
 

Import a CSV file into MySQL database

Answer:

To import a CSV file into the MySQL database, you can use the mysqlimport command.

# mysqlimport mytable.csv test.mytable

The above command import the CSV file into the table named `mytable` in the database `test`

Dec 292010
 

Enable MySQL binary log (bin log)

Answer:

The MySQL binary log contains the changes to the database in binary log format. They are useful for

  1. Database recovery
  2. Replication

To enable it, edit the MySQL configurations, e.g. /etc/my.cnf

..
log-bin
..

Don't forget to restart MySQL to take effect.

# /sbin/service mysqld restart

Nov 222010
 

Initialize MySQL Data Directory

Answer:

When you compiled a new MySQL server, it is needed to create a default data directory.

To do so, you need to use the mysql_install_db command.

# bin/mysql_install_db --user=mysql \
    --basedir=/opt/mysql/mysql \
    --datadir=/opt/mysql/mysql/data

The --datadir is the target directory for storing the database files, where --basedir specifies the MySQL server base directory.

Reference: http://dev.mysql.com/doc/refman/5.0/en/mysql-install-db.html

Nov 032010
 

Optimize table in MySQL

Answer:

OPTIMIZE TABLE command is useful to defragment the database tables. It is particularly useful when you have deleted a large portion of a table or if you have made massive changes to a table with variable-length rows.

To do so, execute the following statement

mysql> OPTIMIZE TABLE my_table;

That's all.

Oct 212010
 

How to show the warnings during mysqlimport

Answer:

When you use mysqlimport to import data from text file, the number of warnings will be displayed at the end of import. But there is no way to show the actual warnings message.

To show the warnings, the only method is get into the mysql client to insert, instead of using mysqlimport.

E.g.

mysql> LOAD DATA INFILE  '/tmp/user.csv' REPLACE INTO TABLE user
Query OK, 100000 rows affected, 192 warnings (2.30 sec)

mysql> SHOW WARNINGS;