Thursday, April 04, 2013

MySQL Database User Permissions

MYSQL Server uses an internal database called mysql to store database user information. We can play around with several information tables liket the followings to get idea about user permissions and related information.


select * from mysql.user;
select * from mysql.db;
select * from mysql.tables_priv;
select * from mysql.columns_priv;
select * from mysql.procs_priv;

Followings are some formatted reports based upon these basic tables.
List of DATABASE Privileges:
SELECT md.host `Host`,
       md.user `User`,
       md.db `Database`,
       REPLACE(
          RTRIM(
             CONCAT(
                IF(md.Select_priv = 'Y', 'Select ', ''),
                IF(md.Insert_priv = 'Y', 'Insert ', ''),
                IF(md.Update_priv = 'Y', 'Update ', ''),
                IF(md.Delete_priv = 'Y', 'Delete ', ''),
                IF(md.Create_priv = 'Y', 'Create ', ''),
                IF(md.Drop_priv = 'Y', 'Drop ', ''),
                IF(md.Grant_priv = 'Y', 'Grant ', ''),
                IF(md.References_priv = 'Y', 'References ', ''),
                IF(md.Index_priv = 'Y', 'Index ', ''),
                IF(md.Alter_priv = 'Y', 'Alter ', ''),
                IF(md.Create_tmp_table_priv = 'Y', 'Create_tmp_table ', ''),
                IF(md.Lock_tables_priv = 'Y', 'Lock_tables ', ''),
                IF(md.Create_view_priv = 'Y', 'Create_view ', ''),
                IF(md.Show_view_priv = 'Y', 'Show_view ', ''),
                IF(md.Create_routine_priv = 'Y', 'Create_routine ', ''),
                IF(md.Alter_routine_priv = 'Y', 'Alter_routine ', ''),
                IF(md.Execute_priv = 'Y', 'Execute ', ''),
                IF(md.Event_priv = 'Y', 'Event ', ''),
                IF(md.Trigger_priv = 'Y', 'Trigger ', ''))),
          ' ',
          ', ')
          AS `Privileges`
  FROM mysql.db md
ORDER BY md.Host, md.User, md.Db
 
LIST OF GLOBAL PRIVILEGES:
 
SELECT mu.host `Host`,
       mu.user `User`,
       REPLACE(
          RTRIM(
             CONCAT(
                IF(mu.Select_priv = 'Y', 'Select ', ''),
                IF(mu.Insert_priv = 'Y', 'Insert ', ''),
                IF(mu.Update_priv = 'Y', 'Update ', ''),
                IF(mu.Delete_priv = 'Y', 'Delete ', ''),
                IF(mu.Create_priv = 'Y', 'Create ', ''),
                IF(mu.Drop_priv = 'Y', 'Drop ', ''),
                IF(mu.Reload_priv = 'Y', 'Reload ', ''),
                IF(mu.Shutdown_priv = 'Y', 'Shutdown ', ''),
                IF(mu.Process_priv = 'Y', 'Process ', ''),
                IF(mu.File_priv = 'Y', 'File ', ''),
                IF(mu.Grant_priv = 'Y', 'Grant ', ''),
                IF(mu.References_priv = 'Y', 'References ', ''),
                IF(mu.Index_priv = 'Y', 'Index ', ''),
                IF(mu.Alter_priv = 'Y', 'Alter ', ''),
                IF(mu.Show_db_priv = 'Y', 'Show_db ', ''),
                IF(mu.Super_priv = 'Y', 'Super ', ''),
                IF(mu.Create_tmp_table_priv = 'Y', 'Create_tmp_table ', ''),
                IF(mu.Lock_tables_priv = 'Y', 'Lock_tables ', ''),
                IF(mu.Execute_priv = 'Y', 'Execute ', ''),
                IF(mu.Repl_slave_priv = 'Y', 'Repl_slave ', ''),
                IF(mu.Repl_client_priv = 'Y', 'Repl_client ', ''),
                IF(mu.Create_view_priv = 'Y', 'Create_view ', ''),
                IF(mu.Show_view_priv = 'Y', 'Show_view ', ''),
                IF(mu.Create_routine_priv = 'Y', 'Create_routine ', ''),
                IF(mu.Alter_routine_priv = 'Y', 'Alter_routine ', ''),
                IF(mu.Create_user_priv = 'Y', 'Create_user ', ''),
                IF(mu.Event_priv = 'Y', 'Event ', ''),
                IF(mu.Trigger_priv = 'Y', 'Trigger ', ''))),
          ' ',
          ', ')
          AS `Privileges`
  FROM mysql.user mu
