Since dec 2006
1'942'871 Visitors
4'218'042 Pages

Nov 2010 Stats
82'909 Visitors
146'476 Pages
196 countries
Full statistics

Help us translate
our tutorials!

JOIN the
OpenManiak Team.
Blaise Carrera
Tutorials creation:
Blaise Carrera
Giovanni Fredducci
Angel Chraniotis
Moham. H. Karvan
Alexandro Silva
Blaise Carrera
Andrei Chertolyas
Sergiy Uvarov
Nickola Kolev
Łukasz Nowatkowski
Ivo Raisr
Catalin Bivolaru
Bogdan A. Costea
Kirill Simonov
Oliver Mucafir
JaeYoung Jeon
Seungyoon Lee
Jie Yu & Si Cheng
Tao Wei
Fumihito Yoshida
Muhammad Takdir
Çağdaş Tülek
Leslie Luthi
Joe Anderson
Jennifer Ockwell
Nigel Titley
Alison Rees
Sabrina Barbey
Blaise Carrera - Useful MySQL commands

Other MySql
Last Change : Aug 21 2009 french flagenglish flag
korean flag

If you like our tutorials, don't hesitate to support us and visit our sponsors!
Si vous aimez nos tutoriaux, n'hésitez pas à nous supporter et visiter nos sponsors!

MySQL is a very powerful open source database server developed by the MYSQL AB company. It is one of the most used in the database server market.
To get more info about MySQL, have a look at the Wikipedia website.

logo mysql We will try here to give you some keys to use the MySQL database or backup it.
For complete details about how to use MySQL, see the MySQL Website.

MySQL can use several types of table engines such as ISAM, MyISAM, HEAP, InnoDB or Berkeley.
By default, a new table will use MyISAM, which will create 3 types of files located in /var/lib/mysql/database_name/:
- structure
- data
- index



Phpmyadmin is the most famous interface to manage MySQL. It is written in PHP and requires the apache web server.
Phpmyadmin will be very useful if you are not an expert to handle SQL databases, tables or the server itself. We strongly recommend to install this tool:

#apt-get install phpmyadmin


By default, there is no password for the root user. So, the first thing to do is to add a new one.
Change a user password:

#mysqladmin -u root password new_root_password
Create a new database

#mysqladmin -u root -p create database_name
Insert a sql file.

#mysql -u root -p database_name < file.sql
Access a database and give all the rights to a user on this database:

#mysql -u root -p database_name
>grant all on database_name.* to newuser@localhost identified by 'userpassword';
>flush privileges;
Get the list of tables in a database and display the entire contents of a table:

#mysql -u root -p
>use database_name;
>show tables;
>select * from table_name;
List your MySQL users:

#mysql -u root -p
>use mysql;
>select * from user;
Reboot the MySQL software:

#/etc/init.d/mysql restart
Reload the MySQL config:

#/etc/init.d/mysql reload


Stop MySQL

#/etc/init.d/mysql stop
Start MySQL server without password:

#mysqld_safe --skip-grant-tables &
Login to MySQL as root:

#mysql -u root
>use mysql;
>update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
>flush privileges;
Restart MySQL Server:

#/etc/init.d/mysql restart
Test your new root user password:

# mysql -u root -p


Backup all your databases:
-u = user ; -p = password (there is no space between the "-p" keyword and the password);

#mysqldump -u root -prootpassword --all-databases > mysql_databases_backup.sql
Backup a specific database:

#mysqldump -u root -prootpasssword --databases database_name > mysql_database_backup.sql
Check details about the mysqldump command.

Restore your MySQL backup:

#mysql -u root -proot < sql_backup.sql
It's important to stress that when you backup a database, this will NOT backup any MySQL user accounts.
As the MySQL user accounts are stored in the "user" table of the "mysql" database, it's a good idea to backup the "mysql" database too.

See below an example where the MySQL databases are saved periodically.

First we create a mysql user with restricted permissions to backup the databases:

#mysql -u root -p
>GRANT SELECT , SHOW DATABASES , LOCK TABLES ON * . * TO backupuser@localhost IDENTIFIED BY 'password' ;
Second we create a shell script by copying the line below in a file called in the /home/sam directory where sam is a user of your choice.

#vi /home/sam/
date=`date -I`; mysqldump --all-databases -u backupuser --password="password" > databasebackup-$date.sql
The date will be added at the end of the file name. By instance, a file saved on January 4, 2007 will be named databasebackup-2007-01-04.sql.

#chown sam /home/sam/
#chmod 700 /home/sam/
Set appropriate rights for the mysqlbackup file. The file ownership is given to sam with full permission (read, write, execute)

Third we add the line below in our Linux user crontab file:

#crontab -e -u sam
30 00 * * * /home/sam/
The mysqlbackup shell script will be launched every day at 00:30 and thus will backup the MySQL databases.

If you liked our tutorials, don't hesitate to support us and visit our sponsors!
Si vous aimez nos tutoriaux, n'hésitez pas à nous supporter et visiter nos sponsors!