Perform Basic MySQL Database Operations

Assuming you are a superuser, create a new database:

mysqladmin -p create db_name

Import data:

mysql -p db_name < db_name.sql

Dump the db:

mysqldump -p –opt db_name > db_name.sql


Here’s a script called db_backup (make the file executable with chmod a+x filename):


# vars
suffix=$(date +%m%d%Y)

# dumb the dbs
mysqldump -u$db_user -p$db_pass –opt asterisk > $path/asterisk.sql
mysqldump -u$db_user -p$db_pass –opt gallery2 > $path/gallery2.sql
mysqldump -u$db_user -p$db_pass –opt wordpress > $path/wordpress.sql

# create an archive, cleanup
tar -P -zcf $path/$filename $path/*.sql
rm -f $path/*.sql

exit 0

Edit the crontab with crontab -e and add the following line (which runs every day at 11 pm and logs to the file specified):

0 23 * * * /backup/ >> /var/log/db_backup.log

Alternately you can backup all databases with one command:

mysqldump –all-databases -p > mysqlbackup.sql

Or backup the database files directly from /var/lib/mysql/. You don’t have to be a guru to administer your databases (though some knowledge of relational databases & SQL definitely helps). PHPMyAdmin is a handy tool that you should setup if you haven’t already.


About Benjamin Perove

Benjamin has been associated with computer technology starting from a very early age, and has contributed to the success of many businesses and enterprises since 2001. He loves to crush pow at Keystone, play acoustic guitar, climb rocks, and ascend mountains on his road bike. Benjamin is an Avalanche fan and currently resides in Boulder, Colorado.

2 Comments so far

  1. Learn MySQL @ November 11th, 2008

    Learn MySQL…

    It is very simple to use this function through the SQL tab in phpMyAdmin (or any other direct interface with mysql). Say you want to change the name of the table from \”tbl_ oldtable\” to \”tbl_ newtable\” then this would be the code:…

Leave a reply

Add this site to your Firefox Search Bar

Twitter Updates

No public Twitter messages.

Recent Entries



This site is optimized for Firefox.