Mysql

All posts in the Mysql category

To Calculate the Database Size in MYSQL

Published August 26, 2010 by Siva

MySQL command – sum up the data_length + index_length is equal to the total table size.
data_length – store the real data.
index_length – store the table index.

List out all the databases size

SELECT table_schema "Data Base Name", sum( data_length + index_length) / 1024 / 1024 
"Data Base Size in MB" FROM information_schema.TABLES GROUP BY table_schema ;

Thanks.
Advertisements

How to Back Up and Restore a MySQL Database

Published February 8, 2010 by Siva

If you’re storing anything in MySQL databases that you do not want to lose, it is very important to make regular backups of your data to protect it from loss. This tutorial will show you two easy ways to backup and restore the data in your MySQL database. You can also use this process to move your data to a new web server.

Back up From the Command Line (using mysqldump)

If you have shell or telnet access to your web server, you can backup your MySQL data by using the mysqldump command. This command connects to the MySQL server and creates an SQL dump file. The dump file contains the SQL statements necessary to re-create the database. Here is the proper syntax:

$ mysqldump –opt -u [uname] -p[pass] [dbname] > [backupfile.sql]
  • [uname] Your database username
  • [pass] The password for your database (note there is no space between -p and the password)
  • [dbname] The name of your database
  • [backupfile.sql] The filename for your database backup
  • [–opt] The mysqldump option

For example, to backup a database named ‘Tutorials’ with the username ‘root’ and with no password to a file tut_backup.sql, you should accomplish this command:

$ mysqldump -u root -p Tutorials > tut_backup.sql

This command will backup the ‘Tutorials’ database into a file called tut_backup.sql which will contain all the SQL statements needed to re-create the database.

With mysqldump command you can specify certain tables of your database you want to backup. For example, to back up only php_tutorials and asp_tutorials tables from the ‘Tutorials’ database accomplish the command below. Each table name has to be separated by space.

$ mysqldump -u root -p Tutorials php_tutorials asp_tutorials > tut_backup.sql

Sometimes it is necessary to back up more that one database at once. In this case you can use the –database option followed by the list of databases you would like to backup. Each database name has to be separated by space.

$ mysqldump -u root -p –databases Tutorials Articles Comments > content_backup.sql

If you want to back up all the databases in the server at one time you should use the –all-databases option. It tells MySQL to dump all the databases it has in storage.

$ mysqldump -u root -p –all-databases > alldb_backup.sql

The mysqldump command has also some other useful options:

–add-drop-table: Tells MySQL to add a DROP TABLE statement before each CREATE TABLE in the dump.

–no-data: Dumps only the database structure, not the contents.

–add-locks: Adds the LOCK TABLES and UNLOCK TABLES statements you can see in the dump file.

The mysqldump command has advantages and disadvantages. The advantages of using mysqldump are that it is simple to use and it takes care of table locking issues for you. The disadvantage is that the command locks tables. If the size of your tables is very big mysqldump can lock out users for a long period of time.

Back up your MySQL Database with Compress

If your mysql database is very big, you might want to compress the output of mysqldump. Just use the mysql backup command below and pipe the output to gzip, then you will get the output as gzip file.

$ mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

If you want to extract the .gz file, use the command below:

$ gunzip [backupfile.sql.gz]

Restoring your MySQL Database

Above we backup the Tutorials database into tut_backup.sql file. To re-create the Tutorials database you should follow two steps:

  • Create an appropriately named database on the target machine
  • Load the file using the mysql command:
$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]

Have a look how you can restore your tut_backup.sql file to the Tutorials database.

$ mysql -u root -p Tutorials < tut_backup.sql

To restore compressed backup files you can do the following:

gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]

If you need to restore a database that already exists, you’ll need to use mysqlimport command. The syntax for mysqlimport is as follows:

mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

Backing Up and Restoring using PHPMyAdmin

It is assumed that you have phpMyAdmin installed since a lot of web service providers use it. To backup your MySQL database using PHPMyAdmin just follow a couple of steps:

  • Open phpMyAdmin.
  • Select your database by clicking the database name in the list on the left of the screen.
  • Click the Export link. This should bring up a new screen that says View dump of database (or something similar).
  • In the Export area, click the Select All link to choose all of the tables in your database.
  • In the SQL options area, click the right options.
  • Click on the Save as file option and the corresponding compression option and then click the ‘Go’ button. A dialog box should appear prompting you to save the file locally.

Restoring your database is easy as well as backing it up. Make the following:

  • Open phpMyAdmin.
  • Create an appropriately named database and select it by clicking the database name in the list on the left of the screen. If you would like to rewrite the backup over an existing database then click on the database name, select all the check boxes next to the table names and select Drop to delete all existing tables in the database.
  • Click the SQL link. This should bring up a new screen where you can either type in SQL commands, or upload your SQL file.
  • Use the browse button to find the database file.
  • Click Go button. This will upload the backup, execute the SQL commands and re-create your database.

Courtesy : http://www.webcheatsheet.com/SQL/mysql_backup_restore.php

Install php5 and php4 in cPanel server

Published February 1, 2010 by Siva

Install Apache(apache2.2.2)

