Friday, August 24, 2012

How To Restore MySQL database

Restore a database

In this example, to restore the sugarcrm database, execute mysql with < as shown below. When you are restoring the dumpfilename.sql on a remote database, make sure to create the sugarcrm database before you can perform the restore.
# mysql -u root -ptmppassword

mysql> create database sugarcrm;
Query OK, 1 row affected (0.02 sec)

# mysql -u root -ptmppassword sugarcrm < /tmp/sugarcrm.sql

# mysql -u root -p[root_password] [database_name] < dumpfilename.sql

2. Backup a local database and restore to remote server using single command:

This is a sleek option, if you want to keep a read-only database on the remote-server, which is a copy of the master database on local-server. The example below will backup the sugarcrm database on the local-server and restore it as sugarcrm1 database on the remote-server. Please note that you should first create the sugarcrm1 database on the remote-server before executing the following command.

Backup and Restore MySQL Database Using mysqldump

Backup a single database:

This example takes a backup of sugarcrm database and dumps the output to sugarcrm.sql

# mysqldump -u root -ptmppassword sugarcrm > sugarcrm.sql

# mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
 
The sugarcrm.sql will contain drop table, create table and insert command for all the tables in the sugarcrm database. Following is a partial output of sugarcrm.sql, showing the dump information of accounts_contacts table:



Backup multiple databases:

If you want to backup multiple databases, first identify the databases that you want to backup using the show databases as shown below:
# mysql -u root -ptmppassword

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bugs               |
| mysql              |
| sugarcr            |
+--------------------+
4 rows in set (0.00 sec)
For example, if you want to take backup of both sugarcrm and bugs database, execute the mysqldump as shown below:


# mysqldump -u root -ptmppassword --databases bugs sugarcrm > bugs_sugarcrm.sql
Verify the bugs_sugarcrm.sql dumpfile contains both the database backup.

# grep -i "Current database:" /tmp/bugs_sugarcrm.sql
-- Current Database: `mysql`
-- Current Database: `sugarcrm`

3. Backup all the databases:

The following example takes a backup of  all the database of the MySQL instance.
# mysqldump -u root -ptmppassword --all-databases > /tmp/all-database.sql


Backup a specific table:

In this example, we backup only the accounts_contacts table from sugarcrm database.
# mysqldump -u root -ptmppassword sugarcrm accounts_contacts \
      > /tmp/sugarcrm_accounts_contacts.sql
 

4. Different mysqldump group options:

  • –opt is a group option, which is same as –add-drop-table, –add-locks, –create-options, –quick, –extended-insert, –lock-tables, –set-charset, and –disable-keys. opt is enabled by default, disable with –skip-opt.
  • –compact is a group option, which gives less verbose output (useful for debugging). Disables structure comments and header/footer constructs. Enables options –skip-add-drop-table –no-set-names –skip-disable-keys –skip-add-locks


Wednesday, August 15, 2012

Oracle Database Control

Oracle Database Control
When Oracle starts it goes through 3 stages which are
Startup (nomount) Oracle first reads the parameter file (pfile or spfile) and obtains all memory parameters in order to configure the SGA, memory allocatations take place and the necessary background processes are started. Remember the Memory allocation with the background processes is called the INSTANCE.
Mount At this stage Oracle opens and reads the controlfile and obtains the location of the datafiles but it does not open them.
Open The last stage is to open the datafiles (also called the DATABASE) for reading and writing, Oracle makes sure that the datafiles are consistent.
Startup
Nomount Mode startup nomount;

Note: The INSTANCE is born, memory allocated and background processes started but the controlfile has not been read and the datafiles are not open for reading or writing
Mount Mode startup mount;
Note: The controlfile has been read at this stage but the datafiles are not open for reading or writing.
Read Only Mode startup open read only
Note: the database files are open for reading only
Open Mode startup
startup open
Note: database files will be open for reading and writing, this is the normal mode of operation.
Force mode startup force;
Note: this performs a 'shutdown abort' followed by a 'startup'
Restricted Mode startup restrict;
# To change into restricted mode
alter system enable restricted session;
alter system disable restricted session;