ORDER BY mu.Host, mu.User
 
List of privileges for TABLES:
 
SELECT mt.host `Host`,
       mt.user `User`,
       CONCAT(mt.Db, '.', mt.Table_name) `Tables`,
       REPLACE(mt.Table_priv, ',', ', ') AS `Privileges`
  FROM mysql.tables_priv mt
 WHERE mt.Table_name IN
          (SELECT DISTINCT t.table_name `tables`
             FROM information_schema.tables AS t
            WHERE    t.table_type IN
                        ('BASE TABLE', 'SYSTEM VIEW', 'TEMPORARY', '')
                  OR t.table_type <> 'VIEW' AND t.create_options IS NOT NULL)
ORDER BY mt.Host,
         mt.User,
         mt.Db,
         mt.Table_name;
 
List of privileges for VIEWS:

SELECT mv.host `Host`,
       mv.user `User`,
       CONCAT(mv.Db, '.', mv.Table_name) `Views`,
       REPLACE(mv.Table_priv, ',', ', ') AS `Privileges`
  FROM mysql.tables_priv mv
 WHERE mv.Table_name IN (SELECT DISTINCT v.table_name `views`
                           FROM information_schema.views AS v)
ORDER BY mv.Host,
         mv.User,
         mv.Db,
         mv.Table_name;
 
List of privileges for TABLE COLUMNS:
 
SELECT mtc.host `Host`,
       mtc.user `User`,
       CONCAT(mtc.Db,
              '.',
              mtc.Table_name,
              '.',
              mtc.Column_name)
          `Tables Columns`,
       REPLACE(mtc.Column_priv, ',', ', ') AS `Privileges`
  FROM mysql.columns_priv mtc
 WHERE mtc.Table_name IN
          (SELECT DISTINCT t.table_name `tables`
             FROM information_schema.tables AS t
            WHERE    t.table_type IN
                        ('BASE TABLE', 'SYSTEM VIEW', 'TEMPORARY', '')
                  OR t.table_type <> 'VIEW' AND t.create_options IS NOT NULL)
ORDER BY mtc.Host,
         mtc.User,
         mtc.Db,
         mtc.Table_name,
         mtc.Column_name;
 
List of privileges for VIEW COLUMNS:
 
SELECT mvc.host `Host`,
       mvc.user `User`,
       CONCAT(mvc.Db,
              '.',
              mvc.Table_name,
              '.',
              mvc.Column_name)
          `Views Columns`,
       REPLACE(mvc.Column_priv, ',', ', ') AS `Privileges`
  FROM mysql.columns_priv mvc
 WHERE mvc.Table_name IN (SELECT DISTINCT v.table_name `views`
                            FROM information_schema.views AS v)
ORDER BY mvc.Host,
         mvc.User,
         mvc.Db,
         mvc.Table_name,
         mvc.Column_name;
 
List of privileges for PROCEDURES:

SELECT mp.host `Host`,
       mp.user `User`,
       CONCAT(mp.Db, '.', mp.Routine_name) `Procedures`,
       REPLACE(mp.Proc_priv, ',', ', ') AS `Privileges`
  FROM mysql.procs_priv mp
 WHERE mp.Routine_type = 'PROCEDURE'
ORDER BY mp.Host,
         mp.User,
         mp.Db,
         mp.Routine_name;
 
List of privileges for FUNCTIONS:
 
SELECT mf.host `Host`,
       mf.user `User`,
       CONCAT(mf.Db, '.', mf.Routine_name) `Procedures`,
       REPLACE(mf.Proc_priv, ',', ', ') AS `Privileges`
  FROM mysql.procs_priv mf
 WHERE mf.Routine_type = 'FUNCTION'
ORDER BY mf.Host,
         mf.User,
         mf.Db,
         mf.Routine_name;
 
 

Wednesday, April 03, 2013

Oracle BI Discoverer 11g Installation


We are going to install OBI 11g with following sequences.
  • Install Oracle database 11.2.0.3 standard edition database.
  • Install JDK jdk 1.6 or 1.7
  • Install Repository Creation Utility (RCU) 11.1.0.6 (ofm_rcu_win_11.1.1.6.0_disk1_1of1.zip)
  • Weblogic Server 10.3.6 (wls1036_generic.jar)
  • Oracle Portal, Reports,Forms & Discoverer 11g and patching with 11.1.6
  • Run configuration for Discoverer
pfrd 11.1.1.2 Includes

