Friday, October 31, 2014

[转] SQL Server 2008 : 基于策略的管理(Policy-Based Management)

SQL Server 2008 : 基于策略的管理(Policy-Based Management)

在SQL Server 2008中提出了一套新的管理机制,就是所谓的基于策略的管理(姑且可以称为PBM)。它的主要优势如下
  • 按需管理:PBM提供了系统配置的逻辑视图,因此DBA们可以预先定义各自所需要的数据服务配置,而不用等到这些需要实际发生的时候再去配置。
  • 智能监控:PBM可以持续监控系统的配置变化,并阻止那些违反了策略的配置变化操作。
  • 虚拟管理:通过PBM,DBA们可以对多台服务器进行规模化管理,在企业内部统一实施某些强制性配置会变得更加方便。
简单地说,基于策略管理的两个方面是:评估和监视。
  • SQL Server 2008的PBM支持4种执行模式,这4种模式决定了策略对目标的影响程度。这四种模式分别是:
    • 按需(On Demand):这种模式下的策略可以有管理员自由的选择是否应用,例如管理员可以手动调用这些策略来检查目标的依从性,或者通过DDL Trigger来订阅这些策略。
    • 更新时阻止(On Change - Prevent):这是最严格的一种,SQL Server 2008通过DDL Trigger的方式在订阅该策略的目标上发生操作时实施检查操作对策略的符合性,如果违反策略则回滚该操作,以达到强制策略的效果。
    • 更新时记录(On Change - Log Only):SQL Server 2008通过Event Notification的机制在在订阅该策略的目标上发生操作时实施检查操作对策略的符合性,如果违反策略则发送消息,就将该违反操作通过Service Broker的队列发送进行记录。
    • 按计划(On Schedule):通过SQL Agent的作业来调用策略对目标对象进行检查。
基于策略管理的术语
  • 对象(Facets):包括策略管理中某个方面的相关配置属性。例如在Surface Area中包括了像Database Mail Enabled以及CLR Integration Enabled之类的SQL Server功能的属性。
  • 条件(Conditions):表示一个方面的状态。条件是基于单个方面的,并且可以被一个或多个策略使用。例如,DBA可以建立一个名为Minimal Surface Area的条件,在这个条件中将Surface Area Facet中的所有属性都设置为False。
  • 策略(Policies):包括了用于约束单个或多个目标的条件。例如DBA可以创建一个名为Locked Down Server的策略,在这一策略中将Minimal Surface Area条件指派给某台服务器。
  • 类别(Categories):包含一条或多条策略。数据库拥有者可以将一个或多个分类绑定到数据库上。例如,DBA可以创建一个名为Corporate DB Policies的分类,其中包含一条强制数据库对象命名规则的策略和一条强制数据库兼容级别的策略,并将该分类绑定到业务数据库上。通常所有数据库都绑定到默认分类,但是可以在服务器或数据库级别上将分类设置为激活(Active)或暂停(Inactive),这样管理员就可以灵活控制策略的强制性。
  • 目标(Targets):目标代表像服务器、数据库、登录、表以及其他数据库对象各种被指派策略的实体。在一个SQL Server实例中的所有目标组成了一个目标层级。对于某个策略,DBA可以通过对目标层级进行筛选来定义一个目标集合。例如,DBA可以定义一个包含Production架构拥有的所有索引的目标集合。
为策略检查配置警报
如果某项策略被违反,SQL Server 2008会生成相应的警报,因此可以通过在SQL Agent中配置警报来监控这些事件

执行模式
事件号
On Change - Prevent
(if automatic)
34050
On Change - Prevent
(if On Demand)
34051
On schedule
34052
On change
34053

策略管理的安全性

属于PolicyAdministratorRole的成员才可以制定和修改策略定义,这个角色的成员是必须要小心控制的,因为恶意用户可以通过制定苛刻的策略来达到类似于拒绝服务攻击的效果。
image
【备注】这个角色是在MSDB里面

基于策略管理的常规配置步骤为:

  1. 根据Facet创建Condition,Condition可以作为Policy的检查条件,也可以是用于确定策略应用范围的筛选条件。
  2. 引用已经创建好的策略来定义策略,同时可以在策略检查条件可以应用的对象集上附加筛选条件,例如一个检查Multiple Part Name对象状态的策略就可以有表、存储过程、视图、同义词等一系列的对象可以进行选择并附加筛选条件,默认的筛选条件是Every,也就是这个对象集中所有的对象。当然不是所有条件都可以充当筛选条件的,在二月CTP的测试中就发现包含LIKE运算符的表达式的条件就不能充当筛选条件。
  3. 如果需要可以在Server Restriction中定义需要应用策略的SQL实例条件。
  4. 如果需要还可以在Policy Management节点上定义策略组,并在策略的定义中将策略归入某个类别,然后由服务器管理员或数据库拥有者订阅某个策略组。不过要注意看看我的前一个帖子,默认策略组都是强制订阅的,要启用自选订阅要在策略组管理中设置。
  5. 然后就等着策略帮你自动管理SQL Server了,这就看策略定义时选择的执行模式了。