1. Download apache2.2.2(httpd-2.2.2.tar.gz) store in /usr/local/src
2. Type the following to un-tar the file into a directory called apache_[version]: tar -xvf apache_[version].tar
3.  cd into /usr/local/apache_[version] (or wherever you un-tared it)
4. Type the following to prepare for building, replacing [path] with your own path, such as /usr/local/usr/local/apache_new
5. ./configure –prefix=[path] –enable-module=so
6. make
7. make install
8. Check if you have mod_so enabled:
* cd to the Apache bin directory (/usr/local/apache_new/bin/ or wherever you installed Apache originally)
* Type ./httpd -l
9. Open httpd.conf(/usr/local/apache_new/conf/) and find a line starting with ServerAdmin.
ServerAdmin admin@example.com
10. Find a line starting with ServerName:
ServerName server1.example.com:80
11. Save the file.
12. cd up a directory (type cd ..)
13. Start Apache using the following command:
# ./bin/apachectl start
14. Call the IP(192.168.1.5) or domain name(server1.example.com)from browser You will get a page “IT WORKS”

Install Mysql 4.1

Add a login user and group for mysqld to run:
# groupadd mysql
# useradd -g mysql mysql
# tar zxvf mysqlversion.tar.gz
# cd mysqlversion
# ./configure–prefix=/usr/local/mysql
# make
# make install
# cp support-files/my-medium.cnf /etc/my.cnf
# cd /usr/local/mysql

If you haven’t installed  mysql before, you must create the mysql grant tables:
#bin/mysql_install_db –user=mysql
(If you run the command as root, you should use the –user option. The value of the option should be the name of the login account that you have created in the first step to use for runnning the server)

#cd /usr/local/mysql
#chown -R root .
#chown -R mysql var
#chgrp -R mysql .

Install PHP(PHP4.4.2)

Unpack a source archive
# tar -zxvf php-4.3.3.tar.gz

Configure PHP as Apache module with MySQL support
#./configure –with-apxs2=/usr/local/apache/bin/apxs –with-mysql=/usr/local/mysql

Compile the sources
# make

Install the compiled Apache module
# make install

Copy php.ini-dist to /usr/local/lib/php.ini
# cp ./php.ini-dist /usr/local/lib/php.ini

Integrate PHP with Apache

Open httpd.conf(/usr/local/apache_new/conf) open in your text editor

AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps

Save the file, go up a directory (cd ..), and stop and restart Apache by typing:

./bin/apachectl stop Followed by ./bin/apachectl start

Find the document root from httpd.conf(/usr/local/apache_new/conf) file
DocumentRoot “/usr/local/apache_new/htdocs”

Configuring Websites

Edit /usr/local/apache2/conf/httpd.conf and make sure it has the
correct document root settings :: /var/www/
(change the defautlt documetroot in config file (#DocumentRoot “/usr/local/apachenew/htdocs”) to /var/www/ and also change the the ‘directory’ directive(#<Directory “/usr/local/apachenew/htdocs”) to /var/www/(search for the DocumentRoot and change))

Un-comment the following options …
-> Include conf/extra/httpd-vhosts.conf
-> Include conf/extra/httpd-default.conf

Then edit the v-hosts file(conf/extra/httpd-vhosts.conf) and setup a couple of dummy sites just to make sure everything is working as it should(Comment out all the default VirtualHost entries inside this file)

NameVirtualHost ServerIP:80

<VirtualHost ServerIP:80>
DocumentRoot /var/www/test1/
ServerName test1.com
</VirtualHost>

<VirtualHost ServerIP:80>
DocumentRoot /var/www/test2/
ServerName test2.com
</VirtualHost>

Create the two directories in /var/www/ and place a index.html file in  each, with some domain specific text in, so we can differentiate between the two when testing.

Launch Apache
# /usr/local/apache2/bin/apachectl start

Check the two sites

Dump just the table structure to a file in MySQL

Published January 13, 2010 by Siva

We will use the mysqldump utility the same as if we were backing upo the entire database

Syntax:

mysqldump -d -h localhost -u root -p databasename > dumpfile.sql

The only option that is different than creating an entire backup is the -d switch, which tells mysqldump not to output the data.

Example:

mysqldump -d -h localhost -u root -p sampledatabase  > dumpfile.sql

Changing mysql data directory to protect db corruption

Published July 16, 2009 by Siva

MySQL data are located under /var/lib/mysql by default, which could cause a problem to corrupt huge database.

Here the steps to change the data directory

Step1 :

To stop mysql deamon, simply drop the command line

/etc/init.d/mysql stop

Step 2:

to open the my.cnf file

#whereis my.cnf

#vim /etc/mysql/my.cnf (this is my server location…it may change…)

datadir=/home/mysql/ #was /var/lib/mysql/

Step 3:

Create a /home/mysql directory and give clearance to mysql

mkdir /home/mysql
chown -R mysql /home/mysql
chgrp -R mysql /home/mysql

Copy data and preserve permissions, etc.

cp -Rp /var/lib/mysql/* /home/mysql/

Step 4

Start the mysql service

/etc/init.d/mysql start

Regards
Siva

To finding the hacked website (Gumblar/Martuz)..

Published June 1, 2009 by Siva

To finding the website is hacked by someone or badware scripts running on the server. You can find here…

http://unmaskparasites.com/

This site should be healthy report…. If the report shows badware running..

You need to clean the site on the server & restore the old backup..

Then send review to google web tools &b google will unblock from google blacklist.

Email me for further doubts..

Thanks…