April 12

MySQL: Change root Password in MySQL

If you have never set a root password for MySQL server, the server does not require a password at all for connecting as root.
To setup root password for first time, use mysqladmin command at shell prompt as follows:
$ mysqladmin -u root password NEWPASSWORD

However, if you want to change (or update) a root password, then you need to use the following command:
$ mysqladmin -u root -p’oldpassword’ password newpass
For example, If the old password is abc, you can set the new password to 123456, enter:
$ mysqladmin -u root -p  password

Change MySQL password for other users

To change a normal user password you need to type (let us assume you would like to change password for user vivek) the following command:
$ mysqladmin -u vivek -p oldpassword password newpass
Changing MySQL root user password using MySQL sql command

This is another method. MySQL stores username and passwords in user table inside MySQL database. You can directly update password using the following method to update or change password for user vivek:

1) Login to mysql server, type the following command at shell prompt:
$ mysql -u root -p

2) Use mysql database (type command at mysql> prompt):

mysql> use mysql;

3) Change password for user vivek, enter:

mysql> update user set password=PASSWORD(“NEWPASSWORD”) where User=’vivek’;

4) Finally, reload the privileges:

mysql> flush privileges;
mysql> quit

The last method can be used with PHP, Python or Perl scripting mysql API.

By: Vivek Gite

Category: Databases | Comments Off on MySQL: Change root Password in MySQL
April 12

MYSQL: Mysql Backup Options

1. Backing Up With MySQLDump

Backing up a single DB:
mysqldump –user [user name] –password databasename > dumpfilename

Backing up an entire MySQL server:
mysqldump –user=[user name] –password -A > dumpfilename

Backing up individual tables:
mysqldump –user=[user name] –password –databases DB_NAME –tables TABLE_NAME > dumpfilename

2. How to Backup MySQL Database automatically (for Linux users)
Create a cron job:
15 2 * * * /usr/bin/mysqldump –opt –all-databases -u root -pPASSWORD > /foo/bar/db-`date +%Y-%m-%d`.sql

3. Backup Your Database into an XML File Using PHP

mysqldump —user [user name] —password=[password] [database name] > [dump file]

4. Dump a remote database locally
ssh user@server “/usr/bin/mysqldump -u user -p password database_name” | dd of=/where/you/want/the/dump.sql

5. How to – Using PHP To Backup MySQL Database

Execute a database backup query from PHP file. Below is an example of using SELECT INTO OUTFILE query for creating table backup:
view plaincopy to clipboardprint?

<?php
include ‘config.php’;
include ‘opendb.php’;

$tableName = ‘mypet’;
$backupFile = ‘backup/mypet.sql’;
$query = “SELECT * INTO OUTFILE ‘$backupFile’ FROM $tableName”;
$result = mysql_query($query);

include ‘closedb.php’;
?>

To restore the backup you just need to run LOAD DATA INFILE query like this :
view plaincopy to clipboardprint?

<?php
include ‘config.php’;
include ‘opendb.php’;

$tableName = ‘mypet’;
$backupFile = ‘mypet.sql’;
$query = “LOAD DATA INFILE ‘backupFile’ INTO TABLE $tableName”;
$result = mysql_query($query);

include ‘closedb.php’;
?>

6. How to e-mail yourself an automatic backup of your MySQL database table with PHP

This script will send an e-mail to you with an .sql file attached, thus enabling you to back up specific tables easily. You could even set up an e-mail account just to receive these backups… A file over 2MB may cause PHP issues

<?php
// Create the mysql backup file
// edit this section
$dbhost = “yourhost”; // usually localhost
$dbuser = “yourusername”;
$dbpass = “yourpassword”;
$dbname = “yourdb”;
$sendto = “Webmaster <[email protected]>”;
$sendfrom = “Automated Backup <[email protected]>”;
$sendsubject = “Daily Mysql Backup”;
$bodyofemail = “Here is the daily backup.”;
// don’t need to edit below this section