V18772-01_1of4.zip
V18772-01_2of4.zip
V18772-01_3of4.zip
V18772-01_4of4.zip

pfrd 11.1.1.6 Includes
V29883-01.zip

./config.sh to configure 11.1.1.6

Environments:
Make sure the /etc/hosts are like this
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
#::1 localhost6.localdomain6 localhost6
192.168.10.11 biserver.domain.com biserver

Packages:
With an Oracle Database 11g installed, you need only to install one package - compat-db


Install OBI Repository Creation Utility (RCU)

Download the installation files from the http://www.oracle.com/technetwork/middleware/data-integrator/downloads/index.html on your desktop.

(Note** It has to be the same version as BI software). The name of the installer would be fm_rcu_win_11.1.1.6.0_disk1_1of1.zip

Unzip the file, navigate to the bin directory and start the RCU => rcu.bat.

Welcome > Next


Accept default of Create. Click Next:



·         Leave default of Oracle Database:
·         Set hostname
·         Set port
·         Set service name
·         Set username
·         Set password
·         Leave Role as SYSDBA 




Select the Oracle Business Intelligence Component.This action automatically selects the Metadata Services (MDS) schema (under the AS Common Schemas group).





So at this point, Repository Creation succeded!!

Our next step is to Install Weblogic server & creating middleware home


Install Oracle WebLogic Server and Create the Middleware Home:

Environment Preperation:

We need to make sure that weblogic is using jdk1.7.0_17 while running installer.

SETTING JAVA HOME:
We have installed JDK1.7 into following directory:

Cd /usr/java/jdk1.7.0_17/bin

[oracle@test bin]$ ./java -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

[oracle@test bin]$ cd..
[oracle@test jdk1.7.0_17]$ echo $PATH
/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/fnadvi/bin

[oracle@test jdk1.7.0_17]$ pwd
/usr/java/jdk1.7.0_17

[oracle@test jdk1.7.0_17]$ export JAVA_HOME=$PWD
[oracle@test jdk1.7.0_17]$  echo $JAVA_HOME
/usr/java/jdk1.7.0_17

[oracle@test jdk1.7.0_17]$ export PATH=$JAVA_HOME/bin:$PATH

[oracle@test jdk1.7.0_17]$ echo $PATH
/usr/java/jdk1.7.0_17/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/fnadvi/bin

[oracle@test jdk1.7.0_17]$ cd ../
[oracle@test java]$ java -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)




Install weblogic 10.3.6
Make sure that the weblogic is running using jdk1.7.0 java binaries by running it from java directory, as shown below:

[oracle@test distr]$ /usr/java/jdk1.7.0_17/bin/java -jar wls1036_generic.jar
Extracting 0%....................................................................................................100%




Now that the Weblogic server is installed. Our final step is installing Oracle Discoverer 11g.


Installing Forms,Reports & Discoverer 11.1

1.  We will be installing Oracle Portal, Reports, Forms & Discoverer 11.1.1.2 and then will pathc it with 11.1.1.6



NOTE: Select Install Software - DO NOT CONFIGURE****
We'll do that after applying patches.






Patching to 11.1.1.6:






Configure Discoverer under 11.1.1.6 patched home:

Once everything is installed and patched. Run configure Portal,Forms,Reports & Discoverer.
Configure CONFIG.sh 


cd /u01/app/oracle/middleware/as_1/bin
./config.sh





At this point.....Installation of Oracle Discoverer 11g has been successfully installed.


Just one additional task is, you need to copy the database tns file, (where EUL Schema resides) needs to be copied over to Middleware Instance Directory. Ie, for my case it's

/u01/app/oracle/middleware/asinst_1/config

The link to use Discover would be prety much like this:http://hostname:8888/discoverer/viewer OR 
http://hostname:8090/discoverer/viewer


Note that, even if webcache is down, you can still access Discoverer with the 8888 port!!!

Wednesday, March 27, 2013

Move SQL Server transaction log files to a different location via TSQL


To optimize I/O performance of a database, it's always a good idea to keep LOG file & DATA file in separate physical drives. 

The transaction log file records every data change and DML transaction executed in the database.  Writing to the transaction log file is sequential in nature as compared to the database files which are typically random I/O.  As such, placing the log file on separate physical disk from database will allow the disk to work in sequential manner and perform optimally.  

I'm going to show a demonstration of moving LOG files to another drive.


1: Capture database and transaction log file information

USE AdventureWorks
GO
sp_helpfile
GO

2: Set database to single user mode and detach database