我们下面用一个例子来演示一下如何使用策略进行管理工作。
我们的需求是:管理员需要确保服务器上所有数据库中的存储过程都是加密过的

1. 创建条件(Condition)。
image
点击“新建条件”
image
输入名称,在“方面”里面选择“存储过程”
image
在表达式中,“字段”中选择“@IsEncrypted”,“值”设置为True(默认为True)
作为演示,其他的内容我们就不设置了。直接点击“确定”关闭“创建新条件”对话框。我们可以在对象资源管理器中看到新增加的一个条件。如下图所示
image

2. 创建一个策略(Policy)
接下来,我们基于这个条件创建文明的策略。
image
点击“新建策略”
image
输入一个名称,并且在“检查条件”中选择刚才创建号的“存储过程是否加密”这个条件。默认情况下,会有另外一些设置。如下图
image
作为演示的第一个步骤,我们先不对其他选项做任何设置。直接点击“确定”退出该对话框
image
3. 启用该策略
策略创建好之后,默认是没有启用的。我们必须通过菜单进行启用
image
很不幸,我们点击“启用”之后,会收到下面的一个错误消息
 image
这个消息提示说:按需模式不支持“启用”。也就是说,如果按需评估,则不需要启用。
其实,按需的意思就是手工评估。

4. 使用该策略对数据库进行按需评估
现在我们有了这么一个策略,我们怎么样验证某个数据库中的存储过程是否满足该策略的要求呢?
image
选择刚才创建好的策略,然后右键菜单中点击“评估”
image
这个工具就会检测当前服务器上面所有数据库中的所有存储过程是否已加密。在上面的对话框中,还可以点击“导出结果”得到一个xml文件,可以作为其他用途,例如编写专门的报告
image
与此同时,我们还可以直接针对某个特定的存储过程进行评估
image
image
选中策略后,点击“评估”
image
评估的结果仍然是没有通过,因为我们的存储过程没有加密。

5. 使用策略进行主动监控。
与评估不同,监控则有主动性。例如,如果我们在服务器上启用了该策略,那么任何存储过程在被增加或者修改的时候,我们就可以主动干预,确保它必须加密。
我们再来看一下评估模式
image
这四种模式中,其实分为两大类
“按需”和“按计划”这两种很类似,都是作为评估用的。他们的评估结果大多数通过“查看历史记录”的方式进行查看的。他们的区别只是在于“按计划”是一个排好的日程,定期自动做的,而“按需”是全靠手工地做
image
image
而“更改时:仅记录”和“更改时:禁止”则带有主动性。
两者的区别在于前者仅仅做记录,而后者可以明确地阻止用户的操作。

6. 使用“更改时:禁止”这种评估模式进行工作。
我们要实现的需求就是,如果用户新建存储过程,不进行加密,就不允许保存。
image
我们首先将策略的评估模式更改为“更改时:禁止”,并且选中“已启用”的复选框。然后点击“确定”关闭该对话框
接下来,我们尝试去创建一个很简单的存储过程
image
这个语句太平常不过了。也没有任何问题。然后我们点击F5,执行代码
image
我们收到了这么一个消息,说是因为不匹配策略,所以被回滚了。有点神奇对吧
那么,我们怎么样才能成功创建该存储过程呢?我们需要做的是,修改存储过程的代码,指定为其加密
image
这样的话,该存储过程就可以被创建了。如下
image
需要注意观察一下它的图标,右下角带有一个小锁,这就表示该存储过程被加密了。加密的意思是不允许查看源代码。(不管你用哪种方式)

7. 关于策略的其他好处
上面我们完整地演示了如何使用策略进行评估或者监视的过程。那么,是不是说每一台服务器都需要我们这样一步一步地创建条件,创建策略呢?
显然不是的,基于策略的管理机制的好处就在于策略本身可以复用,实现一次定义,多次使用。
第一步,我们可以把策略导出
image
导出的是一个XML文件。里面包含了有关条件和策略的定义
image

