将用户添加到 Oracle

Oracle 数据库管理员在 Oracle 实例中创建用户帐户并为这些帐户分配存储(表空间)和权限。

数据库管理器(系统用户)可使用 Oracle 工具创建用户、为用户创建默认表空间以及授予创建数据库对象的权限。

或者,数据库管理员可使用 ArcGIS 客户端中的创建数据库用户工具或脚本创建数据所有者。 该工具需要 Standard 或 Advanced 许可才能运行。 Database users created with this tool are granted the following privileges:

  • CREATE SESSION
  • CREATE SEQUENCE
  • CREATE TABLE
  • CREATE TRIGGER
  • CREATE VIEW
  • SELECT ON DBA_ROLES

添加可创建数据库对象的用户

可从 ArcGIS Desktop 运行创建数据库用户工具或在 Python 脚本中调用该工具,以创建可创建表、要素类、视图、触发器和序列的数据库用户。

必须以 Oracle 系统用户身份连接到数据库才能运行创建数据库用户工具。

使用创建数据库用户工具

  1. 启动 ArcGIS Pro
  2. Connect to the database or geodatabase as the sys user.
  3. 打开创建数据库用户工具。

    该工具位于“数据管理”工具箱的“地理数据库管理”工具集中。

  4. 指定输入数据库连接的数据库连接。
  5. 为工具将要创建的用户和方案输入名称。
  6. 为数据库用户输入密码。
  7. 如果您已具有组角色并希望此用户成为该组的成员,请指定该用户的角色。
  8. 输入用作用户默认表空间的表空间名称。 如果表空间不存在,则此工具将在 Oracle 默认存储位置创建表空间。 工具将创建 400 MB 的表空间。

    如果不指定表空间,则使用 Oracle 默认表空间。

  9. 单击确定 (ArcMap) 或运行 (ArcGIS Pro)。

运行 Python 脚本

