Backing up and Restoring your MySQL database – Ubuntu

Posted by | Posted in Bash, Code, Command-Line, Guides, Linux, MySQL, Ubuntu | Posted on 23-07-2009

If you manage any type of databases, you know the importance of backing up your data. Whether you have file corruption from a failing hard drive, a run away script or from a hacking attempt; loosing your databases can and will break your business. If you’ve never backed up your database, now is a good time to start.

The easiest way to backup your MySQL database is with “/usr/bin/mysqldump”. You’ll need to access this application via command. Whether it is from the a console or an SSH connection.

mysqldump – a database backup program
mysqldump [options] [db_name [tbl_name ...]]

There are three general ways to invoke mysqldump:

mysqldump [options] db_name [tables]
mysqldump [options] --databases db_name1 [db_name2 db_name3...]
mysqldump [options] --all-databases

By default, the mysqldump command will dump the contents of your database directly into your terminal and not into a file. This output can be redirected anyway and to anywhere you like. But it is commonly used in this manner:

mysqldump -u [username] -p [password] [databasename] > [backup.sql]

[username] – your database username
[password] – the password for your database
[databasename] – the name of your database
[backup.sql] – the file to which the database contents needs to be directed to

If you don’t want to use your password in the command line, you can always prompt for your password by excluding it from your command.

mysqldump -u root -p DATABASE > DATABASE-BACKUP-DATE.sql
Enter password:

If you forgot your password, you’ll get an error like this.

mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: YES) when trying to connect

Or if you didn’t enter a password when one is needed, you’ll get this error.

mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect

If you’d like the convenience of having the database automatically delete the old database before re-writing the content you can use the “–add-drop-table” feature.

 mysqldump --add-drop-table -u root -p PASSWORD DATABASE > DATABASE-BACKUP-DATE.sql

If you need to only backup specific tables of the database you can use this command.

mysqldump --add-drop-table -u root -p PASSWORD DATABASE TABLE> DATABASE-TABLE-BACKUP-DATE.sql

The syntax for the command is:

mysqldump -u [username] -p [password] [databasename] [table1 table2 ....]

[tables] – This is a list of tables to backup. Each table is separated by a space.

If you are a server administrator and need to backup multiple databases at the same time, you can use the next command. You just have to be sure that the “-u” user account has permission to read those databases.

mysqldump -u root -p PASSWORD --databases DATABASE1 DATABASE2 DATABASE3 > DATABASE-1-2-3-BACKUP-DATE.sql

Or if you just want to back up ALL the databases at one, use the “–all-databases” parameter to backup all the databases in the server in one easy step.

mysqldump --all-databases> ALL-DATABASES-DATE.sql

How to back up only the data structure.

While in development, most programmers only need to back up their database structure, not their test data. To do this, use the “–no-data” parameter.

mysqldump --no-data --databases DATABASE1 DATABASE2 DATABASE3 > DATABASE-1-2-3-Structure-Bakcup-DATE.sql

If you have large databases and not enough hard drive space to store them in the regular SQL, text format, you can compressing the backup file on the fly.

mysqldump --all-databases | bzip2 -c >all-databases-backup-date.sql.bz2
mysqldump --all-databases | gzip >all-databases-backup-date.sql.gz

If you are away from your servers, or have multiple servers that you manage, you can still backup your databases while you are away. Use the bash script below in conjunction with a cron-tab to automate your backups.

date=`date -I`
mysqldump --all-databases | gzip > /var/mysql_backup/mysql-backup-all-$date.sql.gz

How to restore your databases.

If the unforeseen happens and you need to totally rebuild your databases, you can do so easily from the mysqldump SQL files.


Mysql Synopsis:

mysql -u [username] -p [password] [database_to_restore] < [backup_file.sql]

Restoring from the compressed files.

gunzip < DATABASE-BACKUP.sql.gz | mysql -u root -p PASSWORD DATABASE

Or do 2 at a time.

cat DATABASE-BACKUP-1.sql DATABASE-BACKUP-2.sql | mysql -u root -p PASSWORD

Installing and setting up Apache2 with Vhosts – Ubuntu

Posted by | Posted in Command-Line, Guides, Linux, Ubuntu | Posted on 22-07-2009

Apache is the most widely used website hosting applications. At the time of this article, Apache is reported to have about 70% of the market. While Microsoft IIS has dropped dramatically to 30%. I’ve personally been using Apache for the last 15 years had have never ever had any problems with the application. All the problems I’ve had were related to typos in my configuration files.