为了做测验,我把刚才创建好的条件和策略都删除掉。下面来看看,如果通过导入策略的方式来部署。
image
image
点击“确定”之后,我们很高兴地看到条件和策略都被重新创建了
image
我们选中“存储过程必须经过加密”这个策略,右键菜单中选择“评估”,它可以正常工作,这证明我们导入是成功的。
image


Saturday, October 25, 2014

Browsers (IE, Chrome, Firefox) can not open any website, but PING command shows connection is good.

Problem: 
After updated Windows 8 to 8.1, all the browsers (IE11, Chrome, Firefox) can not open any website, and showed the same result like "Page can not be displayed". 

However, PING command showed the internet connection was OK. Even Windows Store worked just good and Avast was able to update.

Also, IE showed "Unable to open the search page" message box after following a few steps.


Solutions that did NOT work:
  • Reseted browser settings.
  • Checked the proxy settings.
  • Manually set fixed IP address and DNS instead of DHCP.
  • Virus scan didn't find anything. (Avast Free edition)
  • All anti-virus software (Avast) was uninstalled. 
  • Windows firewall was turned off. 
  • UAC was turned off.
  • Windows diagnose tools couldn't find any problem corruptedcorrupted. 
  • Router settings were good. (I could PING google.com through them.)
  • Used some anti-Trojan and system-fix tools. (**安装了百度卫士和金山急救箱等,然后进行修复和扫描,无效。)
  • Restarted computer (n times)...

Cause:
TCP/IP protocol or Winsock settings are corrupted.


Solution: 
Go to command line (Admin) and reset TCP/IP stack and WINSOCK.
  • Reset WINSOCK entries to installation defaults:
    C:\>netsh winsock reset catalog 
  • Reset IPv4 TCP/IP stack to installation defaults.
    C:\>netsh int ipv4 reset reset.log 
  • Reset IPv6 TCP/IP stack to installation defaults.
    C:\>netsh int ipv6 reset reset.log 
Done. Problem solved.



Reference:
http://www.techsupportforum.com/forums/f56/help-internet-explorer-not-working-910890.html



Tuesday, October 21, 2014

Chinese characters cannot be shown correctly in Onedrive on iPad or iPhone

问题:在iPad、iPhone上用Onedrive显示文本文档时,中文显示为乱码
Problem: Chinese characters cannot be shown correctly in Onedrive on iPad or iPhone.

截图/Screenshot:

原因:Windows记事本默认使用ASCII保存文本文档,导致其它系统无法识别其中的中文。
Cause: Windows Notepad uses ASCII as default encoding for txt files. Other system (like iOS) may not recognize Chinese characters or other non-English characters correctly if they are in ASCII.

解决方法:当用记事本保存文本文档时,将编码改为UTF-8(Unicode编码)。
Solution: When storing txt files on Onedrive by Notepad, change the encoding to UTF-8 (Unicode encoding).

**也可用其它默认使用Unicode编码的软件如notepad++来代替记事本。
**You can also use other software like notepad++ which uses Unicode encoding by default.

Friday, October 10, 2014

ISOLATION LEVEL

ISOLATION LEVEL

英文描述来自70-461 TRAINNING EXAM, 解释不一定准确;"*"为个人理解说明,已测供参。Some descriptions are from 70-461 Training Exam and may not be accurate. "*" parts are my notes. All tested in SQL Server 2012.)

READ UNCOMMITTED
You use the SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED statement when you want to allow statements to read rows altered by other transactions, but those transactions have not yet been committed.
* Writer does NOT block Reader。Reader may read uncommitted data by writer (dirty read).

READ COMMITTED
You use the SET TRANSACTION ISOLATION LEVEL READ COMMITTED statement when you want to ensure that statements cannot read data altered by other transactions when those transactions have not yet been committed. When you set the READ COMMITTED transaction level, data can be altered by other transactions between individual statements within the current transaction.
* When READ COMMITTED SNAPSHOT OFFWriter blocks Reader.
* When READ COMMITTED SNAPSHOT ON[LZ1] 
    ** Writer does NOT block ReaderREADER will read original data.
    **READER may read different data because writer may change it during current transaction.
 [LZ1] 
ALTER DATABASE Testdb1 SET READ_COMMITTED_SNAPSHOT ON;
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

SNAPSHOT
You use the SET TRANSACTION ISOLATION LEVEL SNAPSHOT [LZ2] statement when you want to ensure that data read by any statement in a transaction will be transactionally consistent with how that data existed at the start of the transaction. SNAPSHOT transactions do not block other transactions from writing data.
* Writer does NOT block ReaderREADER will read original data.
* READER will read original data even write change it or insert new rows during current transaction. No phantom reading (consistent).
ALTER DATABASE Testdb1 SET ALLOW_SNAPSHOT_ISOLATION ON;
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;