Use MASTER
GO
-- Set database to single user mode
ALTER DATABASE adventureWorks
SET SINGLE_USER
GO
-- Detach the database
sp_detach_db 'AdventureWorks'
GO

*** Now the database is detached.  Once the detach process is completed, then you can copy and paste the new transaction log file then delete the old transaction log file via Windows Explorer.  Once this is completed, we can attach the database with SQL Server database log file at new location with the following script:

3: Attach database with log file at new location

USE master
GO
-- Now Attach the database
sp_attach_DB 'AdventureWorks',
'D:\Program Files\Microsoft SQL Server\MSSQL\Data\AdventureWorks_Data.mdf',
'E:\Move LogFile here through T-SQL\AdventureWorks_Log.ldf'
GO

4. Validate the LOG moving

After the final attach command  transaction log file has been moved to new location and database is operational with log file on new location.  Verifying the new database transaction log location can be accomplished by re-running 

USE AdventureWorks
GO
sp_helpfile
GO

Thursday, March 14, 2013

Bringing a database out of Restoring state in SQL Server



What if I restored a database with “NO RECOVERY” option keeping in mind that I’d keep continuing with another Differential backup set / Transactional backup set next. But then I’ve decided not to proceed further?

At this moment, database is in “Restoring” state and inaccesible.

I need to run following script bringing a database back from restoring state to normal.


RESTORE DATABASE
WITH RECOVERY

MSSQL Server : The log or differential backup cannot be restored because no files are ready to rollforward

I was facing following error while restoring Differential backup.

Restore failed for Server ''. (Microsoft.SqlServer.Smo) Additional Information: System.Data.SqlClient.SqlError: The log or differential backup cannot be restored because no files are ready to rollforward. (Microsoft.SqlServer.Smo) 

The reason for this erro is: no database that was left in non-operational mode, and thus has not been cleaned up such that uncommitted transactions have not been rolled back. 

The easy way to reproduce this error is to 

-Backup the database using full recover mode 
- Do full and differential backups. 
- Restore the DB, First from Full backup then Differential

I got the above error while trying to restore the differential backup (after you just restored the full backup). 

Reason for the error:

I restored the Full backup with Recovery option. It must be restored with NORECOVERY option to allow rest of the backup sets to be restored (Differential/Transactional)

In the Microsoft SQL Server Management Studio there are three options on the Option "page" while restoring a database. 

Option 1 (the default): Leave the database ready to use by rolling back uncommitted transactions. Additional transaction logs cannot be restored.(RESTORE WITH RECOVERY) 

Option 2: Leave the database non-operational, and do not roll back uncommitted transactions. Additional transaction logs can be restored.(RESTORE WITH NORECOVERY) 

To properly restore a database that is backup up using full recovery mode with full and differential backups, here are steps:

Restore Full Backup:

  1. Open the Restore Database window in Microsoft SQL Server Management Studio
  2. Ensure the To database field is filled in with the name you want.
  3. Choose From device as the Source for restore.
  4. Choose the full backup file you want to restore. In most cases this is the most recent full backup file.
  5. Click the Options page on the left navigation.
  6. Choose Leave the database non-operational, and do not roll back uncommitted transactions. Additional transaction logs can be restored.(RESTORE WITH NORECOVERY). 
Restore Differential backup:
  1. Open the Restore Database window in Microsoft SQL Server Management Studio
  2. Ensure the To database field is filled in with the name you want. The same that you specified in step 2 for the Restore Full backup
  3. Choose From device as the Source for restore.
  4. Choose the differential backup file you want to restore. In most cases this is the most recent differential backup file.
  5. Click the Options page on the left navigation.
  6. Choose the default: Leave the database ready to use by rolling back uncommitted transactions. Additional transaction logs cannot be restored.(RESTORE WITH RECOVERY) 

If you want to restore Transactional log after Differential backup restore, then Choose "RESTORE WITH NORECOVERY" instead of "RESTORE WITH RECOVERY".

RESTORE WITH RECOVERY Needs to be the final step of a recovery process.

MSSQL Server: The tail of the log for the database "DATABASE" has not been backed up.

I was facing the following error error when attempting to restore a database in Microsoft SQL Server:

System.Data.SqlClient.SqlError: The tail of the log for the database "DATABASE" has not been backed up. Use BACKUP LOG WITH NORECOVERY to backup the log if it contains work you do not want to lose. Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log.


Solution:

Select "Overwrite the existing database" from Restore option.



This will overwrite the tail of of database log , as my DB is corrupted and wanted to restore from last full backup so it's fine for my case.