MySQL dump and compress directly

01/2011 // Category: MySQL

Do a mysql dump and compress directly with gzip

mysqldump mysqldump_options | gzip > outputfile.sql.gz

Gunzip and import using gzip

gunzip < outputfile.sql.gz | mysql mysqldump_options 

Dumping MySQL Stored Procedures, Triggers and Functions

10/2010 // Category: MySQL

mysqldump will backup by default all the triggers but NOT the stored procedures/functions. There are 2 mysqldump parameters that control this behavior:

    * –routines – FALSE by default
    * –triggers – TRUE by default

To include in an existing backup script also triggers and stored procedures, add the –routines command line parameter:

mysqldump  --routines > dumpfile.sql 

To dump only the stored procedures and triggers and not the mysql tables and data:

mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt  > dumpfile.sql

To import them to another database server run:

mysql  < dumpfile.sql

Or with character encoding settings

mysql --default_character_set utf8 database  < dumpfile.sql

Executing SQL Statements from a Text File

04/2010 // Category: MySQL

The mysql client is mostly used interactively, like this:
shell> mysql db_name

To execute sql commands located in a file, invoke mysql like this:
shell> mysql db_name < text_file

If you place a USE db_name statement as the first statement in the file, it is unnecessary to specify the database name on the command line:
shell> mysql < text_file

______

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

Step by step instructions

1. Start mysql

2. If required, first select the database you wish to run the sql commands on
mysql> USE databasename;

3. Now execute the sql commands in the text file
mysql> SOURCE c:\sqlcommands.sql;

pagetop