To get Apache up and running, you need to do 2 things.

1) install apache
2) start apache

That is basically all you need to know, to get apache up and running. Fairly simple. But of course human nature creeps in and we want to start customizing and personalizing.

To Installing apache2 via apt-get:

sudo apt-get install apache2

Starting apache2 for the first time

sudo /etc/init.d/apache2 start

If you get this error:

apache2: Could not determine the server’s fully qualified domain name, using for ServerName

You need to add “ServerName localhost” to the “/etc/apache2/conf.d/fqdn” file. This can all be done in a single command:

echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn

If you need to Start|Stop|Reload|etc Apache’s httpd daemon on Ubuntu, you’d want to use the “/etc/init.d/apache2″ commands:

Usage: /etc/init.d/apache2 {

If you are using any other operating system, you’d want to use the apachectl command:

apachectl – Apache HTTP Server Control Interface


apachectl commands

start : Loads the org.apache.httpd launchd job.
stop, graceful-stop : Unloads the org.apache.httpd launchd job.
restart, graceful : Unloads, then loads the org.apache.httpd launchd job.
fullstatus : Displays  a  full status report from mod_status. For this to work, 
you need to have mod_status enabled on your server and a text-based 
browser such as lynx available on your system. The URL used to access the 
status report can be set by editing the STATUSURL variable in the script.
status : Displays a brief status report. Similar to the fullstatus option, except 
that the list of requests currently being served is omitted.
configtest : Run a configuration file syntax test. It parses the configuration files 
and either reports Syntax Ok or detailed information about  the  particular 
syntax error. This is equivalent to apachectl -t.
The following option was available in earlier versions but has been removed. 
(It is now built into the httpd daemon).
startssl : To  start  httpd  with  SSL  support,  you should edit your configuration 
file to include the relevant directives and then use the normal apachectl start.

The main files/directories you will be working with first are:
apache2.conf : the configuration file
sites-available : a directory to let Apache know what sites are available to be accessed by httpd
sites-enables : a directory to let Apache know which websites need to be loading on start

A list of the default apache2 configuration file structure in “/etc/apache2/”:

dmccoy@server:/etc/apache2# ls -l /etc/apache2/
total 48
-rw-r--r-- 1 root root 10104 2009-04-01 11:01 apache2.conf
drwxr-xr-x 2 root root  4096 2009-07-17 05:16 conf.d
-rw-r--r-- 1 root root   378 2009-04-01 11:01 envvars
-rw-r--r-- 1 root root     0 2009-07-13 10:57 httpd.conf
drwxr-xr-x 2 root root 12288 2009-07-17 05:16 mods-available
drwxr-xr-x 2 root root  4096 2009-07-13 10:57 mods-enabled
-rw-r--r-- 1 root root   513 2009-04-01 11:01 ports.conf
drwxr-xr-x 2 root root  4096 2009-07-22 00:54 sites-available
drwxr-xr-x 2 root root  4096 2009-07-13 10:57 sites-enabled

The default web site is located at:


if you launch the domain in a web browser “http://localhost” you should see a message “It Works!”. If you are using the server version with out a gui, you can use “lynx http://localhost”. If lynx isn’t installed, you can use apt-get to install it.

If you need to maintain more then (1) website, you can use a Virtual Host to manage its location and access. Below is the default Virtual Host sample, located in “/etc/apache2/sites-available/default/”

There are (4) lines that you need to change.
1) You’ll need to change the ServerAdmin from webmaster@localhost to email address.
2) If you are changing your default location of DocumentRoot /var/www, you’ll need to edit the path here. An instance where you’d need to change this directory, is if it was located on a different hard drive or hard drive partition.
3) If you change your DocumentRoot, then you’ll need to change the Directory from /var/www/ to your new path.
4) You’ll also need to change your log names to allow easier management of multiple domains. ErrorLog /var/log/apache2/error.log and CustomLog /var/log/apache2/access.log combined could be changed to and

< VirtualHost *:80 >
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www
        < Directory />
                Options FollowSymLinks
                AllowOverride None
        < /Directory >
        < Directory /var/www/ >
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        < /Directory >
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        < Directory "/usr/lib/cgi-bin" >
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        < /Directory >
        ErrorLog /var/log/apache2/error.log
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        CustomLog /var/log/apache2/access.log combined
    Alias /doc/ "/usr/share/doc/"
    < Directory "/usr/share/doc/" >
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from ::1/128
    < /Directory >
< /VirtualHost >

Below is a sample VirtualHost file for a domain.

