Posts Tagged ‘MySQL’

Clear MySQL bin log files

Monday, December 12th, 2011

You need MySQL bin logs only if you are replicating the databases.

1. Disable bin logging
nano /etc/my.cnf
2. Comment all bin logging related lines

#log_bin = /var/log/mysql/mysql-bin.log
#expire_logs_days = 10
#max_binlog_size = 100M

3. Restart MySQL
4. Delete the bin log files by entering in mysql with:
mysql
5. Run the following command:
PURGE BINARY LOGS TO 'mysql-bin.03';

where 03 is the last number of the bin log you want to clear.

Convert MYSQL tables to INNODB script

Tuesday, July 12th, 2011

If you want to convert MYSQL tables to INNODB use the following script:

mysql -u root -p -e "SHOW TABLES IN DATABASE-NAME;" | tail -n +2 | xargs -I '{}' echo "ALTER TABLE {} ENGINE=INNODB;" > alter_table.sql
and then run:
perl -p -i -e 's/(search_[a-z_]+ ENGINE=)INNODB/\1MYISAM/g' alter_table.sql

Replace “DATABASE-NAME” with your database name. You also need root access (preferably).

How to properly save diacritics in MySQL database

Wednesday, January 27th, 2010

To save diacritics in a MySQL database you have 5 steps to follow:

1. Set(create) your database with collation utf8_general_ci;

2. Set(create) your table with utf8_general_ci collation also;

3. Make sure the text fields where you’ll insert diacritics have also the utf8_general_ci collation

4. Convert your string into ‘utf-8′ using any method you like. I recommend using PHP’s multibyte functions

e.g. $my_string = mb_convert_encoding($txt, $charset, mb_detect_encoding($my_string));

where:  $my_string is the string that will be inserted

$charset is thecharset where your diacritics are included.

Check also here the Complete list of Popular character encodings

5. Before the insert query execute this query SET NAMES latin2; (if your diacritics are from one of the latin charsets)

Backing Up All MySQL Databases

Monday, January 18th, 2010

To backup all the databases in MySQL use:

mysqldump --all-databases -pPASSWORD | bzip2 -c > databasebackup.sql.bz2

The password is for the user Root.

To restore MySQL use:

mysql -pPASSWORD < databasebackup.sql

How to import large .sql files into MySQL via shell?

Thursday, October 1st, 2009

If you seek to import a large MySQL file via the shell in Linux, the following command might be helpful to you:

shell>mysql -uusername -ppassword db_name < dumpfile.sql

If you are already running mysql, you can execute an SQL script file using the source or \. command:

mysql> source dump.sql
mysql> \. dump.sql