Basic administration commands for MySQL and Linux.

This posting shows some basic commands I use for administrating MySQL databases and using Linux.

Mysql
Show all databases:
mysql> show databases;

Run all commands on this database from now on:
mysql> use myDatabase;

Show all tables inthe selected database:
mysql> show tables;

Show all field/column names in a table:
mysql> describe myTable;

You have table with a field named “username” and you want to show what is in all rows where that field has a certain value. I.e. show all data on a certain user:
mysql> SELECT * FROM myTable WHERE username="myUser";

This may return so much data, that it is hard to see on the screen. Then you can end with \G instead of ; and it will show in another way:
mysql> SELECT * FROM myTable WHERE username="myUser"\G

You may want to find out of a table is damaged, using the command:
mysql> check table myTable;
You usually get the suspicion when the program that uses the database report (sometimes strange) errors

If a table is crashed or damaged, you can try to repair it:
mysql> repair table myTable;
Repairing a table can take a long time - don’t give up!

If that does not fix the problem, you can try:
mysql> repair table myTable EXTENDED;

Linux
This section assumes that you log in via ssh. If you are running Windows you can use the Putty program to do that.

The largest difference between Linux and Windows is, that Linux uses a / to separate directories, where Windows uses \. The next largest is that Linux don’t have drive letters. There is just one file system, and one root: / (think c:\). If you have a DVD-drive, it will be “mounted into the file structure”, so you can e.g. find it as /media/dvd/ instead of d:\.

Change directory (just like in DOS):
cd other_dir

Go back again:
cd ..

Change into a directory with a long name (here my_huge_directory_name_to_go_to):
cd my_h

Then the directory will be compleated, if it is possible. Nice.

Show all files in a folder (l=long, h=human readable sizes):
ls -lh

Delte a file:
rm myFile

Delete all files containing 4 zeros in the current folder (Think before you type - you can delete a lot of stuff in one command!):
rm *0000*

Delete an empty directory:
rmdir myDir

Rename or move a file or directory:
mv myFile newName
mv myFile someDir/

Copy a file:
cp myFile newFile

Copy a directory:
cp -r myDir newDir

Cancel almost any command that is running:
Press CTRL+C

Look in a text file, without changing it:
less myFile
Press up/down arrow or page up/down to move up or down. Press q to quit. Press /, type some text and enter to search for the text. Press n to search for it again and N to search for it upwards.

Run a program or script, that exists in the current folder:
./myProgram
You need the ./ to say that the program is in the current folder. For security reasons the shell (the command interpreter) does not look in the current folder when looking for programs to run.

Putty
Copy something: Just select the text with the mouse, and Putty will automatically copy it to the clip board.
Paste something: Just right click or press CTRL+insert, and putty will paste the text where the curser is.

Comments are closed.