< VirtualHost *:80 >
        ServerAdmin dmccoy @ server .com
        ServerAlias server
        DocumentRoot /var/www
        < Directory />
                Options FollowSymLinks
                AllowOverride None
        < /Directory >
        < Directory /var/www/ >
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        < /Directory >
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        < Directory "/usr/lib/cgi-bin" >
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        < /Directory >
        ErrorLog /var/log/apache2/
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        CustomLog /var/log/apache2/ combined
    Alias /doc/ "/usr/share/doc/"
    < Directory "/usr/share/doc/" >
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from ::1/128
    < /Directory >
< /VirtualHost >

You’ll Notice that I added (2) extra lines. I added “ServerName” and “ServerAlias“.

ServerName : Description : Hostname and port that the server uses to identify itself
ServerName : Syntax : ServerName fully-qualified-domain-name[:port]
ServerName : Usage : ServerName

ServerAlias : Description : Alternate names for a host used when matching requests to name-virtual hosts
ServerAlias : Syntax : ServerAlias hostname [hostname] …
ServerAlias : Usage : ServerAlias server server2

If by change you need manage another domain, you can copy the default virtualhost file and edit those same 4 lines to create another domain. And if you have another, you repeat the steps until all your domains are entered.

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/

Now that you’ve made your sites available to httpd (Apache), you can now enable them to be used by httpd. The easiest way to do this, is to make a symbolic link from the sites-available directory to the sites-enabled directory. Below is how I would enable the default domain for this server.

sudo ln -s /etc/apache2/sites-available/default /etc/apache2/sites-enabled/default

Once you have enabled all the domains you need, you can now restart Apache to make them active.

sudo /etc/init.d/apache2 restart
[sudo] password for dmccoy: 
 * Restarting web server apache2
 ... waiting    ...done.

How to download files via command line – OS X, Ubuntu : Wget, cURL

Posted by | Posted in Command-Line, Linux, OS X, Ubuntu | Posted on 21-07-2009

For most server administrators doing system updates or installing patches via a command shell is normal, but for the average user trying to patch his new system, this task might be a little difficult. Opening a terminal and downloading a file, just by typing in a few words, is not as simple as clicking a download icon on a website, tho, once you learn how to use Wget or cURL, it will be.

From my understanding, Wget is a stand-alone application, that is command-line only. And, cURL is a cross-platfrom API library called libcurl. Personally, I like Wget better then cURL, but that’s just because I am bias.

If you want to know all the nitty-gritty differences, check out curl vs Wget.

Wget – The non-interactive network downloader. (I refer to it as, WWW Get or Web Get).

wget [option]… [URL]…

curl – transfer a URL

curl [options] [URL...]

How to download a file with wget:


How to download a file with cURL:

curl -O
curl >

Use Dstat to monitor your server – Ubuntu

Posted by | Posted in Command-Line, Linux, Ubuntu | Posted on 21-07-2009

Dstat is one of the best tools you can use for monitoring your server during peak times. It is a combination of vmstat, iostat, netstat, nfsstat and ifstat. This program allows you to access and view key elements of your system’s resources instantly.

dstat – versatile tool for generating system resource statistics

dstat [-afv] [options..] [delay [count]]

Using dstat to relate disk-throughput with network-usage (eth0), total CPU-usage and 
system counters:
       dstat -dnyc -N eth0 -C total -f 5
       Checking dstat’s behaviour and the system’s impact on dstat:
       dstat -taf --debug
       Using the time plugin together with cpu, net, disk, system, load, proc and 
       topcpu plugins:
       dstat -tcndylp -M topcpu
       this is identical to
       dstat -M time,cpu,net,disk,sys,load,proc,topcpu
       Using dstat to relate cpu stats with interrupts per device:
       dstat -tcyif

Installing Dstat

sudo apt-get install dstat
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 58.0kB of archives.
After this operation, 406kB of additional disk space will be used.
Get:1 jaunty/universe dstat 0.6.8-1 [58.0kB]
Fetched 58.0kB in 1s (50.0kB/s)
Selecting previously deselected package dstat.
(Reading database ... 24720 files and directories currently installed.)
Unpacking dstat (from .../archives/dstat_0.6.8-1_all.deb) ...
Processing triggers for man-db ...
Setting up dstat (0.6.8-1) ...

Dstat updates every second to display its information. Use “control C” to quit the application.

