ABHIONLINUX
Site useful for linux administration and web hosting

Showing posts with label mysql commands. Show all posts
Showing posts with label mysql commands. Show all posts

2009/11/28

MySQL Commands:

1.login mysql from GNU/Linux shell.
$mysql -h hostname -u root -p

2.Creating a database.
mysql> create database [Database_Name];

3.List all databases.
mysql> show databases;

4.List all the tables in a database.
mysql> show tables;

5.For Switching to a particular database.
mysql> use [Database_Name];

6.List database field formats in a table.
mysql> describe [Table_Name];

7.Delete a database.
mysql> drop database [Database_Name];

8.Delete a table.
mysql> drop table [Table_Name];

9.List all data in a table.
mysql> SELECT * FROM [Table_Name];

10. Show unique records.
SELECT DISTINCT [column name] FROM [table name];

11. Change a users password.(from unix shell).
mysqladmin -u root -h hostname.blah.org -p password 'new-password'

12. Change a users password.(from MySQL prompt).
SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');

13. Allow the user "sree" to connect to the server from localhost using the password "mypass"
grant usage on *.* to 'sree'@'localhost' identified by 'mypass';

14. Update database permissions/privilages.
FLUSH PRIVILEGES;

15. Dump all databases for backup. Backup file is sql commands to recreate all db's.
mysqldump -u root -p password --opt >/tmp/alldatabases.sql

16. Dump one database for backup.
mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

17. Dump a table from a database.
mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

18. Restore database (or database table) from backup.
mysql -u username -ppassword databasename < /tmp/databasename.sql

or

# mysql database_name < database.sql