Note: Users with restricted access can access the system only (normally DBA's)
Alter operation mode # You must be in mount mode to alter the database, if not then shutdown the database first
alter database open;
alter database open read only;
alter database restricted;
Display operation mode # Display instance state in nomount mode v$database is unavailable.
# MODES:
#   started (nomount mode)
#   mounted (mount mode)
#   open (open mode)

select status from v$instance;
# Obtain the database operation mode, the database must be in mounted or open mode to access v$database
# MODES:
#   mounted (mount mode)
#   read write (open mode)
#   read only (open mode)

select open_mode from v$database;
# Is restricted session enabled
select logins from v$instance;
Parameter File
As stated in the Oracle structure web page, Oracle can access two types of parameter file pfile (text based) or spfile (binary based), you can specify which parameter you want to start the database with
pfile startup pfile='c:\oracle\pfiles\initD10.ora'
spfile # There is no option to specify a spfile but you can use the pfile option and in the pfile # point to the spfile
Display the parameter file in use select name, value from v$parameter where name = 'spfile';
show parameter spfile;
For more information on Oracle's parameter file see Oracle Structure.
Shutdown
Oracle has a number of options to shutdown the database, there are four to choose from
Normal No new connections
Will wait until all users have disconnected
Note: you could be waiting a long time, not normally used
Transactional No new connections or transactions
Once all transactions have finished disconnect the client
Note: again you could be waiting a long time
Immediate No new connections
Uncommitted transactins will be rolled back then disconnect the client

Note: normal shutdown command used
Abort No new connections
Disconnect client immediately (no rolling back any transactions)
Note: last resort shutdown command, a automatic recovery will take place when you start the database
Shutdown Commands
Shutdown commands shutdown normal;
shutdown transactional;
shutdown immediate;
shutdown abort;

startup force;
Note: 'startup force' will run a 'shutdown abort' immediately followed by a 'startup'
Display if shutdown is in progress select shutdown_pending from v$instance;
Restricted and Quiesce Modes
When you have problems on an Oracle database sometimes you would like the database fully running but allow only privilege users access (normally DBA's), Oracle offers two modes
Restricted In this mode any users without the restricted privilege will not be allowed to connect to the database, however any users that are still connected will be allowed to use any DML/DDL.
Note: make sure all users are disconnected (need to kill their sessions) before changing the mode into restricted.
Quiesce In this mode all users transactions are allowed to complete, once completed the user is unable to perform any DML/DDL commands (their sessions appear to freeze) but remain connected to the database. Users will be allowed to login but the session will again appear to freeze.
DBA's can perform any DML/DDL as normal, their sessions are unaffected, this also applies to users with the restricted session privilege.
Useful Commands
Restricted alter system enable restricted session;
alter system disable restricted session;

select logins from v$instance;
Grant restricted privilege grant restricted session to <user>;
Quiesce alter system quiesce restricted;
alter system unquiesce;

select active_state from v$instance;
Basic Database Information
To obtain general database information the following can be used
Display Parameter file select name, value from v$parameter where name = 'spfile';
show parameter spfile;
Parameter settings select name, value, isdefault, isses_modifiable, issys_modifiable from v$parameter;
Instance state select status from v$instance;
Database operation mode select open_mode from v$database;
Shutdown pending select shutdown_pending from v$instance;
Restricted Session Mode select logins from v$instance;
Quiesce Mode select active_state from v$instance;
Oracle version select * from v$version;
select * from product_component_version;
Log Mode select log_mode from v$database;
archive log list;
License info select * from v$license;
Database options select * from v$option;
Memory Info show sga
select * from v$sga;
select * from v$sgainfo;
select * from v$ssgastat;
Display controlfiles select * from v$controlfile;
select * from v$controlfile_record_section;
Display Oracle processes select * from v$bgprocesses;
Log file locations # display background (alert.log destination), core and user destinations
show parameter dump_dest;