$ dstat
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw 
  0   0 100   0   0   0|  13k   29k|   0     0 |   0     0 |   7    12 
  0   1  99   0   0   0|  32k    0 | 418B  404B|   0     0 |  11    14 
  0   0 100   0   0   0|   0     0 |1360B  420B|   0     0 |  17    14 
  0   0  99   1   0   0|   0   856k| 132B  420B|   0     0 |  22    22 
  0   0 100   0   0   0|   0     0 | 132B  420B|   0     0 |  10    14 
  0   0 100   0   0   0|   0     0 | 192B  420B|   0     0 |  12    16 ^C

A screen shot of the unmodified output of dstat:

Picture 9

Family Commands for dstat:

    Performance tools
       ifstat(1), iftop(8), iostat(1), mpstat(1), netstat(1), nfsstat(1), nstat, vmstat(1), xosview(1)
   Debugging tools
       htop(1), lslk(1), lsof(8), top(1)
   Process tracing
       ltrace(1), pmap(1), ps(1), pstack(1), strace(1)
   Binary debugging
       ldd(1), file(1), nm(1), objdump(1), readelf(1)
   Memory usage tools
       free(1), memusage, memusagestat, slabtop(1)
   Accounting tools
       dump-acct, dump-utmp, sa(8)
   Hardware debugging tools
       dmidecode, ifinfo(1), lsdev(1), lshal(1), lshw(1), lsmod(8), lspci(8), lsusb(8), smartctl(8), x86info(1)
   Application debugging
       mailstats(8), qshape(1)
   Xorg related tools
       xdpyinfo(1), xrestop(1)
   Other useful info

A beginners guide to setting up MySQL – Ubuntu

Posted by | Posted in Code, Command-Line, Guides, Linux, MySQL, Ubuntu | Posted on 20-07-2009

MySQL is a very popular and widely used database system that can be used on almost any operating system, but primarly MySQL is used on LAMP (Linux/Apache/MySQL/PHP) systems.

Setting up a MySQL server is fairly easy. If you already know how to work a commmand-line interface, then you are well on your way to setting up a MySQL system. There are (4) things that you need to do, in-order to use a MySQL database system.

1) Install MySQL Server
2) Set a password for the root account
3) Set limitation privileges on the root account
4) Create a database

Installing mysql server via apt-get

$ sudo apt-get install mysql-server

On a default install, the root users does not need a password to access any databases and you can login with no password like this.

$ mysql -u root

If a password is required, use the extra switch -p:

$ mysql -u root -p
Enter password:

To add a root password when there isn’t one. Use the following command. Be sure to change the PASSWORD to your own password and to make sure that it is encapsulated with quotation marks.

mysqladmin -u root password "PASSWORD"

Now that we have the root account locked down with a password, we need to change its accessibility. While you are logged into mysql used these following commands to secure the root account.

mysql> use mysql;
mysql> delete from user where Host like "%";
mysql> grant all privileges on *.* to root@"%.DOMAIN.COM" identified by 'PASSWORD' with grant option;
mysql> grant all privileges on *.* to root@localhost identified by 'PASSWORD' with grant option;
mysql> flush privileges;
mysql> exit;

Now that we can login and feel secure, we create a database. Change DATABASE to any name you’d like.

mysql> create database DATABASE;
Query OK, 1 row affected (0.00 sec)

And that’s it. You can now check that you can connect to the MySQL server using this command:

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 61
Server version: 5.0.75-0ubuntu10.2 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

Or, If you want to check your version

mysqladmin -u root -p version
Enter password: 
mysqladmin  Ver 8.41 Distrib 5.0.75, for debian-linux-gnu on i486
Copyright (C) 2000-2006 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Server version          5.0.75-0ubuntu10.2
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 3 hours 7 min 4 sec
Threads: 2  Questions: 6596  Slow queries: 0  Opens: 969  Flush tables: 1  Open tables: 19  Queries per second avg: 0.588

If you need to start|stop|restart MySQL, use the commands in /etc/init.d/mysql

sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
sudo /etc/init.d/mysql restart

Notice :

preforming the above security options for the root account will break the /etc/init.d/mysql scripts.

sudo ./mysql status
/usr/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'debian-sys-maint'@'localhost' (using password: YES)'
@:/etc/init.d$ sudo /etc/init.dmysql restart
 * Stopping MySQL database server mysqld!
 * Starting MySQL database server mysqld
/usr/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'debian-sys-maint'@'localhost' (using password: YES)'

To fix this issue, you need to edit the /etc/mysql/debian.cnf file. There are (2) lines that need to be changed. Change the password hash to a new password. Unfortunately, this password is plain text.