若要编写用户创建的脚本,请按照以下步骤操作:

  1. 在 ArcGIS 客户端计算机上创建文本文件,然后将以下脚本复制到此文件。

    """
    Name: create_database_user.py
    Description: Provide connection information to a database user.
    Type create_database_user.py -h or create_database_user.py --help for usage
    """
    
    # Import system modules
    import arcpy
    import os
    import optparse
    import sys
    
    
    # Define usage and version
    parser = optparse.OptionParser(usage = "usage: %prog [Options]", version="%prog 1.0 for 10.1 release")
    
    #Define help and options
    parser.add_option ("--DBMS", dest="Database_type", type="choice", choices=['SQLSERVER', 'ORACLE', 'POSTGRESQL', ''], default="", help="Type of enterprise DBMS:  SQLSERVER, ORACLE, or POSTGRESQL.")                   
    parser.add_option ("-i", dest="Instance", type="string", default="", help="DBMS instance name")
    parser.add_option ("-D", dest="Database", type="string", default="none", help="Database name:  Not required for Oracle")
    parser.add_option ("--auth", dest="Account_authentication", type ="choice", choices=['DATABASE_AUTH', 'OPERATING_SYSTEM_AUTH'], default='DATABASE_AUTH', help="Authentication type options (case-sensitive):  DATABASE_AUTH, OPERATING_SYSTEM_AUTH.  Default=DATABASE_AUTH")
    parser.add_option ("-U", dest="Dbms_admin", type="string", default="", help="DBMS administrator user")
    parser.add_option ("-P", dest="Dbms_admin_pwd", type="string", default="", help="DBMS administrator password")
    parser.add_option ("--utype", dest="user_type", type ="choice", choices=['DATABASE_USER', 'OPERATING_SYSTEM_USER'], default='DATABASE_USER', help="Authentication type options (case-sensitive):  DATABASE_USER, OPERATING_SYSTEM_USER.  Default=DATABASE_USER")
    parser.add_option ("-u", dest="dbuser", type="string", default="", help="database user name")
    parser.add_option ("-p", dest="dbuser_pwd", type="string", default="", help="database user password")
    parser.add_option ("-r", dest="role", type="string", default="", help="role to be granted to the user")
    parser.add_option ("-t", dest="Tablespace", type="string", default="", help="Tablespace name")
    # Check if value entered for option
    try:
    	(options, args) = parser.parse_args()
    
    	#Check if no system arguments (options) entered
    	if len(sys.argv) == 1:
    		print "%s: error: %s\n" % (sys.argv[0], "No command options given")
    		parser.print_help()
    		sys.exit(3)
    
    	#Usage parameters for spatial database connection
    	database_type = options.Database_type.upper()
    	instance = options.Instance
    	database = options.Database.lower()	
    	account_authentication = options.Account_authentication.upper()
    	dbms_admin = options.Dbms_admin
    	dbms_admin_pwd = options.Dbms_admin_pwd
    	dbuser = options.dbuser
    	dbuser_pwd = options.dbuser_pwd	
    	tablespace = options.Tablespace
    	user_type = options.user_type
    	role = options.role
    
    	
    	if (database_type == "SQLSERVER"):
    		database_type = "SQL_SERVER"
    	
    	if( database_type ==""):	
    		print(" \n%s: error: \n%s\n" % (sys.argv[0], "DBMS type (--DBMS) must be specified."))
    		parser.print_help()
    		sys.exit(3)		
    	
    	if(database_type == "SQL_SERVER"):
    		if( account_authentication == "DATABASE_AUTH" and dbms_admin == ""):
    			print("\n%s: error: %s\n" % (sys.argv[0], "DBMS administrator must be specified with database authentication"))
    			sys.exit(3)
    		if( account_authentication == "OPERATING_SYSTEM_AUTH" and dbms_admin != ""):
    			print("\nWarning: %s\n" % ("Ignoring DBMS administrator specified when using operating system authentication..."))
    	else:		
    		if( dbuser.lower() == ""):
    			print("\n%s: error: %s\n" % (sys.argv[0], "Database user must be specified."))
    			sys.exit(3)		
    		if( dbms_admin == ""):
    			print("\n%s: error: %s\n" % (sys.argv[0], "DBMS administrator must be specified!"))
    			sys.exit(3)
    
    	if ( user_type == "DATABASE_USER" and (dbuser =="" or dbuser_pwd =="")):
    		print(" \n%s: error: \n%s\n" % (sys.argv[0], "To create database authenticated user, user name and password must be specified!"))
    		parser.print_help()
    		sys.exit(3)	
    
    	# Get the current product license
    	product_license=arcpy.ProductInfo()
    	
    	# Checks required license level
    	if product_license.upper() == "ARCVIEW" or product_license.upper() == 'ENGINE':
    		print("\n" + product_license + " license found!" + "  Creating a user in an enterprise geodatabase or database requires an ArcGIS Desktop Standard or Advanced, ArcGIS Engine with the Geodatabase Update extension, or ArcGIS Server license.")
    		sys.exit("Re-authorize ArcGIS before creating a database user.")
    	else:
    		print("\n" + product_license + " license available!  Continuing to create...")
    		arcpy.AddMessage("+++++++++")
    
    	# Local variables
    	instance_temp = instance.replace("\\","_")
    	instance_temp = instance_temp.replace("/","_")
    	instance_temp = instance_temp.replace(":","_")
    	Conn_File_NameT = instance_temp + "_" + database + "_" + dbms_admin   
    
    	if os.environ.get("TEMP") == None:
    		temp = "c:\\temp"	
    	else:
    		temp = os.environ.get("TEMP")
    	
    	if os.environ.get("TMP") == None:
    		temp = "/usr/tmp"		
    	else:
    		temp = os.environ.get("TMP")  
    
    	Connection_File_Name = Conn_File_NameT + ".sde"
    	Connection_File_Name_full_path = temp + os.sep + Conn_File_NameT + ".sde"
    	
    	# Check for the .sde file and delete it if present
    	arcpy.env.overwriteOutput=True
    	if os.path.exists(Connection_File_Name_full_path):
    		os.remove(Connection_File_Name_full_path)
    
    	try:
    		print("\nCreating Database Connection File...\n")
    		# Process: Create Database Connection File...
    		# Usage:  out_file_location, out_file_name, DBMS_TYPE, instnace, database, account_authentication, username, password, save_username_password(must be true)
    		#arcpy.CreateDatabaseConnection_management(temp , Connection_File_Name, database_type, instance, database, account_authentication, dbms_admin, dbms_admin_pwd, "TRUE")
    		arcpy.CreateDatabaseConnection_management(out_folder_path=temp, out_name=Connection_File_Name, database_platform=database_type, instance=instance, database=database, account_authentication=account_authentication, username=dbms_admin, password=dbms_admin_pwd, save_user_pass="TRUE")
    	        for i in range(arcpy.GetMessageCount()):
    			if "000565" in arcpy.GetMessage(i):   #Check if database connection was successful
    				arcpy.AddReturnMessage(i)
    				arcpy.AddMessage("\n+++++++++")
    				arcpy.AddMessage("Exiting!!")
    				arcpy.AddMessage("+++++++++\n")
    				sys.exit(3)            
    			else:
    				arcpy.AddReturnMessage(i)
    				arcpy.AddMessage("+++++++++\n")
    
    		print("Creating database user...\n")
    		arcpy.CreateDatabaseUser_management(input_workspace=Connection_File_Name_full_path, user_authentication_type=user_type, user_name=dbuser, user_password=dbuser_pwd, role=role, tablespace_name=tablespace)
    		for i in range(arcpy.GetMessageCount()):
    			arcpy.AddReturnMessage(i)
    		arcpy.AddMessage("+++++++++\n")
    	except:
    		for i in range(arcpy.GetMessageCount()):
    			arcpy.AddReturnMessage(i)
    			
    #Check if no value entered for option	
    except SystemExit as e:
    	if e.code == 2:
    		parser.usage = ""
    		print("\n")
    		parser.print_help()   
    		parser.exit(2)

    您可以在安装了 ArcGIS Pro(Standard 或 Advanced)或 ArcGIS Server 的计算机上运行脚本。

  2. .py 扩展名保存该文件。
  3. 运行脚本,同时提供 Oracle 数据库特定的选项和信息以及要创建的用户。

    在以下示例中,通过 Microsoft Windows 服务器运行的脚本名称为 create_database_user.py。 经数据库验证的用户(地理数据)将使用 Oracle 连接字符串 dbsrv/orcl 在数据库中进行创建。 未将此用户添加到角色。

    create_database_user.py --DBMS ORACLE -i dbsrv/orcl -U sys -P $hHhH --utype DATABASE_USER -u geodata -p 0wn1t

    这是在 Linux 计算机上运行脚本的示例:

    ./create_database_user.py --DBMS ORACLE -i dbsrv/orcl -U sys -P $hHhH --utype DATABASE_USER -u geodata -p 0wn1t

    提示:

    在命令提示符处输入 -h--help 以获取语法帮助。

此时,数据库已具有可创建表的用户。

如果已存在数据集,所有者可向需要查看数据的用户授予数据集的 SELECT 权限,并向需要编辑数据的用户授予数据集的 SELECT、INSERT、UPDATE 和 DELETE 权限。 有关说明,请参阅授予和撤消数据集权限

使用 Oracle 工具创建用户

如果希望创建的用户具有的权限与创建数据库用户工具授予的权限不同或没有直接授予用户的权限,则可使用 Oracle 工具执行该操作。

有关使用 Oracle 工具创建数据库用户的说明,请参阅 Oracle 文档。