REPEATABLE READ
You use the SET TRANSACTION ISOLATION LEVEL REPEATABLE READ statement when you want to ensure that statements cannot read data altered by other transactions but not yet committed and that other transactions cannot modify data read by the current transaction until the current transaction commits. When the REPEATABLE READ isolation level is set, it is possible for other transactions to insert new rows that meet the search conditions of statements included in the current transaction.
* Writer blocks Reader.
* Writer can NOT modify rows read by Reader, but insert new rows. Result in phantom read.

SERIALIZABLE
You use the SET TRANSACTION ISOLATION LEVEL SERIALIZABLE statement when you want to ensure that Transact-SQL statements cannot read data that has been altered but not committed, that other transactions cannot modify data read by the current transaction until the transaction commits, and that other transactions cannot insert new rows with key values that would fall into the range read by statements in the current transaction until that transaction completes.
* WRITER  and READER block each other
* During READER's transaction, WRITER can NOT do any modification. no matter if it ”fall into the range”.(the underline part is not accurate.)


总结:
·         READ UNCOMMITTED, SNAPSHOT(both types): WRITER doesn't block READER
·         During the following types of READER transaction, the WRITER can …

UPDATE
INSERT
Result in current READER transaction
READ UNCOMMITTED
O
O
Dirty data
READ COMMITTED (both types)
O
O
Rows change (not consistent)
SNAPSHOT
O
O
No rows change (consistent)
REPEATABLE READ
X
O
No rows change, but phantom read
SERIALIZABLE
X
X
No rows change

Based on personal study. Please point out any mistake if you find it. Thanks! 个人理解,如若有误,请指出,谢谢!

Tuesday, October 7, 2014

CPU Too Hot: ASUS N61J Laptop DIY - Replace Thermal Paste; Cleaning the Fan; Replace Memory Chips

Before. The same as it's done.

Loose the screws for the heat pipes and the fan.
 (Left - CPU,  Right - ATi GPU) 

CPU
(This model doesn't have integrated graphic card)

Clean the old thermal paste. Don't remove the plastic film.

ATi (It's AMD now) GPU

Put new thermal paste on CPU and GPU, clean the fan (fold it and clean it).
Put them back together.
Temperature goes from 80C back to 60C when idle.


Replace memory chips

 Should use 204-Pin DDR3 SO-DIMM DDR3 1066 (PC3 8500) Laptop Memory.

Bye bye Kingston. Hello Corsair.

 Stable

No problem

Friday, October 3, 2014

Error: "No mapping between account names and security ids was done."

Error: "No mapping between account names and security ids was done."
This error message popped up when adding a domain user during SQL Server installation on a client server.
Found out a solution using SYSPREP.
If you do choose the SYSPREP route though here is what you need to do:
  1. From the Start Menu select Run
  2. Enter C:\Windows\System32\sysprep\sysprep.exe in the box and click OK
  3. Be sure that Enter System Out-of-Box Experience (OOBE) is selected
  4. Check the box next to Generalize (If this is not select the SID won’t get changed)
  5. Click OK and follow the prompts when the system reboots.

It took me about 15 minutes on a Hyper-V vm.

Here are some references:
http://www.bradleyschacht.com/error-no-mapping-between-account-names-and-security-ids-was-done/
http://blogs.technet.com/b/markrussinovich/archive/2009/11/03/3291024.aspx


Which picture is better for a frame?



USB 2.0 flash drive speed

A friend bought a USB 2.0 flash drive at a lovely 50% OFF discount. Today I found its writing speed was only 5-6 MB/s!! Compared to mine 9-10 MB/s.
Sometimes, the performance is also on discount when people buy some small-brand discounted hardware.

Copy files to Windows Server Core 2008 R2

I built a Server Core system on Hyper-V and wanted to copy some files to it. I checked Google for solutions but didn't really find a good one. So I figured out my own way to this problem. Maybe not a smart way, but worked for me.

  1. Make sure the Server Core is within the same LAN as your other computers you want to copy files from.
  2. Share a folder to 'everyone' on the other computer.
  3. Put your files into the folder.
  4. On the Server Core, send Ctrl+Alter+Del to call Task Manager.
  5. Use Run, and type "\\<other computer's IP>\<folder name>\*". (You don't actually open the folder to see what's inside it cause you don't have the function at all.)
  6. Here comes the username and password messagebox, and you go enter them.
  7. Go back to CMD, use DOS command to copy your files to the local Server Core. Something like this:  "COPY  \\10.0.0.123\sharedfolder\*.*  C:\"
  8. N files copied. Done.

Wednesday, October 1, 2014