# Automatically generated for Debian scripts. DO NOT TOUCH!
host     = localhost
user     = debian-sys-maint
socket   = /var/run/mysqld/mysqld.sock
user     = debian-sys-maint
socket   = /var/run/mysqld/mysqld.sock
basedir  = /usr

Then you need to log into MySQL and permissions to the “debian-sys-maint” user account.


Now, if for some reason, you overwrite, or toast your root password, You need to reset it. At this point in time, the start|stop features of /etc/init.d/mysql will probably no longer work and you need to kill the process old school style.

ps -aux |grep mysql
root      5340  0.0  0.0   1872   548 pts/2    S    05:17   0:00 /bin/sh /usr/bin/mysqld_safe
mysql     5379  0.0  3.5 129412 27116 pts/2    Sl   05:17   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/ --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
root      5381  0.0  0.0   3052   704 pts/2    S    05:17   0:00 logger -p daemon.err -t mysqld_safe -i -t mysqld
root      5543  0.0  0.1   3336   792 pts/2    R+   05:23   0:00 grep mysql

What you are seeing here is a list of processes that contain the word “mysql”. From left to right you can see there is a USER and the PROCESS ID (4 digits), LOAD variables, RUN time and then the COMMAND. What we are interested in is the (4) digit process ID numbers. In this case, 5340, 5379 and 5381. The 5543 process, as you can see is “grep mysql”. This is our search process ID and most likely doesn’t exist anymore.

In-order to stop those (3) processes, you need to kill them. Literally. You need to use the “kill” command to stop the process from running, and to keep it from relaunching, you add the “-9″. This refers to the type of kill you are preforming. In this case the kill cannot be blocked and won’t relaunch.

# sudo kill -9 5340
# sudo kill -9 5379
# sudo kill -9 5381
kill: No such process

As you can see the 5381 process no longer existed after we killed the 5379 process.

Now that we have mysql completely stopped, we can restart the mysql server and change the root password. To be able to do this start mysql with the “–skip-grant-tables” followed by the “&” character. The skip grant tables will bypass the mysql database and won’t load the current usernames and passwords. the “&” will embed the process in the background, so you can keep working from the same command-shell.

sudo mysqld --skip-grant-tables &
InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
090720  5:42:50  InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
090720  5:42:50  InnoDB: Started; log sequence number 0 115369069
090720  5:42:50 [Note] mysqld: ready for connections.
Version: '5.0.75-0ubuntu10.2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)

Now that MySQL is restarted, go ahead and log back into the server and use the mysql database. Notice, we don’t need a password this time.

mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.75-0ubuntu10.2 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

Now lets change the root password.

mysql> UPDATE user SET password = password('NEW PASSWORD') WHERE user = 'root' AND host='localhost';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

The Rows mached:1, means (1) row was updated and the password was updated successfully!

Flush your privileges and quit.

mysql> FLUSH privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit;

Now restart your mysql server to reactivate the accounts and passwords.

sudo /etc/init.d/mysql restart
 * Stopping MySQL database server mysqld
090720  5:52:22 [Note] mysqld: Normal shutdown
090720  5:52:22  InnoDB: Starting shutdown...
090720  5:52:23  InnoDB: Shutdown completed; log sequence number 0 115369069
090720  5:52:23 [Note] mysqld: Shutdown complete
 * Starting MySQL database server mysqld
 * Checking for corrupt, not cleanly closed and upgrade needing tables.
[1]+  Done                    sudo mysqld --skip-grant-tables

Using MyTop – Ubuntu

Posted by | Posted in Command-Line, Linux, MySQL, Ubuntu | Posted on 20-07-2009

If you ever need to know how well your databases are being queried, mytop is the key. MyTop is a clone of Top for MySQL.

Your Ubuntu server won’t come pre-installed with mytop, so you’ll have to to apt-get to install it.

/usr/bin/sudo /usr/bin/apt-get install mytop

Once mytop is installed and your mysql server is setup, you can view your database stats.

-u : user account
-p : password
-h : server, normally localhost
-d : database you want to view
-P : port, if different from default


You should see something like this come up.

MySQL on localhost (5.0.75-0ubuntu10.2)    up 0+00:32:06 [02:09:34] Queries: 5.0
qps:    0 Slow:     0.0         Se/In/Up/De(%):    00/00/00/00   qps now:    0 
Slow qps: 0.0  Threads:    1 (   1/   0) 00/00/00/00  Key Efficiency: 90.0%  
Bps in/out:   0.1/  6.9   Now in/out:   8.3/ 1.3k
 Id      User         Host/IP         DB      Time    Cmd Query or State 
 --      ----         -------         --      ----    --- ----------  