$backupfile = $dbname . date(“Y-m-d”) . ‘.sql’;
system(“mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile”);

// Mail the file

include(‘Mail.php’);
include(‘Mail/mime.php’);

$message = new Mail_mime();
$text = “$bodyofemail”;
$message->setTXTBody($text);
$message->AddAttachment($backupfile);
$body = $message->get();
$extraheaders = array(“From”=>”$sendfrom”, “Subject”=>”$sendsubject”);
$headers = $message->headers($extraheaders);
$mail = Mail::factory(“mail”);
$mail->send(“$sendto”, $headers, $body);

// Delete the file from your server
unlink($backupfile);
?>

7. Ubuntu Linux Backup MySQL server Shell Script

If you have a dedicated VPS server running Ubuntu Linux. Here is how to backup all your mysql server databases to your ftp server

Install lftp

lftp is a file transfer program that allows sophisticated ftp, http and other connections to other hosts. If site is specified then lftp will connect to that site otherwise a connection has to be established with the open command. To install lftp, enter:

sudo apt-get install lftp

Shell script to backup MySQL database server

Following is the shell script. It will dump all database to /backup/mysql and later it will upload to FTP server. You need to setup correct username and password before using the script:

#!/bin/bash
### MySQL Server Login Info ###
MUSER=”root”
MPASS=”MYSQL-ROOT-PASSWORD”
MHOST=”localhost”
MYSQL=”$(which mysql)”
MYSQLDUMP=”$(which mysqldump)”
BAK=”/backup/mysql”
GZIP=”$(which gzip)”
### FTP SERVER Login info ###
FTPU=”FTP-SERVER-USER-NAME”
FTPP=”FTP-SERVER-PASSWORD”
FTPS=”FTP-SERVER-IP-ADDRESS”
NOW=$(date +”%d-%m-%Y”)

### See comments below ###
### [ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/* ###
[ ! -d “$BAK” ] && mkdir -p “$BAK”

DBS=”$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse ‘show databases’)”
for db in $DBS
do
FILE=$BAK/$db.$NOW-$(date +”%T”).gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

lftp -u $FTPU,$FTPP -e “mkdir /mysql/$NOW;cd /mysql/$NOW; mput /backup/mysql/*; quit” $FTPS

Save script as /home/your-name/mysql.backup.sh file. Setup executable permission:
$ chmod +x /home/your-name/mysql.backup.sh
To backup MySQL, enter:
/home/your-name/mysql.backup.sh
OR
sudo /home/your-name/mysql.backup.sh
Run MySQL backup script as cron job

To automate procedure setup a cron job. For example run backup everyday at midnight (i.e once a day), enter:
$ sudo crontab -e
Append following cron job:
@midnight /home/you/mysql.backup.sh >/dev/null 2>&1

Category: Databases | Comments Off on MYSQL: Mysql Backup Options
April 10

MYSQL: Restore a database from a mysqldump

Go to the directory that your mysqldump is located and Login:
mysql -u root -p

Create the database container if it down not already exist:
mysql> create database mydb;

Switch to using the database:
mysql> use mydb;

Restore the file:
mysql> source db_backup.dump;

Category: Databases | Comments Off on MYSQL: Restore a database from a mysqldump
April 5

MSSQL: File extension definitions

.LDF is used for Log File

.TRN is used for backup of the transaction log.

.MDF is used for Data File

.BAK is used for backup of database (data and committed log)

By: Mak

Category: Databases | Comments Off on MSSQL: File extension definitions
October 28

MSSQL: List SQL all network SQL server from the command line

 

From a command prompt use the follow:

sqlcmd -L

To send to a file use: sqlcmd -L >sqllist.txt

The older commands are isql and osql
I also use the “Data Sources (ODBC)” connector from Administrator Tools and start the creation of a System DSN.  From the SQL Server drop down box you can see the ones advertising themselves.

Category: Databases | Comments Off on MSSQL: List SQL all network SQL server from the command line