38      root       localhost      mysql         0  Query show full processlist

to exit mytop, press the “Q” key.

Using PHP to return an IP address

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

After I’ve determine which interfaces are active on my computer(s), I can use this script to report back the IP address of said interface.

# input String ("en0")
# output String IP
function get_network_interface_ip($interface) {
  @exec('/usr/sbin/ipconfig getifaddr '. $interface, $retval);
 return $retval[0];
}// end get_network_iterface_ip
echo get_network_interface_ip($interface);

Using PHP to report an active network interface

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

I use this script, after I determine what available interfaces the computer has, to report which interfaces are active.

# input String ("en0")
# output "NULL or "Active" 
function check_interface_activity($interface){
  @exec('ifconfig| awk \'/flags=|media|inet / {if (substr($2, 1, 6) == "flags=") printf("\n%s ", $1);
  else if ($1 == "inet") printf("%s ", $2);
  else if ($1 == "media:") printf(substr($0, 9))}\' | awk \'$1 ~ /^'.$interface.'/\' | awk \'{print $7}\'',$retval);
 return $retval[0];
} // end check_interface
echo check_interface($interface);

Using PHP to gather all active network interfaces

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

I use this script to gather all Ethernet based network activity to determine which network port is active and then determine what the hardware address and IP address are.

# input NONE
# output String ("en0, en1")
function get_all_active_network_interfaces() {
  $inclusion = array(en0,en1);
  @exec('/sbin/ifconfig -lu', $retval);
  $interfaces = explode(" ",$retval[0]);
  foreach($interfaces as $interface){
     if (in_array($interface,$inclusion)){
       $ret.=$interface ." \n \r";
     } // end if
  }// end foreach
return $ret;
} // end get_all_active_network_interfaces
echo get_all_active_network_interfaces();

This script is similar to the above script, but doesn’t include the constraints of the inclusion list.

# input NONE
# output String ("en0, en1, etc") 
function get_all_active_network_interfaces() {
  @exec('/sbin/ifconfig -lu', $retval);
  $interfaces = explode(" ",$retval[0]);
    foreach($interfaces as $interface){
      $ret .= $interface ." \n \r";
    }// end foreach
return $ret;
} // end get_all_active_network_interfaces
echo get_all_active_network_interfaces();

How to use scp – Ubuntu

Posted by | Posted in Command-Line, Linux, Ubuntu | Posted on 17-07-2009

I am currently switching web servers at work from a physical server to a virtual server. The differences are for a different topic, but during this switch, I am going to be copying files from my old server to my new one. Normally you’d copy your files down to your local computer, lets say your laptop or desktop, before upload them to your new server. The not only takes a lot of time, but also eats up hard drive space. I am going to show you how to copy directly from one server to another.

I am going to use SSH’s SCP application. /usr/bin/scp – secure copy (remote file copy program).

For general discussion, I am already remotely logged into my old server via SSH and I have the files that I want to transfer up already TAR’d up. I’ll cover SSH and TAR in later posts. But for right now, I am just going to demonstrate how you should use SCP.

The usage format to transfer a file “up” to another system normally looks something like this.,

/usr/bin/scp /path/to/local.file user@server.address:/path/to/new/location.file

I am going to copy a mysql (database) file from my old server’s backup directory to my home directory on the new server

/usr/bin/scp /backups/mysql_database_dump.sql.tar's password: 
mysql_database_dump.sql.tar              100%  391MB   6.4MB/s   01:01

Now, if I wanted to copy a file from my new server to my old server, while still on my old server, I would reverse the sequence.

scp /path/to/new/location/'s password: 
mysql_database_dump.sql.tar              100%  391MB   6.4MB/s   01:01

Family Commands of scp are

/usr/bin/rcp - alias of scp - secure copy (remote file copy program)
/usr/bin/sftp - secure file transfer program
/usr/bin/ssh - OpenSSH SSH client (remote login program)
/usr/bin/ssh-add - adds RSA or DSA identities to the authentication agent
/usr/bin/ssh-agent - authentication agent
/usr/bin/ssh-keygen - authentication key generation, management and conversion
/etc/ssh/ssh_config - OpenSSH SSH client configuration files
/usr/sbin/sshd - OpenSSH SSH daemon

Join the mailing list

Check your email and confirm the subscription